tidy tool for Template Toolkit
修訂 | 2f41a4f0745ef4ddd42474222b4544178fe9b6f8 (tree) |
---|---|
時間 | 2015-08-18 19:22:49 |
作者 | hylom <hylom@user...> |
Commiter | hylom |
add --debug option, and change Indenter.extract_directives to use regex for directive search
@@ -14,18 +14,22 @@ USAGE = ''' tttidy - tidy tool for template toolkit | ||
14 | 14 | |
15 | 15 | INDENT_LENGTH = 4 |
16 | 16 | INDENT_CHAR = ' ' |
17 | -START_TAG = '[%' | |
18 | -END_TAG = '%]' | |
17 | +START_TAG = r'\[%-?' | |
18 | +END_TAG = r'-?%]' | |
19 | 19 | |
20 | 20 | class Indenter(object): |
21 | 21 | '''Object for indenting template''' |
22 | - def __init__(self): | |
22 | + def __init__(self, debug=False): | |
23 | 23 | '''Initializer''' |
24 | 24 | self.indent_length = INDENT_LENGTH |
25 | 25 | self.indent_char = INDENT_CHAR |
26 | - self.start_tag = START_TAG | |
27 | - self.end_tag = END_TAG | |
26 | + self.start_tag = re.compile(START_TAG) | |
27 | + self.end_tag = re.compile(END_TAG) | |
28 | 28 | self.reset() |
29 | + self.debug = debug | |
30 | + | |
31 | + def debug_print(self, s): | |
32 | + sys.stderr.write(s + '\n') | |
29 | 33 | |
30 | 34 | def reset(self): |
31 | 35 | self._in_template = False |
@@ -53,10 +57,41 @@ class Indenter(object): | ||
53 | 57 | return line |
54 | 58 | else: |
55 | 59 | return line[0:c] |
56 | - ret = [_strip_comment(x) for x in ret] | |
60 | + ret = [_strip_comment(x).strip() for x in ret] | |
57 | 61 | return ret |
58 | 62 | |
63 | + def _line_scan(self, line): | |
64 | + if self._in_template: | |
65 | + #cursor = line.find(self.end_tag) | |
66 | + m = self.end_tag.search(line) | |
67 | + if m == None: | |
68 | + return line | |
69 | + else: | |
70 | + cursor = m.start() | |
71 | + offset = m.end() - m.start() | |
72 | + body = line[:cursor] | |
73 | + offset = 2 | |
74 | + rest = line[cursor + offset:] | |
75 | + self._in_template = False | |
76 | + return body + self._line_scan(rest) | |
77 | + else: | |
78 | + #cursor = line.find(self.start_tag) | |
79 | + m = self.start_tag.search(line) | |
80 | + if m == None: | |
81 | + return '' | |
82 | + else: | |
83 | + cursor = m.start() | |
84 | + offset = m.end() - m.start() | |
85 | + rest = line[cursor + offset:] | |
86 | + self._in_template = True | |
87 | + return self._line_scan(rest) | |
88 | + | |
59 | 89 | def _calc_indent_level(self, line): |
90 | + if len(line) == 0: | |
91 | + return (0, 0) | |
92 | + | |
93 | + self.debug_print(line) | |
94 | + | |
60 | 95 | count_if = len(re.findall(u'^(IF|FOREACH)\s', line)) |
61 | 96 | count_if += len(re.findall(u'\s(IF|FOREACH)\s', line)) |
62 | 97 | count_if += len(re.findall(u'^BLOCK(\s|;)', line)) |
@@ -80,6 +115,7 @@ class Indenter(object): | ||
80 | 115 | counter_next = 0 |
81 | 116 | # case: IF - ELSIF - ELSE - END directives all exists in one line |
82 | 117 | if count_if == count_end and count_if > 0 and count_elsif > 0: |
118 | + self.debug_print("if:{} end:{} elsif:{} cc:{} cn:{}".format(count_if, count_end, count_elsif, counter_current, counter_next)) | |
83 | 119 | return (counter_current, counter_next) |
84 | 120 | |
85 | 121 | # else |
@@ -98,31 +134,9 @@ class Indenter(object): | ||
98 | 134 | counter_current -= 1 |
99 | 135 | counter_next += 1 |
100 | 136 | |
137 | + self.debug_print("if:{} end:{} elsif:{} cc:{} cn:{}".format(count_if, count_end, count_elsif, counter_current, counter_next)) | |
101 | 138 | return (counter_current, counter_next) |
102 | 139 | |
103 | - def _line_scan(self, line): | |
104 | - if self._in_template: | |
105 | - cursor = line.find(self.end_tag) | |
106 | - if cursor == -1: | |
107 | - return line | |
108 | - else: | |
109 | - body = line[:cursor] | |
110 | - offset = 2 | |
111 | - rest = line[cursor + offset:] | |
112 | - self._in_template = False | |
113 | - return body + self._line_scan(rest) | |
114 | - else: | |
115 | - cursor = line.find(self.start_tag) | |
116 | - if cursor == -1: | |
117 | - return '' | |
118 | - else: | |
119 | - offset = 2 | |
120 | - if line[cursor+2] == '-': | |
121 | - offset += 1 | |
122 | - rest = line[cursor + offset:] | |
123 | - self._in_template = True | |
124 | - return self._line_scan(rest) | |
125 | - | |
126 | 140 | |
127 | 141 | def _error_exit(msg=None): |
128 | 142 | if msg != None: |
@@ -131,10 +145,9 @@ def _error_exit(msg=None): | ||
131 | 145 | sys.stderr.write(USAGE) |
132 | 146 | sys.exit(-1) |
133 | 147 | |
134 | -def indent(template): | |
148 | +def indent(template, debug=False): | |
135 | 149 | '''add indent''' |
136 | - indenter = Indenter() | |
137 | - #r = indenter.extract_directives(template) | |
150 | + indenter = Indenter(debug) | |
138 | 151 | r = indenter.indent(template) |
139 | 152 | return r |
140 | 153 |
@@ -151,6 +164,7 @@ def main(): | ||
151 | 164 | parser.add_argument('file', type=file) |
152 | 165 | parser.add_argument('--unindent', '-u', action='store_true') |
153 | 166 | parser.add_argument('--directives', action='store_true') |
167 | + parser.add_argument('--debug', action='store_true') | |
154 | 168 | |
155 | 169 | args = parser.parse_args() |
156 | 170 |
@@ -170,7 +184,7 @@ def main(): | ||
170 | 184 | |
171 | 185 | # do indent |
172 | 186 | ret = unindent(args.file) |
173 | - ret = indent(ret) | |
187 | + ret = indent(ret, args.debug) | |
174 | 188 | print "\n".join(ret) |
175 | 189 | |
176 | 190 |