Show More
@@ -10,34 +10,19 b' from i18n import gettext as _' | |||
|
10 | 10 | from node import * |
|
11 | 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 | 13 | def parsestring(s, quoted=True): |
|
22 | 14 | '''parse a string using simple c-like syntax. |
|
23 | 15 | string must be in quotes if quoted is True.''' |
|
24 | fp = cStringIO.StringIO() | |
|
25 | 16 | if quoted: |
|
26 | 17 | first = s[0] |
|
27 | 18 | if len(s) < 2: raise SyntaxError(_('string too short')) |
|
28 | 19 | if first not in "'\"": raise SyntaxError(_('invalid quote')) |
|
29 | 20 | if s[-1] != first: raise SyntaxError(_('unmatched quotes')) |
|
30 | s = s[1:-1] | |
|
31 | escape = False | |
|
32 | for c in s: | |
|
33 | if escape: | |
|
34 | fp.write(esctable.get(c, c)) | |
|
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() | |
|
21 | s = s[1:-1].decode('string_escape') | |
|
22 | if first in s: raise SyntaxError(_('string ends early')) | |
|
23 | return s | |
|
24 | ||
|
25 | return s.decode('string_escape') | |
|
41 | 26 | |
|
42 | 27 | class templater(object): |
|
43 | 28 | '''template expansion engine. |
General Comments 0
You need to be logged in to leave comments.
Login now