help.py
136 lines
| 4.3 KiB
| text/x-python
|
PythonLexer
/ mercurial / help.py
|
r3795 | # help.py - help data for mercurial | ||
# | ||||
# Copyright 2006 Matt Mackall <mpm@selenic.com> | ||||
# | ||||
|
r8225 | # This software may be used and distributed according to the terms of the | ||
|
r10263 | # GNU General Public License version 2 or any later version. | ||
|
r3795 | |||
|
r9539 | from i18n import gettext, _ | ||
import sys, os | ||||
|
r9678 | import extensions | ||
|
r14168 | import util | ||
|
r8863 | |||
def moduledoc(file): | ||||
|
r8879 | '''return the top-level python documentation for the given file | ||
|
r10282 | Loosely inspired by pydoc.source_synopsis(), but rewritten to | ||
handle triple quotes and to return the whole text instead of just | ||||
the synopsis''' | ||||
|
r8863 | result = [] | ||
line = file.readline() | ||||
while line[:1] == '#' or not line.strip(): | ||||
line = file.readline() | ||||
|
r10282 | if not line: | ||
break | ||||
|
r8863 | |||
start = line[:3] | ||||
|
r12401 | if start == '"""' or start == "'''": | ||
|
r8863 | line = line[3:] | ||
while line: | ||||
if line.rstrip().endswith(start): | ||||
line = line.split(start)[0] | ||||
if line: | ||||
result.append(line) | ||||
break | ||||
elif not line: | ||||
return None # unmatched delimiter | ||||
result.append(line) | ||||
line = file.readline() | ||||
else: | ||||
return None | ||||
return ''.join(result) | ||||
|
r10364 | def listexts(header, exts, maxlength, indent=1): | ||
|
r8879 | '''return a text listing of the given extensions''' | ||
if not exts: | ||||
return '' | ||||
result = '\n%s\n\n' % header | ||||
for name, desc in sorted(exts.iteritems()): | ||||
|
r10364 | result += '%s%-*s %s\n' % (' ' * indent, maxlength + 2, | ||
':%s:' % name, desc) | ||||
|
r8864 | return result | ||
|
r8879 | def extshelp(): | ||
|
r9539 | doc = loaddoc('extensions')() | ||
|
r8863 | |||
|
r8871 | exts, maxlength = extensions.enabled() | ||
|
r8879 | doc += listexts(_('enabled extensions:'), exts, maxlength) | ||
|
r8864 | |||
|
r8871 | exts, maxlength = extensions.disabled() | ||
|
r8879 | doc += listexts(_('disabled extensions:'), exts, maxlength) | ||
|
r8863 | |||
return doc | ||||
|
r7013 | |||
|
r9539 | def loaddoc(topic): | ||
"""Return a delayed loader for help/topic.txt.""" | ||||
|
r3798 | |||
|
r9539 | def loader(): | ||
if hasattr(sys, 'frozen'): | ||||
module = sys.executable | ||||
else: | ||||
module = __file__ | ||||
base = os.path.dirname(module) | ||||
|
r7293 | |||
|
r9539 | for dir in ('.', '..'): | ||
docdir = os.path.join(base, dir, 'help') | ||||
if os.path.isdir(docdir): | ||||
break | ||||
|
r7677 | |||
|
r9539 | path = os.path.join(docdir, topic + ".txt") | ||
|
r14168 | doc = gettext(util.readfile(path)) | ||
|
r12820 | for rewriter in helphooks.get(topic, []): | ||
doc = rewriter(topic, doc) | ||||
return doc | ||||
|
r9539 | return loader | ||
|
r7677 | |||
|
r13888 | helptable = sorted([ | ||
|
r12145 | (["config", "hgrc"], _("Configuration Files"), loaddoc('config')), | ||
|
r9539 | (["dates"], _("Date Formats"), loaddoc('dates')), | ||
(["patterns"], _("File Name Patterns"), loaddoc('patterns')), | ||||
|
r10282 | (['environment', 'env'], _('Environment Variables'), | ||
loaddoc('environment')), | ||||
(['revs', 'revisions'], _('Specifying Single Revisions'), | ||||
loaddoc('revisions')), | ||||
(['mrevs', 'multirevs'], _('Specifying Multiple Revisions'), | ||||
loaddoc('multirevs')), | ||||
|
r12818 | (['revset', 'revsets'], _("Specifying Revision Sets"), loaddoc('revsets')), | ||
|
r9539 | (['diffs'], _('Diff Formats'), loaddoc('diffs')), | ||
|
r12771 | (['merge-tools'], _('Merge Tools'), loaddoc('merge-tools')), | ||
|
r10282 | (['templating', 'templates'], _('Template Usage'), | ||
loaddoc('templates')), | ||||
|
r9539 | (['urls'], _('URL Paths'), loaddoc('urls')), | ||
|
r8879 | (["extensions"], _("Using additional features"), extshelp), | ||
|
r14044 | (["subrepo", "subrepos"], _("Subrepositories"), loaddoc('subrepos')), | ||
(["hgweb"], _("Configuring hgweb"), loaddoc('hgweb')), | ||||
(["glossary"], _("Glossary"), loaddoc('glossary')), | ||||
(["hgignore", "ignore"], _("syntax for Mercurial ignore files"), | ||||
loaddoc('hgignore')), | ||||
|
r13888 | ]) | ||
|
r12820 | |||
# Map topics to lists of callable taking the current topic help and | ||||
# returning the updated version | ||||
helphooks = { | ||||
} | ||||
def addtopichook(topic, rewriter): | ||||
helphooks.setdefault(topic, []).append(rewriter) | ||||
|
r13593 | |||
def makeitemsdoc(topic, doc, marker, items): | ||||
"""Extract docstring from the items key to function mapping, build a | ||||
.single documentation block and use it to overwrite the marker in doc | ||||
""" | ||||
entries = [] | ||||
for name in sorted(items): | ||||
text = (items[name].__doc__ or '').rstrip() | ||||
if not text: | ||||
continue | ||||
text = gettext(text) | ||||
lines = text.splitlines() | ||||
lines[1:] = [(' ' + l.strip()) for l in lines[1:]] | ||||
entries.append('\n'.join(lines)) | ||||
entries = '\n\n'.join(entries) | ||||
return doc.replace(marker, entries) | ||||