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