• R/O
  • HTTP
  • SSH
  • HTTPS

提交

標籤
無標籤

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

tidy tool for Template Toolkit


Commit MetaInfo

修訂2f41a4f0745ef4ddd42474222b4544178fe9b6f8 (tree)
時間2015-08-18 19:22:49
作者hylom <hylom@user...>
Commiterhylom

Log Message

add --debug option, and change Indenter.extract_directives to use regex for directive search

Change Summary

差異

--- a/tttidy.py
+++ b/tttidy.py
@@ -14,18 +14,22 @@ USAGE = ''' tttidy - tidy tool for template toolkit
1414
1515 INDENT_LENGTH = 4
1616 INDENT_CHAR = ' '
17-START_TAG = '[%'
18-END_TAG = '%]'
17+START_TAG = r'\[%-?'
18+END_TAG = r'-?%]'
1919
2020 class Indenter(object):
2121 '''Object for indenting template'''
22- def __init__(self):
22+ def __init__(self, debug=False):
2323 '''Initializer'''
2424 self.indent_length = INDENT_LENGTH
2525 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)
2828 self.reset()
29+ self.debug = debug
30+
31+ def debug_print(self, s):
32+ sys.stderr.write(s + '\n')
2933
3034 def reset(self):
3135 self._in_template = False
@@ -53,10 +57,41 @@ class Indenter(object):
5357 return line
5458 else:
5559 return line[0:c]
56- ret = [_strip_comment(x) for x in ret]
60+ ret = [_strip_comment(x).strip() for x in ret]
5761 return ret
5862
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+
5989 def _calc_indent_level(self, line):
90+ if len(line) == 0:
91+ return (0, 0)
92+
93+ self.debug_print(line)
94+
6095 count_if = len(re.findall(u'^(IF|FOREACH)\s', line))
6196 count_if += len(re.findall(u'\s(IF|FOREACH)\s', line))
6297 count_if += len(re.findall(u'^BLOCK(\s|;)', line))
@@ -80,6 +115,7 @@ class Indenter(object):
80115 counter_next = 0
81116 # case: IF - ELSIF - ELSE - END directives all exists in one line
82117 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))
83119 return (counter_current, counter_next)
84120
85121 # else
@@ -98,31 +134,9 @@ class Indenter(object):
98134 counter_current -= 1
99135 counter_next += 1
100136
137+ self.debug_print("if:{} end:{} elsif:{} cc:{} cn:{}".format(count_if, count_end, count_elsif, counter_current, counter_next))
101138 return (counter_current, counter_next)
102139
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-
126140
127141 def _error_exit(msg=None):
128142 if msg != None:
@@ -131,10 +145,9 @@ def _error_exit(msg=None):
131145 sys.stderr.write(USAGE)
132146 sys.exit(-1)
133147
134-def indent(template):
148+def indent(template, debug=False):
135149 '''add indent'''
136- indenter = Indenter()
137- #r = indenter.extract_directives(template)
150+ indenter = Indenter(debug)
138151 r = indenter.indent(template)
139152 return r
140153
@@ -151,6 +164,7 @@ def main():
151164 parser.add_argument('file', type=file)
152165 parser.add_argument('--unindent', '-u', action='store_true')
153166 parser.add_argument('--directives', action='store_true')
167+ parser.add_argument('--debug', action='store_true')
154168
155169 args = parser.parse_args()
156170
@@ -170,7 +184,7 @@ def main():
170184
171185 # do indent
172186 ret = unindent(args.file)
173- ret = indent(ret)
187+ ret = indent(ret, args.debug)
174188 print "\n".join(ret)
175189
176190