Simplify pulling out of tags in check-templates.

Before this change, the way we'd strip tags of punctuation
was just sort of messed up, because we'd strip the start tags
one way and strip the end tags another, and we had conditionals
for the different flavors of tags, instead of doing the stripping
when we already knew what flavor of tag we were dealing with.

(imported from commit 60c5ebd45e21b88bbfc98ff4b43dbbc6b32b38a1)
This commit is contained in:
Steve Howell 2014-02-27 16:23:06 -05:00 committed by Jessica McKellar
parent 1788f5c576
commit 9da693c371
1 changed files with 10 additions and 17 deletions

View File

@ -20,19 +20,15 @@ def validate(fn, check_indent=True):
line %d, col %d line %d, col %d
''' % (fn, end_tag, state.line, state.col)) ''' % (fn, end_tag, state.line, state.col))
def start_tag_matcher(s): def start_tag_matcher(s, start_tag):
start_line = state.line start_line = state.line
start_col = state.col start_col = state.col
state.depth += 1 state.depth += 1
if s[0] == '<':
tag = s[1:-1]
elif s[0] == '{':
tag = s[3:-2]
start_tag = tag.split()[0]
old_matcher = state.matcher old_matcher = state.matcher
def f(end_tag): def f(end_tag):
problem = None problem = None
if not matching_tags(start_tag, end_tag): if start_tag != end_tag:
problem = 'Mismatched tag.' problem = 'Mismatched tag.'
elif check_indent and state.line > start_line + 1 and state.col != start_col: elif check_indent and state.line > start_line + 1 and state.col != start_col:
problem = 'Bad indentation.' problem = 'Bad indentation.'
@ -76,24 +72,27 @@ def validate(fn, check_indent=True):
if c == '<': if c == '<':
s = get_html_tag(text, state.i) s = get_html_tag(text, state.i)
if s.startswith('</'): if s.startswith('</'):
state.matcher(s) end_tag = s[2:-1]
state.matcher(end_tag)
else: else:
tag = s[1:-1].split()[0] tag = s[1:-1].split()[0]
ignore = s.startswith('<!--') or s.endswith('/>') or tag in ['meta', '!DOCTYPE'] ignore = s.startswith('<!--') or s.endswith('/>') or tag in ['meta', '!DOCTYPE']
if not ignore: if not ignore:
start_tag_matcher(s) start_tag_matcher(s, tag)
advance(len(s)) advance(len(s))
continue continue
if looking_at("{{#") or looking_at("{{^"): if looking_at("{{#") or looking_at("{{^"):
s = get_handlebars_tag(text, state.i) s = get_handlebars_tag(text, state.i)
start_tag_matcher(s) tag = s[3:-2].split()[0]
start_tag_matcher(s, tag)
advance(len(s)) advance(len(s))
continue continue
if looking_at("{{/"): if looking_at("{{/"):
s = get_handlebars_tag(text, state.i) s = get_handlebars_tag(text, state.i)
state.matcher(s) end_tag = s[3:-2]
state.matcher(end_tag)
advance(len(s)) advance(len(s))
continue continue
@ -120,12 +119,6 @@ def get_html_tag(text, i):
s = text[i:end+1] s = text[i:end+1]
return s return s
def matching_tags(start_tag, end_tag):
if end_tag[0] == '<':
return start_tag == end_tag[2:-1]
if end_tag[0] == '{':
return start_tag == end_tag[3:-2]
def check_our_files(): def check_our_files():
git_files = map(str.strip, subprocess.check_output(['git', 'ls-files']).split('\n')) git_files = map(str.strip, subprocess.check_output(['git', 'ls-files']).split('\n'))