Skip to content

Commit

Permalink
Identify multiple jinja tags in a single line
Browse files Browse the repository at this point in the history
  • Loading branch information
AsherGlick committed Nov 3, 2023
1 parent e94afb5 commit 1540377
Showing 1 changed file with 34 additions and 4 deletions.
38 changes: 34 additions & 4 deletions xml_converter/generators/jinja_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,18 @@ def preprocess(self, source: str, name: Optional[str], filename: Optional[str] =
# but does not end on a line by itself
############################################################################
def indent_flow(self, line: str) -> Tuple[IndentFlow, str]:
tag_match = re.match(tag_regex, line)
matched_tags = parse_out_tags(line)

if tag_match is None:
if len(matched_tags) == 0:
return IndentFlow.keep, ""

parsed_tag_line = tag_match.groups()[0].strip()
if len(matched_tags) > 1:
# This is maybe wrong, but we will assume for now that if there are
# multiple tags on a single line then the entire "block" of those
# tags is on the line and no indentation stuff should be changed.
return IndentFlow.keep, ""

parsed_tag_line = matched_tags[0].strip('- ')

if parsed_tag_line.startswith("for"):
return IndentFlow.push, "for"
Expand Down Expand Up @@ -152,7 +158,8 @@ def indent_flow(self, line: str) -> Tuple[IndentFlow, str]:
# return IndentFlow.pop, "filter"

# # TODO: Do more testing to branch between set and blockset
# elif parsed_tag_line.startswith("set"):
elif parsed_tag_line.startswith("set"):
return IndentFlow.keep, ""
# return IndentFlow.push, "set" # or maybe `IndentFlow.keep, ""` if not a block set
# elif parsed_tag_line.startswith("endset"):
# return IndentFlow.pop, "set"
Expand Down Expand Up @@ -210,3 +217,26 @@ def unindent_block(block: List[str]) -> List[str]:
next_indent = None

return [line[index:] for line in block]


################################################################################
# parse_out_tags
#
# This function parses out jinja control flow tags from a string of text.
################################################################################
def parse_out_tags(line: str) -> List[str]:
tags = []

while True:
start_index = line.find("{%")

if start_index == -1:
return tags

end_index = line.find("%}")

if end_index == -1:
return tags

tags.append(line[start_index + 2:end_index])
line = line[end_index + 2:]

0 comments on commit 1540377

Please sign in to comment.