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