changeset 16710:a17983680f12

help: introduce topicmatch for finding topics matching a keyword
author Augie Fackler <raf@durin42.com>
date Sun, 13 May 2012 04:27:08 -0500
parents 9eca39a91964
children 497deec204d1
files mercurial/help.py
diffstat 1 files changed, 50 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/help.py	Sun May 13 04:06:07 2012 -0500
+++ b/mercurial/help.py	Sun May 13 04:27:08 2012 -0500
@@ -6,9 +6,9 @@
 # GNU General Public License version 2 or any later version.
 
 from i18n import gettext, _
-import sys, os
+import itertools, sys, os
 import extensions, revset, fileset, templatekw, templatefilters, filemerge
-import util
+import encoding, util
 
 def listexts(header, exts, indent=1):
     '''return a text listing of the given extensions'''
@@ -27,6 +27,54 @@
     doc += listexts(_('disabled extensions:'), extensions.disabled())
     return doc
 
+def topicmatch(kw):
+    """Return help topics matching kw.
+
+    Returns {'section': [(name, summary), ...], ...} where section is
+    one of topics, commands, extensions, or extensioncommands.
+    """
+    kw = encoding.lower(kw)
+    def lowercontains(container):
+        return kw in encoding.lower(_(container))
+    results = {'topics': [],
+               'commands': [],
+               'extensions': [],
+               'extensioncommands': [],
+               }
+    for names, header, doc in helptable:
+        if (sum(map(lowercontains, names))
+            or lowercontains(header)
+            or lowercontains(doc())):
+            results['topics'].append((names[0], header))
+    import commands # avoid cycle
+    for cmd, entry in commands.table.iteritems():
+        if cmd.startswith('debug'):
+            continue
+        if len(entry) == 3:
+            summary = entry[2]
+        else:
+            summary = ''
+        docs = getattr(entry[0], '__doc__', None) or ''
+        if kw in cmd or lowercontains(summary) or lowercontains(docs):
+            doclines = _(docs).splitlines()
+            if doclines:
+                summary = doclines[0]
+            cmdname = cmd.split('|')[0].lstrip('^')
+            results['commands'].append((cmdname, summary))
+    for name, docs in itertools.chain(
+        extensions.enabled().iteritems(),
+        extensions.disabled().iteritems()):
+        # extensions.load ignores the UI argument
+        mod = extensions.load(None, name, '')
+        if lowercontains(name) or lowercontains(docs):
+            results['extensions'].append((name, _(docs).splitlines()[0]))
+        for cmd, entry in getattr(mod, 'cmdtable', {}).iteritems():
+            if kw in cmd or lowercontains(entry[2]):
+                cmdname = cmd.split('|')[0].lstrip('^')
+                results['extensioncommands'].append(
+                    (cmdname, _(getattr(cmd, '__doc__', ''))))
+    return results
+
 def loaddoc(topic):
     """Return a delayed loader for help/topic.txt."""