diff --git a/j2cli/cli.py b/j2cli/cli.py index fae229a..727ec08 100644 --- a/j2cli/cli.py +++ b/j2cli/cli.py @@ -44,10 +44,12 @@ class Jinja2TemplateRenderer(object): 'jinja2.ext.loopcontrols', ) - def __init__(self, cwd, allow_undefined, j2_env_params): + def __init__(self, cwd, allow_undefined, no_compact=False, j2_env_params={}): # Custom env params j2_env_params.setdefault('keep_trailing_newline', True) j2_env_params.setdefault('undefined', jinja2.Undefined if allow_undefined else jinja2.StrictUndefined) + j2_env_params.setdefault('trim_blocks', not no_compact) + j2_env_params.setdefault('lstrip_blocks', not no_compact) j2_env_params.setdefault('extensions', self.ENABLED_EXTENSIONS) j2_env_params.setdefault('loader', FilePathLoader(cwd)) @@ -118,7 +120,10 @@ def render_command(cwd, environ, stdin, argv): help='Load custom Jinja2 tests from a Python file.') parser.add_argument('--customize', default=None, metavar='python-file.py', dest='customize', help='A Python file that implements hooks to fine-tune the j2cli behavior') - parser.add_argument('--undefined', action='store_true', dest='undefined', help='Allow undefined variables to be used in templates (no error will be raised)') + parser.add_argument('--no-compact', action='store_true', dest='no_compact', + help='Do not compact space around Jinja2 blocks.') + parser.add_argument('-U', '--undefined', action='store_true', dest='undefined', + help='Allow undefined variables to be used in templates (no error will be raised.)') parser.add_argument('-o', metavar='outfile', dest='output_file', help="Output to a file instead of stdout") parser.add_argument('template', help='Template file to process') parser.add_argument('data', nargs='?', default=None, help='Input data file path; "-" to use stdin') @@ -183,7 +188,7 @@ def render_command(cwd, environ, stdin, argv): context = customize.alter_context(context) # Renderer - renderer = Jinja2TemplateRenderer(cwd, args.undefined, j2_env_params=customize.j2_environment_params()) + renderer = Jinja2TemplateRenderer(cwd, args.undefined, args.no_compact, j2_env_params=customize.j2_environment_params()) customize.j2_environment(renderer._env) # Filters, Tests diff --git a/j2cli/context.py b/j2cli/context.py index f8035d4..547727e 100644 --- a/j2cli/context.py +++ b/j2cli/context.py @@ -1,11 +1,6 @@ +import six import sys -# Patch basestring in for python 3 compat -try: - basestring -except NameError: - basestring = str - #region Parsers def _parse_ini(data_string): @@ -120,7 +115,7 @@ def _parse_env(data_string): $ j2 config.j2 - < data.env """ # Parse - if isinstance(data_string, basestring): + if isinstance(data_string, six.string_types): data = filter( lambda l: len(l) == 2 , ( diff --git a/requirements-dev.txt b/requirements-dev.txt index 2ac3dff..f837c52 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -1,3 +1,4 @@ wheel nose exdoc +six diff --git a/setup.py b/setup.py index 71410d6..5733ee5 100755 --- a/setup.py +++ b/setup.py @@ -47,6 +47,7 @@ install_requires=[ 'jinja2 >= 2.7.2', + 'six >= 1.10', ], extras_require={ 'yaml': [pyyaml_version,]