OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2013 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2013 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 import copy | 6 import copy |
7 import cStringIO | 7 import cStringIO |
8 import optparse | 8 import optparse |
9 import os | 9 import os |
10 import re | 10 import re |
(...skipping 14 matching lines...) Expand all Loading... |
25 indent_string = indent_re.match(statement).group() | 25 indent_string = indent_re.match(statement).group() |
26 if statement.rstrip()[-1:] == ':': | 26 if statement.rstrip()[-1:] == ':': |
27 indent_string += ' ' | 27 indent_string += ' ' |
28 output.write(statement + '\n') | 28 output.write(statement + '\n') |
29 else: | 29 else: |
30 line_ending = '' | 30 line_ending = '' |
31 while line and line[-1] in '\\"\n\r': | 31 while line and line[-1] in '\\"\n\r': |
32 line_ending = line[-1] + line_ending | 32 line_ending = line[-1] + line_ending |
33 line = line[:-1] | 33 line = line[:-1] |
34 | 34 |
35 m = expr_re.search(line) | 35 ms = list(expr_re.finditer(line)) |
36 if m: | 36 if ms: |
37 line = line.replace('%', '%%') | 37 # Only replace % with %% outside of the expr matches. |
| 38 new_line = '' |
| 39 start = 0 |
| 40 for m in ms: |
| 41 new_line += line[start:m.start()].replace('%', '%%') |
| 42 new_line += line[m.start():m.end()] |
| 43 start = m.end() |
| 44 new_line += line[start:].replace('%', '%%') |
| 45 line = new_line |
| 46 |
38 subst_line = r'r"""%s""" %% (%s,)' % ( | 47 subst_line = r'r"""%s""" %% (%s,)' % ( |
39 re.sub(expr_re, '%s', line), | 48 re.sub(expr_re, '%s', line), |
40 ', '.join(re.findall(expr_re, line))) | 49 ', '.join(re.findall(expr_re, line))) |
41 else: | 50 else: |
42 subst_line = r'r"""%s"""' % line | 51 subst_line = r'r"""%s"""' % line |
43 | 52 |
44 out_string = r'%s__outfile__.write(%s + %s)' % ( | 53 out_string = r'%s__outfile__.write(%s + %s)' % ( |
45 indent_string, | 54 indent_string, |
46 subst_line, | 55 subst_line, |
47 repr(line_ending)) | 56 repr(line_ending)) |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 _, args = parser.parse_args(args) | 102 _, args = parser.parse_args(args) |
94 if not args: | 103 if not args: |
95 return | 104 return |
96 | 105 |
97 with open(args[0]) as f: | 106 with open(args[0]) as f: |
98 print TemplateToPython( | 107 print TemplateToPython( |
99 f.read(), re.compile(STATEMENT_RE), re.compile(EXPR_RE)) | 108 f.read(), re.compile(STATEMENT_RE), re.compile(EXPR_RE)) |
100 | 109 |
101 if __name__ == '__main__': | 110 if __name__ == '__main__': |
102 sys.exit(main(sys.argv[1:])) | 111 sys.exit(main(sys.argv[1:])) |
OLD | NEW |