##// END OF EJS Templates
gendoc: make commnd __doc__ and extension __doc__ as translatable...
Takumi IINO -
r19231:814291b5 default
parent child Browse files
Show More
@@ -1,156 +1,156 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 import encoding
7 from mercurial import encoding
8 from mercurial import minirst
8 from mercurial import minirst
9 from mercurial.commands import table, globalopts
9 from mercurial.commands import table, globalopts
10 from mercurial.i18n import _
10 from mercurial.i18n import gettext, _
11 from mercurial.help import helptable
11 from mercurial.help import helptable
12 from mercurial import extensions
12 from mercurial import extensions
13 from mercurial import util
13 from mercurial import util
14
14
15 def get_desc(docstr):
15 def get_desc(docstr):
16 if not docstr:
16 if not docstr:
17 return "", ""
17 return "", ""
18 # sanitize
18 # sanitize
19 docstr = docstr.strip("\n")
19 docstr = docstr.strip("\n")
20 docstr = docstr.rstrip()
20 docstr = docstr.rstrip()
21 shortdesc = docstr.splitlines()[0].strip()
21 shortdesc = docstr.splitlines()[0].strip()
22
22
23 i = docstr.find("\n")
23 i = docstr.find("\n")
24 if i != -1:
24 if i != -1:
25 desc = docstr[i + 2:]
25 desc = docstr[i + 2:]
26 else:
26 else:
27 desc = shortdesc
27 desc = shortdesc
28
28
29 desc = textwrap.dedent(desc)
29 desc = textwrap.dedent(desc)
30
30
31 return (shortdesc, desc)
31 return (shortdesc, desc)
32
32
33 def get_opts(opts):
33 def get_opts(opts):
34 for opt in opts:
34 for opt in opts:
35 if len(opt) == 5:
35 if len(opt) == 5:
36 shortopt, longopt, default, desc, optlabel = opt
36 shortopt, longopt, default, desc, optlabel = opt
37 else:
37 else:
38 shortopt, longopt, default, desc = opt
38 shortopt, longopt, default, desc = opt
39 allopts = []
39 allopts = []
40 if shortopt:
40 if shortopt:
41 allopts.append("-%s" % shortopt)
41 allopts.append("-%s" % shortopt)
42 if longopt:
42 if longopt:
43 allopts.append("--%s" % longopt)
43 allopts.append("--%s" % longopt)
44 desc += default and _(" (default: %s)") % default or ""
44 desc += default and _(" (default: %s)") % default or ""
45 yield (", ".join(allopts), desc)
45 yield (", ".join(allopts), desc)
46
46
47 def get_cmd(cmd, cmdtable):
47 def get_cmd(cmd, cmdtable):
48 d = {}
48 d = {}
49 attr = cmdtable[cmd]
49 attr = cmdtable[cmd]
50 cmds = cmd.lstrip("^").split("|")
50 cmds = cmd.lstrip("^").split("|")
51
51
52 d['cmd'] = cmds[0]
52 d['cmd'] = cmds[0]
53 d['aliases'] = cmd.split("|")[1:]
53 d['aliases'] = cmd.split("|")[1:]
54 d['desc'] = get_desc(attr[0].__doc__)
54 d['desc'] = get_desc(gettext(attr[0].__doc__))
55 d['opts'] = list(get_opts(attr[1]))
55 d['opts'] = list(get_opts(attr[1]))
56
56
57 s = 'hg ' + cmds[0]
57 s = 'hg ' + cmds[0]
58 if len(attr) > 2:
58 if len(attr) > 2:
59 if not attr[2].startswith('hg'):
59 if not attr[2].startswith('hg'):
60 s += ' ' + attr[2]
60 s += ' ' + attr[2]
61 else:
61 else:
62 s = attr[2]
62 s = attr[2]
63 d['synopsis'] = s.strip()
63 d['synopsis'] = s.strip()
64
64
65 return d
65 return d
66
66
67 def show_doc(ui):
67 def show_doc(ui):
68 # print options
68 # print options
69 ui.write(minirst.section(_("Options")))
69 ui.write(minirst.section(_("Options")))
70 for optstr, desc in get_opts(globalopts):
70 for optstr, desc in get_opts(globalopts):
71 ui.write("%s\n %s\n\n" % (optstr, desc))
71 ui.write("%s\n %s\n\n" % (optstr, desc))
72
72
73 # print cmds
73 # print cmds
74 ui.write(minirst.section(_("Commands")))
74 ui.write(minirst.section(_("Commands")))
75 commandprinter(ui, table, minirst.subsection)
75 commandprinter(ui, table, minirst.subsection)
76
76
77 # print topics
77 # print topics
78 for names, sec, doc in helptable:
78 for names, sec, doc in helptable:
79 if names[0] == "config":
79 if names[0] == "config":
80 # The config help topic is included in the hgrc.5 man
80 # The config help topic is included in the hgrc.5 man
81 # page.
81 # page.
82 continue
82 continue
83 for name in names:
83 for name in names:
84 ui.write(".. _%s:\n" % name)
84 ui.write(".. _%s:\n" % name)
85 ui.write("\n")
85 ui.write("\n")
86 ui.write(minirst.section(sec))
86 ui.write(minirst.section(sec))
87 if util.safehasattr(doc, '__call__'):
87 if util.safehasattr(doc, '__call__'):
88 doc = doc()
88 doc = doc()
89 ui.write(doc)
89 ui.write(doc)
90 ui.write("\n")
90 ui.write("\n")
91
91
92 ui.write(minirst.section(_("Extensions")))
92 ui.write(minirst.section(_("Extensions")))
93 ui.write(_("This section contains help for extensions that are "
93 ui.write(_("This section contains help for extensions that are "
94 "distributed together with Mercurial. Help for other "
94 "distributed together with Mercurial. Help for other "
95 "extensions is available in the help system."))
95 "extensions is available in the help system."))
96 ui.write("\n\n"
96 ui.write("\n\n"
97 ".. contents::\n"
97 ".. contents::\n"
98 " :class: htmlonly\n"
98 " :class: htmlonly\n"
99 " :local:\n"
99 " :local:\n"
100 " :depth: 1\n\n")
100 " :depth: 1\n\n")
101
101
102 for extensionname in sorted(allextensionnames()):
102 for extensionname in sorted(allextensionnames()):
103 mod = extensions.load(None, extensionname, None)
103 mod = extensions.load(None, extensionname, None)
104 ui.write(minirst.subsection(extensionname))
104 ui.write(minirst.subsection(extensionname))
105 ui.write("%s\n\n" % mod.__doc__)
105 ui.write("%s\n\n" % gettext(mod.__doc__))
106 cmdtable = getattr(mod, 'cmdtable', None)
106 cmdtable = getattr(mod, 'cmdtable', None)
107 if cmdtable:
107 if cmdtable:
108 ui.write(minirst.subsubsection(_('Commands')))
108 ui.write(minirst.subsubsection(_('Commands')))
109 commandprinter(ui, cmdtable, minirst.subsubsubsection)
109 commandprinter(ui, cmdtable, minirst.subsubsubsection)
110
110
111 def commandprinter(ui, cmdtable, sectionfunc):
111 def commandprinter(ui, cmdtable, sectionfunc):
112 h = {}
112 h = {}
113 for c, attr in cmdtable.items():
113 for c, attr in cmdtable.items():
114 f = c.split("|")[0]
114 f = c.split("|")[0]
115 f = f.lstrip("^")
115 f = f.lstrip("^")
116 h[f] = c
116 h[f] = c
117 cmds = h.keys()
117 cmds = h.keys()
118 cmds.sort()
118 cmds.sort()
119
119
120 for f in cmds:
120 for f in cmds:
121 if f.startswith("debug"):
121 if f.startswith("debug"):
122 continue
122 continue
123 d = get_cmd(h[f], cmdtable)
123 d = get_cmd(h[f], cmdtable)
124 ui.write(sectionfunc(d['cmd']))
124 ui.write(sectionfunc(d['cmd']))
125 # synopsis
125 # synopsis
126 ui.write("::\n\n")
126 ui.write("::\n\n")
127 synopsislines = d['synopsis'].splitlines()
127 synopsislines = d['synopsis'].splitlines()
128 for line in synopsislines:
128 for line in synopsislines:
129 # some commands (such as rebase) have a multi-line
129 # some commands (such as rebase) have a multi-line
130 # synopsis
130 # synopsis
131 ui.write(" %s\n" % line)
131 ui.write(" %s\n" % line)
132 ui.write('\n')
132 ui.write('\n')
133 # description
133 # description
134 ui.write("%s\n\n" % d['desc'][1])
134 ui.write("%s\n\n" % d['desc'][1])
135 # options
135 # options
136 opt_output = list(d['opts'])
136 opt_output = list(d['opts'])
137 if opt_output:
137 if opt_output:
138 opts_len = max([len(line[0]) for line in opt_output])
138 opts_len = max([len(line[0]) for line in opt_output])
139 ui.write(_("Options:\n\n"))
139 ui.write(_("Options:\n\n"))
140 for optstr, desc in opt_output:
140 for optstr, desc in opt_output:
141 if desc:
141 if desc:
142 s = "%-*s %s" % (opts_len, optstr, desc)
142 s = "%-*s %s" % (opts_len, optstr, desc)
143 else:
143 else:
144 s = optstr
144 s = optstr
145 ui.write("%s\n" % s)
145 ui.write("%s\n" % s)
146 ui.write("\n")
146 ui.write("\n")
147 # aliases
147 # aliases
148 if d['aliases']:
148 if d['aliases']:
149 ui.write(_(" aliases: %s\n\n") % " ".join(d['aliases']))
149 ui.write(_(" aliases: %s\n\n") % " ".join(d['aliases']))
150
150
151
151
152 def allextensionnames():
152 def allextensionnames():
153 return extensions.enabled().keys() + extensions.disabled().keys()
153 return extensions.enabled().keys() + extensions.disabled().keys()
154
154
155 if __name__ == "__main__":
155 if __name__ == "__main__":
156 show_doc(sys.stdout)
156 show_doc(sys.stdout)
General Comments 0
You need to be logged in to leave comments. Login now