Skip to content

Commit

Permalink
Merge branch 'Dylan-Brotherston-main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
noahmorrison committed Mar 21, 2021
2 parents 90413a2 + 410a9c1 commit 5e1c128
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 10 deletions.
25 changes: 15 additions & 10 deletions chevron/renderer.py
Expand Up @@ -48,7 +48,7 @@ def _html_escape(string):
return string


def _get_key(key, scopes, warn=False):
def _get_key(key, scopes, warn, keep, def_ldel, def_rdel):
"""Get a key from the current scope"""

# If the key is a dot
Expand Down Expand Up @@ -94,6 +94,9 @@ def _get_key(key, scopes, warn=False):
if warn:
sys.stderr.write("Could not find key '%s'%s" % (key, linesep))

if keep:
return "%s %s %s" % (def_ldel, key, def_rdel)

return ''


Expand Down Expand Up @@ -127,7 +130,7 @@ def _get_partial(name, partials_dict, partials_path, partials_ext):

def render(template='', data={}, partials_path='.', partials_ext='mustache',
partials_dict={}, padding='', def_ldel='{{', def_rdel='}}',
scopes=None, warn=False):
scopes=None, warn=False, keep=False):
"""Render a mustache template.
Renders a mustache template with a data scope and partial capability.
Expand Down Expand Up @@ -172,7 +175,9 @@ def render(template='', data={}, partials_path='.', partials_ext='mustache',
scopes -- The list of scopes that get_key will look through
warn -- Issue a warning to stderr when a template substitution isn't found in the data
warn -- Issue a warning to stderr when a template substitution isn't found in the data
keep -- Keep unreplaced tags when a template substitution isn't found in the data
Returns:
Expand Down Expand Up @@ -225,7 +230,7 @@ def render(template='', data={}, partials_path='.', partials_ext='mustache',
# If we're a variable tag
elif tag == 'variable':
# Add the html escaped key to the output
thing = _get_key(key, scopes, warn=warn)
thing = _get_key(key, scopes, warn=warn, keep=keep, def_ldel=def_ldel, def_rdel=def_rdel)
if thing is True and key == '.':
# if we've coerced into a boolean by accident
# (inverted tags do this)
Expand All @@ -238,15 +243,15 @@ def render(template='', data={}, partials_path='.', partials_ext='mustache',
# If we're a no html escape tag
elif tag == 'no escape':
# Just lookup the key and add it
thing = _get_key(key, scopes, warn=warn)
thing = _get_key(key, scopes, warn=warn, keep=keep, def_ldel=def_ldel, def_rdel=def_rdel)
if not isinstance(thing, unicode_type):
thing = unicode(str(thing), 'utf-8')
output += thing

# If we're a section tag
elif tag == 'section':
# Get the sections scope
scope = _get_key(key, scopes, warn=warn)
scope = _get_key(key, scopes, warn=warn, keep=keep, def_ldel=def_ldel, def_rdel=def_rdel)

# If the scope is a callable (as described in
# https://mustache.github.io/mustache.5.html)
Expand Down Expand Up @@ -287,7 +292,7 @@ def render(template='', data={}, partials_path='.', partials_ext='mustache',
padding=padding,
def_ldel=def_ldel, def_rdel=def_rdel,
scopes=data and [data]+scopes or scopes,
warn=warn))
warn=warn, keep=keep))

if python3:
output += rend
Expand Down Expand Up @@ -324,7 +329,7 @@ def render(template='', data={}, partials_path='.', partials_ext='mustache',
partials_ext=partials_ext,
partials_dict=partials_dict,
def_ldel=def_ldel, def_rdel=def_rdel,
warn=warn)
warn=warn, keep=keep)

if python3:
output += rend
Expand All @@ -338,7 +343,7 @@ def render(template='', data={}, partials_path='.', partials_ext='mustache',
# If we're an inverted section
elif tag == 'inverted section':
# Add the flipped scope to the scopes
scope = _get_key(key, scopes, warn=warn)
scope = _get_key(key, scopes, warn=warn, keep=keep, def_ldel=def_ldel, def_rdel=def_rdel)
scopes.insert(0, not scope)

# If we're a partial
Expand All @@ -359,7 +364,7 @@ def render(template='', data={}, partials_path='.', partials_ext='mustache',
partials_dict=partials_dict,
def_ldel=def_ldel, def_rdel=def_rdel,
padding=part_padding, scopes=scopes,
warn=warn)
warn=warn, keep=keep)

# If the partial was indented
if left.isspace():
Expand Down
53 changes: 53 additions & 0 deletions test_spec.py
Expand Up @@ -499,6 +499,59 @@ def test_disabled_partials(self):
self.assertEqual(resultEmpty, expected)
os.chdir('..')

# https://github.com/noahmorrison/chevron/pull/94
def test_keep(self):
args = {
'template': '{{ first }} {{ second }} {{ third }}',
'data': {
"first": "1st",
"third": "3rd",
},
}

result = chevron.render(**args)
expected = '1st 3rd'
self.assertEqual(result, expected)

args['keep'] = True

result = chevron.render(**args)
expected = '1st {{ second }} 3rd'
self.assertEqual(result, expected)

args['template'] = '{{first}} {{second}} {{third}}'
result = chevron.render(**args)
expected = '1st {{ second }} 3rd'
self.assertEqual(result, expected)

args['template'] = '{{ first }} {{ second }} {{ third }}'
result = chevron.render(**args)
expected = '1st {{ second }} 3rd'
self.assertEqual(result, expected)

# https://github.com/noahmorrison/chevron/pull/94
def test_keep_from_partials(self):
args = {
'template': '{{ first }} {{> with_missing_key }} {{ third }}',
'data': {
"first": "1st",
"third": "3rd",
},
'partials_dict': {
'with_missing_key': '{{missing_key}}',
},
}

result = chevron.render(**args)
expected = '1st 3rd'
self.assertEqual(result, expected)

args['keep'] = True

result = chevron.render(**args)
expected = '1st {{ missing_key }} 3rd'
self.assertEqual(result, expected)


# Run unit tests from command line
if __name__ == "__main__":
Expand Down

0 comments on commit 5e1c128

Please sign in to comment.