gendoc.py
172 lines
| 4.8 KiB
| text/x-python
|
PythonLexer
/ doc / gendoc.py
Erik Zielke
|
r12780 | import os, sys, textwrap | ||
Benoit Boissinot
|
r1814 | # import from the live mercurial repo | ||
sys.path.insert(0, "..") | ||||
Cédric Duval
|
r9130 | # fall back to pure modules if required C extensions are not available | ||
sys.path.append(os.path.join('..', 'mercurial', 'pure')) | ||||
Matt Mackall
|
r5209 | from mercurial import demandimport; demandimport.enable() | ||
Martin Geisler
|
r9485 | from mercurial import encoding | ||
Benoit Boissinot
|
r1814 | from mercurial.commands import table, globalopts | ||
Cédric Duval
|
r9240 | from mercurial.i18n import _ | ||
Matt Mackall
|
r3797 | from mercurial.help import helptable | ||
Erik Zielke
|
r12781 | from mercurial import extensions | ||
Benoit Boissinot
|
r1814 | |||
def get_desc(docstr): | ||||
if not docstr: | ||||
return "", "" | ||||
# sanitize | ||||
docstr = docstr.strip("\n") | ||||
docstr = docstr.rstrip() | ||||
shortdesc = docstr.splitlines()[0].strip() | ||||
i = docstr.find("\n") | ||||
if i != -1: | ||||
Matt Mackall
|
r10282 | desc = docstr[i + 2:] | ||
Benoit Boissinot
|
r1814 | else: | ||
Erik Zielke
|
r12780 | desc = shortdesc | ||
desc = textwrap.dedent(desc) | ||||
Benoit Boissinot
|
r1814 | return (shortdesc, desc) | ||
def get_opts(opts): | ||||
FUJIWARA Katsunori
|
r11321 | for opt in opts: | ||
if len(opt) == 5: | ||||
shortopt, longopt, default, desc, optlabel = opt | ||||
else: | ||||
shortopt, longopt, default, desc = opt | ||||
Benoit Boissinot
|
r1814 | allopts = [] | ||
if shortopt: | ||||
allopts.append("-%s" % shortopt) | ||||
if longopt: | ||||
allopts.append("--%s" % longopt) | ||||
desc += default and _(" (default: %s)") % default or "" | ||||
yield(", ".join(allopts), desc) | ||||
Erik Zielke
|
r12756 | def get_cmd(cmd, cmdtable): | ||
Benoit Boissinot
|
r1814 | d = {} | ||
Erik Zielke
|
r12756 | attr = cmdtable[cmd] | ||
Benoit Boissinot
|
r1814 | cmds = cmd.lstrip("^").split("|") | ||
d['cmd'] = cmds[0] | ||||
d['aliases'] = cmd.split("|")[1:] | ||||
d['desc'] = get_desc(attr[0].__doc__) | ||||
d['opts'] = list(get_opts(attr[1])) | ||||
Matt Mackall
|
r7376 | |||
s = 'hg ' + cmds[0] | ||||
if len(attr) > 2: | ||||
if not attr[2].startswith('hg'): | ||||
Ori Avtalion
|
r8546 | s += ' ' + attr[2] | ||
Matt Mackall
|
r7376 | else: | ||
s = attr[2] | ||||
Martin Geisler
|
r9622 | d['synopsis'] = s.strip() | ||
Matt Mackall
|
r7376 | |||
Benoit Boissinot
|
r1814 | return d | ||
Martin Geisler
|
r12776 | def section(ui, s): | ||
ui.write("%s\n%s\n\n" % (s, "-" * encoding.colwidth(s))) | ||||
def subsection(ui, s): | ||||
ui.write("%s\n%s\n\n" % (s, '"' * encoding.colwidth(s))) | ||||
Erik Zielke
|
r12781 | def subsubsection(ui, s): | ||
ui.write("%s\n%s\n\n" % (s, "." * encoding.colwidth(s))) | ||||
def subsubsubsection(ui, s): | ||||
ui.write("%s\n%s\n\n" % (s, "#" * encoding.colwidth(s))) | ||||
Martin Geisler
|
r12776 | |||
Benoit Boissinot
|
r1814 | def show_doc(ui): | ||
# print options | ||||
Martin Geisler
|
r12776 | section(ui, _("Options")) | ||
Benoit Boissinot
|
r1814 | for optstr, desc in get_opts(globalopts): | ||
Martin Geisler
|
r12812 | ui.write("%s\n %s\n\n" % (optstr, desc)) | ||
Benoit Boissinot
|
r1814 | |||
# print cmds | ||||
Martin Geisler
|
r12776 | section(ui, _("Commands")) | ||
Erik Zielke
|
r12781 | commandprinter(ui, table, subsection) | ||
Erik Zielke
|
r12756 | |||
# print topics | ||||
for names, sec, doc in helptable: | ||||
for name in names: | ||||
ui.write(".. _%s:\n" % name) | ||||
ui.write("\n") | ||||
Martin Geisler
|
r12776 | section(ui, sec) | ||
Erik Zielke
|
r12756 | if hasattr(doc, '__call__'): | ||
doc = doc() | ||||
ui.write(doc) | ||||
ui.write("\n") | ||||
Erik Zielke
|
r12781 | section(ui, _("Extensions")) | ||
Martin Geisler
|
r12804 | ui.write(_("This section contains help for extensions that are distributed " | ||
Erik Zielke
|
r12781 | "together with Mercurial. Help for other extensions is available " | ||
"in the help system.")) | ||||
ui.write("\n\n" | ||||
".. contents::\n" | ||||
" :class: htmlonly\n" | ||||
" :local:\n" | ||||
" :depth: 1\n\n") | ||||
for extensionname in sorted(allextensionnames()): | ||||
mod = extensions.load(None, extensionname, None) | ||||
subsection(ui, extensionname) | ||||
ui.write("%s\n\n" % mod.__doc__) | ||||
cmdtable = getattr(mod, 'cmdtable', None) | ||||
if cmdtable: | ||||
subsubsection(ui, _('Commands')) | ||||
commandprinter(ui, cmdtable, subsubsubsection) | ||||
def commandprinter(ui, cmdtable, sectionfunc): | ||||
Benoit Boissinot
|
r1814 | h = {} | ||
Erik Zielke
|
r12756 | for c, attr in cmdtable.items(): | ||
Christian Ebert
|
r6488 | f = c.split("|")[0] | ||
f = f.lstrip("^") | ||||
h[f] = c | ||||
Benoit Boissinot
|
r1814 | cmds = h.keys() | ||
cmds.sort() | ||||
for f in cmds: | ||||
Matt Mackall
|
r10282 | if f.startswith("debug"): | ||
continue | ||||
Erik Zielke
|
r12756 | d = get_cmd(h[f], cmdtable) | ||
Erik Zielke
|
r12781 | sectionfunc(ui, d['cmd']) | ||
Christian Ebert
|
r6488 | # synopsis | ||
Martin Geisler
|
r12813 | ui.write("::\n\n") | ||
synopsislines = d['synopsis'].splitlines() | ||||
for line in synopsislines: | ||||
# some commands (such as rebase) have a multi-line | ||||
# synopsis | ||||
Martin Geisler
|
r12814 | ui.write(" %s\n" % line) | ||
Martin Geisler
|
r12813 | ui.write('\n') | ||
Christian Ebert
|
r6488 | # description | ||
ui.write("%s\n\n" % d['desc'][1]) | ||||
# options | ||||
opt_output = list(d['opts']) | ||||
if opt_output: | ||||
opts_len = max([len(line[0]) for line in opt_output]) | ||||
Erik Zielke
|
r12780 | ui.write(_("options:\n\n")) | ||
Christian Ebert
|
r6488 | for optstr, desc in opt_output: | ||
if desc: | ||||
s = "%-*s %s" % (opts_len, optstr, desc) | ||||
else: | ||||
s = optstr | ||||
Erik Zielke
|
r12780 | ui.write("%s\n" % s) | ||
Christian Ebert
|
r6488 | ui.write("\n") | ||
# aliases | ||||
if d['aliases']: | ||||
ui.write(_(" aliases: %s\n\n") % " ".join(d['aliases'])) | ||||
Benoit Boissinot
|
r1814 | |||
Matt Mackall
|
r3797 | |||
Erik Zielke
|
r12781 | def allextensionnames(): | ||
extensionnames = [] | ||||
extensionsdictionary = extensions.enabled()[0] | ||||
extensionnames.extend(extensionsdictionary.keys()) | ||||
extensionsdictionary = extensions.disabled()[0] | ||||
extensionnames.extend(extensionsdictionary.keys()) | ||||
return extensionnames | ||||
Benoit Boissinot
|
r1814 | if __name__ == "__main__": | ||
show_doc(sys.stdout) | ||||