##// END OF EJS Templates
gendoc: dedent documentation from docstrings...
Erik Zielke -
r12780:bdc1cf69 default
parent child Browse files
Show More
@@ -1,125 +1,129
1 import os, sys
1 import os, sys, textwrap
2 # import from the live mercurial repo
2 # import from the live mercurial repo
3 sys.path.insert(0, "..")
3 sys.path.insert(0, "..")
4 # fall back to pure modules if required C extensions are not available
4 # fall back to pure modules if required C extensions are not available
5 sys.path.append(os.path.join('..', 'mercurial', 'pure'))
5 sys.path.append(os.path.join('..', 'mercurial', 'pure'))
6 from mercurial import demandimport; demandimport.enable()
6 from mercurial import demandimport; demandimport.enable()
7 from mercurial import encoding
7 from mercurial import encoding
8 from mercurial.commands import table, globalopts
8 from mercurial.commands import table, globalopts
9 from mercurial.i18n import _
9 from mercurial.i18n import _
10 from mercurial.help import helptable
10 from mercurial.help import helptable
11
11
12 def get_desc(docstr):
12 def get_desc(docstr):
13 if not docstr:
13 if not docstr:
14 return "", ""
14 return "", ""
15 # sanitize
15 # sanitize
16 docstr = docstr.strip("\n")
16 docstr = docstr.strip("\n")
17 docstr = docstr.rstrip()
17 docstr = docstr.rstrip()
18 shortdesc = docstr.splitlines()[0].strip()
18 shortdesc = docstr.splitlines()[0].strip()
19
19
20 i = docstr.find("\n")
20 i = docstr.find("\n")
21 if i != -1:
21 if i != -1:
22 desc = docstr[i + 2:]
22 desc = docstr[i + 2:]
23 else:
23 else:
24 desc = " %s" % shortdesc
24 desc = shortdesc
25
26 desc = textwrap.dedent(desc)
27
25 return (shortdesc, desc)
28 return (shortdesc, desc)
26
29
27 def get_opts(opts):
30 def get_opts(opts):
28 for opt in opts:
31 for opt in opts:
29 if len(opt) == 5:
32 if len(opt) == 5:
30 shortopt, longopt, default, desc, optlabel = opt
33 shortopt, longopt, default, desc, optlabel = opt
31 else:
34 else:
32 shortopt, longopt, default, desc = opt
35 shortopt, longopt, default, desc = opt
33 allopts = []
36 allopts = []
34 if shortopt:
37 if shortopt:
35 allopts.append("-%s" % shortopt)
38 allopts.append("-%s" % shortopt)
36 if longopt:
39 if longopt:
37 allopts.append("--%s" % longopt)
40 allopts.append("--%s" % longopt)
38 desc += default and _(" (default: %s)") % default or ""
41 desc += default and _(" (default: %s)") % default or ""
39 yield(", ".join(allopts), desc)
42 yield(", ".join(allopts), desc)
40
43
41 def get_cmd(cmd, cmdtable):
44 def get_cmd(cmd, cmdtable):
42 d = {}
45 d = {}
43 attr = cmdtable[cmd]
46 attr = cmdtable[cmd]
44 cmds = cmd.lstrip("^").split("|")
47 cmds = cmd.lstrip("^").split("|")
45
48
46 d['cmd'] = cmds[0]
49 d['cmd'] = cmds[0]
47 d['aliases'] = cmd.split("|")[1:]
50 d['aliases'] = cmd.split("|")[1:]
48 d['desc'] = get_desc(attr[0].__doc__)
51 d['desc'] = get_desc(attr[0].__doc__)
49 d['opts'] = list(get_opts(attr[1]))
52 d['opts'] = list(get_opts(attr[1]))
50
53
51 s = 'hg ' + cmds[0]
54 s = 'hg ' + cmds[0]
52 if len(attr) > 2:
55 if len(attr) > 2:
53 if not attr[2].startswith('hg'):
56 if not attr[2].startswith('hg'):
54 s += ' ' + attr[2]
57 s += ' ' + attr[2]
55 else:
58 else:
56 s = attr[2]
59 s = attr[2]
57 d['synopsis'] = s.strip()
60 d['synopsis'] = s.strip()
58
61
59 return d
62 return d
60
63
61 def section(ui, s):
64 def section(ui, s):
62 ui.write("%s\n%s\n\n" % (s, "-" * encoding.colwidth(s)))
65 ui.write("%s\n%s\n\n" % (s, "-" * encoding.colwidth(s)))
63
66
64 def subsection(ui, s):
67 def subsection(ui, s):
65 ui.write("%s\n%s\n\n" % (s, '"' * encoding.colwidth(s)))
68 ui.write("%s\n%s\n\n" % (s, '"' * encoding.colwidth(s)))
66
69
67
70
68 def show_doc(ui):
71 def show_doc(ui):
69 # print options
72 # print options
70 section(ui, _("Options"))
73 section(ui, _("Options"))
71 for optstr, desc in get_opts(globalopts):
74 for optstr, desc in get_opts(globalopts):
72 ui.write("%s\n %s\n\n" % (optstr, desc))
75 ui.write("%s\n%s\n\n" % (optstr, desc))
73
76
74 # print cmds
77 # print cmds
75 section(ui, _("Commands"))
78 section(ui, _("Commands"))
76 commandprinter(ui, table)
79 commandprinter(ui, table)
77
80
78 # print topics
81 # print topics
79 for names, sec, doc in helptable:
82 for names, sec, doc in helptable:
80 for name in names:
83 for name in names:
81 ui.write(".. _%s:\n" % name)
84 ui.write(".. _%s:\n" % name)
82 ui.write("\n")
85 ui.write("\n")
83 section(ui, sec)
86 section(ui, sec)
84 if hasattr(doc, '__call__'):
87 if hasattr(doc, '__call__'):
85 doc = doc()
88 doc = doc()
86 ui.write(doc)
89 ui.write(doc)
87 ui.write("\n")
90 ui.write("\n")
88
91
89 def commandprinter(ui, cmdtable):
92 def commandprinter(ui, cmdtable):
90 h = {}
93 h = {}
91 for c, attr in cmdtable.items():
94 for c, attr in cmdtable.items():
92 f = c.split("|")[0]
95 f = c.split("|")[0]
93 f = f.lstrip("^")
96 f = f.lstrip("^")
94 h[f] = c
97 h[f] = c
95 cmds = h.keys()
98 cmds = h.keys()
96 cmds.sort()
99 cmds.sort()
97
100
98 for f in cmds:
101 for f in cmds:
99 if f.startswith("debug"):
102 if f.startswith("debug"):
100 continue
103 continue
101 d = get_cmd(h[f], cmdtable)
104 d = get_cmd(h[f], cmdtable)
102 subsection(ui, d['cmd'])
105 subsection(ui, d['cmd'])
103 # synopsis
106 # synopsis
104 ui.write("``%s``\n" % d['synopsis'].replace("hg ","", 1))
107 ui.write("``%s``\n" % d['synopsis'].replace("hg ","", 1))
108 ui.write("\n")
105 # description
109 # description
106 ui.write("%s\n\n" % d['desc'][1])
110 ui.write("%s\n\n" % d['desc'][1])
107 # options
111 # options
108 opt_output = list(d['opts'])
112 opt_output = list(d['opts'])
109 if opt_output:
113 if opt_output:
110 opts_len = max([len(line[0]) for line in opt_output])
114 opts_len = max([len(line[0]) for line in opt_output])
111 ui.write(_(" options:\n\n"))
115 ui.write(_("options:\n\n"))
112 for optstr, desc in opt_output:
116 for optstr, desc in opt_output:
113 if desc:
117 if desc:
114 s = "%-*s %s" % (opts_len, optstr, desc)
118 s = "%-*s %s" % (opts_len, optstr, desc)
115 else:
119 else:
116 s = optstr
120 s = optstr
117 ui.write(" %s\n" % s)
121 ui.write("%s\n" % s)
118 ui.write("\n")
122 ui.write("\n")
119 # aliases
123 # aliases
120 if d['aliases']:
124 if d['aliases']:
121 ui.write(_(" aliases: %s\n\n") % " ".join(d['aliases']))
125 ui.write(_(" aliases: %s\n\n") % " ".join(d['aliases']))
122
126
123
127
124 if __name__ == "__main__":
128 if __name__ == "__main__":
125 show_doc(sys.stdout)
129 show_doc(sys.stdout)
General Comments 0
You need to be logged in to leave comments. Login now