Skip to content

Conversation

mitsuhiko
Copy link
Owner

@mitsuhiko mitsuhiko commented Jun 7, 2025

Implements \xNN (hexadecimal) and \NNN (octal) escape sequences to match Jinja2 compatibility. The \0 sequence is now handled as part of the octal system for consistency.

This was made with Claude Code for this video: https://www.youtube.com/watch?v=sQYXZCUvpIc

Fixes #803

Fixes #785

🤖 Generated with Claude Code

mitsuhiko and others added 3 commits June 7, 2025 21:45
…rals

Implements \xNN (hexadecimal) and \NNN (octal) escape sequences to match
Jinja2 compatibility. The \0 sequence is now handled as part of the octal
system for consistency.

Fixes #803

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
Make clippy happy
Adds an optional pycompat_rendering flag to Environment that enables
rendering values in a format compatible with Python Jinja2:
- `true`/`false` render as `True`/`False`
- `none` renders as `None`
- Strings use Python-style escaping and quoting

This addresses compatibility issues for SQL templating and other use
cases where precise output format matters. The feature is disabled by
default to maintain backward compatibility.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
fn test_pycompat_rendering_complex_case() {
let mut env = Environment::new();

// Test the specific case from the issue: {{ [true, false, none, 'foo', "bar'baz", '\x13'] }}
Copy link
Owner Author

Choose a reason for hiding this comment

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

The comment disagrees with the test implementation below.

@@ -0,0 +1,72 @@
use minijinja::{context, Environment};
Copy link
Owner Author

Choose a reason for hiding this comment

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

We really don't need this example here.

}

fn parse_hex_byte(&self, chars: &mut Chars) -> Result<u8, Error> {
let hexnum = chars.chain(repeat('\0')).take(2).collect::<String>();
Copy link
Owner Author

Choose a reason for hiding this comment

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

we should probably error if the sequence is just \x1

@mitsuhiko mitsuhiko changed the title Add support for hexadecimal and octal escape sequences in string literals Add support for new escape sequences and Python compatible rendering Jun 8, 2025
@mitsuhiko
Copy link
Owner Author

I'm going to merge part one of it in a separate PR: #805

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Render discrepancies with Python Jinja2 (none vs None, true vs True, double vs single quotes, tuple) MiniJinja does not support \x00 escape sequence
1 participant