Skip to content

Commit

Permalink
Merge pull request noahmorrison#51 from dmorrison42/partial_indentation
Browse files Browse the repository at this point in the history
Allow tabs in partial indentation (Fixes noahmorrison#49?)
  • Loading branch information
noahmorrison committed Apr 7, 2019
2 parents 5291b0d + 8bda5e5 commit e706f89
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
10 changes: 5 additions & 5 deletions chevron/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def _get_partial(name, partials_dict, partials_path, partials_ext):


def render(template='', data={}, partials_path='.', partials_ext='mustache',
partials_dict={}, padding=0, def_ldel='{{', def_rdel='}}',
partials_dict={}, padding='', def_ldel='{{', def_rdel='}}',
scopes=None):
"""Render a mustache template.
Expand Down Expand Up @@ -208,7 +208,7 @@ def render(template='', data={}, partials_path='.', partials_ext='mustache',
# Add padding to the key and add it to the output
if not isinstance(key, unicode_type):
key = unicode(key, 'utf-8')
output += key.replace('\n', '\n' + (' ' * padding))
output += key.replace('\n', '\n' + padding)

# If we're a variable tag
elif tag == 'variable':
Expand Down Expand Up @@ -332,11 +332,11 @@ def render(template='', data={}, partials_path='.', partials_ext='mustache',
partial = _get_partial(key, partials_dict,
partials_path, partials_ext)

# Find how much to pad the partial
# Find what to pad the partial with
left = output.split('\n')[-1]
part_padding = padding
if left.isspace():
part_padding += left.count(' ')
part_padding += left

# Render the partial
part_out = render(template=partial, partials_path=partials_path,
Expand All @@ -348,7 +348,7 @@ def render(template='', data={}, partials_path='.', partials_ext='mustache',
# If the partial was indented
if left.isspace():
# then remove the spaces from the end
part_out = part_out.rstrip(' ')
part_out = part_out.rstrip(' \t')

# Add the partials output to the ouput
if python3:
Expand Down
14 changes: 14 additions & 0 deletions test_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,20 @@ def test_nest_loops_with_same_key(self):

self.assertEqual(result, expected)

# https://github.com/noahmorrison/chevron/issues/49
def test_partial_indentation(self):
args = {
'template': '\t{{> count }}',
'partials_dict': {
'count': '\tone\n\ttwo'
}
}

result = chevron.render(**args)
expected = '\t\tone\n\t\ttwo'

self.assertEqual(result, expected)


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

0 comments on commit e706f89

Please sign in to comment.