Skip to content

Commit 92148bb

Browse files
committed
add CLI parameter --config.keepComments=true/false to control whether comments should be kept
1 parent 2740360 commit 92148bb

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- support Python 3.14, drop support for Python 3.6, 3.7 and 3.8
66
- generated HTML should match mjml 4.18.0
7+
- comments in mjml will be present in the generated html by default now to match the reference implementation
78
- various fixes to match HTML generated the JS mjml, found by htmlcompare 0.4
89
- type hints contributed by @xoudini
910
- correctly render fractional width for mj-column (reported by @SinnySupernova)

mjml/scripts/mjml.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88
99
Options:
1010
--template-dir=<path> base dir for mj-include (default: path of mjml file)
11+
--config.keepComments=False whether comments in mjml should be present in the generated html (default: true)
1112
"""
1213
# ruff: noqa: E501
1314

1415
import sys
1516
from io import BytesIO
1617
from pathlib import Path
18+
from typing import Union
1719

1820
from docopt import docopt
1921

@@ -25,14 +27,19 @@ def main():
2527
mjml_filename = arguments['<MJML-FILE>']
2628
output_filename = arguments['<OUTPUT-FILE>']
2729
template_dir = arguments['--template-dir']
30+
keep_comments_str = arguments['--config.keepComments']
31+
keep_comments = _parse_bool(keep_comments_str, default=True)
32+
if keep_comments is None:
33+
sys.stderr.write("value for --config.keepComments should be either true or false\n")
34+
sys.exit(1)
2835

2936
if mjml_filename == '-':
3037
stdin = sys.stdin.buffer
3138
mjml_fp = BytesIO(stdin.read())
32-
result = mjml_to_html(mjml_fp, template_dir=template_dir)
39+
result = mjml_to_html(mjml_fp, template_dir=template_dir, keep_comments=keep_comments)
3340
else:
3441
with Path(mjml_filename).open('rb') as mjml_fp:
35-
result = mjml_to_html(mjml_fp, template_dir=template_dir)
42+
result = mjml_to_html(mjml_fp, template_dir=template_dir, keep_comments=keep_comments)
3643
assert not result.errors, result.errors
3744

3845
html_str = result.html
@@ -47,5 +54,20 @@ def main():
4754
sys.stdout.buffer.write(html_bytes)
4855

4956

57+
def _parse_bool(value: Union[str, None], *, default: bool) -> Union[bool, None]:
58+
if value is None:
59+
return default
60+
truthy = {'true', '1', 'yes', 'y'}
61+
falsey = {'false', '0', 'no', 'n'}
62+
63+
value_lower = value.strip().lower()
64+
if value_lower in truthy:
65+
return True
66+
elif value_lower in falsey:
67+
return False
68+
else:
69+
return None
70+
71+
5072
if __name__ == '__main__':
5173
main()

0 commit comments

Comments
 (0)