# HG changeset patch
# User Edouard Tisserant
# Date 1566289954 -7200
# Node ID ce04d79b8e5795bc3c5f5771d9aadda9f8649fd1
# Parent  282500e03dbccf5313f80ca2530adfb08c13b1a0
Pass HMITree to SVG transform. It seems it could really help to reduce JS tree binding logic in the end.

diff -r 282500e03dbc -r ce04d79b8e57 svghmi/gen_index_xhtml.xslt
--- a/svghmi/gen_index_xhtml.xslt	Tue Aug 20 10:30:59 2019 +0200
+++ b/svghmi/gen_index_xhtml.xslt	Tue Aug 20 10:32:34 2019 +0200
@@ -2,6 +2,7 @@
 <xsl:stylesheet xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:svg="http://www.w3.org/2000/svg" xmlns:exsl="http://exslt.org/common" xmlns:ns="beremiz" xmlns:cc="http://creativecommons.org/ns#" xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:dc="http://purl.org/dc/elements/1.1/" extension-element-prefixes="ns" version="1.0" exclude-result-prefixes="ns">
   <xsl:output method="xml"/>
   <xsl:variable name="geometry" select="ns:GetSVGGeometry()"/>
+  <xsl:variable name="hmitree" select="ns:GetHMITree()"/>
   <xsl:template match="@* | node()">
     <xsl:copy>
       <xsl:apply-templates select="@* | node()"/>
@@ -9,22 +10,48 @@
   </xsl:template>
   <xsl:template match="/">
     <xsl:copy>
-      <xsl:apply-templates mode="testgeo" select="$geometry"/>
+      <xsl:comment>
+        <xsl:apply-templates mode="testgeo" select="$geometry"/>
+      </xsl:comment>
+      <xsl:comment>
+        <xsl:text>blah
+</xsl:text>
+        <xsl:apply-templates mode="testtree" select="$hmitree"/>
+      </xsl:comment>
       <xsl:apply-templates select="@* | node()"/>
     </xsl:copy>
   </xsl:template>
   <xsl:template mode="testgeo" match="bbox">
-    <xsl:comment>
-      <xsl:text>ID: </xsl:text>
-      <xsl:value-of select="@Id"/>
-      <xsl:text> x: </xsl:text>
-      <xsl:value-of select="@x"/>
-      <xsl:text> y: </xsl:text>
-      <xsl:value-of select="@y"/>
-      <xsl:text> w: </xsl:text>
-      <xsl:value-of select="@w"/>
-      <xsl:text> h: </xsl:text>
-      <xsl:value-of select="@h"/>
-    </xsl:comment>
+    <xsl:text>ID: </xsl:text>
+    <xsl:value-of select="@Id"/>
+    <xsl:text> x: </xsl:text>
+    <xsl:value-of select="@x"/>
+    <xsl:text> y: </xsl:text>
+    <xsl:value-of select="@y"/>
+    <xsl:text> w: </xsl:text>
+    <xsl:value-of select="@w"/>
+    <xsl:text> h: </xsl:text>
+    <xsl:value-of select="@h"/>
+    <xsl:text>
+</xsl:text>
+  </xsl:template>
+  <xsl:template mode="testtree" match="*">
+    <xsl:param name="indent" select="''"/>
+    <xsl:value-of select="$indent"/>
+    <xsl:text> </xsl:text>
+    <xsl:value-of select="local-name()"/>
+    <xsl:text> </xsl:text>
+    <xsl:value-of select="@name"/>
+    <xsl:text> </xsl:text>
+    <xsl:value-of select="@type"/>
+    <xsl:text> </xsl:text>
+    <xsl:value-of select="@path"/>
+    <xsl:text>
+</xsl:text>
+    <xsl:apply-templates mode="testtree" select="*">
+      <xsl:with-param name="indent">
+        <xsl:value-of select="concat($indent,'&gt;')"/>
+      </xsl:with-param>
+    </xsl:apply-templates>
   </xsl:template>
 </xsl:stylesheet>
diff -r 282500e03dbc -r ce04d79b8e57 svghmi/gen_index_xhtml.ysl2
--- a/svghmi/gen_index_xhtml.ysl2	Tue Aug 20 10:30:59 2019 +0200
+++ b/svghmi/gen_index_xhtml.ysl2	Tue Aug 20 10:32:34 2019 +0200
@@ -18,6 +18,7 @@
      * <bbox x="0" y="0" w="42" h="42">
      */
     variable "geometry", "ns:GetSVGGeometry()";
+    variable "hmitree", "ns:GetHMITree()";
    
     /* Identity template :
      *  - copy every attributes 
@@ -31,14 +32,26 @@
     /* copy root node and add geometry as comment for a test */
     template "/" {
       xsl:copy {
-          apply "$geometry", mode="testgeo";
+          comment {
+              apply "$geometry", mode="testgeo";
+          }
+          comment {
+              | blah
+              apply "$hmitree", mode="testtree";
+          }
           apply "@* | node()";
       }
     }
 
     template "bbox", mode="testgeo"{
-          comment {
-              > ID: «@Id» x: «@x» y: «@y» w: «@w» h: «@h»
-          }
+        | ID: «@Id» x: «@x» y: «@y» w: «@w» h: «@h»
+    }
+
+    template "*", mode="testtree"{
+        param "indent", "''";
+        | «$indent» «local-name()» «@name» «@type» «@path»
+        apply "*", mode="testtree" {
+            with "indent" value "concat($indent,'>')"
+        };
     }
 }
diff -r 282500e03dbc -r ce04d79b8e57 svghmi/svghmi.py
--- a/svghmi/svghmi.py	Tue Aug 20 10:30:59 2019 +0200
+++ b/svghmi/svghmi.py	Tue Aug 20 10:32:34 2019 +0200
@@ -9,7 +9,7 @@
 from __future__ import absolute_import
 import os
 import shutil
-from itertools import izip
+from itertools import izip, imap
 from pprint import pprint, pformat
 
 import wx
@@ -71,12 +71,33 @@
         else:
             self.children.append(node)
             
+    def etree(self):
+
+        attribs = dict(name=self.name)
+        if self.path is not None:
+            attribs["path"]=".".join(self.path)
+
+        res = etree.Element(self.nodetype, **attribs)
+
+        if hasattr(self, "children"): 
+            for child_etree in imap(lambda c:c.etree(), self.children):
+                res.append(child_etree)
+
+        return res
+
+# module scope for HMITree root
+# so that CTN can use HMITree deduced in Library
+# note: this only works because library's Generate_C is 
+#       systematicaly invoked before CTN's CTNGenerate_C
+
+hmi_tree_root = None
 
 class SVGHMILibrary(POULibrary):
     def GetLibraryPath(self):
          return paths.AbsNeighbourFile(__file__, "pous.xml")
 
     def Generate_C(self, buildpath, varlist, IECCFLAGS):
+        global hmi_tree_root
 
         """
         PLC Instance Tree:
@@ -209,6 +230,11 @@
 
         return res
 
+    def GetHMITree(self):
+        global hmi_tree_root
+        res = [hmi_tree_root.etree()]
+        return res
+
     def CTNGenerate_C(self, buildpath, locations):
         """
         Return C code generated by iec2c compiler
@@ -224,7 +250,8 @@
 
             # TODO : move to __init__
             transform = XSLTransform(os.path.join(ScriptDirectory, "gen_index_xhtml.xslt"),
-                          [("GetSVGGeometry", lambda *_ignored:self.GetSVGGeometry())])
+                          [("GetSVGGeometry", lambda *_ignored:self.GetSVGGeometry()),
+                           ("GetHMITree", lambda *_ignored:self.GetHMITree())])
 
 
             # load svg as a DOM with Etree