comparison doc/gendoc.py @ 1814:7956893e8458

generate hg manpage from commands.py docstring gendoc.py is a script generating a part of the manpage (the commands help and options) from the docstring in commands.py. It avoids duplicating the doc between the doc/ directory and the docstrings. To generate the manpage, 'make doc' will create all the necessary intermediate files.
author Benoit Boissinot <benoit.boissinot@ens-lyon.org>
date Tue, 28 Feb 2006 00:48:49 +0100
parents
children 54fd4d3b4fce
comparison
equal deleted inserted replaced
1811:6cb548cffdf5 1814:7956893e8458
1 import sys, textwrap
2 # import from the live mercurial repo
3 sys.path.insert(0, "..")
4 from mercurial.commands import table, globalopts
5 from mercurial.i18n import gettext as _
6
7 def get_desc(docstr):
8 if not docstr:
9 return "", ""
10 # sanitize
11 docstr = docstr.strip("\n")
12 docstr = docstr.rstrip()
13 shortdesc = docstr.splitlines()[0].strip()
14
15 i = docstr.find("\n")
16 if i != -1:
17 desc = docstr[i+2:]
18 else:
19 desc = " %s" % shortdesc
20 return (shortdesc, desc)
21
22 def get_opts(opts):
23 for shortopt, longopt, default, desc in opts:
24 allopts = []
25 if shortopt:
26 allopts.append("-%s" % shortopt)
27 if longopt:
28 allopts.append("--%s" % longopt)
29 desc += default and _(" (default: %s)") % default or ""
30 yield(", ".join(allopts), desc)
31
32 def get_cmd(cmd):
33 d = {}
34 attr = table[cmd]
35 cmds = cmd.lstrip("^").split("|")
36
37 d['synopsis'] = attr[2]
38 d['cmd'] = cmds[0]
39 d['aliases'] = cmd.split("|")[1:]
40 d['desc'] = get_desc(attr[0].__doc__)
41 d['opts'] = list(get_opts(attr[1]))
42 return d
43
44
45 def show_doc(ui):
46 def bold(s, text=""):
47 ui.write("%s\n%s\n%s\n" % (s, "="*len(s), text))
48 def underlined(s, text=""):
49 ui.write("%s\n%s\n%s\n" % (s, "-"*len(s), text))
50
51 # print options
52 underlined(_("OPTIONS"))
53 for optstr, desc in get_opts(globalopts):
54 ui.write("%s::\n %s\n\n" % (optstr, desc))
55
56 # print cmds
57 underlined(_("COMMANDS"))
58 h = {}
59 for c, attr in table.items():
60 f = c.split("|")[0]
61 f = f.lstrip("^")
62 h[f] = c
63 cmds = h.keys()
64 cmds.sort()
65
66 for f in cmds:
67 if f.startswith("debug"): continue
68 d = get_cmd(h[f])
69 # synopsis
70 ui.write("%s::\n" % d['synopsis'].replace("hg ","", 1))
71 # description
72 ui.write("%s\n\n" % d['desc'][1])
73 # options
74 opt_output = list(d['opts'])
75 if opt_output:
76 opts_len = max([len(line[0]) for line in opt_output])
77 ui.write(_(" options:\n"))
78 for optstr, desc in opt_output:
79 if desc:
80 s = "%-*s %s" % (opts_len, optstr, desc)
81 else:
82 s = optstr
83 s = textwrap.fill(s, initial_indent=4 * " ",
84 subsequent_indent=(6 + opts_len) * " ")
85 ui.write("%s\n" % s)
86 ui.write("\n")
87 # aliases
88 if d['aliases']:
89 ui.write(_(" aliases: %s\n\n") % " ".join(d['aliases']))
90
91 if __name__ == "__main__":
92 show_doc(sys.stdout)