Commits

Anonymous committed 631a8df

Solved issues with newlines on HTML. Probably all things related to {{...}} and {{=...}} are done. Next steps: implement web2py "extend", "include" and "block" keywords

  • Participants
  • Parent commits 10a3bfb

Comments (0)

Files changed (4)

File new_template.py

 string_on_html = 'response.write("%s", escape=False)'
 
 def parse_string(text):
-    return text.replace('\\', '\\\\').replace('\n', '\\n').replace('"', '\\"')
+    return text.replace('\\', '\\\\').replace('\n', '\\\\n').replace('"', '\\"')
 
 
 def parse_line(line):
             response.extend(parsed_code)
         else:
             response.append(indentation_level * '    ' + string_on_html % parse_string(''.join(line)))
-                        
     return '\n'.join(response)

File quotes_on.html

+response.write("'''\"\"\"\"'''", escape=False)
+a = 1
+response.write("'''\"\"\"\"'''\"\"\"\\n\"\"\"'''\"\"\"\\n", escape=False)

File quotes_on_view.html

+'''""""'''{{a = 1}}'''""""'''"""
+"""'''"""

File test_new_template.py

 
 DEBUG = False
 
-
 def do_it(self):
     parsed_template = parse_template(self.input_)
     if DEBUG:
 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("{\\n{ bla bla bla }\\n}", escape=False)'''
+        self.expected_output = '''response.write("{\\\\n{ bla bla bla }\\\\n}", escape=False)'''
         self.do_it()
 
 
         self.input_ = """{{
 a = 'some \\' }}\\
 string'
-b = '\\\\\'}}'
+b = '\\\\\\'}}'
 }}"""
         self.expected_output = """a = 'some \\' }}\\
 string'
-b = '\\\\\'}}'"""
+b = '\\\\\\'}}'"""
         self.do_it()
 
 
         self.do_it()
 
 class TestHTML(unittest.TestCase):
+
     do_it = do_it
 
-    def test_single_quote_outsite_python_code(self):
+    def test_single_quote_outside_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):
+    def test_double_quote_outside_python_code(self):
         self.input_ = '''"{{a = 1}}'''
         self.expected_output = """response.write("\\\"", escape=False)
 a = 1"""
         self.do_it()
 
+    def test_new_line_on_HTML_should_be_backslashed_on_response_write(self):
+        self.input_ = """
+"""
+        self.expected_output = """response.write("\\\\n", escape=False)"""
+        self.do_it()
+
+
+    def test_all_kind_of_quotes_outside_python_code(self):
+        input_file = open('quotes_on_view.html', 'r')
+        output_file = open('quotes_on.html', 'r')
+        self.input_ = input_file.read()
+        self.expected_output = output_file.read()[:-1] # :-1 removes \n
+        input_file.close()
+        output_file.close()
+        self.do_it()
+
 
 class TestIndentation(unittest.TestCase):
     do_it = do_it
 code_on_for
 other_code
 pass
-some_code_outsite
+some_code_outside
 for c in d:
 code_on_second_for
 other_code_on_second_for
 try:
 other_try_code
 more_one_line
-except SomeExcetion, e:
+except SomeException, e:
 lets_solve_this_exception
 more_code
 except:
 for a in b:
     code_on_for
     other_code
-some_code_outsite
+some_code_outside
 for c in d:
     code_on_second_for
     other_code_on_second_for
 try:
     other_try_code
     more_one_line
-except SomeExcetion, e:
+except SomeException, e:
     lets_solve_this_exception
     more_code
 except:
 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()
 
 
 response.write(" test", escape=False)'''
         self.do_it()
 
-#Some thoughts...
-#About HTML:
-# what if we have quotes on HTML?
-# response.write should change \n by \\n on HTML strings
-
 
 if __name__ == '__main__':
     if len(argv) == 1:
 
 
     unittest.main()
+    write_responses.close()