##// END OF EJS Templates
gendoc: make anchors for help topic names
Martin Geisler -
r9297:3d78a6c5 default
parent child Browse files
Show More
@@ -1,112 +1,115 b''
1 import os, sys, textwrap
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.commands import table, globalopts
7 from mercurial.commands import table, globalopts
8 from mercurial.i18n import _
8 from mercurial.i18n import _
9 from mercurial.help import helptable
9 from mercurial.help import helptable
10
10
11 def get_desc(docstr):
11 def get_desc(docstr):
12 if not docstr:
12 if not docstr:
13 return "", ""
13 return "", ""
14 # sanitize
14 # sanitize
15 docstr = docstr.strip("\n")
15 docstr = docstr.strip("\n")
16 docstr = docstr.rstrip()
16 docstr = docstr.rstrip()
17 shortdesc = docstr.splitlines()[0].strip()
17 shortdesc = docstr.splitlines()[0].strip()
18
18
19 i = docstr.find("\n")
19 i = docstr.find("\n")
20 if i != -1:
20 if i != -1:
21 desc = docstr[i+2:]
21 desc = docstr[i+2:]
22 else:
22 else:
23 desc = " %s" % shortdesc
23 desc = " %s" % shortdesc
24 return (shortdesc, desc)
24 return (shortdesc, desc)
25
25
26 def get_opts(opts):
26 def get_opts(opts):
27 for shortopt, longopt, default, desc in opts:
27 for shortopt, longopt, default, desc in opts:
28 allopts = []
28 allopts = []
29 if shortopt:
29 if shortopt:
30 allopts.append("-%s" % shortopt)
30 allopts.append("-%s" % shortopt)
31 if longopt:
31 if longopt:
32 allopts.append("--%s" % longopt)
32 allopts.append("--%s" % longopt)
33 desc += default and _(" (default: %s)") % default or ""
33 desc += default and _(" (default: %s)") % default or ""
34 yield(", ".join(allopts), desc)
34 yield(", ".join(allopts), desc)
35
35
36 def get_cmd(cmd):
36 def get_cmd(cmd):
37 d = {}
37 d = {}
38 attr = table[cmd]
38 attr = table[cmd]
39 cmds = cmd.lstrip("^").split("|")
39 cmds = cmd.lstrip("^").split("|")
40
40
41 d['cmd'] = cmds[0]
41 d['cmd'] = cmds[0]
42 d['aliases'] = cmd.split("|")[1:]
42 d['aliases'] = cmd.split("|")[1:]
43 d['desc'] = get_desc(attr[0].__doc__)
43 d['desc'] = get_desc(attr[0].__doc__)
44 d['opts'] = list(get_opts(attr[1]))
44 d['opts'] = list(get_opts(attr[1]))
45
45
46 s = 'hg ' + cmds[0]
46 s = 'hg ' + cmds[0]
47 if len(attr) > 2:
47 if len(attr) > 2:
48 if not attr[2].startswith('hg'):
48 if not attr[2].startswith('hg'):
49 s += ' ' + attr[2]
49 s += ' ' + attr[2]
50 else:
50 else:
51 s = attr[2]
51 s = attr[2]
52 d['synopsis'] = s
52 d['synopsis'] = s
53
53
54 return d
54 return d
55
55
56 def show_doc(ui):
56 def show_doc(ui):
57 def section(s):
57 def section(s):
58 ui.write("%s\n%s\n\n" % (s, "-" * len(s)))
58 ui.write("%s\n%s\n\n" % (s, "-" * len(s)))
59 def subsection(s):
59 def subsection(s):
60 ui.write("%s\n%s\n\n" % (s, '"' * len(s)))
60 ui.write("%s\n%s\n\n" % (s, '"' * len(s)))
61
61
62 # print options
62 # print options
63 section(_("OPTIONS"))
63 section(_("OPTIONS"))
64 for optstr, desc in get_opts(globalopts):
64 for optstr, desc in get_opts(globalopts):
65 ui.write("%s\n %s\n\n" % (optstr, desc))
65 ui.write("%s\n %s\n\n" % (optstr, desc))
66
66
67 # print cmds
67 # print cmds
68 section(_("COMMANDS"))
68 section(_("COMMANDS"))
69 h = {}
69 h = {}
70 for c, attr in table.items():
70 for c, attr in table.items():
71 f = c.split("|")[0]
71 f = c.split("|")[0]
72 f = f.lstrip("^")
72 f = f.lstrip("^")
73 h[f] = c
73 h[f] = c
74 cmds = h.keys()
74 cmds = h.keys()
75 cmds.sort()
75 cmds.sort()
76
76
77 for f in cmds:
77 for f in cmds:
78 if f.startswith("debug"): continue
78 if f.startswith("debug"): continue
79 d = get_cmd(h[f])
79 d = get_cmd(h[f])
80 # synopsis
80 # synopsis
81 ui.write(".. _%s:\n\n" % d['cmd'])
81 ui.write(".. _%s:\n\n" % d['cmd'])
82 ui.write("``%s``\n" % d['synopsis'].replace("hg ","", 1))
82 ui.write("``%s``\n" % d['synopsis'].replace("hg ","", 1))
83 # description
83 # description
84 ui.write("%s\n\n" % d['desc'][1])
84 ui.write("%s\n\n" % d['desc'][1])
85 # options
85 # options
86 opt_output = list(d['opts'])
86 opt_output = list(d['opts'])
87 if opt_output:
87 if opt_output:
88 opts_len = max([len(line[0]) for line in opt_output])
88 opts_len = max([len(line[0]) for line in opt_output])
89 ui.write(_(" options:\n\n"))
89 ui.write(_(" options:\n\n"))
90 for optstr, desc in opt_output:
90 for optstr, desc in opt_output:
91 if desc:
91 if desc:
92 s = "%-*s %s" % (opts_len, optstr, desc)
92 s = "%-*s %s" % (opts_len, optstr, desc)
93 else:
93 else:
94 s = optstr
94 s = optstr
95 s = textwrap.fill(s, initial_indent=4 * " ",
95 s = textwrap.fill(s, initial_indent=4 * " ",
96 subsequent_indent=(6 + opts_len) * " ")
96 subsequent_indent=(6 + opts_len) * " ")
97 ui.write("%s\n" % s)
97 ui.write("%s\n" % s)
98 ui.write("\n")
98 ui.write("\n")
99 # aliases
99 # aliases
100 if d['aliases']:
100 if d['aliases']:
101 ui.write(_(" aliases: %s\n\n") % " ".join(d['aliases']))
101 ui.write(_(" aliases: %s\n\n") % " ".join(d['aliases']))
102
102
103 # print topics
103 # print topics
104 for names, sec, doc in helptable:
104 for names, sec, doc in helptable:
105 for name in names:
106 ui.write(".. _%s:\n" % name)
107 ui.write("\n")
105 section(sec.upper())
108 section(sec.upper())
106 if callable(doc):
109 if callable(doc):
107 doc = doc()
110 doc = doc()
108 ui.write(doc)
111 ui.write(doc)
109 ui.write("\n")
112 ui.write("\n")
110
113
111 if __name__ == "__main__":
114 if __name__ == "__main__":
112 show_doc(sys.stdout)
115 show_doc(sys.stdout)
General Comments 0
You need to be logged in to leave comments. Login now