Commits

Rick Copeland committed cf97ece

Added XML processing instructions

Comments (0)

Files changed (4)

-* XML PIs
 * XML Comment tests
 * XML attribute support (non-py:-namespaced)
 * XML attributes: py:content, py:replace, py:strip, py:attrs

fastpt/tests/v2/test_xml.py

 import os
 ?>${os.path.join('a', 'b', 'c')}</div>''')
         rsp = tpl(dict(name='Rick')).__fpt__.render()
-        print rsp
-        assert rsp == 'a/b/c'
+        assert rsp == '<div>a/b/c</div>'
 
     def test_indent(self):
-        tpl = XMLTemplate('''%py
+        tpl = XMLTemplate('''<div
+><?py #
     import os
     import re
-%end
-${os.path.join('a','b','c')}''')
+?>${os.path.join('a','b','c')}</div>''')
         rsp = tpl(dict(name='Rick')).__fpt__.render()
-        assert rsp == 'a/b/c'
+        assert rsp == '<div>a/b/c</div>'
 
     def test_short(self):
-        tpl = XMLTemplate('''%py import os
-${os.path.join('a','b','c')}''')
+        tpl = XMLTemplate('''<div
+><?py import os
+?>${os.path.join('a', 'b', 'c')}</div>''')
         rsp = tpl(dict(name='Rick')).__fpt__.render()
-        assert rsp == 'a/b/c'
+        assert rsp == '<div>a/b/c</div>'
 
     def test_mod(self):
-        tpl = XMLTemplate('''%py% import os
-%def test()
-${os.path.join('a','b','c')}\\
-%end
-${test()}''')
+        tpl = XMLTemplate('''<div
+><?py %import os
+?><py:def function="test()"
+>${os.path.join('a', 'b', 'c')}</py:def
+>${test()}</div>''')
         rsp = tpl(dict(name='Rick')).__fpt__.render()
-        assert rsp == 'a/b/c'
+        assert rsp == '<div>a/b/c</div>'
 
 if __name__ == '__main__':
     main()
             yield self.line(line)
 
     def _normalize(self, text):
+        if text.startswith('#\n'):
+            text = text[2:]
         prefix = None
         for line in text.splitlines():
             if prefix is None:

fastpt/v2/xml_template.py

             return self._compile_comment(node)
         elif isinstance(node, dom.Text):
             return self._compile_text(node)
+        elif isinstance(node, dom.ProcessingInstruction):
+            return self._compile_pi(node)
         elif node.tagName.startswith('py:'):
             # Handle directives
             compiler = getattr(self, '_compile_%s' % node.tagName.split(':')[-1])
             return compiler(node)
-        elif isinstance(node, dom.ProcessingInstruction):
-            return self._compile_pi(node)
         elif self.mode == 'xml':
             return self._compile_xml(node)
         else:
         else:
             yield ir.TextNode(u'/>')
 
+    def _compile_pi(self, node):
+        body = ir.TextNode(node.data.strip())
+        node = ir.PythonNode(body)
+        if node.module_level:
+            self.mod_py.append(node)
+        else:
+            yield node
+
     def _compile_import(self, node):
         href = node.getAttribute('href')
         if node.hasAttribute('alias'):
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.