Commits

Álvaro Justen - Turicas  committed 7176496

unindent on break; added tests on backslahes

  • Participants
  • Parent commits d488db0

Comments (0)

Files changed (2)

File new_template.py

     else:
         return line
 
-def should_unindent(parsed_line):
-    unindentation_words = ('pass', 'else', 'elif', 'except')
+def should_unindent_before(parsed_line):
+    unindentation_words = ('else', 'elif', 'except')
+    return parsed_line.split()[0].replace(':', '').strip() in unindentation_words
+
+
+def should_unindent_after(parsed_line):
+    unindentation_words = ('pass', 'break')
     return parsed_line.split()[0].replace(':', '').strip() in unindentation_words
 
 
         if character == '\n' and not in_string:
             parsed_line = parse_line(''.join(line[:-1]))
             if parsed_line:
-                if should_unindent(parsed_line):
+                if should_unindent_before(parsed_line):
                     indentation_level -= 1
                 if parsed_line.strip() != 'pass':
                     response.append(indentation_level * '    ' + parsed_line)
+                if should_unindent_after(parsed_line):
+                    indentation_level -= 1
             line = []
-        if last_character == ':' and not in_string and character == '\n':
-            indentation_level += 1
+            if last_character == ':':
+                indentation_level += 1
         last_character = character
 
     if line:
         parsed_line = parse_line(''.join(line))
         if parsed_line:
-            if should_unindent(parsed_line):
+            if should_unindent_before(parsed_line):
                 indentation_level -= 1
             if parsed_line.strip() != 'pass':
                 response.append(indentation_level * '    ' + parsed_line)
+            if should_unindent_after(parsed_line):
+                indentation_level -= 1
             if last_character == ':' and not in_string:
                 indentation_level += 1
 
     last_quote = ''
     in_comment = False
     dict_count = 0
-    counter_slash = False
+    backslash = False
 
     for character in template_code:
 
 
         line.append(character)
 
-        if character in quotes and not counter_slash and (not last_quote or character == last_quote):
+        if in_python_code and character in quotes and not backslash and (not last_quote or character == last_quote):
             last_quote = character
             in_string = not in_string
             if not in_string:
                 response.extend(parsed_code)
                 line = []
         if character == '\\':
-            counter_slash = True
+            backslash = True
         else:
-            counter_slash = False
+            backslash = False
 
     if line:
         if in_python_code:

File test_new_template.py

         self.do_it()
 
 
+    def test_single_quote_outsite_python_code(self):
+        self.input_ = """'{{a = 1}}"""
+        self.expected_output = """response.write("'", escape=False)
+a = 1"""
+        self.do_it()
+
+
+    def test_double_quote_outsite_python_code(self):
+        self.input_ = '''"{{a = 1}}'''
+        self.expected_output = """response.write('"', escape=False)
+a = 1"""
+        self.do_it()
+
+
     def test_counterslash_and_quote_on_python_strings(self):
         self.input_ = """{{
 a = 'some \\' }}\\
     response.write("\nb\n", escape=False)'''
         self.do_it()
 
-#test unindent: break continue return
+
+    def test_unindent_with_break_keyword(self):
+        self.input_ = '''{{for a in b:
+break}} test'''
+        self.expected_output = '''for a in b:
+    break
+response.write(" test", escape=False)'''
+        self.do_it()
+
+
+    def test_unindent_with_break_keyword_in_second_level_indentation(self):
+        self.input_ = '''{{for a in b:
+if a == 10:
+break
+pass}} test'''
+        self.expected_output = '''for a in b:
+    if a == 10:
+        break
+response.write(" test", escape=False)'''
+        self.do_it()
+
+
+    def test_backslash_state_with_multiple_backslashes(self):
+        self.input_ = '''{{a = '\\\\testing'}}'bla'''
+        self.expected_output = '''a = '\\\\testing'
+response.write("'bla", escape=False)'''
+        self.do_it()
+
+
+#what if we have quotes on HTML?
+#test unindent: continue return
 #response.write should change \n by \\n
+#test \' on python code
 
 if __name__ == '__main__':
     if len(argv) == 1: