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