##// END OF EJS Templates
doc: show short description of each commands in generated documents...
FUJIWARA Katsunori -
r20689:401f9b66 default
parent child Browse files
Show More
@@ -1,203 +1,205 b''
1 1 """usage: %s DOC ...
2 2
3 3 where DOC is the name of a document
4 4 """
5 5
6 6 import os, sys, textwrap
7 7 # import from the live mercurial repo
8 8 sys.path.insert(0, "..")
9 9 # fall back to pure modules if required C extensions are not available
10 10 sys.path.append(os.path.join('..', 'mercurial', 'pure'))
11 11 from mercurial import demandimport; demandimport.enable()
12 12 from mercurial import minirst
13 13 from mercurial.commands import table, globalopts
14 14 from mercurial.i18n import gettext, _
15 15 from mercurial.help import helptable, loaddoc
16 16 from mercurial import extensions
17 17 from mercurial import util
18 18
19 19 def get_desc(docstr):
20 20 if not docstr:
21 21 return "", ""
22 22 # sanitize
23 23 docstr = docstr.strip("\n")
24 24 docstr = docstr.rstrip()
25 25 shortdesc = docstr.splitlines()[0].strip()
26 26
27 27 i = docstr.find("\n")
28 28 if i != -1:
29 29 desc = docstr[i + 2:]
30 30 else:
31 31 desc = shortdesc
32 32
33 33 desc = textwrap.dedent(desc)
34 34
35 35 return (shortdesc, desc)
36 36
37 37 def get_opts(opts):
38 38 for opt in opts:
39 39 if len(opt) == 5:
40 40 shortopt, longopt, default, desc, optlabel = opt
41 41 else:
42 42 shortopt, longopt, default, desc = opt
43 43 optlabel = _("VALUE")
44 44 allopts = []
45 45 if shortopt:
46 46 allopts.append("-%s" % shortopt)
47 47 if longopt:
48 48 allopts.append("--%s" % longopt)
49 49 if isinstance(default, list):
50 50 allopts[-1] += " <%s[+]>" % optlabel
51 51 elif (default is not None) and not isinstance(default, bool):
52 52 allopts[-1] += " <%s>" % optlabel
53 53 if '\n' in desc:
54 54 # only remove line breaks and indentation
55 55 desc = ' '.join(l.lstrip() for l in desc.split('\n'))
56 56 desc += default and _(" (default: %s)") % default or ""
57 57 yield (", ".join(allopts), desc)
58 58
59 59 def get_cmd(cmd, cmdtable):
60 60 d = {}
61 61 attr = cmdtable[cmd]
62 62 cmds = cmd.lstrip("^").split("|")
63 63
64 64 d['cmd'] = cmds[0]
65 65 d['aliases'] = cmd.split("|")[1:]
66 66 d['desc'] = get_desc(gettext(attr[0].__doc__))
67 67 d['opts'] = list(get_opts(attr[1]))
68 68
69 69 s = 'hg ' + cmds[0]
70 70 if len(attr) > 2:
71 71 if not attr[2].startswith('hg'):
72 72 s += ' ' + attr[2]
73 73 else:
74 74 s = attr[2]
75 75 d['synopsis'] = s.strip()
76 76
77 77 return d
78 78
79 79 def showdoc(ui):
80 80 # print options
81 81 ui.write(minirst.section(_("Options")))
82 82 multioccur = False
83 83 for optstr, desc in get_opts(globalopts):
84 84 ui.write("%s\n %s\n\n" % (optstr, desc))
85 85 if optstr.endswith("[+]>"):
86 86 multioccur = True
87 87 if multioccur:
88 88 ui.write(_("\n[+] marked option can be specified multiple times\n"))
89 89 ui.write("\n")
90 90
91 91 # print cmds
92 92 ui.write(minirst.section(_("Commands")))
93 93 commandprinter(ui, table, minirst.subsection)
94 94
95 95 # print help topics
96 96 # The config help topic is included in the hgrc.5 man page.
97 97 helpprinter(ui, helptable, minirst.section, exclude=['config'])
98 98
99 99 ui.write(minirst.section(_("Extensions")))
100 100 ui.write(_("This section contains help for extensions that are "
101 101 "distributed together with Mercurial. Help for other "
102 102 "extensions is available in the help system."))
103 103 ui.write("\n\n"
104 104 ".. contents::\n"
105 105 " :class: htmlonly\n"
106 106 " :local:\n"
107 107 " :depth: 1\n\n")
108 108
109 109 for extensionname in sorted(allextensionnames()):
110 110 mod = extensions.load(None, extensionname, None)
111 111 ui.write(minirst.subsection(extensionname))
112 112 ui.write("%s\n\n" % gettext(mod.__doc__))
113 113 cmdtable = getattr(mod, 'cmdtable', None)
114 114 if cmdtable:
115 115 ui.write(minirst.subsubsection(_('Commands')))
116 116 commandprinter(ui, cmdtable, minirst.subsubsubsection)
117 117
118 118 def showtopic(ui, topic):
119 119 extrahelptable = [
120 120 (["common"], '', loaddoc('common')),
121 121 (["hg.1"], '', loaddoc('hg.1')),
122 122 (["hgignore.5"], '', loaddoc('hgignore.5')),
123 123 (["hgrc.5"], '', loaddoc('hgrc.5')),
124 124 (["hgignore.5.gendoc"], '', loaddoc('hgignore')),
125 125 (["hgrc.5.gendoc"], '', loaddoc('config')),
126 126 ]
127 127 helpprinter(ui, helptable + extrahelptable, None, include=[topic])
128 128
129 129 def helpprinter(ui, helptable, sectionfunc, include=[], exclude=[]):
130 130 for names, sec, doc in helptable:
131 131 if exclude and names[0] in exclude:
132 132 continue
133 133 if include and names[0] not in include:
134 134 continue
135 135 for name in names:
136 136 ui.write(".. _%s:\n" % name)
137 137 ui.write("\n")
138 138 if sectionfunc:
139 139 ui.write(sectionfunc(sec))
140 140 if util.safehasattr(doc, '__call__'):
141 141 doc = doc()
142 142 ui.write(doc)
143 143 ui.write("\n")
144 144
145 145 def commandprinter(ui, cmdtable, sectionfunc):
146 146 h = {}
147 147 for c, attr in cmdtable.items():
148 148 f = c.split("|")[0]
149 149 f = f.lstrip("^")
150 150 h[f] = c
151 151 cmds = h.keys()
152 152 cmds.sort()
153 153
154 154 for f in cmds:
155 155 if f.startswith("debug"):
156 156 continue
157 157 d = get_cmd(h[f], cmdtable)
158 158 ui.write(sectionfunc(d['cmd']))
159 # short description
160 ui.write(d['desc'][0])
159 161 # synopsis
160 162 ui.write("::\n\n")
161 163 synopsislines = d['synopsis'].splitlines()
162 164 for line in synopsislines:
163 165 # some commands (such as rebase) have a multi-line
164 166 # synopsis
165 167 ui.write(" %s\n" % line)
166 168 ui.write('\n')
167 169 # description
168 170 ui.write("%s\n\n" % d['desc'][1])
169 171 # options
170 172 opt_output = list(d['opts'])
171 173 if opt_output:
172 174 opts_len = max([len(line[0]) for line in opt_output])
173 175 ui.write(_("Options:\n\n"))
174 176 multioccur = False
175 177 for optstr, desc in opt_output:
176 178 if desc:
177 179 s = "%-*s %s" % (opts_len, optstr, desc)
178 180 else:
179 181 s = optstr
180 182 ui.write("%s\n" % s)
181 183 if optstr.endswith("[+]>"):
182 184 multioccur = True
183 185 if multioccur:
184 186 ui.write(_("\n[+] marked option can be specified"
185 187 " multiple times\n"))
186 188 ui.write("\n")
187 189 # aliases
188 190 if d['aliases']:
189 191 ui.write(_(" aliases: %s\n\n") % " ".join(d['aliases']))
190 192
191 193
192 194 def allextensionnames():
193 195 return extensions.enabled().keys() + extensions.disabled().keys()
194 196
195 197 if __name__ == "__main__":
196 198 doc = 'hg.1.gendoc'
197 199 if len(sys.argv) > 1:
198 200 doc = sys.argv[1]
199 201
200 202 if doc == 'hg.1.gendoc':
201 203 showdoc(sys.stdout)
202 204 else:
203 205 showtopic(sys.stdout, sys.argv[1])
General Comments 0
You need to be logged in to leave comments. Login now