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