From 8ec21b9eff5709f46c4ec1dcde1552ea59881f68 Mon Sep 17 00:00:00 2001 From: Hiu Kwok Date: Wed, 30 Jun 2021 23:47:06 +1000 Subject: [PATCH 1/2] yasha-31: Latex support --- .idea/.gitignore | 3 ++ .idea/misc.xml | 86 +++++++++++++++++++++++++++++++++++++++++++++++ .idea/modules.xml | 8 +++++ .idea/vcs.xml | 6 ++++ .idea/yasha.iml | 9 +++++ yasha/cli.py | 6 ++-- yasha/yasha.py | 57 +++++++++++++++++++++---------- 7 files changed, 155 insertions(+), 20 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 .idea/yasha.iml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..4ba9b42 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,86 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..3d9b352 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/yasha.iml b/.idea/yasha.iml new file mode 100644 index 0000000..d6ebd48 --- /dev/null +++ b/.idea/yasha.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/yasha/cli.py b/yasha/cli.py index 3ba4168..aa73dfa 100644 --- a/yasha/cli.py +++ b/yasha/cli.py @@ -146,11 +146,12 @@ def load_extensions(file): @click.option("-M", is_flag=True, help="Outputs Makefile compatible list of dependencies. Doesn't render the template.") @click.option("-MD", is_flag=True, help="Creates Makefile compatible .d file alongside the rendered template.") @click.option('--version', is_flag=True, callback=print_version, expose_value=False, is_eager=True, help="Print version and exit.") +@click.option("--latex", "-latex", is_flag=True, help="Interpret template file in LaTeX format.") def cli( template_variables, template, output, variables, extensions, encoding, include_path, no_variable_file, no_extension_file, no_trim_blocks, no_lstrip_blocks, remove_trailing_newline, - mode, m, md): + mode, m, md, latex): """Reads the given Jinja TEMPLATE and renders its content into a new file. For example, a template called 'foo.c.j2' will be written into 'foo.c' in case the output file is not @@ -228,7 +229,8 @@ def cli( mode=mode, trim_blocks=not no_trim_blocks, lstrip_blocks=not no_lstrip_blocks, - keep_trailing_newline=not remove_trailing_newline + keep_trailing_newline=not remove_trailing_newline, + latex_support=latex ) # Get template diff --git a/yasha/yasha.py b/yasha/yasha.py index 9d5c229..f49867b 100644 --- a/yasha/yasha.py +++ b/yasha/yasha.py @@ -129,7 +129,7 @@ def parse_cli_variables(args): def load_jinja( path, tests, filters, classes, mode, - trim_blocks, lstrip_blocks, keep_trailing_newline): + trim_blocks, lstrip_blocks, keep_trailing_newline, latex_support): from jinja2.defaults import BLOCK_START_STRING, BLOCK_END_STRING, \ VARIABLE_START_STRING, VARIABLE_END_STRING, \ COMMENT_START_STRING, COMMENT_END_STRING, \ @@ -142,23 +142,44 @@ def load_jinja( None: jinja.Undefined, } - env = jinja.Environment( - block_start_string=BLOCK_START_STRING, - block_end_string=BLOCK_END_STRING, - variable_start_string=VARIABLE_START_STRING, - variable_end_string=VARIABLE_END_STRING, - comment_start_string=COMMENT_START_STRING, - comment_end_string=COMMENT_END_STRING, - line_statement_prefix=LINE_STATEMENT_PREFIX, - line_comment_prefix=LINE_COMMENT_PREFIX, - trim_blocks=trim_blocks, - lstrip_blocks=lstrip_blocks, - newline_sequence=NEWLINE_SEQUENCE, - keep_trailing_newline=keep_trailing_newline, - extensions=classes, - undefined=undefined[mode], - loader=jinja.FileSystemLoader(path) - ) + if latex_support: + env = jinja.Environment( + block_start_string= '\BLOCK{', + block_end_string='}', + variable_start_string='\VAR{', + variable_end_string='}', + comment_start_string='\#{', + comment_end_string='}', + line_statement_prefix='%%', + line_comment_prefix='%#', + trim_blocks=True, + lstrip_blocks=lstrip_blocks, + autoescape = False, + newline_sequence=NEWLINE_SEQUENCE, + keep_trailing_newline=keep_trailing_newline, + extensions=classes, + undefined=undefined[mode], + loader=jinja.FileSystemLoader(path) + ) + else: + env = jinja.Environment( + block_start_string=BLOCK_START_STRING, + block_end_string=BLOCK_END_STRING, + variable_start_string=VARIABLE_START_STRING, + variable_end_string=VARIABLE_END_STRING, + comment_start_string=COMMENT_START_STRING, + comment_end_string=COMMENT_END_STRING, + line_statement_prefix=LINE_STATEMENT_PREFIX, + line_comment_prefix=LINE_COMMENT_PREFIX, + trim_blocks=trim_blocks, + lstrip_blocks=lstrip_blocks, + newline_sequence=NEWLINE_SEQUENCE, + keep_trailing_newline=keep_trailing_newline, + extensions=classes, + undefined=undefined[mode], + loader=jinja.FileSystemLoader(path) + ) + env.tests.update(tests) env.filters.update(filters) return env From 5324b8af90b0e230b24e12d870420697edace213 Mon Sep 17 00:00:00 2001 From: Hiu Kwok Date: Wed, 30 Jun 2021 23:51:20 +1000 Subject: [PATCH 2/2] Yasha-31: Minimise code change --- .idea/.gitignore | 3 -- .idea/misc.xml | 86 ----------------------------------------------- .idea/modules.xml | 8 ----- .idea/vcs.xml | 6 ---- .idea/yasha.iml | 9 ----- 5 files changed, 112 deletions(-) delete mode 100644 .idea/.gitignore delete mode 100644 .idea/misc.xml delete mode 100644 .idea/modules.xml delete mode 100644 .idea/vcs.xml delete mode 100644 .idea/yasha.iml diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 26d3352..0000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index 4ba9b42..0000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,86 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index 3d9b352..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 35eb1dd..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/yasha.iml b/.idea/yasha.iml deleted file mode 100644 index d6ebd48..0000000 --- a/.idea/yasha.iml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - \ No newline at end of file