1. Juan BC
  2. trex

Commits

Juan BC  committed b35c921

broke

  • Participants
  • Parent commits 185be06
  • Branches default

Comments (0)

Files changed (2)

File trex/renderer.py

View file
 #===============================================================================
 
 import os
+import itertools
+
 
 from lxml import html, etree
-
+    
 
 #===============================================================================
 # CONSTANTS
 
 OUTPUT_TAG = "span"
 
+XPATH = "//python[ancestor::name!='python']"
+
 #===============================================================================
 # ERROR
 #===============================================================================
         return "%s - (%s) at %s" % (self.__class__.__name__,
                                    ", ".join(self._paths),
                                    hex(id(self)))
-
-    def _format_code(self, src):
-        """Return the code whitout empty and only comments lines and with the
-        correctly indented
+                                   
+    def _stringify_children(self, node):
+        """Return al the content of a node as string.
         
         """
+        parts = [node.text]
+        for child in node:
+            nodestr= etree.tostring(child, pretty_print=True, method="html")
+            parts.append(nodestr)
+        return "".join(parts)
+        
+    def _format_code(self, src):
+        """Return the code whitout tag names, empty lines and only comments
+        lines and with the correctly indented
+        
+        """
+
         lines = [l.rstrip() for l in src.splitlines() 
                  if l.strip() and not l.strip().startswith("#")]
         has_margin = lambda l: l.startswith(" ") or l.startswith("\t")
             else:
                 break
         if dom == None:
-            msg = "Template '%s' not found in paths: %s" % (fpath,
-                                                            ", ".join(self._paths))
+            msg = "Template '%s' not found in paths: %s"
+            msg = msg % (fpath, ", ".join(self._paths))
             raise RenderError(msg)
 
         template_globals = globals()
         template_locals = {}
-        for py_element in dom.iterfind("//%s" % PYTHON_HTML_TAG):
+        for py_element in dom.iterfind(XPATH):
             
-            # format and resolve code
-            src = self._format_code(py_element.text or "")
+            #preserve the attributes and tail for the output
+            attribs = dict(py_element.attrib)
+            tail = py_element.tail or ""
+            
+            # format code
+            # src = self._stringify_children(py_element)
+            src = self._format_code(py_element.text)
             response_list = []
+            
+            template_locals.update(echo=response_list.append)
             if src:
-                template_locals.update(echo=response_list.append)
                 exec(src, template_globals, template_locals)
             response = "".join([str(e) for e in response_list])
             
             # create the replacement
-            div_element = html.HtmlElement(); div_element.tag = OUTPUT_TAG
-            for k, v in py_element.attrib.items():
-                div_element.set(k, v)
-            div_element.text =  response + (py_element.tail or "") 
+            out_element = html.HtmlElement()
+            out_element.tag = OUTPUT_TAG
+            for k, v in attribs.items():
+                out_element.attrib[k] = v
+            out_element.text =  response + tail 
             
             # replace
             parent_element = py_element.getparent()
             if parent_element != None:
-                parent_element.replace(py_element, div_element)
+                parent_element.replace(py_element, out_element)
             else:
                 msg = "'<%s>...</%s>' must be inside '<html></html> tags." + \
                       "Check %s'" % (PYTHON_HTML_TAG, PYTHON_HTML_TAG, rendering_path)
                 raise RenderError(msg)
         
-        return etree.tostring(dom.getroot(), pretty_print=True, method="html")
+        render = etree.tostring(dom.getroot(), encoding="utf-8",
+                                pretty_print=True, method="html")
+        return render
         
     @property
     def paths(self):
         return self._paths
 
+print Renderer().render("/home/juan/Escritorio/trex/trex/test/coso.html")
 
 #===============================================================================
 # MAIN

File trex/test/coso.html

View file
     <title>sin título</title>
     <meta http-equiv="content-type" content="text/html;charset=utf-8" />
     <meta name="generator" content="Geany 0.20" />
-    <python></python>
+    <python id="dddd">
+        echo("dd")
+    </python> jjj
 </head>
 
 <body>
 
         <python id="1">
-            import random; import trex
+            <python></python>
+            import random
             import datetime
-			
             echo(random.randint(0, 99))
             print("hola")
             #APP.algo = 1
-            echo(trex.APP)
         </python>
 
         <python>