Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to preserve comments when parsing templates #2037

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Unreleased
- Use modern packaging metadata with ``pyproject.toml`` instead of ``setup.cfg``.
:pr:`1793`
- Use ``flit_core`` instead of ``setuptools`` as build backend.
- Add the ``preserve_comments`` parameter to ``Environment.parse`` to preserve comments in template ASTs. :pr:`2037`


Version 3.1.5
Expand Down
19 changes: 15 additions & 4 deletions src/jinja2/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,7 @@ def parse(
source: str,
name: t.Optional[str] = None,
filename: t.Optional[str] = None,
preserve_comments: bool = False,
pawamoy marked this conversation as resolved.
Show resolved Hide resolved
) -> nodes.Template:
"""Parse the sourcecode and return the abstract syntax tree. This
tree of nodes is used by the compiler to convert the template into
Expand All @@ -608,17 +609,26 @@ def parse(

If you are :ref:`developing Jinja extensions <writing-extensions>`
this gives you a good overview of the node tree generated.

.. versionchanged:: 3.2
Added `preserve_comments` parameter.
"""
try:
return self._parse(source, name, filename)
return self._parse(source, name, filename, preserve_comments)
except TemplateSyntaxError:
self.handle_exception(source=source)

def _parse(
self, source: str, name: t.Optional[str], filename: t.Optional[str]
self,
source: str,
name: t.Optional[str],
filename: t.Optional[str],
preserve_comments: bool = False,
) -> nodes.Template:
"""Internal parsing function used by `parse` and `compile`."""
return Parser(self, source, name, filename).parse()
return Parser(
self, source, name, filename, preserve_comments=preserve_comments
).parse()

def lex(
self,
Expand Down Expand Up @@ -663,12 +673,13 @@ def _tokenize(
name: t.Optional[str],
filename: t.Optional[str] = None,
state: t.Optional[str] = None,
preserve_comments: bool = False,
) -> TokenStream:
"""Called by the parser to do the preprocessing and filtering
for all the extensions. Returns a :class:`~jinja2.lexer.TokenStream`.
"""
source = self.preprocess(source, name, filename)
stream = self.lexer.tokenize(source, name, filename, state)
stream = self.lexer.tokenize(source, name, filename, state, preserve_comments)

for ext in self.iter_extensions():
stream = ext.filter_stream(stream) # type: ignore
Expand Down
34 changes: 29 additions & 5 deletions src/jinja2/lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,17 +146,22 @@
f"({'|'.join(re.escape(x) for x in sorted(operators, key=lambda x: -len(x)))})"
)

ignored_tokens = frozenset(
comment_tokens = frozenset(
[
TOKEN_COMMENT_BEGIN,
TOKEN_COMMENT,
TOKEN_COMMENT_END,
TOKEN_WHITESPACE,
TOKEN_LINECOMMENT_BEGIN,
TOKEN_LINECOMMENT_END,
TOKEN_LINECOMMENT,
]
)
ignored_tokens = frozenset(
pawamoy marked this conversation as resolved.
Show resolved Hide resolved
[
TOKEN_WHITESPACE,
*comment_tokens,
]
)
ignore_if_empty = frozenset(
[TOKEN_WHITESPACE, TOKEN_DATA, TOKEN_COMMENT, TOKEN_LINECOMMENT]
)
Expand Down Expand Up @@ -607,22 +612,37 @@ def tokenize(
name: t.Optional[str] = None,
filename: t.Optional[str] = None,
state: t.Optional[str] = None,
preserve_comments: bool = False,
pawamoy marked this conversation as resolved.
Show resolved Hide resolved
) -> TokenStream:
"""Calls tokeniter + tokenize and wraps it in a token stream."""
"""Calls tokeniter + tokenize and wraps it in a token stream.

.. versionchanged:: 3.2
Added `preserve_comments` parameter.
"""
stream = self.tokeniter(source, name, filename, state)
return TokenStream(self.wrap(stream, name, filename), name, filename)
return TokenStream(
self.wrap(stream, name, filename, preserve_comments), name, filename
)

def wrap(
self,
stream: t.Iterable[t.Tuple[int, str, str]],
name: t.Optional[str] = None,
filename: t.Optional[str] = None,
preserve_comments: bool = False,
pawamoy marked this conversation as resolved.
Show resolved Hide resolved
) -> t.Iterator[Token]:
"""This is called with the stream as returned by `tokenize` and wraps
every token in a :class:`Token` and converts the value.

.. versionchanged:: 3.2
Added `preserve_comments` parameter.
"""
ignored = ignored_tokens
if preserve_comments:
ignored -= comment_tokens

for lineno, token, value_str in stream:
if token in ignored_tokens:
if token in ignored:
continue

value: t.Any = value_str
Expand All @@ -631,6 +651,10 @@ def wrap(
token = TOKEN_BLOCK_BEGIN
elif token == TOKEN_LINESTATEMENT_END:
token = TOKEN_BLOCK_END
elif token == TOKEN_LINECOMMENT_BEGIN:
token = TOKEN_COMMENT_BEGIN
elif token == TOKEN_LINECOMMENT_END:
token = TOKEN_COMMENT_END
Comment on lines +624 to +627
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: Possible (small?) performance hit. Not sure how to avoid it.

# we are not interested in those tokens in the parser
elif token in (TOKEN_RAW_BEGIN, TOKEN_RAW_END):
continue
Expand Down
7 changes: 7 additions & 0 deletions src/jinja2/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,13 @@ def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Any:
return self.expr2.as_const(eval_ctx)


class Comment(Stmt):
"""A template comment."""
Copy link
Contributor Author

@pawamoy pawamoy Oct 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Could maybe add .. versionadded:: 3.2, is that a valid directive?


fields = ("data",)
data: str


def args_as_const(
node: t.Union["_FilterTestCommon", "Call"], eval_ctx: t.Optional[EvalContext]
) -> t.Tuple[t.List[t.Any], t.Dict[t.Any, t.Any]]:
Expand Down
10 changes: 9 additions & 1 deletion src/jinja2/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,12 @@ def __init__(
name: t.Optional[str] = None,
filename: t.Optional[str] = None,
state: t.Optional[str] = None,
preserve_comments: bool = False,
pawamoy marked this conversation as resolved.
Show resolved Hide resolved
) -> None:
self.environment = environment
self.stream = environment._tokenize(source, name, filename, state)
self.stream = environment._tokenize(
source, name, filename, state, preserve_comments
)
pawamoy marked this conversation as resolved.
Show resolved Hide resolved
self.name = name
self.filename = filename
self.closed = False
Expand Down Expand Up @@ -1025,6 +1028,11 @@ def flush_data() -> None:
else:
body.append(rv)
self.stream.expect("block_end")
elif token.type == "comment_begin":
pawamoy marked this conversation as resolved.
Show resolved Hide resolved
flush_data()
next(self.stream)
body.append(nodes.Comment(next(self.stream).value))
self.stream.expect("comment_end")
else:
raise AssertionError("internal parsing error")

Expand Down
22 changes: 22 additions & 0 deletions tests/test_lexnparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,28 @@ def assert_error(code, expected):
)
assert_error("{% unknown_tag %}", "Encountered unknown tag 'unknown_tag'.")

def test_comment_preservation(self, env):
ast = env.parse("{# foo #}{{ bar }}", preserve_comments=True)
assert len(ast.body) == 2
assert isinstance(ast.body[0], nodes.Comment)
assert ast.body[0].data == " foo "

ast = env.parse("{# foo #}{{ bar }}", preserve_comments=False)
assert len(ast.body) == 1
assert not isinstance(ast.body[0], nodes.Comment)

def test_line_comment_preservation(self, env):
env = Environment(line_comment_prefix="#")

ast = env.parse("# foo\n{{ bar }}", preserve_comments=True)
assert len(ast.body) == 2
assert isinstance(ast.body[0], nodes.Comment)
assert ast.body[0].data == " foo"

ast = env.parse("# foo\n{{ bar }}", preserve_comments=False)
assert len(ast.body) == 1
assert not isinstance(ast.body[0], nodes.Comment)


class TestSyntax:
def test_call(self, env):
Expand Down