eggs/mercurial-1.7.3-py2.6-linux-x86_64.egg/hgext/interhg.py
changeset 69 c6bca38c1cbf
equal deleted inserted replaced
68:5ff1fc726848 69:c6bca38c1cbf
       
     1 # interhg.py - interhg
       
     2 #
       
     3 # Copyright 2007 OHASHI Hideya <ohachige@gmail.com>
       
     4 #
       
     5 # Contributor(s):
       
     6 #   Edward Lee <edward.lee@engineering.uiuc.edu>
       
     7 #
       
     8 # This software may be used and distributed according to the terms of the
       
     9 # GNU General Public License version 2 or any later version.
       
    10 
       
    11 '''expand expressions into changelog and summaries
       
    12 
       
    13 This extension allows the use of a special syntax in summaries, which
       
    14 will be automatically expanded into links or any other arbitrary
       
    15 expression, much like InterWiki does.
       
    16 
       
    17 A few example patterns (link to bug tracking, etc.) that may be used
       
    18 in your hgrc::
       
    19 
       
    20   [interhg]
       
    21   issues = s!issue(\\d+)!<a href="http://bts/issue\\1">issue\\1</a>!
       
    22   bugzilla = s!((?:bug|b=|(?=#?\\d{4,}))(?:\\s*#?)(\\d+))!<a..=\\2">\\1</a>!i
       
    23   boldify = s!(^|\\s)#(\\d+)\\b! <b>#\\2</b>!
       
    24 '''
       
    25 
       
    26 import re
       
    27 from mercurial.hgweb import hgweb_mod
       
    28 from mercurial import templatefilters, extensions
       
    29 from mercurial.i18n import _
       
    30 
       
    31 interhg_table = []
       
    32 
       
    33 def uisetup(ui):
       
    34     orig_escape = templatefilters.filters["escape"]
       
    35 
       
    36     def interhg_escape(x):
       
    37         escstr = orig_escape(x)
       
    38         for regexp, format in interhg_table:
       
    39             escstr = regexp.sub(format, escstr)
       
    40         return escstr
       
    41 
       
    42     templatefilters.filters["escape"] = interhg_escape
       
    43 
       
    44 def interhg_refresh(orig, self, *args, **kwargs):
       
    45     interhg_table[:] = []
       
    46     for key, pattern in self.repo.ui.configitems('interhg'):
       
    47         # grab the delimiter from the character after the "s"
       
    48         unesc = pattern[1]
       
    49         delim = re.escape(unesc)
       
    50 
       
    51         # identify portions of the pattern, taking care to avoid escaped
       
    52         # delimiters. the replace format and flags are optional, but delimiters
       
    53         # are required.
       
    54         match = re.match(r'^s%s(.+)(?:(?<=\\\\)|(?<!\\))%s(.*)%s([ilmsux])*$'
       
    55                          % (delim, delim, delim), pattern)
       
    56         if not match:
       
    57             self.repo.ui.warn(_("interhg: invalid pattern for %s: %s\n")
       
    58                               % (key, pattern))
       
    59             continue
       
    60 
       
    61         # we need to unescape the delimiter for regexp and format
       
    62         delim_re = re.compile(r'(?<!\\)\\%s' % delim)
       
    63         regexp = delim_re.sub(unesc, match.group(1))
       
    64         format = delim_re.sub(unesc, match.group(2))
       
    65 
       
    66         # the pattern allows for 6 regexp flags, so set them if necessary
       
    67         flagin = match.group(3)
       
    68         flags = 0
       
    69         if flagin:
       
    70             for flag in flagin.upper():
       
    71                 flags |= re.__dict__[flag]
       
    72 
       
    73         try:
       
    74             regexp = re.compile(regexp, flags)
       
    75             interhg_table.append((regexp, format))
       
    76         except re.error:
       
    77             self.repo.ui.warn(_("interhg: invalid regexp for %s: %s\n")
       
    78                               % (key, regexp))
       
    79     return orig(self, *args, **kwargs)
       
    80 
       
    81 extensions.wrapfunction(hgweb_mod.hgweb, 'refresh', interhg_refresh)