88
99Options:
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
1415import sys
1516from io import BytesIO
1617from pathlib import Path
18+ from typing import Union
1719
1820from 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+
5072if __name__ == '__main__' :
5173 main ()
0 commit comments