SVGHMI: Introduced "emit" to simplify producing per-topic code and content from anywhere in code. Applied to debug output, next to JS. svghmi
authorEdouard Tisserant <edouard.tisserant@gmail.com>
Sun, 19 Apr 2020 10:48:34 +0200
branchsvghmi
changeset 2940 034b6ce4f885
parent 2939 4296ab974d4d
child 2941 ef13a4007538
SVGHMI: Introduced "emit" to simplify producing per-topic code and content from anywhere in code. Applied to debug output, next to JS.
svghmi/detachable_pages.ysl2
svghmi/gen_index_xhtml.xslt
svghmi/gen_index_xhtml.ysl2
svghmi/geometry.ysl2
svghmi/hmi_tree.ysl2
svghmi/inline_svg.ysl2
--- a/svghmi/detachable_pages.ysl2	Sat Apr 18 14:06:41 2020 +0200
+++ b/svghmi/detachable_pages.ysl2	Sun Apr 19 10:48:34 2020 +0200
@@ -157,9 +157,7 @@
 template "*", mode="per_page_widget_template";
 
 
-debug:detachable_pages;
-
-template "debug:detachable-pages", mode="debug" {
+emit "debug:detachable-pages" {
 
     | DETACHABLES:
     foreach "$detachable_elements"{
--- a/svghmi/gen_index_xhtml.xslt	Sat Apr 18 14:06:41 2020 +0200
+++ b/svghmi/gen_index_xhtml.xslt	Sun Apr 19 10:48:34 2020 +0200
@@ -1,5 +1,5 @@
 <?xml version="1.0"?>
-<xsl:stylesheet xmlns:func="http://exslt.org/functions" xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" xmlns:svg="http://www.w3.org/2000/svg" xmlns:str="http://exslt.org/strings" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:exsl="http://exslt.org/common" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ns="beremiz" xmlns:cc="http://creativecommons.org/ns#" xmlns:regexp="http://exslt.org/regular-expressions" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:debug="debug" xmlns:dc="http://purl.org/dc/elements/1.1/" extension-element-prefixes="ns func exsl regexp str dyn" version="1.0" exclude-result-prefixes="ns str regexp exsl func dyn debug">
+<xsl:stylesheet xmlns:func="http://exslt.org/functions" xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" xmlns:epilogue="epilogue" xmlns:svg="http://www.w3.org/2000/svg" xmlns:str="http://exslt.org/strings" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:exsl="http://exslt.org/common" xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:preamble="preamble" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ns="beremiz" xmlns:cc="http://creativecommons.org/ns#" xmlns:regexp="http://exslt.org/regular-expressions" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:debug="debug" xmlns:dc="http://purl.org/dc/elements/1.1/" extension-element-prefixes="ns func exsl regexp str dyn" version="1.0" exclude-result-prefixes="ns func exsl regexp str dyn debug preamble epilogue">
   <xsl:output method="xml" cdata-section-elements="xhtml:script"/>
   <xsl:variable name="hmi_elements" select="//svg:*[starts-with(@inkscape:label, 'HMI:')]"/>
   <xsl:variable name="hmitree" select="ns:GetHMITree()"/>
@@ -157,7 +157,6 @@
     <xsl:variable name="class_b" select="$indexed_hmitree/*[@hmipath = $b]/@class"/>
     <func:result select="$class_a and $class_b and $class_a = $class_b"/>
   </func:function>
-  <debug:hmi-tree/>
   <xsl:template mode="testtree" match="*">
     <xsl:param name="indent" select="''"/>
     <xsl:value-of select="$indent"/>
@@ -178,7 +177,8 @@
       </xsl:with-param>
     </xsl:apply-templates>
   </xsl:template>
-  <xsl:template mode="debug" match="debug:hmi-tree">
+  <debug:hmi-tree/>
+  <xsl:template match="debug:hmi-tree">
     <xsl:text>Raw HMI tree
 </xsl:text>
     <xsl:apply-templates mode="testtree" select="$hmitree"/>
@@ -196,7 +196,7 @@
   </xsl:template>
   <xsl:variable name="geometry" select="ns:GetSVGGeometry()"/>
   <debug:geometry/>
-  <xsl:template mode="debug" match="debug:geometry">
+  <xsl:template match="debug:geometry">
     <xsl:text>ID, x, y, w, h
 </xsl:text>
     <xsl:for-each select="$geometry">
@@ -461,7 +461,7 @@
   </xsl:template>
   <xsl:template mode="per_page_widget_template" match="*"/>
   <debug:detachable-pages/>
-  <xsl:template mode="debug" match="debug:detachable-pages">
+  <xsl:template match="debug:detachable-pages">
     <xsl:text>DETACHABLES:
 </xsl:text>
     <xsl:for-each select="$detachable_elements">
@@ -591,7 +591,7 @@
   </xsl:variable>
   <xsl:variable name="result_svg_ns" select="exsl:node-set($result_svg)"/>
   <debug:inline-svg/>
-  <xsl:template mode="debug" match="debug:inline-svg">
+  <xsl:template match="debug:inline-svg">
     <xsl:text>Unlinked :
 </xsl:text>
     <xsl:for-each select="$to_unlink">
@@ -600,6 +600,14 @@
 </xsl:text>
     </xsl:for-each>
   </xsl:template>
+  <preamble:hmi-widget/>
+  <xsl:template match="preamble:hmi-widget">
+    <xsl:text>var hmi_widgets = {
+</xsl:text>
+    <xsl:apply-templates mode="hmi_elements" select="$hmi_elements"/>
+    <xsl:text>}
+</xsl:text>
+  </xsl:template>
   <xsl:template mode="hmi_elements" match="svg:*">
     <xsl:variable name="widget" select="func:widget(@id)"/>
     <xsl:variable name="eltid" select="@id"/>
@@ -2079,16 +2087,11 @@
 </xsl:text>
     <xsl:text>
 </xsl:text>
-    <xsl:apply-templates select="document('')/*/jspreamble"/>
+    <xsl:apply-templates select="document('')/*/preamble:*"/>
     <xsl:text>var hmi_hash = [</xsl:text>
     <xsl:value-of select="$hmitree/@hash"/>
     <xsl:text>];
 </xsl:text>
-    <xsl:text>var hmi_widgets = {
-</xsl:text>
-    <xsl:apply-templates mode="hmi_elements" select="$hmi_elements"/>
-    <xsl:text>}
-</xsl:text>
     <xsl:text>
 </xsl:text>
     <xsl:text>var heartbeat_index = </xsl:text>
@@ -3222,20 +3225,20 @@
 </xsl:text>
     <xsl:text>};
 </xsl:text>
-  </xsl:template>
-  <xsl:template mode="debug_as_comment" match="*[namespace-uri()='debug']">
-    <xsl:comment>
-      <xsl:value-of select="local-name()"/>
-      <xsl:text> :
-</xsl:text>
-      <xsl:apply-templates mode="debug" select="."/>
-    </xsl:comment>
+    <xsl:apply-templates select="document('')/*/epilogue:*"/>
   </xsl:template>
   <xsl:template match="/">
     <xsl:comment>
       <xsl:text>Made with SVGHMI. https://beremiz.org</xsl:text>
     </xsl:comment>
-    <xsl:apply-templates mode="debug_as_comment" select="document('')/*/debug:*"/>
+    <xsl:for-each select="document('')/*/debug:*">
+      <xsl:comment>
+        <xsl:value-of select="local-name()"/>
+        <xsl:text> :
+</xsl:text>
+        <xsl:apply-templates select="."/>
+      </xsl:comment>
+    </xsl:for-each>
     <html xmlns:svg="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/1999/xhtml">
       <head/>
       <body style="margin:0;overflow:hidden;">
--- a/svghmi/gen_index_xhtml.ysl2	Sat Apr 18 14:06:41 2020 +0200
+++ b/svghmi/gen_index_xhtml.ysl2	Sun Apr 19 10:48:34 2020 +0200
@@ -3,6 +3,14 @@
 // overrides yslt's output function to set CDATA
 decl output(method, cdata-section-elements="xhtml:script");
 
+// helper to emit some content to internal namespaces
+decl emit(*name) alias - {
+    *name;
+    template *name {
+        content;
+    }
+};
+
 istylesheet
             /* From Inkscape */
             xmlns:dc="http://purl.org/dc/elements/1.1/"
@@ -13,13 +21,17 @@
             xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
             xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
             xmlns:xhtml="http://www.w3.org/1999/xhtml"
+
+            /* Internal namespaces to allow emit code/content from anywhere */
             xmlns:debug="debug"
+            xmlns:preamble="preamble"
+            xmlns:epilogue="epilogue"
 
-            /* Our namespace to invoke python code */
+            /* Namespace to invoke python code */
             xmlns:ns="beremiz"
+
             extension-element-prefixes="ns func exsl regexp str dyn"
-            exclude-result-prefixes="ns str regexp exsl func dyn debug" {
-
+            exclude-result-prefixes="ns func exsl regexp str dyn debug preamble epilogue" {
 
     const "hmi_elements", "//svg:*[starts-with(@inkscape:label, 'HMI:')]";
 
@@ -37,18 +49,16 @@
 
     include scripts.ysl2
 
-    template "*[namespace-uri()='debug']", mode="debug_as_comment" {
-        comment {
-            | «local-name()» :
-            apply ".", mode="debug";
-        }
-    }
-
     template "/" {
         comment > Made with SVGHMI. https://beremiz.org
 
         // all debug output from included definitions, as comments
-        // apply "document('')/*/debug:*", mode="debug_as_comment";
+        foreach "document('')/*/debug:*" {
+            comment {
+                | «local-name()» :
+                apply ".";
+            }
+        }
 
         html xmlns="http://www.w3.org/1999/xhtml"
              xmlns:svg="http://www.w3.org/2000/svg"
--- a/svghmi/geometry.ysl2	Sat Apr 18 14:06:41 2020 +0200
+++ b/svghmi/geometry.ysl2	Sun Apr 19 10:48:34 2020 +0200
@@ -7,10 +7,8 @@
 // <bbox x="0" y="0" w="42" h="42">
 const "geometry", "ns:GetSVGGeometry()";
 
-debug:geometry;
-
 // Debug data
-template "debug:geometry", mode="debug" {
+emit "debug:geometry" {
     | ID, x, y, w, h
     foreach "$geometry"
         |  «@Id» «@x» «@y» «@w» «@h»
--- a/svghmi/hmi_tree.ysl2	Sat Apr 18 14:06:41 2020 +0200
+++ b/svghmi/hmi_tree.ysl2	Sun Apr 19 10:48:34 2020 +0200
@@ -126,8 +126,6 @@
     result "$class_a and $class_b and $class_a = $class_b";
 }
 
-debug:hmi_tree;
-
 // Debug data
 template "*", mode="testtree"{
     param "indent", "''";
@@ -138,7 +136,7 @@
         with "indent" value "concat($indent,'>')"
     };
 }
-template "debug:hmi-tree", mode="debug" {
+emit "debug:hmi-tree" {
     | Raw HMI tree
     apply "$hmitree", mode="testtree";
     |
--- a/svghmi/inline_svg.ysl2	Sat Apr 18 14:06:41 2020 +0200
+++ b/svghmi/inline_svg.ysl2	Sun Apr 19 10:48:34 2020 +0200
@@ -115,8 +115,7 @@
 const "result_svg" apply "/", mode="inline_svg";
 const "result_svg_ns", "exsl:node-set($result_svg)";
 
-debug:inline_svg;
-template "debug:inline-svg", mode="debug" {
+emit "debug:inline-svg" {
     | Unlinked :
     foreach "$to_unlink"{
         | «@id»