hgext/interhg.py
branchstable
changeset 21160 564f55b25122
parent 21028 a0f437e2f5a9
parent 21159 024f38f6d5f6
child 21161 ef59019f4771
equal deleted inserted replaced
21028:a0f437e2f5a9 21160:564f55b25122
     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 testedwith = 'internal'
       
    32 
       
    33 interhg_table = []
       
    34 
       
    35 def uisetup(ui):
       
    36     orig_escape = templatefilters.filters["escape"]
       
    37 
       
    38     def interhg_escape(x):
       
    39         escstr = orig_escape(x)
       
    40         for regexp, format in interhg_table:
       
    41             escstr = regexp.sub(format, escstr)
       
    42         return escstr
       
    43 
       
    44     templatefilters.filters["escape"] = interhg_escape
       
    45 
       
    46 def interhg_refresh(orig, self, *args, **kwargs):
       
    47     interhg_table[:] = []
       
    48     for key, pattern in self.repo.ui.configitems('interhg'):
       
    49         # grab the delimiter from the character after the "s"
       
    50         unesc = pattern[1]
       
    51         delim = re.escape(unesc)
       
    52 
       
    53         # identify portions of the pattern, taking care to avoid escaped
       
    54         # delimiters. the replace format and flags are optional, but delimiters
       
    55         # are required.
       
    56         match = re.match(r'^s%s(.+)(?:(?<=\\\\)|(?<!\\))%s(.*)%s([ilmsux])*$'
       
    57                          % (delim, delim, delim), pattern)
       
    58         if not match:
       
    59             self.repo.ui.warn(_("interhg: invalid pattern for %s: %s\n")
       
    60                               % (key, pattern))
       
    61             continue
       
    62 
       
    63         # we need to unescape the delimiter for regexp and format
       
    64         delim_re = re.compile(r'(?<!\\)\\%s' % delim)
       
    65         regexp = delim_re.sub(unesc, match.group(1))
       
    66         format = delim_re.sub(unesc, match.group(2))
       
    67 
       
    68         # the pattern allows for 6 regexp flags, so set them if necessary
       
    69         flagin = match.group(3)
       
    70         flags = 0
       
    71         if flagin:
       
    72             for flag in flagin.upper():
       
    73                 flags |= re.__dict__[flag]
       
    74 
       
    75         try:
       
    76             regexp = re.compile(regexp, flags)
       
    77             interhg_table.append((regexp, format))
       
    78         except re.error:
       
    79             self.repo.ui.warn(_("interhg: invalid regexp for %s: %s\n")
       
    80                               % (key, regexp))
       
    81     return orig(self, *args, **kwargs)
       
    82 
       
    83 extensions.wrapfunction(hgweb_mod.hgweb, 'refresh', interhg_refresh)