mirror of
https://github.com/zulip/zulip.git
synced 2025-11-05 06:23:38 +00:00
check-templates: Show filename for tokenize errors.
This commit is contained in:
@@ -5,6 +5,7 @@ from collections import defaultdict
|
|||||||
|
|
||||||
from .template_parser import (
|
from .template_parser import (
|
||||||
tokenize,
|
tokenize,
|
||||||
|
FormattedException,
|
||||||
Token,
|
Token,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -133,7 +134,7 @@ def split_for_id_and_class(element: str) -> List[str]:
|
|||||||
|
|
||||||
|
|
||||||
def html_branches(text: str, fn: Optional[str] = None) -> List[HtmlTreeBranch]:
|
def html_branches(text: str, fn: Optional[str] = None) -> List[HtmlTreeBranch]:
|
||||||
tree = html_tag_tree(text)
|
tree = html_tag_tree(text, fn)
|
||||||
branches: List[HtmlTreeBranch] = []
|
branches: List[HtmlTreeBranch] = []
|
||||||
|
|
||||||
def walk(node: Node, tag_info_list: Optional[List[TagInfo]] = None) -> None:
|
def walk(node: Node, tag_info_list: Optional[List[TagInfo]] = None) -> None:
|
||||||
@@ -156,7 +157,7 @@ def html_branches(text: str, fn: Optional[str] = None) -> List[HtmlTreeBranch]:
|
|||||||
return branches
|
return branches
|
||||||
|
|
||||||
|
|
||||||
def html_tag_tree(text: str) -> Node:
|
def html_tag_tree(text: str, fn: Optional[str]=None) -> Node:
|
||||||
tokens = tokenize(text)
|
tokens = tokenize(text)
|
||||||
top_level = Node(token=None, parent=None)
|
top_level = Node(token=None, parent=None)
|
||||||
stack = [top_level]
|
stack = [top_level]
|
||||||
@@ -184,7 +185,13 @@ def build_id_dict(templates: List[str]) -> (Dict[str, List[str]]):
|
|||||||
for fn in templates:
|
for fn in templates:
|
||||||
with open(fn) as f:
|
with open(fn) as f:
|
||||||
text = f.read()
|
text = f.read()
|
||||||
|
|
||||||
|
try:
|
||||||
list_tags = tokenize(text)
|
list_tags = tokenize(text)
|
||||||
|
except FormattedException as e:
|
||||||
|
raise Exception('''
|
||||||
|
fn: %s
|
||||||
|
%s''' % (fn, e))
|
||||||
|
|
||||||
for tag in list_tags:
|
for tag in list_tags:
|
||||||
info = get_tag_info(tag)
|
info = get_tag_info(tag)
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
from typing import Callable, List, Optional, Text
|
from typing import Callable, List, Optional, Text
|
||||||
|
|
||||||
|
class FormattedException(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
class TemplateParserException(Exception):
|
class TemplateParserException(Exception):
|
||||||
def __init__(self, message: str) -> None:
|
def __init__(self, message: str) -> None:
|
||||||
self.message = message
|
self.message = message
|
||||||
@@ -148,9 +151,14 @@ def tokenize(text: str) -> List[Token]:
|
|||||||
advance(1)
|
advance(1)
|
||||||
continue
|
continue
|
||||||
except TokenizationException as e:
|
except TokenizationException as e:
|
||||||
raise TemplateParserException('''%s at Line %d Col %d:"%s"''' %
|
raise FormattedException(
|
||||||
(e.message, state.line, state.col,
|
'''%s at Line %d Col %d:"%s"''' % (
|
||||||
e.line_content))
|
e.message,
|
||||||
|
state.line,
|
||||||
|
state.col,
|
||||||
|
e.line_content
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
line_span = len(s.split('\n'))
|
line_span = len(s.split('\n'))
|
||||||
token = Token(
|
token = Token(
|
||||||
@@ -197,7 +205,12 @@ def validate(fn: Optional[str] = None, text: Optional[str] = None, check_indent:
|
|||||||
with open(fn) as f:
|
with open(fn) as f:
|
||||||
text = f.read()
|
text = f.read()
|
||||||
|
|
||||||
|
try:
|
||||||
tokens = tokenize(text)
|
tokens = tokenize(text)
|
||||||
|
except FormattedException as e:
|
||||||
|
raise TemplateParserException('''
|
||||||
|
fn: %s
|
||||||
|
%s''' % (fn, e))
|
||||||
|
|
||||||
class State:
|
class State:
|
||||||
def __init__(self, func: Callable[[Token], None]) -> None:
|
def __init__(self, func: Callable[[Token], None]) -> None:
|
||||||
|
|||||||
Reference in New Issue
Block a user