Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Yasha-31: Provide LaTeX support #71

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions yasha/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
57 changes: 39 additions & 18 deletions yasha/yasha.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, \
Expand All @@ -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