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