- <copy>
- <copy-of select="@*"/>
- <variable name="start-node" select="text()[contains(., '{🔗')][1]"/>
- <choose>
- <when test="$start-node">
- <variable name="remaining">
- <value-of select="substring-after($start-node, '{🔗')"/>
- <copy-of select="$start-node/following-sibling::node()"/>
- </variable>
- <variable name="end-node" select="exsl:node-set($remaining)/node()[self::text() and contains(., '>}') and not(preceding-sibling::*)][1]"/>
- <variable name="hyperlink">
- <for-each select="$end-node/preceding-sibling::node()">
- <value-of select="."/>
- </for-each>
- <value-of select="substring-before($end-node, '>}')"/>
- </variable>
- <choose>
- <when test="contains($hyperlink, '<')">
- <variable name="ltcomponents">
- <call-template name="LesML:split">
- <with-param name="source" select="$hyperlink"/>
- <with-param name="separator" select="'<'"/>
- </call-template>
- </variable>
- <variable name="ltcomponent-nodes" select="exsl:node-set($ltcomponents)/*"/>
- <variable name="rest">
- <html:div>
- <value-of select="substring-after($end-node, '>}')"/>
- <copy-of select="$end-node/following-sibling::node()"/>
- </html:div>
- </variable>
- <variable name="processed-rest">
- <apply-templates select="exsl:node-set($rest)/*" mode="LesML:linkify"/>
- </variable>
- <copy-of select="$start-node/preceding-sibling::node()"/>
- <value-of select="substring-before($start-node, '{🔗')"/>
- <element name="html:a">
- <attribute name="href">
- <value-of select="$ltcomponent-nodes[last()]"/>
- </attribute>
- <choose>
- <when test="count($ltcomponent-nodes)>2 or normalize-space($ltcomponent-nodes[1])!=''">
- <value-of select="$ltcomponent-nodes[1]"/>
- <for-each select="$ltcomponent-nodes[position()>1 and position()!=last()]">
- <text><</text>
- <value-of select="."/>
- </for-each>
- </when>
- <otherwise>
- <value-of select="$ltcomponent-nodes[last()]"/>
- </otherwise>
- </choose>
- </element>
- <copy-of select="exsl:node-set($processed-rest)/*/node()"/>
- </when>
- <otherwise>
- <variable name="rest">
- <html:div>
- <copy-of select="$remaining"/>
- </html:div>
- </variable>
- <variable name="processed-rest">
- <apply-templates select="exsl:node-set($rest)/*" mode="LesML:linkify"/>
- </variable>
- <copy-of select="$start-node/preceding-sibling::node()"/>
- <value-of select="substring-before($start-node, '{🔗')"/>
- <text>{🔗</text>
- <copy-of select="exsl:node-set($processed-rest)/*/node()"/>
- </otherwise>
- </choose>
- </when>
- <otherwise>
+ <variable name="end-node" select="text()[contains(., '>}')][1]"/>
+ <variable name="has-start-node" select="$end-node/preceding-sibling::text()[contains(., '{🔗') and not(following-sibling::*)] or string-length(substring-after($end-node, '{🔗'))>string-length(substring-after($end-node, '>}'))"/>
+ <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, '>}')"/>
+ </variable>
+ <variable name="start-node" select="exsl:node-set($preceding)/text()[contains(., '{🔗') and not(following-sibling::*)][last()]"/>
+ <variable name="start-tokens-fragment">
+ <call-template name="LesML:split">
+ <with-param name="source" select="string($start-node)"/>
+ <with-param name="separator" select="'{🔗'"/>
+ </call-template>
+ </variable>
+ <variable name="start-tokens" select="exsl:node-set($start-tokens-fragment)/*"/>
+ <variable name="hyperlink">
+ <value-of select="$start-tokens[last()]"/>
+ <for-each select="$start-node/following-sibling::node()">
+ <choose>
+ <when test="self::text()">
+ <value-of select="."/>
+ </when>
+ <when test="self::processing-instruction()[local-name()='LesML-Link-Escape']">
+ <text>🔗</text>
+ </when>
+ </choose>
+ </for-each>
+ </variable>
+ <choose>
+ <when test="contains($hyperlink, '<')">
+ <variable name="ltcomponents-fragment">
+ <call-template name="LesML:split">
+ <with-param name="source" select="$hyperlink"/>
+ <with-param name="separator" select="'<'"/>
+ </call-template>
+ </variable>
+ <variable name="ltcomponents" select="exsl:node-set($ltcomponents-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()">
+ <text>{🔗</text>
+ </if>
+ </for-each>
+ <element name="html:a">
+ <attribute name="href">
+ <value-of select="$ltcomponents[last()]"/>
+ </attribute>
+ <choose>
+ <when test="count($ltcomponents)>2 or normalize-space($ltcomponents[1])!=''">
+ <for-each select="$ltcomponents[position()!=last()]">
+ <value-of select="."/>
+ <if test="position()!=last()">
+ <text><</text>
+ </if>
+ </for-each>
+ </when>
+ <otherwise>
+ <value-of select="$ltcomponents[last()]"/>
+ </otherwise>
+ </choose>
+ </element>
+ <value-of select="substring-after($end-node, '>}')"/>
+ <copy-of select="$end-node/following-sibling::node()"/>
+ </copy>
+ </variable>
+ <apply-templates select="exsl:node-set($wrapped)/*" mode="LesML:linkify"/>
+ </when>
+ <otherwise>
+ <variable name="escaped">
+ <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()">
+ <text>{🔗</text>
+ </if>
+ </for-each>
+ <text>{</text>
+ <processing-instruction name="LesML-Link-Escape"/>
+ <copy-of select="$hyperlink"/>
+ <text>>}</text>
+ <value-of select="substring-after($end-node, '>}')"/>
+ <copy-of select="$end-node/following-sibling::node()"/>
+ </copy>
+ </variable>
+ <apply-templates select="exsl:node-set($escaped)/*" mode="LesML:linkify"/>
+ </otherwise>
+ </choose>
+ </when>
+ <otherwise>
+ <copy>
+ <copy-of select="@*"/>