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