svghmi/widgets_common.ysl2
author Edouard Tisserant <edouard.tisserant@gmail.com>
Thu, 19 Mar 2020 09:31:07 +0100
branchsvghmi
changeset 2886 6c82fad8be65
parent 2884 50b9832c51fc
child 2889 4eeed820fd3a
permissions -rw-r--r--
SVGHMI: Simplification and optimization. func:parselabel becomes a template.

All labels are parsed into a global variable containing resulting widget
description as a node-set. func:widget(id) was added for random access to
widget description.
// widgets_common.ysl2

in xsl decl labels(*ptr, name="defs_by_labels") alias call-template {
    with "hmi_element", "$hmi_element";
    with "labels"{text *ptr};
};

in xsl decl optional_labels(*ptr, name="defs_by_labels") alias call-template {
    with "hmi_element", "$hmi_element";
    with "labels"{text *ptr};
    with "mandatory","'no'";
};

template "svg:*", mode="hmi_elements" {
    const "widget", "func:widget(@id)";
    const "eltid","@id";
    |   "«@id»": {
    |     type: "«$widget/@type»",
    |     args: [
        foreach "$widget/arg"
    |         "«@value»"`if "position()!=last()" > ,`
    |     ],
    |     indexes: [
    foreach "$widget/path" {
        choose {
            when "not(@index)" {
                warning > Widget «$widget/@type» id="«$eltid»" : No match for path "«@value»" in HMI tree
            }
            otherwise {
    |             «@index»`if "position()!=last()" > ,`
            }
        }
    }
    |     ],
    |     element: id("«@id»"),
    apply "$widget", mode="widget_defs" with "hmi_element",".";
    |   }`if "position()!=last()" > ,`
}


function "defs_by_labels" {
    param "labels","''";
    param "mandatory","'yes'";
    param "hmi_element";
    const "widget_type","@type";
    foreach "str:split($labels)" {
        const "name",".";
        const "elt_id","$result_svg_ns//*[@id = $hmi_element/@id]//*[@inkscape:label=$name][1]/@id";
        choose {
            when "not($elt_id)" {
                if "$mandatory='yes'" {
                    // TODO FIXME error > «$widget_type» widget must have a «$name» element
                    warning > «$widget_type» widget must have a «$name» element
                }
                // otherwise produce nothing
            }
            otherwise {
                |     «$name»_elt: id("«$elt_id»"),
            }
        }
    }
}

def "func:escape_quotes" {
    param "txt";
    // have to use a python string to enter escaped quote
    const "frst", !"substring-before($txt,'\"')"!;
    const "frstln", "string-length($frst)";
    choose {
        when "$frstln > 0 and string-length($txt) > $frstln" {
            result !"concat($frst,'\\\"',func:escape_quotes(substring-after($txt,'\"')))"!;
        }
        otherwise {
            result "$txt";
        }
    }
}