comparison mercurial/hgweb/webutil.py @ 26162:268b39770c28

hgweb: extract web substitutions table generation to own function It doesn't use any state in hgweb except for the repo instance. Move it to a standalone function.
author Gregory Szorc <gregory.szorc@gmail.com>
date Sat, 22 Aug 2015 15:40:33 -0700
parents a103ecb8a04a
children 41957e50e109
comparison
equal deleted inserted replaced
26161:16d54bbdbf89 26162:268b39770c28
5 # 5 #
6 # This software may be used and distributed according to the terms of the 6 # This software may be used and distributed according to the terms of the
7 # GNU General Public License version 2 or any later version. 7 # GNU General Public License version 2 or any later version.
8 8
9 import os, copy 9 import os, copy
10 import re
10 from mercurial import match, patch, error, ui, util, pathutil, context 11 from mercurial import match, patch, error, ui, util, pathutil, context
11 from mercurial.i18n import _ 12 from mercurial.i18n import _
12 from mercurial.node import hex, nullid, short 13 from mercurial.node import hex, nullid, short
13 from mercurial.templatefilters import revescape 14 from mercurial.templatefilters import revescape
14 from common import ErrorResponse, paritygen 15 from common import ErrorResponse, paritygen
538 539
539 class wsgiui(ui.ui): 540 class wsgiui(ui.ui):
540 # default termwidth breaks under mod_wsgi 541 # default termwidth breaks under mod_wsgi
541 def termwidth(self): 542 def termwidth(self):
542 return 80 543 return 80
544
545 def getwebsubs(repo):
546 websubtable = []
547 websubdefs = repo.ui.configitems('websub')
548 # we must maintain interhg backwards compatibility
549 websubdefs += repo.ui.configitems('interhg')
550 for key, pattern in websubdefs:
551 # grab the delimiter from the character after the "s"
552 unesc = pattern[1]
553 delim = re.escape(unesc)
554
555 # identify portions of the pattern, taking care to avoid escaped
556 # delimiters. the replace format and flags are optional, but
557 # delimiters are required.
558 match = re.match(
559 r'^s%s(.+)(?:(?<=\\\\)|(?<!\\))%s(.*)%s([ilmsux])*$'
560 % (delim, delim, delim), pattern)
561 if not match:
562 repo.ui.warn(_("websub: invalid pattern for %s: %s\n")
563 % (key, pattern))
564 continue
565
566 # we need to unescape the delimiter for regexp and format
567 delim_re = re.compile(r'(?<!\\)\\%s' % delim)
568 regexp = delim_re.sub(unesc, match.group(1))
569 format = delim_re.sub(unesc, match.group(2))
570
571 # the pattern allows for 6 regexp flags, so set them if necessary
572 flagin = match.group(3)
573 flags = 0
574 if flagin:
575 for flag in flagin.upper():
576 flags |= re.__dict__[flag]
577
578 try:
579 regexp = re.compile(regexp, flags)
580 websubtable.append((regexp, format))
581 except re.error:
582 repo.ui.warn(_("websub: invalid regexp for %s: %s\n")
583 % (key, regexp))
584 return websubtable