comparison mercurial/help.py @ 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 770190bff625
children 497deec204d1
comparison
equal deleted inserted replaced
16709:9eca39a91964 16710:a17983680f12
4 # 4 #
5 # This software may be used and distributed according to the terms of the 5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2 or any later version. 6 # GNU General Public License version 2 or any later version.
7 7
8 from i18n import gettext, _ 8 from i18n import gettext, _
9 import sys, os 9 import itertools, sys, os
10 import extensions, revset, fileset, templatekw, templatefilters, filemerge 10 import extensions, revset, fileset, templatekw, templatefilters, filemerge
11 import util 11 import encoding, util
12 12
13 def listexts(header, exts, indent=1): 13 def listexts(header, exts, indent=1):
14 '''return a text listing of the given extensions''' 14 '''return a text listing of the given extensions'''
15 if not exts: 15 if not exts:
16 return '' 16 return ''
24 def extshelp(): 24 def extshelp():
25 doc = loaddoc('extensions')() 25 doc = loaddoc('extensions')()
26 doc += listexts(_('enabled extensions:'), extensions.enabled()) 26 doc += listexts(_('enabled extensions:'), extensions.enabled())
27 doc += listexts(_('disabled extensions:'), extensions.disabled()) 27 doc += listexts(_('disabled extensions:'), extensions.disabled())
28 return doc 28 return doc
29
30 def topicmatch(kw):
31 """Return help topics matching kw.
32
33 Returns {'section': [(name, summary), ...], ...} where section is
34 one of topics, commands, extensions, or extensioncommands.
35 """
36 kw = encoding.lower(kw)
37 def lowercontains(container):
38 return kw in encoding.lower(_(container))
39 results = {'topics': [],
40 'commands': [],
41 'extensions': [],
42 'extensioncommands': [],
43 }
44 for names, header, doc in helptable:
45 if (sum(map(lowercontains, names))
46 or lowercontains(header)
47 or lowercontains(doc())):
48 results['topics'].append((names[0], header))
49 import commands # avoid cycle
50 for cmd, entry in commands.table.iteritems():
51 if cmd.startswith('debug'):
52 continue
53 if len(entry) == 3:
54 summary = entry[2]
55 else:
56 summary = ''
57 docs = getattr(entry[0], '__doc__', None) or ''
58 if kw in cmd or lowercontains(summary) or lowercontains(docs):
59 doclines = _(docs).splitlines()
60 if doclines:
61 summary = doclines[0]
62 cmdname = cmd.split('|')[0].lstrip('^')
63 results['commands'].append((cmdname, summary))
64 for name, docs in itertools.chain(
65 extensions.enabled().iteritems(),
66 extensions.disabled().iteritems()):
67 # extensions.load ignores the UI argument
68 mod = extensions.load(None, name, '')
69 if lowercontains(name) or lowercontains(docs):
70 results['extensions'].append((name, _(docs).splitlines()[0]))
71 for cmd, entry in getattr(mod, 'cmdtable', {}).iteritems():
72 if kw in cmd or lowercontains(entry[2]):
73 cmdname = cmd.split('|')[0].lstrip('^')
74 results['extensioncommands'].append(
75 (cmdname, _(getattr(cmd, '__doc__', ''))))
76 return results
29 77
30 def loaddoc(topic): 78 def loaddoc(topic):
31 """Return a delayed loader for help/topic.txt.""" 79 """Return a delayed loader for help/topic.txt."""
32 80
33 def loader(): 81 def loader():