Show More
@@ -1,203 +1,203 | |||
|
1 | 1 | # help.py - help data for mercurial |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2006 Matt Mackall <mpm@selenic.com> |
|
4 | 4 | # |
|
5 | 5 | # This software may be used and distributed according to the terms of the |
|
6 | 6 | # GNU General Public License version 2 or any later version. |
|
7 | 7 | |
|
8 | 8 | from i18n import gettext, _ |
|
9 | 9 | import itertools, sys, os |
|
10 | 10 | import extensions, revset, fileset, templatekw, templatefilters, filemerge |
|
11 | 11 | import encoding, util, minirst |
|
12 | 12 | |
|
13 | 13 | def listexts(header, exts, indent=1): |
|
14 | 14 | '''return a text listing of the given extensions''' |
|
15 | 15 | rst = [] |
|
16 | 16 | if exts: |
|
17 | 17 | rst.append('\n%s\n\n' % header) |
|
18 | 18 | for name, desc in sorted(exts.iteritems()): |
|
19 | 19 | rst.append('%s:%s: %s\n' % (' ' * indent, name, desc)) |
|
20 | 20 | return rst |
|
21 | 21 | |
|
22 | 22 | def extshelp(): |
|
23 | 23 | rst = loaddoc('extensions')().splitlines(True) |
|
24 | 24 | rst.extend(listexts(_('enabled extensions:'), extensions.enabled())) |
|
25 | 25 | rst.extend(listexts(_('disabled extensions:'), extensions.disabled())) |
|
26 | 26 | doc = ''.join(rst) |
|
27 | 27 | return doc |
|
28 | 28 | |
|
29 | 29 | def optrst(options, verbose): |
|
30 | 30 | data = [] |
|
31 | 31 | multioccur = False |
|
32 | 32 | for option in options: |
|
33 | 33 | if len(option) == 5: |
|
34 | 34 | shortopt, longopt, default, desc, optlabel = option |
|
35 | 35 | else: |
|
36 | 36 | shortopt, longopt, default, desc = option |
|
37 | 37 | optlabel = _("VALUE") # default label |
|
38 | 38 | |
|
39 | 39 | if _("DEPRECATED") in desc and not verbose: |
|
40 | 40 | continue |
|
41 | 41 | |
|
42 | 42 | so = '' |
|
43 | 43 | if shortopt: |
|
44 | 44 | so = '-' + shortopt |
|
45 | 45 | lo = '--' + longopt |
|
46 | 46 | if default: |
|
47 | 47 | desc += _(" (default: %s)") % default |
|
48 | 48 | |
|
49 | 49 | if isinstance(default, list): |
|
50 | 50 | lo += " %s [+]" % optlabel |
|
51 | 51 | multioccur = True |
|
52 | 52 | elif (default is not None) and not isinstance(default, bool): |
|
53 | 53 | lo += " %s" % optlabel |
|
54 | 54 | |
|
55 | 55 | data.append((so, lo, desc)) |
|
56 | 56 | |
|
57 | 57 | rst = minirst.maketable(data, 1) |
|
58 | 58 | |
|
59 | 59 | if multioccur: |
|
60 | 60 | rst.append(_("\n[+] marked option can be specified multiple times\n")) |
|
61 | 61 | |
|
62 | 62 | return ''.join(rst) |
|
63 | 63 | |
|
64 | 64 | def topicmatch(kw): |
|
65 | 65 | """Return help topics matching kw. |
|
66 | 66 | |
|
67 | 67 | Returns {'section': [(name, summary), ...], ...} where section is |
|
68 | 68 | one of topics, commands, extensions, or extensioncommands. |
|
69 | 69 | """ |
|
70 | 70 | kw = encoding.lower(kw) |
|
71 | 71 | def lowercontains(container): |
|
72 | 72 | return kw in encoding.lower(container) # translated in helptable |
|
73 | 73 | results = {'topics': [], |
|
74 | 74 | 'commands': [], |
|
75 | 75 | 'extensions': [], |
|
76 | 76 | 'extensioncommands': [], |
|
77 | 77 | } |
|
78 | 78 | for names, header, doc in helptable: |
|
79 | 79 | if (sum(map(lowercontains, names)) |
|
80 | 80 | or lowercontains(header) |
|
81 | 81 | or lowercontains(doc())): |
|
82 | 82 | results['topics'].append((names[0], header)) |
|
83 | 83 | import commands # avoid cycle |
|
84 | 84 | for cmd, entry in commands.table.iteritems(): |
|
85 | 85 | if cmd.startswith('debug'): |
|
86 | 86 | continue |
|
87 | 87 | if len(entry) == 3: |
|
88 | 88 | summary = entry[2] |
|
89 | 89 | else: |
|
90 | 90 | summary = '' |
|
91 | 91 | # translate docs *before* searching there |
|
92 | 92 | docs = _(getattr(entry[0], '__doc__', None)) or '' |
|
93 | 93 | if kw in cmd or lowercontains(summary) or lowercontains(docs): |
|
94 | 94 | doclines = docs.splitlines() |
|
95 | 95 | if doclines: |
|
96 | 96 | summary = doclines[0] |
|
97 | 97 | cmdname = cmd.split('|')[0].lstrip('^') |
|
98 | 98 | results['commands'].append((cmdname, summary)) |
|
99 | 99 | for name, docs in itertools.chain( |
|
100 | 100 | extensions.enabled().iteritems(), |
|
101 | 101 | extensions.disabled().iteritems()): |
|
102 | 102 | # extensions.load ignores the UI argument |
|
103 | 103 | mod = extensions.load(None, name, '') |
|
104 | 104 | if lowercontains(name) or lowercontains(docs): |
|
105 | 105 | # extension docs are already translated |
|
106 | 106 | results['extensions'].append((name, docs.splitlines()[0])) |
|
107 | 107 | for cmd, entry in getattr(mod, 'cmdtable', {}).iteritems(): |
|
108 | 108 | if kw in cmd or (len(entry) > 2 and lowercontains(entry[2])): |
|
109 | 109 | cmdname = cmd.split('|')[0].lstrip('^') |
|
110 | 110 | if entry[0].__doc__: |
|
111 | 111 | cmddoc = gettext(entry[0].__doc__).splitlines()[0] |
|
112 | 112 | else: |
|
113 | 113 | cmddoc = _('(no help text available)') |
|
114 | 114 | results['extensioncommands'].append((cmdname, cmddoc)) |
|
115 | 115 | return results |
|
116 | 116 | |
|
117 | 117 | def loaddoc(topic): |
|
118 | 118 | """Return a delayed loader for help/topic.txt.""" |
|
119 | 119 | |
|
120 | 120 | def loader(): |
|
121 | 121 | if util.mainfrozen(): |
|
122 | 122 | module = sys.executable |
|
123 | 123 | else: |
|
124 | 124 | module = __file__ |
|
125 | 125 | base = os.path.dirname(module) |
|
126 | 126 | |
|
127 | 127 | for dir in ('.', '..'): |
|
128 | 128 | docdir = os.path.join(base, dir, 'help') |
|
129 | 129 | if os.path.isdir(docdir): |
|
130 | 130 | break |
|
131 | 131 | |
|
132 | 132 | path = os.path.join(docdir, topic + ".txt") |
|
133 | 133 | doc = gettext(util.readfile(path)) |
|
134 | 134 | for rewriter in helphooks.get(topic, []): |
|
135 | 135 | doc = rewriter(topic, doc) |
|
136 | 136 | return doc |
|
137 | 137 | |
|
138 | 138 | return loader |
|
139 | 139 | |
|
140 | 140 | helptable = sorted([ |
|
141 | 141 | (["config", "hgrc"], _("Configuration Files"), loaddoc('config')), |
|
142 | 142 | (["dates"], _("Date Formats"), loaddoc('dates')), |
|
143 | 143 | (["patterns"], _("File Name Patterns"), loaddoc('patterns')), |
|
144 | 144 | (['environment', 'env'], _('Environment Variables'), |
|
145 | 145 | loaddoc('environment')), |
|
146 | 146 | (['revs', 'revisions'], _('Specifying Single Revisions'), |
|
147 | 147 | loaddoc('revisions')), |
|
148 | 148 | (['mrevs', 'multirevs'], _('Specifying Multiple Revisions'), |
|
149 | 149 | loaddoc('multirevs')), |
|
150 | 150 | (['revset', 'revsets'], _("Specifying Revision Sets"), loaddoc('revsets')), |
|
151 | 151 | (['fileset', 'filesets'], _("Specifying File Sets"), loaddoc('filesets')), |
|
152 | 152 | (['diffs'], _('Diff Formats'), loaddoc('diffs')), |
|
153 | 153 | (['merge-tools'], _('Merge Tools'), loaddoc('merge-tools')), |
|
154 | 154 | (['templating', 'templates', 'template', 'style'], _('Template Usage'), |
|
155 | 155 | loaddoc('templates')), |
|
156 | 156 | (['urls'], _('URL Paths'), loaddoc('urls')), |
|
157 | 157 | (["extensions"], _("Using Additional Features"), extshelp), |
|
158 | (["subrepo", "subrepos"], _("Subrepositories"), loaddoc('subrepos')), | |
|
159 | (["hgweb"], _("Configuring hgweb"), loaddoc('hgweb')), | |
|
160 | (["glossary"], _("Glossary"), loaddoc('glossary')), | |
|
161 | (["hgignore", "ignore"], _("Syntax for Mercurial Ignore Files"), | |
|
162 | loaddoc('hgignore')), | |
|
163 | (["phases"], _("Working with Phases"), loaddoc('phases')), | |
|
158 | (["subrepo", "subrepos"], _("Subrepositories"), loaddoc('subrepos')), | |
|
159 | (["hgweb"], _("Configuring hgweb"), loaddoc('hgweb')), | |
|
160 | (["glossary"], _("Glossary"), loaddoc('glossary')), | |
|
161 | (["hgignore", "ignore"], _("Syntax for Mercurial Ignore Files"), | |
|
162 | loaddoc('hgignore')), | |
|
163 | (["phases"], _("Working with Phases"), loaddoc('phases')), | |
|
164 | 164 | ]) |
|
165 | 165 | |
|
166 | 166 | # Map topics to lists of callable taking the current topic help and |
|
167 | 167 | # returning the updated version |
|
168 | 168 | helphooks = {} |
|
169 | 169 | |
|
170 | 170 | def addtopichook(topic, rewriter): |
|
171 | 171 | helphooks.setdefault(topic, []).append(rewriter) |
|
172 | 172 | |
|
173 | 173 | def makeitemsdoc(topic, doc, marker, items): |
|
174 | 174 | """Extract docstring from the items key to function mapping, build a |
|
175 | 175 | .single documentation block and use it to overwrite the marker in doc |
|
176 | 176 | """ |
|
177 | 177 | entries = [] |
|
178 | 178 | for name in sorted(items): |
|
179 | 179 | text = (items[name].__doc__ or '').rstrip() |
|
180 | 180 | if not text: |
|
181 | 181 | continue |
|
182 | 182 | text = gettext(text) |
|
183 | 183 | lines = text.splitlines() |
|
184 | 184 | doclines = [(lines[0])] |
|
185 | 185 | for l in lines[1:]: |
|
186 | 186 | # Stop once we find some Python doctest |
|
187 | 187 | if l.strip().startswith('>>>'): |
|
188 | 188 | break |
|
189 | 189 | doclines.append(' ' + l.strip()) |
|
190 | 190 | entries.append('\n'.join(doclines)) |
|
191 | 191 | entries = '\n\n'.join(entries) |
|
192 | 192 | return doc.replace(marker, entries) |
|
193 | 193 | |
|
194 | 194 | def addtopicsymbols(topic, marker, symbols): |
|
195 | 195 | def add(topic, doc): |
|
196 | 196 | return makeitemsdoc(topic, doc, marker, symbols) |
|
197 | 197 | addtopichook(topic, add) |
|
198 | 198 | |
|
199 | 199 | addtopicsymbols('filesets', '.. predicatesmarker', fileset.symbols) |
|
200 | 200 | addtopicsymbols('merge-tools', '.. internaltoolsmarker', filemerge.internals) |
|
201 | 201 | addtopicsymbols('revsets', '.. predicatesmarker', revset.symbols) |
|
202 | 202 | addtopicsymbols('templates', '.. keywordsmarker', templatekw.dockeywords) |
|
203 | 203 | addtopicsymbols('templates', '.. filtersmarker', templatefilters.filters) |
General Comments 0
You need to be logged in to leave comments.
Login now