]> Devi Nivas Git - chevron.git/commitdiff
Added partial padding
authornoah morrison <noah@morrison.ph>
Wed, 12 Nov 2014 02:36:06 +0000 (21:36 -0500)
committernoah morrison <noah@morrison.ph>
Wed, 12 Nov 2014 02:36:06 +0000 (21:36 -0500)
Partials now can be indented in a silly manner based on the spec

(the silly manner being variables inside the partial have a
 god given ability to reset the indentation)

entei.py

index 6f570da766fa58897c0da99a486b24697f68e222..3e9872a7d5bd96fe6e1de1d0c028be856033aa2a 100755 (executable)
--- a/entei.py
+++ b/entei.py
@@ -142,7 +142,7 @@ def tokenize(template):
 
 
 def render(template, data, partials_path='.', partials_ext='mustache',
-           partials_dict={}):
+           partials_dict={}, padding=0):
     """Render a mustache template.
 
     Renders a mustache template with a data scope and partial capability.
@@ -241,7 +241,7 @@ def render(template, data, partials_path='.', partials_ext='mustache',
                 scopes.insert(0, not scope)
 
         elif tag == 'literal':
-            output += key
+            output += key.replace('\n', '\n' + (' ' * padding))
 
         elif tag == 'variable':
             output += html_escape(str(get_key(key)))
@@ -273,8 +273,19 @@ def render(template, data, partials_path='.', partials_ext='mustache',
 
         elif tag == 'partial':
             partial = get_partial(key)
-            output += render(partial, scopes, partials_path,
-                             partials_ext, partials_dict)
+
+            left = output.split('\n')[-1]
+            part_padding = padding
+            if left.isspace():
+                part_padding += left.count(' ')
+
+            part_out = render(partial, scopes, partials_path,
+                              partials_ext, partials_dict, part_padding)
+
+            if left.isspace():
+                part_out = part_out.rstrip(' ')
+
+            output += part_out
 
         else:
             print('>>', tag)