eggs/mercurial-1.7.3-py2.6-linux-x86_64.egg/mercurial/help.py
changeset 69 c6bca38c1cbf
equal deleted inserted replaced
68:5ff1fc726848 69:c6bca38c1cbf
       
     1 # help.py - help data for mercurial
       
     2 #
       
     3 # Copyright 2006 Matt Mackall <mpm@selenic.com>
       
     4 #
       
     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.
       
     7 
       
     8 from i18n import gettext, _
       
     9 import sys, os
       
    10 import extensions
       
    11 
       
    12 
       
    13 def moduledoc(file):
       
    14     '''return the top-level python documentation for the given file
       
    15 
       
    16     Loosely inspired by pydoc.source_synopsis(), but rewritten to
       
    17     handle triple quotes and to return the whole text instead of just
       
    18     the synopsis'''
       
    19     result = []
       
    20 
       
    21     line = file.readline()
       
    22     while line[:1] == '#' or not line.strip():
       
    23         line = file.readline()
       
    24         if not line:
       
    25             break
       
    26 
       
    27     start = line[:3]
       
    28     if start == '"""' or start == "'''":
       
    29         line = line[3:]
       
    30         while line:
       
    31             if line.rstrip().endswith(start):
       
    32                 line = line.split(start)[0]
       
    33                 if line:
       
    34                     result.append(line)
       
    35                 break
       
    36             elif not line:
       
    37                 return None # unmatched delimiter
       
    38             result.append(line)
       
    39             line = file.readline()
       
    40     else:
       
    41         return None
       
    42 
       
    43     return ''.join(result)
       
    44 
       
    45 def listexts(header, exts, maxlength, indent=1):
       
    46     '''return a text listing of the given extensions'''
       
    47     if not exts:
       
    48         return ''
       
    49     result = '\n%s\n\n' % header
       
    50     for name, desc in sorted(exts.iteritems()):
       
    51         result += '%s%-*s %s\n' % (' ' * indent, maxlength + 2,
       
    52                                    ':%s:' % name, desc)
       
    53     return result
       
    54 
       
    55 def extshelp():
       
    56     doc = loaddoc('extensions')()
       
    57 
       
    58     exts, maxlength = extensions.enabled()
       
    59     doc += listexts(_('enabled extensions:'), exts, maxlength)
       
    60 
       
    61     exts, maxlength = extensions.disabled()
       
    62     doc += listexts(_('disabled extensions:'), exts, maxlength)
       
    63 
       
    64     return doc
       
    65 
       
    66 def loaddoc(topic):
       
    67     """Return a delayed loader for help/topic.txt."""
       
    68 
       
    69     def loader():
       
    70         if hasattr(sys, 'frozen'):
       
    71             module = sys.executable
       
    72         else:
       
    73             module = __file__
       
    74         base = os.path.dirname(module)
       
    75 
       
    76         for dir in ('.', '..'):
       
    77             docdir = os.path.join(base, dir, 'help')
       
    78             if os.path.isdir(docdir):
       
    79                 break
       
    80 
       
    81         path = os.path.join(docdir, topic + ".txt")
       
    82         doc = gettext(open(path).read())
       
    83         for rewriter in helphooks.get(topic, []):
       
    84             doc = rewriter(topic, doc)
       
    85         return doc
       
    86 
       
    87     return loader
       
    88 
       
    89 helptable = [
       
    90     (["config", "hgrc"], _("Configuration Files"), loaddoc('config')),
       
    91     (["dates"], _("Date Formats"), loaddoc('dates')),
       
    92     (["patterns"], _("File Name Patterns"), loaddoc('patterns')),
       
    93     (['environment', 'env'], _('Environment Variables'),
       
    94      loaddoc('environment')),
       
    95     (['revs', 'revisions'], _('Specifying Single Revisions'),
       
    96      loaddoc('revisions')),
       
    97     (['mrevs', 'multirevs'], _('Specifying Multiple Revisions'),
       
    98      loaddoc('multirevs')),
       
    99     (['revset', 'revsets'], _("Specifying Revision Sets"), loaddoc('revsets')),
       
   100     (['diffs'], _('Diff Formats'), loaddoc('diffs')),
       
   101     (['merge-tools'], _('Merge Tools'), loaddoc('merge-tools')),
       
   102     (['templating', 'templates'], _('Template Usage'),
       
   103      loaddoc('templates')),
       
   104     (['urls'], _('URL Paths'), loaddoc('urls')),
       
   105     (["extensions"], _("Using additional features"), extshelp),
       
   106     (["subrepo", "subrepos"], _("Subrepositories"), loaddoc('subrepos')),
       
   107     (["hgweb"], _("Configuring hgweb"), loaddoc('hgweb')),
       
   108     (["glossary"], _("Glossary"), loaddoc('glossary')),
       
   109 ]
       
   110 
       
   111 # Map topics to lists of callable taking the current topic help and
       
   112 # returning the updated version
       
   113 helphooks = {
       
   114 }
       
   115 
       
   116 def addtopichook(topic, rewriter):
       
   117     helphooks.setdefault(topic, []).append(rewriter)