##// END OF EJS Templates
gendoc: automatically create help for default extensions....
Erik Zielke -
r12781:0d09991f default
parent child Browse files
Show More
@@ -1,129 +1,167 b''
1 1 import os, sys, textwrap
2 2 # import from the live mercurial repo
3 3 sys.path.insert(0, "..")
4 4 # fall back to pure modules if required C extensions are not available
5 5 sys.path.append(os.path.join('..', 'mercurial', 'pure'))
6 6 from mercurial import demandimport; demandimport.enable()
7 7 from mercurial import encoding
8 8 from mercurial.commands import table, globalopts
9 9 from mercurial.i18n import _
10 10 from mercurial.help import helptable
11 from mercurial import extensions
11 12
12 13 def get_desc(docstr):
13 14 if not docstr:
14 15 return "", ""
15 16 # sanitize
16 17 docstr = docstr.strip("\n")
17 18 docstr = docstr.rstrip()
18 19 shortdesc = docstr.splitlines()[0].strip()
19 20
20 21 i = docstr.find("\n")
21 22 if i != -1:
22 23 desc = docstr[i + 2:]
23 24 else:
24 25 desc = shortdesc
25 26
26 27 desc = textwrap.dedent(desc)
27 28
28 29 return (shortdesc, desc)
29 30
30 31 def get_opts(opts):
31 32 for opt in opts:
32 33 if len(opt) == 5:
33 34 shortopt, longopt, default, desc, optlabel = opt
34 35 else:
35 36 shortopt, longopt, default, desc = opt
36 37 allopts = []
37 38 if shortopt:
38 39 allopts.append("-%s" % shortopt)
39 40 if longopt:
40 41 allopts.append("--%s" % longopt)
41 42 desc += default and _(" (default: %s)") % default or ""
42 43 yield(", ".join(allopts), desc)
43 44
44 45 def get_cmd(cmd, cmdtable):
45 46 d = {}
46 47 attr = cmdtable[cmd]
47 48 cmds = cmd.lstrip("^").split("|")
48 49
49 50 d['cmd'] = cmds[0]
50 51 d['aliases'] = cmd.split("|")[1:]
51 52 d['desc'] = get_desc(attr[0].__doc__)
52 53 d['opts'] = list(get_opts(attr[1]))
53 54
54 55 s = 'hg ' + cmds[0]
55 56 if len(attr) > 2:
56 57 if not attr[2].startswith('hg'):
57 58 s += ' ' + attr[2]
58 59 else:
59 60 s = attr[2]
60 61 d['synopsis'] = s.strip()
61 62
62 63 return d
63 64
64 65 def section(ui, s):
65 66 ui.write("%s\n%s\n\n" % (s, "-" * encoding.colwidth(s)))
66 67
67 68 def subsection(ui, s):
68 69 ui.write("%s\n%s\n\n" % (s, '"' * encoding.colwidth(s)))
69 70
71 def subsubsection(ui, s):
72 ui.write("%s\n%s\n\n" % (s, "." * encoding.colwidth(s)))
73
74 def subsubsubsection(ui, s):
75 ui.write("%s\n%s\n\n" % (s, "#" * encoding.colwidth(s)))
76
70 77
71 78 def show_doc(ui):
72 79 # print options
73 80 section(ui, _("Options"))
74 81 for optstr, desc in get_opts(globalopts):
75 82 ui.write("%s\n%s\n\n" % (optstr, desc))
76 83
77 84 # print cmds
78 85 section(ui, _("Commands"))
79 commandprinter(ui, table)
86 commandprinter(ui, table, subsection)
80 87
81 88 # print topics
82 89 for names, sec, doc in helptable:
83 90 for name in names:
84 91 ui.write(".. _%s:\n" % name)
85 92 ui.write("\n")
86 93 section(ui, sec)
87 94 if hasattr(doc, '__call__'):
88 95 doc = doc()
89 96 ui.write(doc)
90 97 ui.write("\n")
91 98
92 def commandprinter(ui, cmdtable):
99 section(ui, _("Extensions"))
100 ui.write(_("This section contains help for extensions that is distributed "
101 "together with Mercurial. Help for other extensions is available "
102 "in the help system."))
103 ui.write("\n\n"
104 ".. contents::\n"
105 " :class: htmlonly\n"
106 " :local:\n"
107 " :depth: 1\n\n")
108
109 for extensionname in sorted(allextensionnames()):
110 mod = extensions.load(None, extensionname, None)
111 subsection(ui, extensionname)
112 ui.write("%s\n\n" % mod.__doc__)
113 cmdtable = getattr(mod, 'cmdtable', None)
114 if cmdtable:
115 subsubsection(ui, _('Commands'))
116 commandprinter(ui, cmdtable, subsubsubsection)
117
118 def commandprinter(ui, cmdtable, sectionfunc):
93 119 h = {}
94 120 for c, attr in cmdtable.items():
95 121 f = c.split("|")[0]
96 122 f = f.lstrip("^")
97 123 h[f] = c
98 124 cmds = h.keys()
99 125 cmds.sort()
100 126
101 127 for f in cmds:
102 128 if f.startswith("debug"):
103 129 continue
104 130 d = get_cmd(h[f], cmdtable)
105 subsection(ui, d['cmd'])
131 sectionfunc(ui, d['cmd'])
106 132 # synopsis
107 133 ui.write("``%s``\n" % d['synopsis'].replace("hg ","", 1))
108 134 ui.write("\n")
109 135 # description
110 136 ui.write("%s\n\n" % d['desc'][1])
111 137 # options
112 138 opt_output = list(d['opts'])
113 139 if opt_output:
114 140 opts_len = max([len(line[0]) for line in opt_output])
115 141 ui.write(_("options:\n\n"))
116 142 for optstr, desc in opt_output:
117 143 if desc:
118 144 s = "%-*s %s" % (opts_len, optstr, desc)
119 145 else:
120 146 s = optstr
121 147 ui.write("%s\n" % s)
122 148 ui.write("\n")
123 149 # aliases
124 150 if d['aliases']:
125 151 ui.write(_(" aliases: %s\n\n") % " ".join(d['aliases']))
126 152
127 153
154 def allextensionnames():
155 extensionnames = []
156
157 extensionsdictionary = extensions.enabled()[0]
158 extensionnames.extend(extensionsdictionary.keys())
159
160 extensionsdictionary = extensions.disabled()[0]
161 extensionnames.extend(extensionsdictionary.keys())
162
163 return extensionnames
164
165
128 166 if __name__ == "__main__":
129 167 show_doc(sys.stdout)
General Comments 0
You need to be logged in to leave comments. Login now