[PATCH 3 of 3] help: support 'hg help topic:section' (issue2804)

Yun Lee yun.lee.bj at gmail.com
Sat May 28 07:02:37 CDT 2011


# HG changeset patch
# User Yun Lee <yun.lee.bj at gmail.com>
# Date 1306583669 -28800
# Node ID d24e00381f8dfcfa3f371ce0f926ddd0a5fec1b8
# Parent  cca76ef2ae3333badd5d553a5c12f8bef7797eee
help: support 'hg help topic:section' (issue2804)

diff -r cca76ef2ae33 -r d24e00381f8d mercurial/commands.py
--- a/mercurial/commands.py	Sat May 28 19:51:32 2011 +0800
+++ b/mercurial/commands.py	Sat May 28 19:54:29 2011 +0800
@@ -2711,8 +2711,59 @@
             addglobalopts(True)
 
     def helptopic(name):
+        def fetchsectionblocks(text, sectionname, indent):
+            blocks = minirst.findblocks(text)
+            for b in blocks:
+                b['indent'] += indent
+            blocks = minirst.findliteralblocks(blocks)
+            blocks, pruned = minirst.prunecontainers(blocks, [])
+            blocks = minirst.findsections(blocks)
+            blocks = minirst.inlineliterals(blocks)
+            blocks = minirst.hgrole(blocks)
+            blocks = minirst.splitparagraphs(blocks)
+            blocks = minirst.updatefieldlists(blocks)
+            blocks = minirst.updateoptionlists(blocks)
+            blocks = minirst.addmargins(blocks)
+            blocks = minirst.prunecomments(blocks)
+            blocks = minirst.findadmonitions(blocks)
+
+            isinsection = False
+            sectionblocks = []
+
+            for block in blocks:
+                if isinsection and block['type'] == 'section':
+                    break
+
+                if block['type'] == 'section' and sectionname in block['lines'][0]:
+                    isinsection = True
+
+                if isinsection:
+                    sectionblocks.append(block)
+
+            return sectionblocks
+
+
+        def searchsection(text, sectionname, width, indent=0):
+            sectionblocks = fetchsectionblocks(text, sectionname, indent)
+
+            if not sectionblocks:
+                return (None, None)
+
+            firstparagraph = ''.join(sectionblocks[2]['lines'])
+            header = firstparagraph.split('.')[0]
+            description = '\n'.join(minirst.formatblock(b, width) \
+                                    for b in sectionblocks[2:])
+
+            return header, description
+
+        if ':' in name:
+            topicname, sectionname = name.split(':')
+        else:
+            topicname, sectionname = (name, None)
+
+
         for names, header, doc in help.helptable:
-            if name in names:
+            if topicname in names:
                 break
         else:
             raise error.UnknownCommand(name)
@@ -2723,14 +2774,24 @@
         if hasattr(doc, '__call__'):
             doc = doc()
 
-        ui.write("%s\n\n" % header)
-        ui.write("%s\n" % minirst.format(doc, textwidth, indent=4))
-        try:
-            cmdutil.findcmd(name, table)
-            ui.write(_('\nuse "hg help -c %s" to see help for '
-                       'the %s command\n') % (name, name))
-        except error.UnknownCommand:
-            pass
+        if sectionname:
+            sectionheader, sectiondesc = \
+                    searchsection(doc, sectionname, textwidth, indent=4)
+            if sectionheader and sectiondesc:
+                ui.write("%s\n\n" % sectionheader)
+                ui.write("%s\n" % sectiondesc)
+            else:
+                raise error.UnknownCommand(name)
+        else:
+            ui.write("%s\n\n" % header)
+            ui.write("%s\n" % minirst.format(doc, textwidth, indent=4))
+            try:
+                cmdutil.findcmd(name, table)
+                ui.write(_('\nuse "hg help -c %s" to see help for '
+                           'the %s command\n') % (name, name))
+            except error.UnknownCommand:
+                pass
+
 
     def helpext(name):
         try:


More information about the Mercurial-devel mailing list