- <copy>
- <copy-of select="@*"/>
- <variable name="start-node" select="text()[contains(., $start-sigil)][1]"/>
- <choose>
- <when test="$start-node">
- <variable name="remaining">
- <value-of select="substring-after($start-node, $start-sigil)"/>
- <copy-of select="$start-node/following-sibling::node()"/>
- </variable>
- <variable name="end-node" select="exsl:node-set($remaining)/node()[self::text() and contains(., $end-sigil)][1]"/>
- <choose>
- <when test="$end-node">
- <variable name="restoftext" select="substring-after($end-node, $end-sigil)"/>
- <variable name="maybe-langtag">
- <if test="$langtag-supported and starts-with($restoftext, '@') and contains($restoftext, '$')">
- <value-of select="substring-before(substring-after($restoftext, '@'), '$')"/>
- </if>
- </variable>
- <variable name="langtag">
- <if test="translate($maybe-langtag, '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-', '')=''">
- <value-of select="$maybe-langtag"/>
- </if>
- </variable>
- <variable name="rest">
- <html:div>
- <choose>
- <when test="string($langtag)!=''">
- <value-of select="substring-after($restoftext, '$')"/>
- </when>
- <otherwise>
- <value-of select="$restoftext"/>
- </otherwise>
- </choose>
- <copy-of select="$end-node/following-sibling::node()"/>
- </html:div>
- </variable>
- <variable name="processed-rest">
- <apply-templates select="exsl:node-set($rest)/*" mode="LesML:inline">
- <with-param name="element-name" select="$element-name"/>
- <with-param name="element-namespace" select="$element-namespace"/>
- <with-param name="start-sigil" select="$start-sigil"/>
- <with-param name="end-sigil" select="$end-sigil"/>
- <with-param name="role" select="$role"/>
- <with-param name="langtag-supported" select="$langtag-supported"/>
- </apply-templates>
- </variable>
- <copy-of select="$start-node/preceding-sibling::node()"/>
- <value-of select="substring-before($start-node, $start-sigil)"/>
- <element name="{$element-name}" namespace="{$element-namespace}">
- <if test="string($role)!=''">
- <attribute name="role">
- <value-of select="$role"/>
- </attribute>
- </if>
- <if test="string($langtag)!=''">
- <if test="$element-namespace='http://www.w3.org/1999/xhtml'">
- <attribute name="lang">
- <value-of select="$langtag"/>
- </attribute>
- </if>
- <attribute name="xml:lang">
- <value-of select="$langtag"/>
- </attribute>
- </if>
- <copy-of select="$end-node/preceding-sibling::node()"/>
- <value-of select="substring-before($end-node, $end-sigil)"/>
- </element>
- <copy-of select="exsl:node-set($processed-rest)/*/node()"/>
- </when>
- <otherwise>
- <apply-templates select="node()" mode="LesML:inline">
- <with-param name="element-name" select="$element-name"/>
- <with-param name="element-namespace" select="$element-namespace"/>
- <with-param name="start-sigil" select="$start-sigil"/>
- <with-param name="end-sigil" select="$end-sigil"/>
- <with-param name="role" select="$role"/>
- <with-param name="langtag-supported" select="$langtag-supported"/>
- </apply-templates>
- </otherwise>
- </choose>
- </when>
- <otherwise>
+ <variable name="end-node" select="text()[contains(., $end-sigil)][1]"/>
+ <variable name="has-start-node" select="$end-node/preceding-sibling::text()[contains(., $start-sigil)] or string-length(substring-after($end-node, $start-sigil))>string-length(substring-after($end-node, $end-sigil))"/>
+ <choose>
+ <when test="$end-node and $has-start-node">
+ <variable name="preceding">
+ <copy-of select="$end-node/preceding-sibling::node()"/>
+ <value-of select="substring-before($end-node, $end-sigil)"/>
+ </variable>
+ <variable name="start-node" select="exsl:node-set($preceding)/text()[contains(., $start-sigil)][last()]"/>
+ <variable name="start-tokens-fragment">
+ <call-template name="LesML:split">
+ <with-param name="source" select="string($start-node)"/>
+ <with-param name="separator" select="$start-sigil"/>
+ </call-template>
+ </variable>
+ <variable name="start-tokens" select="exsl:node-set($start-tokens-fragment)/*"/>
+ <variable name="wrapped">
+ <copy>
+ <copy-of select="@*"/>
+ <copy-of select="$start-node/preceding-sibling::node()"/>
+ <for-each select="$start-tokens[position()!=last()]">
+ <value-of select="."/>
+ <if test="position()!=last()">
+ <value-of select="$start-sigil"/>
+ </if>
+ </for-each>
+ <element name="{$element-name}" namespace="{$element-namespace}">
+ <if test="string($role)!=''">
+ <attribute name="role">
+ <value-of select="$role"/>
+ </attribute>
+ </if>
+ <if test="string($class)!=''">
+ <attribute name="class">
+ <value-of select="$class"/>
+ </attribute>
+ </if>
+ <value-of select="$start-tokens[last()]"/>
+ <copy-of select="$start-node/following-sibling::node()"/>
+ </element>
+ <value-of select="substring-after($end-node, $end-sigil)"/>
+ <copy-of select="$end-node/following-sibling::node()"/>
+ </copy>
+ </variable>
+ <apply-templates select="exsl:node-set($wrapped)/*" mode="LesML:inline">
+ <with-param name="element-name" select="$element-name"/>
+ <with-param name="element-namespace" select="$element-namespace"/>
+ <with-param name="start-sigil" select="$start-sigil"/>
+ <with-param name="end-sigil" select="$end-sigil"/>
+ <with-param name="role" select="$role"/>
+ </apply-templates>
+ </when>
+ <otherwise>
+ <copy>
+ <copy-of select="@*"/>