include yslt_noindent.yml2
// overrides yslt's output function to set CDATA
decl output(method, cdata-section-elements="xhtml:script");
in xsl decl labels(*ptr, name="defs_by_labels") alias call-template {
with "hmi_element", "$hmi_element";
with "labels"{text *ptr};
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
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"
/* Our namespace to invoke python code */
extension-element-prefixes="ns func"
exclude-result-prefixes="ns str regexp exsl func" {
/* This retrieves geometry obtained through "inkscape -S"
* already parsed by python and presented as a list of
* <bbox x="0" y="0" w="42" h="42">
const "geometry", "ns:GetSVGGeometry()";
const "hmitree", "ns:GetHMITree()";
const "svg_root_id", "/svg:svg/@id";
const "hmi_elements", "//svg:*[starts-with(@inkscape:label, 'HMI:')]";
const "hmi_geometry", "$geometry[@Id = $hmi_elements/@id]";
const "hmi_pages", "$hmi_elements[func:parselabel(@inkscape:label)/widget/@type = 'Page']";
const "default_page" choose {
when "count($hmi_pages) > 1" {
"$hmi_pages[func:parselabel(@inkscape:label)/widget/arg[1]/@value = 'Home']";
error "No Home page defined!";
when "count($hmi_pages) = 0" {
error "No page defined!";
otherwise > «func:parselabel($hmi_pages/@inkscape:label)/widget/arg[1]/@value»
noindex > HMI_CURRENT_PAGE
const "categories", "exsl:node-set($_categories)";
const "_indexed_hmitree" apply "$hmitree", mode="index";
const "indexed_hmitree", "exsl:node-set($_indexed_hmitree)";
template "*", mode="index" {
param "parentpath", "''";
when "local-name() = 'HMI_ROOT'" > «$parentpath»
otherwise > «$parentpath»/«@name»
when "not(local-name() = $categories/noindex)" {
attrib "index" > «$index»
attrib "hmipath" > «$path»
/* no node expected below value nodes */
apply "*[1]", mode="index"{
with "parentpath" > «$path»
apply "following-sibling::*[1]", mode="index" {
with "index", "$index + count(exsl:node-set($content)/*)";
with "parentpath" > «$parentpath»
* - copy every attributes
* - copy every sub-elements
template "@* | node()", mode="inline_svg" {
/* use real xsl:copy instead copy-of alias from yslt.yml2 */
xsl:copy apply "@* | node()", mode="inline_svg";
/* replaces inkscape's height and width hints. forces fit */
template "svg:svg/@width", mode="inline_svg";
template "svg:svg/@height", mode="inline_svg";
template "svg:svg", mode="inline_svg" xsl:copy {
attrib "preserveAspectRatio" > none
apply "@* | node()", mode="inline_svg";
/*const "mark" > =HMI=\n*/
/* copy root node and add geometry as comment for a test */
comment > Made with SVGHMI. https://beremiz.org
apply "$hmi_geometry", mode="testgeo";
apply "$hmitree", mode="testtree";
apply "$indexed_hmitree", mode="testtree";
html xmlns="http://www.w3.org/1999/xhtml" {
body style="margin:0;overflow:hidden;" {
apply "svg:svg", mode="inline_svg";
"HMI:WidgetType:param1:param2@path1@path2"
widget type="WidgetType" {
func:function name="func:parselabel" {
const "description", "substring-after($label,'HMI:')";
const "_args", "substring-before($description,'@')";
when "$_args" value "$_args";
otherwise value "$description";
const "_type", "substring-before($args,':')";
when "$_type" value "$_type";
const "ast" if "$type" widget {
foreach "str:split(substring-after($args, ':'), ':')" {
const "paths", "substring-after($description,'@')";
foreach "str:split($paths, '@')" {
func:result select="exsl:node-set($ast)"
| var hmi_hash = [«$hmitree/@hash»];
function evaluate_js_from_descriptions() {
const "midmark" > \n«$mark»
apply """//*[contains(child::svg:desc, $midmark) or \
starts-with(child::svg:desc, $mark)]""",2
foreach "$hmi_elements" {
const "widget", "func:parselabel(@inkscape:label)/widget";
| type: "«$widget/@type»",
| "«@value»"`if "position()!=last()" > ,`
const "hmipath","@value";
const "hmitree_match","$indexed_hmitree/*[@hmipath = $hmipath]";
if "count($hmitree_match) = 0"
error > No match for path "«$hmipath»" in HMI tree
| «$hmitree_match/@index»`if "position()!=last()" > ,`
| element: document.getElementById("«@id»"),
apply "$widget", mode="widget_defs" with "hmi_element",".";
| }`if "position()!=last()" > ,`
foreach "$indexed_hmitree/*" {
| /* «@index» «@hmipath» */ "«substring(local-name(), 5)»"`if "position()!=last()" > ,`
const "desc", "func:parselabel(@inkscape:label)/widget";
const "p", "$hmi_geometry[@Id = $page/@id]";
const "page_ids","""$hmi_geometry[@Id != $page/@id and
@x >= $p/@x and @y >= $p/@y and
@x+@w <= $p/@x+$p/@w and @y+@h <= $p/@y+$p/@h]/@Id""";
const "page_elements", "$hmi_elements[@id = $page_ids]";
| "«$desc/arg[1]/@value»": {
| bbox: [«$p/@x», «$p/@y», «$p/@w», «$p/@h»],
| hmi_widgets.«.»`if "position()!=last()" > ,`
| }`if "position()!=last()" > ,`
| var default_page = "«$default_page»";
| var svg_root = document.getElementById("«$svg_root_id»");
// template "*", mode="code_from_descs" {
// var path, role, name, priv;
// /* if label is used, use it as default name */
// |> name = "«@inkscape:label»";
// | /* -------------- */
// // this breaks indent, but fixing indent could break string literals
// value "substring-after(svg:desc, $mark)";
// // nobody reads generated code anyhow...
template "bbox", mode="testgeo"{
| ID: «@Id» x: «@x» y: «@y» w: «@w» h: «@h»
template "*", mode="testtree"{
> «$indent» «local-name()»
foreach "@*" > «local-name()»=«.»
apply "*", mode="testtree" {
with "indent" value "concat($indent,'>')"
function "defs_by_labels" {
param "mandatory","'yes'";
foreach "str:split($labels)" {
const "elt_id","$hmi_element//*[@inkscape:label=$name][1]/@id";
if "$mandatory='yes' and not($elt_id)" error > Meter widget must have a «$name» element
| «$name»_elt: document.getElementById("«$elt_id»"),
template "widget[@type='Display']", mode="widget_defs" {
| dispatch: function(value) {
when "$hmi_element[self::svg:text]"{
// TODO : care about <tspan> ?
| this.element.textContent = String(value);
error > Display widget as a group not implemented
template "widget[@type='Meter']", mode="widget_defs" {
labels("value min max needle range");
| dispatch: function(value) {
| this.value_elt.textContent = String(value);
| var min = this.range[0];
| var max = this.range[1];
| var totallength = this.range[2];
| var length = Math.max(0,Math.min(totallength,(Number(value)-min)*totallength/(max-min)));
| var tip = this.range_elt.getPointAtLength(length);
// TODO : deal with transformations between needle and range
| this.needle_elt.setAttribute('d', "M "+this.origin.x+","+this.origin.y+" "+tip.x+","+tip.y);
| this.range = [Number(this.min_elt.textContent), Number(this.max_elt.textContent), this.range_elt.getTotalLength()]
| this.origin = this.needle_elt.getPointAtLength(0);
template "widget[@type='Input']", mode="widget_defs" {
| dispatch: function(value) {
| this.value_elt.textContent = String(value);
const "edit_elt_id","$hmi_element/*[@inkscape:label='edit'][1]/@id";
| document.getElementById("«$edit_elt_id»").addEventListener(
| function(evt) {alert('XXX TODO : Edit value');});
foreach "$hmi_element/*[regexp:test(@inkscape:label,'^[=+\-][0-9]+')]" {
| document.getElementById("«@id»").addEventListener(
| function(evt) {var new_val = change_hmi_value(that.indexes[0], "«@inkscape:label»");
| that.value_elt.textContent = String(new_val);});
/* could gray out value until refreshed */
template "widget[@type='Button']", mode="widget_defs" {
template "widget[@type='Toggle']", mode="widget_defs" {
template "widget[@type='Change']", mode="widget_defs" {
template "widget[@type='Jump']", mode="widget_defs" {
| this.element.addEventListener(
| function(evt) {switch_page(that.args[0]);});