[PATCH 3 of 3] hg serve help: HTML output when docutils available

Erik Zielke ez at aragost.com
Fri Oct 22 05:48:00 CDT 2010


# HG changeset patch
# User Erik Zielke <ez at aragost.com>
# Date 1287744391 -7200
# Branch stable
# Node ID c645b773b9274099b8bd8bdef58cf49512039931
# Parent  38e8cecdd528a7323e168cc01193c8b916455c32
hg serve help: HTML output when docutils available.

HTML output will be generated for 'hg serve' help when docutils is
available, otherwise it will fall back to use the ascii output used on
the commandline.

diff -r 38e8cecdd528 -r c645b773b927 mercurial/help.py
--- a/mercurial/help.py	Fri Oct 22 12:40:23 2010 +0200
+++ b/mercurial/help.py	Fri Oct 22 12:46:31 2010 +0200
@@ -162,7 +162,7 @@
         yield(", ".join(allopts), desc)
 
 
-def cleancommands(cmdtable):
+def cleancommands(cmdtable, includealiases=False):
     """"Returns a dictionary from command name to command function and a sorted list of command names.
 
         The command names dictionary and list only includes the commands not their aliases, and the
@@ -170,9 +170,16 @@
     """
     h = {}
     for c, attr in cmdtable.items():
-        f = c.split("|")[0]
-        f = f.lstrip("^")
-        h[f] = c
+        
+        if includealiases:
+            names = c.split("|")
+        else:
+            names = [c.split("|")[0]]
+        
+        for name in names:
+            name = name.lstrip("^")
+            h[name] = c
+
     cmds = h.keys()
     cmds.sort()
     return h, cmds
diff -r 38e8cecdd528 -r c645b773b927 mercurial/hgweb/webcommands.py
--- a/mercurial/hgweb/webcommands.py	Fri Oct 22 12:40:23 2010 +0200
+++ b/mercurial/hgweb/webcommands.py	Fri Oct 22 12:46:31 2010 +0200
@@ -735,6 +735,73 @@
         doc = _('(no help text available)')
     return doc
 
+
+def _helphtml(ui, name=None):
+    from docutils.parsers.rst import roles
+    from docutils import nodes, utils
+    import docutils.core
+    from mercurial import commands  # avoid cycle
+
+    def rolehg(name, rawtext, text, lineno, inliner,  options={}, content=[]):
+        text = "hg " + text
+        linktext = nodes.literal(rawtext, text)
+        parts = text.split()
+        cmd, args = parts[1], parts[2:]
+        if cmd == 'help' and args:
+            cmd = args[0] # link to 'dates' for 'hg help dates'
+        node = nodes.reference(rawtext, '', linktext, refuri='%s' % cmd)
+        return [node], []
+
+    def helpcmd(name):
+        h, cmds = helpmod.cleancommands(commands.table, includealiases=True)
+
+        if name not in h:
+            raise error.UnknownCommand(name)
+
+        command = helpmod.get_cmd(h[name], commands.table)
+        roles.register_local_role("hg", rolehg)
+        ui.pushbuffer()
+        helpmod.commandprinter(ui, command)
+        helprst = ui.popbuffer()
+        parts   = docutils.core.publish_parts(helprst, writer_name='html',
+                                              settings_overrides=
+                                              {'initial_header_level': '4'})
+        ui.write(parts['html_body'])
+
+    def helptopic(name):
+        for names, header, doc in helpmod.helptable:
+            if name in names:
+                break
+        else:
+            raise error.UnknownCommand(name)
+
+        # description
+        if not doc:
+            doc = _("(no help text available)")
+        if hasattr(doc, '__call__'):
+            doc = doc()
+
+        roles.register_local_role("hg", rolehg)
+        parts = docutils.core.publish_parts(doc, writer_name='html',
+                                             settings_overrides=
+                                             {'initial_header_level': '4'})
+        ui.write(parts['html_body'])
+
+
+    i = None
+    queries = (helptopic, helpcmd)
+    for f in queries:
+        try:
+            f(name)
+            i = None
+            break
+        except error.UnknownCommand, inst:
+            i = inst
+    if i:
+        raise i
+
+
+
 def help(web, req, tmpl):
     from mercurial import commands # avoid cycle
 
@@ -775,9 +842,18 @@
 
     u = webutil.wsgiui()
     u.pushbuffer()
+    html = False
     try:
-        commands.help_(u, topicname)
+        try:
+            _helphtml(u, topicname)
+            html = True
+        except ImportError:
+            commands.help_(u, topicname)
     except error.UnknownCommand:
         raise ErrorResponse(HTTP_NOT_FOUND)
     doc = u.popbuffer()
+
+    if not html:
+        doc = '<pre>\n' + cgi.escape(doc) + '</pre>\n'
+
     return tmpl('help', topic=topicname, doc=doc)
diff -r 38e8cecdd528 -r c645b773b927 mercurial/templates/paper/help.tmpl
--- a/mercurial/templates/paper/help.tmpl	Fri Oct 22 12:40:23 2010 +0200
+++ b/mercurial/templates/paper/help.tmpl	Fri Oct 22 12:46:31 2010 +0200
@@ -34,9 +34,7 @@
 <div id="hint">find changesets by author, revision,
 files, or words in the commit message</div>
 </form>
-<pre>
-{doc|escape}
-</pre>
+{doc}
 </div>
 </div>
 


More information about the Mercurial-devel mailing list