1. Álvaro Justen - Turicas
  2. web2py-template

Commits

Álvaro Justen - Turicas  committed d488db0

More tests and implementation on indentation/unindentation issues

  • Participants
  • Parent commits d4d590e
  • Branches default

Comments (0)

Files changed (2)

File new_template.py

View file
     return parsed_line.split()[0].replace(':', '').strip() in unindentation_words
 
 
-def parse_python_code(python_code):
-    indentation_level = 0
+def parse_python_code(python_code, indentation_level=0):
     response = []
 
     in_string = False
                 indentation_level -= 1
             if parsed_line.strip() != 'pass':
                 response.append(indentation_level * '    ' + parsed_line)
+            if last_character == ':' and not in_string:
+                indentation_level += 1
 
-    return response
+    return indentation_level, response
 
 
 def parse_template(template_code):
+    indentation_level = 0
     response = []
     line = []
     open_tag_count = 0
             if character == '\n' and in_comment:
                 in_comment = False
                 if line:
-                    response.extend(parse_python_code(''.join(line)))
+                    indentation_level, parsed_code = parse_python_code(''.join(line), indentation_level)
+                    response.extend(parsed_code)
                     line = []
                 continue
             elif character == '#' or in_comment:
                 in_python_code = True
                 line = line[:-2]
                 if line:
-                    response.append(string_on_html % ''.join(line))
+                    response.append(indentation_level * '    ' + string_on_html % ''.join(line))
                     line = []
         elif character == '}':
             if in_python_code and dict_count > 0 and not in_string:
                 in_python_code = False
                 close_tag_count = 0
                 python_code = ''.join(line[:-2]).strip()
-                response.extend(parse_python_code(python_code))
+                indentation_level, parsed_code = parse_python_code(python_code, indentation_level)
+                response.extend(parsed_code)
                 line = []
         if character == '\\':
             counter_slash = True
 
     if line:
         if in_python_code:
-            response.extend(parse_python_code(''.join(line)))
+            indentation_level, parsed_code = parse_python_code(''.join(line), indentation_level)
+            response.extend(parsed_code)
         else:
-            response.append(string_on_html % ''.join(line))
+            response.append(indentation_level * '    ' + string_on_html % ''.join(line))
                         
     return '\n'.join(response)

File test_new_template.py

View file
                 do_nothing'''
         self.do_it()
 
+
+    def test_indentation_on_different_code_blocks(self):
+        self.input_ = '''{{if my_string == '}}':}}
+bla bla
+{{elif other_string == 'extend':}}
+other HTML
+{{pass}}'''
+        self.expected_output = '''if my_string == '}}':
+    response.write("\nbla bla\n", escape=False)
+elif other_string == 'extend':
+    response.write("\nother HTML\n", escape=False)'''
+        self.do_it()
+
+
+    def test_a_lot_of_indentation_levels_on_different_code_blocks(self):
+        self.input_ = '''{{if my_string == '}}':}}
+bla bla
+{{if other_thing:
+a = 2}}
+foo
+{{for a in b:}}
+bar
+{{pass
+else:
+a = 2}}
+baz
+{{pass
+elif other_string == 'extend':}}
+other HTML
+{{pass}}'''
+        self.expected_output = '''if my_string == '}}':
+    response.write("\nbla bla\n", escape=False)
+    if other_thing:
+        a = 2
+        response.write("\nfoo\n", escape=False)
+        for a in b:
+            response.write("\nbar\n", escape=False)
+    else:
+        a = 2
+        response.write("\nbaz\n", escape=False)
+elif other_string == 'extend':
+    response.write("\nother HTML\n", escape=False)'''
+        self.do_it()
+
+
+    def test_pass_should_unindent(self):
+        self.input_ = '''{{if something:}}
+bla bla bla
+{{pass}}
+some code
+{{if a:}}
+b
+{{pass}}'''
+        self.expected_output = '''if something:
+    response.write("\nbla bla bla\n", escape=False)
+response.write("\nsome code\n", escape=False)
+if a:
+    response.write("\nb\n", escape=False)'''
+        self.do_it()
+
+#test unindent: break continue return
+#response.write should change \n by \\n
+
 if __name__ == '__main__':
     if len(argv) == 1:
         import new_template