author | Patrick Mezard <pmezard@gmail.com> |
Mon, 06 Aug 2007 09:57:23 +0200 | |
changeset 5076 | 4f37ce544c16 |
parent 3797 | 54fd4d3b4fce |
child 5209 | bbdcdc7f170e |
permissions | -rw-r--r-- |
1814
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
1 |
import sys, textwrap |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
2 |
# import from the live mercurial repo |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
3 |
sys.path.insert(0, "..") |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
4 |
from mercurial.commands import table, globalopts |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
5 |
from mercurial.i18n import gettext as _ |
3797
54fd4d3b4fce
Generate docs for help topics
Matt Mackall <mpm@selenic.com>
parents:
1814
diff
changeset
|
6 |
from mercurial.help import helptable |
1814
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
7 |
|
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
8 |
def get_desc(docstr): |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
9 |
if not docstr: |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
10 |
return "", "" |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
11 |
# sanitize |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
12 |
docstr = docstr.strip("\n") |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
13 |
docstr = docstr.rstrip() |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
14 |
shortdesc = docstr.splitlines()[0].strip() |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
15 |
|
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
16 |
i = docstr.find("\n") |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
17 |
if i != -1: |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
18 |
desc = docstr[i+2:] |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
19 |
else: |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
20 |
desc = " %s" % shortdesc |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
21 |
return (shortdesc, desc) |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
22 |
|
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
23 |
def get_opts(opts): |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
24 |
for shortopt, longopt, default, desc in opts: |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
25 |
allopts = [] |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
26 |
if shortopt: |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
27 |
allopts.append("-%s" % shortopt) |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
28 |
if longopt: |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
29 |
allopts.append("--%s" % longopt) |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
30 |
desc += default and _(" (default: %s)") % default or "" |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
31 |
yield(", ".join(allopts), desc) |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
32 |
|
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
33 |
def get_cmd(cmd): |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
34 |
d = {} |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
35 |
attr = table[cmd] |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
36 |
cmds = cmd.lstrip("^").split("|") |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
37 |
|
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
38 |
d['synopsis'] = attr[2] |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
39 |
d['cmd'] = cmds[0] |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
40 |
d['aliases'] = cmd.split("|")[1:] |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
41 |
d['desc'] = get_desc(attr[0].__doc__) |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
42 |
d['opts'] = list(get_opts(attr[1])) |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
43 |
return d |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
44 |
|
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
45 |
|
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
46 |
def show_doc(ui): |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
47 |
def bold(s, text=""): |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
48 |
ui.write("%s\n%s\n%s\n" % (s, "="*len(s), text)) |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
49 |
def underlined(s, text=""): |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
50 |
ui.write("%s\n%s\n%s\n" % (s, "-"*len(s), text)) |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
51 |
|
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
52 |
# print options |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
53 |
underlined(_("OPTIONS")) |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
54 |
for optstr, desc in get_opts(globalopts): |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
55 |
ui.write("%s::\n %s\n\n" % (optstr, desc)) |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
56 |
|
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
57 |
# print cmds |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
58 |
underlined(_("COMMANDS")) |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
59 |
h = {} |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
60 |
for c, attr in table.items(): |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
61 |
f = c.split("|")[0] |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
62 |
f = f.lstrip("^") |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
63 |
h[f] = c |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
64 |
cmds = h.keys() |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
65 |
cmds.sort() |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
66 |
|
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
67 |
for f in cmds: |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
68 |
if f.startswith("debug"): continue |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
69 |
d = get_cmd(h[f]) |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
70 |
# synopsis |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
71 |
ui.write("%s::\n" % d['synopsis'].replace("hg ","", 1)) |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
72 |
# description |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
73 |
ui.write("%s\n\n" % d['desc'][1]) |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
74 |
# options |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
75 |
opt_output = list(d['opts']) |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
76 |
if opt_output: |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
77 |
opts_len = max([len(line[0]) for line in opt_output]) |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
78 |
ui.write(_(" options:\n")) |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
79 |
for optstr, desc in opt_output: |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
80 |
if desc: |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
81 |
s = "%-*s %s" % (opts_len, optstr, desc) |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
82 |
else: |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
83 |
s = optstr |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
84 |
s = textwrap.fill(s, initial_indent=4 * " ", |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
85 |
subsequent_indent=(6 + opts_len) * " ") |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
86 |
ui.write("%s\n" % s) |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
87 |
ui.write("\n") |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
88 |
# aliases |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
89 |
if d['aliases']: |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
90 |
ui.write(_(" aliases: %s\n\n") % " ".join(d['aliases'])) |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
91 |
|
3797
54fd4d3b4fce
Generate docs for help topics
Matt Mackall <mpm@selenic.com>
parents:
1814
diff
changeset
|
92 |
# print topics |
54fd4d3b4fce
Generate docs for help topics
Matt Mackall <mpm@selenic.com>
parents:
1814
diff
changeset
|
93 |
for t in helptable: |
54fd4d3b4fce
Generate docs for help topics
Matt Mackall <mpm@selenic.com>
parents:
1814
diff
changeset
|
94 |
l = t.split("|") |
54fd4d3b4fce
Generate docs for help topics
Matt Mackall <mpm@selenic.com>
parents:
1814
diff
changeset
|
95 |
section = l[-1] |
54fd4d3b4fce
Generate docs for help topics
Matt Mackall <mpm@selenic.com>
parents:
1814
diff
changeset
|
96 |
underlined(_(section).upper()) |
54fd4d3b4fce
Generate docs for help topics
Matt Mackall <mpm@selenic.com>
parents:
1814
diff
changeset
|
97 |
doc = helptable[t] |
54fd4d3b4fce
Generate docs for help topics
Matt Mackall <mpm@selenic.com>
parents:
1814
diff
changeset
|
98 |
if callable(doc): |
54fd4d3b4fce
Generate docs for help topics
Matt Mackall <mpm@selenic.com>
parents:
1814
diff
changeset
|
99 |
doc = doc() |
54fd4d3b4fce
Generate docs for help topics
Matt Mackall <mpm@selenic.com>
parents:
1814
diff
changeset
|
100 |
ui.write(_(doc)) |
54fd4d3b4fce
Generate docs for help topics
Matt Mackall <mpm@selenic.com>
parents:
1814
diff
changeset
|
101 |
ui.write("\n") |
54fd4d3b4fce
Generate docs for help topics
Matt Mackall <mpm@selenic.com>
parents:
1814
diff
changeset
|
102 |
|
1814
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
103 |
if __name__ == "__main__": |
7956893e8458
generate hg manpage from commands.py docstring
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents:
diff
changeset
|
104 |
show_doc(sys.stdout) |