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

Fix custom delimiters in Python #506

Merged
merged 3 commits into from
Apr 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

All notable changes to MiniJinja are documented here.

## 2.0.1

- Fixed an issue that caused custom delimiters to not work in the Python
binding. #506

## 2.0.0

This is a major update to MiniJinja that changes a lot of core internals and
Expand Down
4 changes: 4 additions & 0 deletions minijinja-py/python/minijinja/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ def __init__(
variable_end_string="}}",
comment_start_string="{#",
comment_end_string="#}",
line_statement_prefix=None,
line_comment_prefix=None,
):
super().__init__()
if loader is not None:
Expand Down Expand Up @@ -85,6 +87,8 @@ def __init__(
self.variable_end_string = variable_end_string
self.comment_start_string = comment_start_string
self.comment_end_string = comment_end_string
self.line_statement_prefix = line_statement_prefix
self.line_comment_prefix = line_comment_prefix


DEFAULT_ENVIRONMENT = Environment()
Expand Down
32 changes: 30 additions & 2 deletions minijinja-py/src/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ struct Syntax {
variable_end: String,
comment_start: String,
comment_end: String,
line_statement_prefix: String,
line_comment_prefix: String,
}

impl Default for Syntax {
Expand All @@ -37,8 +39,10 @@ impl Default for Syntax {
block_end: "%}".into(),
variable_start: "{{".into(),
variable_end: "}}".into(),
comment_start: "{%".into(),
comment_end: "%}".into(),
comment_start: "{#".into(),
comment_end: "#}".into(),
line_statement_prefix: "".into(),
line_comment_prefix: "".into(),
}
}
}
Expand All @@ -48,6 +52,8 @@ impl Syntax {
.block_delimiters(self.block_start.clone(), self.block_end.clone())
.variable_delimiters(self.variable_start.clone(), self.variable_end.clone())
.comment_delimiters(self.comment_start.clone(), self.comment_end.clone())
.line_statement_prefix(self.line_statement_prefix.clone())
.line_comment_prefix(self.line_comment_prefix.clone())
.build()
}
}
Expand Down Expand Up @@ -536,6 +542,28 @@ impl Environment {
syntax_getter!(self, comment_end, "#}")
}

#[setter]
pub fn set_line_statement_prefix(&self, value: Option<String>) -> PyResult<()> {
syntax_setter!(self, value.unwrap_or_default(), line_statement_prefix, "")
}

#[getter]
pub fn get_line_statement_prefix(&self) -> Option<String> {
let rv: String = syntax_getter!(self, line_statement_prefix, "");
(!rv.is_empty()).then_some(rv)
}

#[setter]
pub fn set_line_comment_prefix(&self, value: Option<String>) -> PyResult<()> {
syntax_setter!(self, value.unwrap_or_default(), line_comment_prefix, "")
}

#[getter]
pub fn get_line_comment_prefix(&self) -> Option<String> {
let rv: String = syntax_getter!(self, line_comment_prefix, "");
(!rv.is_empty()).then_some(rv)
}

/// Configures the trailing newline trimming feature.
#[setter]
pub fn set_keep_trailing_newline(&self, yes: bool) -> PyResult<()> {
Expand Down
26 changes: 26 additions & 0 deletions minijinja-py/tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,3 +315,29 @@ def test_trim_and_lstrip_blocks():
assert env.render_str(" {% if true %}\nfoo{% endif %}") == " \nfoo"
env = Environment(lstrip_blocks=True, trim_blocks=True)
assert env.render_str(" {% if true %}\nfoo{% endif %}") == "foo"


def test_line_statements():
env = Environment()
assert env.line_statement_prefix is None
assert env.line_comment_prefix is None

env = Environment(line_statement_prefix="#", line_comment_prefix="##")
assert env.line_statement_prefix == "#"
assert env.line_comment_prefix == "##"

rv = env.render_str("# for x in range(3)\n{{ x }}\n# endfor")
assert rv == "0\n1\n2\n"


def test_custom_delimiters():
env = Environment(
variable_start_string="${",
variable_end_string="}",
block_start_string="<%",
block_end_string="%>",
comment_start_string="<!--",
comment_end_string="-->",
)
rv = env.render_str('<% if true %>${ value }<% endif %><!-- nothing -->', value=42)
assert rv == '42'