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

Commits

Álvaro Justen - Turicas  committed b407ab8

Solved some issues on multlines and quotes on HTML code. Still failing in some multiline cases on python code (using {{=...}})

  • Participants
  • Parent commits 7176496
  • Branches default

Comments (0)

Files changed (2)

File new_template.py

View file
 quotes = ("'", '"')
 string_on_html = 'response.write("%s", escape=False)'
 
+def parse_string(text):
+    return text.replace('\\', '\\\\').replace('\n', '\\n').replace('"', '\\"')
+
+
 def parse_line(line):
     if not line:
         return ''
                     indentation_level, parsed_code = parse_python_code(''.join(line), indentation_level)
                     response.extend(parsed_code)
                     line = []
+                last_character = character
                 continue
             elif character == '#' or in_comment:
                 in_comment = True
+                last_character = character
                 continue
         
         if not in_python_code and character != '{':
         elif character == '{':
             if in_python_code and open_tag_count == 0 and not in_string:
                 dict_count += 1
+                last_character = character
                 continue
             if not in_string:
                 open_tag_count += 1
                 in_python_code = True
                 line = line[:-2]
                 if line:
-                    response.append(indentation_level * '    ' + string_on_html % ''.join(line))
+                    response.append(indentation_level * '    ' + string_on_html % parse_string(''.join(line)))
                     line = []
         elif character == '}':
             if in_python_code and dict_count > 0 and not in_string:
                 dict_count -= 1
+                last_character = character
                 continue
             if not in_string:
                 close_tag_count += 1
             indentation_level, parsed_code = parse_python_code(''.join(line), indentation_level)
             response.extend(parsed_code)
         else:
-            response.append(indentation_level * '    ' + string_on_html % ''.join(line))
+            response.append(indentation_level * '    ' + string_on_html % parse_string(''.join(line)))
                         
     return '\n'.join(response)

File test_new_template.py

View file
 it is cool!''')}}"""
         self.expected_output = """response.write("some plain text ", escape=False)
 response.write('''hi''')
-response.write("\n", escape=False)
+response.write("\\n", escape=False)
 response.write(some_function('''testing web2py
 it is cool!'''))"""
         self.do_it()
         self.input_ = '''{
 { bla bla bla }
 }'''
-        self.expected_output = '''response.write("{
-{ bla bla bla }
-}", escape=False)'''
+        self.expected_output = '''response.write("{\\n{ bla bla bla }\\n}", escape=False)'''
         self.do_it()
 
 
         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 \\' }}\\
 other HTML
 {{pass}}'''
         self.expected_output = '''if my_string == '}}':
-    response.write("\nbla bla\n", escape=False)
+    response.write("\\nbla bla\\n", escape=False)
 elif other_string == 'extend':
-    response.write("\nother HTML\n", escape=False)'''
+    response.write("\\nother HTML\\n", escape=False)'''
         self.do_it()
 
 
 other HTML
 {{pass}}'''
         self.expected_output = '''if my_string == '}}':
-    response.write("\nbla bla\n", escape=False)
+    response.write("\\nbla bla\\n", escape=False)
     if other_thing:
         a = 2
-        response.write("\nfoo\n", escape=False)
+        response.write("\\nfoo\\n", escape=False)
         for a in b:
-            response.write("\nbar\n", escape=False)
+            response.write("\\nbar\\n", escape=False)
     else:
         a = 2
-        response.write("\nbaz\n", escape=False)
+        response.write("\\nbaz\\n", escape=False)
 elif other_string == 'extend':
-    response.write("\nother HTML\n", escape=False)'''
+    response.write("\\nother HTML\\n", escape=False)'''
         self.do_it()
 
 
 b
 {{pass}}'''
         self.expected_output = '''if something:
-    response.write("\nbla bla bla\n", escape=False)
-response.write("\nsome code\n", escape=False)
+    response.write("\\nbla bla bla\\n", escape=False)
+response.write("\\nsome code\\n", escape=False)
 if a:
-    response.write("\nb\n", escape=False)'''
+    response.write("\\nb\\n", escape=False)'''
         self.do_it()
 
 
         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_multiline_on_equal_at_python_code(self):
+        self.input_ = '''{{=a + \\
+b}}'''
+        self.expected_output = '''respone.write(a + \\
+b)'''
+        self.do_it()
+
+
+    def test_multiline_on_strings_in_equal_at_python_code(self):
+        self.input_ = '''{{=a + """bla bla bla
+foo bar"""}}'''
+        self.expected_output = '''response.write(a + """bla bla bla
+foo bar""")'''
+        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
+#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"})}}''')
+
 
 if __name__ == '__main__':
     if len(argv) == 1: