Skip to content

Commit d93dd6a

Browse files
committed
Add global no-escape support
1 parent 43f3097 commit d93dd6a

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ The following changes have been made:
1414
- Dropped support for Python 2
1515
- Verified support for Python 3.7 - 3.12
1616
- Fixed bug where variables could be incorrectly resolved from other scopes (see [#3](https://github.com/zanieb/chevron-blue/pull/3))
17+
- Added global `--no-escape` option to disable HTML escaping (see [#4](https://github.com/zanieb/chevron-blue/pull/4))

src/chevron_blue/main.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,12 @@ def is_dir(arg):
113113
action="store_true",
114114
)
115115

116+
parser.add_argument(
117+
"--no-escape",
118+
help="Do not HTML escape values.",
119+
action="store_true",
120+
)
121+
116122
args = vars(parser.parse_args())
117123

118124
try:

src/chevron_blue/renderer.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ def render(
123123
scopes=None,
124124
warn=False,
125125
keep=False,
126+
no_escape=False,
126127
):
127128
"""Render a mustache template.
128129
@@ -170,6 +171,8 @@ def render(
170171
171172
warn -- Issue a warning to stderr when a template substitution isn't found in the data
172173
174+
no_escape -- Do not HTML escape variable values
175+
173176
keep -- Keep unreplaced tags when a template substitution isn't found in the data
174177
175178
@@ -230,7 +233,7 @@ def render(
230233
thing = scopes[1]
231234
if not isinstance(thing, str):
232235
thing = str(thing)
233-
output += _html_escape(thing)
236+
output += thing if no_escape else _html_escape(thing)
234237

235238
# If we're a no html escape tag
236239
elif tag == "no escape":

tests/test_spec.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,20 @@ def test_keep_from_partials(self):
511511
expected = "1st {{ missing_key }} 3rd"
512512
self.assertEqual(result, expected)
513513

514+
# https://github.com/noahmorrison/chevron/pull/94
515+
def test_no_escape(self):
516+
args = {
517+
"template": "{{ html_escaped }}",
518+
"data": {
519+
"html_escaped": '< > & "',
520+
},
521+
"no_escape": True,
522+
}
523+
524+
result = chevron_blue.render(**args)
525+
expected = '< > & "'
526+
self.assertEqual(result, expected)
527+
514528

515529
# Run unit tests from command line
516530
if __name__ == "__main__":

0 commit comments

Comments
 (0)