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

Erik Zielke ez at aragost.com
Fri Oct 22 03:39:24 CDT 2010


# HG changeset patch
# User Erik Zielke <ez at aragost.com>
# Date 1287735870 -7200
# Node ID 5dd8d657ce08255a2f6edb0fd533c54d5babe87a
# Parent  e4bbb6b47f03239bab597977144c9c6680561959
hg serve help: html output when docutils available.

When docutils is available, then there will be generated html output,
otherwise it will fail back to generate the ascii help as before.

diff -r e4bbb6b47f03 -r 5dd8d657ce08 mercurial/hgweb/webcommands.py
--- a/mercurial/hgweb/webcommands.py	Fri Oct 22 10:21:22 2010 +0200
+++ b/mercurial/hgweb/webcommands.py	Fri Oct 22 10:24:30 2010 +0200
@@ -14,8 +14,10 @@
 from common import HTTP_OK, HTTP_FORBIDDEN, HTTP_NOT_FOUND
 from mercurial import graphmod
 from mercurial import help as helpmod
+from mercurial.commands import table
 from mercurial.i18n import _
 
+
 # __all__ is populated with the allowed commands. Be sure to add to it if
 # you're adding a new command, or the new command won't work.
 
@@ -735,6 +737,70 @@
         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
+
+    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(table)
+
+        if name not in h:
+            raise error.UnknownCommand(name)
+
+        command = helpmod.get_cmd(h[name], 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 +841,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 e4bbb6b47f03 -r 5dd8d657ce08 mercurial/templates/paper/help.tmpl
--- a/mercurial/templates/paper/help.tmpl	Fri Oct 22 10:21:22 2010 +0200
+++ b/mercurial/templates/paper/help.tmpl	Fri Oct 22 10:24:30 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