Anonymous avatar Anonymous committed d285796

Now I think all multiline issues on {{=...}} are tested and solved!

Comments (0)

Files changed (2)

     last_quote = ''
     last_character = ''
     line = []
+    open_something = '([{'
+    close_something = ')]}'
+    opened = 0
 
     for character in python_code:
         line.append(character)
+        if not in_string:
+            if character in open_something:
+                opened += 1
+            elif character in close_something:
+                opened -= 1
         if character in quotes and (not last_quote or character == last_quote):
             last_quote = character
             in_string = not in_string
             if not in_string:
                 last_quote = ''
-        if character == '\n' and not in_string:
+        if character == '\n' and not in_string and last_character != '\\' and opened == 0:
             parsed_line = parse_line(''.join(line[:-1]))
             if parsed_line:
                 if should_unindent_before(parsed_line):

test_new_template.py

         self.do_it()
 
 
-    def test_multiline_on_equal_at_python_code(self):
+    def test_multiline_on_equal_at_python_code_with_backslash(self):
         self.input_ = '''{{=a + \\
 b}}'''
-        self.expected_output = '''respone.write(a + \\
+        self.expected_output = '''response.write(a + \\
 b)'''
         self.do_it()
 
         self.do_it()
 
 
-#what if we have quotes on HTML?
-#test unindent: continue return
-#response.write should change \n by \\n on HTML strings
-#need to check strings on HTML *and* on {{=string}} - not on python code (without '=')
-#what about \ on the end of the line and open {, [ and ( in python code?
-#    print parse('''{{=("foo"
-#"bar")}}''')
-#    print parse('''{{=str({"foo":
-#    "bar"})}}''')
+    def test_multiline_on_equal_at_python_code_with_parenthesis_directly(self):
+        self.input_ = '''{{=("foo"
+"bar")}}'''
+        self.expected_output = '''response.write(("foo"
+"bar"))'''
+        self.do_it()
+
+
+    def test_multiline_on_equal_at_python_code_with_parenthesis(self):
+        self.input_ = '''{{=a + ("foo"
+"bar")}}'''
+        self.expected_output = '''response.write(a + ("foo"
+"bar"))'''
+        self.do_it()
+
+
+    def test_multiline_on_equal_at_python_code_with_brackets(self):
+        self.input_ = '''{{=str({"foo":
+"bar"})}}'''
+        self.expected_output = '''response.write(str({"foo":
+"bar"}))'''
+        self.do_it()
+
+
+    def test_multiline_on_python_code_with_indentation(self):
+        self.input_ = '''{{if something:
+="foo" + \\
+"bar"
+pass}}a'''
+        self.expected_output = '''if something:
+    response.write("foo" + \\
+"bar")
+response.write("a", escape=False)'''
+        self.do_it()
+
+
+    def test_multiline_on_python_code_with_indentation_and_parenthesis(self):
+        self.input_ = '''{{if something:
+="foo" + ("bar"
+"baz")
+pass}}a'''
+        self.expected_output = '''if something:
+    response.write("foo" + ("bar"
+"baz"))
+response.write("a", escape=False)'''
+        self.do_it()
+
+
+    def test_multiline_on_equal_at_python_code_with_a_lot_of_parenthesis_and_brackets(self):
+        self.input_ = '''{{=a + "foo" + str([1, 2, 3,
+4, 5, 6]) + {'a': 123,
+'b': 456}.__str__() + ("foo"
+"bar")}}'''
+        self.expected_output = '''response.write(a + "foo" + str([1, 2, 3,
+4, 5, 6]) + {'a': 123,
+'b': 456}.__str__() + ("foo"
+"bar"))'''
+        self.do_it()
+
+
+#Some thoughts...
+#About Python code:
+# test unindent: continue return
+
+#About HTML:
+# what if we have quotes on HTML?
+# response.write should change \n by \\n on HTML strings
+# need to check strings on HTML *and* on {{=string}} - not on python code (without '=')
 
 
 if __name__ == '__main__':
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.