mercurial/templater.py
changeset 3632 231393b7316f
parent 3462 fab28100ea88
child 3633 bf3dec184c78
equal deleted inserted replaced
3631:770c4fc03b8e 3632:231393b7316f
     8 from demandload import demandload
     8 from demandload import demandload
     9 from i18n import gettext as _
     9 from i18n import gettext as _
    10 from node import *
    10 from node import *
    11 demandload(globals(), "cStringIO cgi re sys os time urllib util textwrap")
    11 demandload(globals(), "cStringIO cgi re sys os time urllib util textwrap")
    12 
    12 
    13 esctable = {
       
    14     '\\': '\\',
       
    15     'r': '\r',
       
    16     't': '\t',
       
    17     'n': '\n',
       
    18     'v': '\v',
       
    19     }
       
    20 
       
    21 def parsestring(s, quoted=True):
    13 def parsestring(s, quoted=True):
    22     '''parse a string using simple c-like syntax.
    14     '''parse a string using simple c-like syntax.
    23     string must be in quotes if quoted is True.'''
    15     string must be in quotes if quoted is True.'''
    24     fp = cStringIO.StringIO()
       
    25     if quoted:
    16     if quoted:
    26         first = s[0]
    17         first = s[0]
    27         if len(s) < 2: raise SyntaxError(_('string too short'))
    18         if len(s) < 2: raise SyntaxError(_('string too short'))
    28         if first not in "'\"": raise SyntaxError(_('invalid quote'))
    19         if first not in "'\"": raise SyntaxError(_('invalid quote'))
    29         if s[-1] != first: raise SyntaxError(_('unmatched quotes'))
    20         if s[-1] != first: raise SyntaxError(_('unmatched quotes'))
    30         s = s[1:-1]
    21         s = s[1:-1].decode('string_escape')
    31     escape = False
    22         if first in s: raise SyntaxError(_('string ends early'))
    32     for c in s:
    23         return s
    33         if escape:
    24 
    34             fp.write(esctable.get(c, c))
    25     return s.decode('string_escape')
    35             escape = False
       
    36         elif c == '\\': escape = True
       
    37         elif quoted and c == first: raise SyntaxError(_('string ends early'))
       
    38         else: fp.write(c)
       
    39     if escape: raise SyntaxError(_('unterminated escape'))
       
    40     return fp.getvalue()
       
    41 
    26 
    42 class templater(object):
    27 class templater(object):
    43     '''template expansion engine.
    28     '''template expansion engine.
    44 
    29 
    45     template expansion works like this. a map file contains key=value
    30     template expansion works like this. a map file contains key=value