]> Lady’s Gitweb - LesML/blobdiff - xslt/lesml.xslt
New block behaviour and repository layout
[LesML] / xslt / lesml.xslt
similarity index 59%
rename from parser.xslt
rename to xslt/lesml.xslt
index e5b3bf0fe52ffb60dcbe5a3c37b695ffe5512640f962dbce44ef85236c5bc8f1..9a603834c4bbef7c765730fdbce743b28a721df24c006aea7d1ef3110a405e68 100644 (file)
@@ -1,39 +1,51 @@
 <?xml version="1.0"?>
 <!--
-SPDX-FileCopyrightText: 2024, 2025 Lady <https://www.ladys.computer/about/#lady>
+@(#)💄📝 Les·M·L xslt/lesml.xslt 2026-03-31T01:28:11Z
+SPDX-FileCopyrightText: 2024, 2025, 2026 Lady <https://www.ladys.computer/about/#lady>
 SPDX-License-Identifier: MPL-2.0
 -->
-<!--
-⁌ 💄📝 Les·M·L ∷ parser.xslt
 
-© 2024–2025 Lady [@ Ladys Computer]
+<!-->
+⁌ The 💄📝 Les·M·L transform
+
+∎ Copyright © 2024–2026 Lady [@ Ladys Computer]
+
+⋮ This Source Code Form is subject to the terms of the Mozilla Public
+  License, v 2.0.
+If a copy of the M·P·L was not distributed with this file, You can
+  obtain one at {🔗<https://mozilla.org/MPL/2.0/>}.
+
+This file implements a transformation, via X·S·L·T, from an H·T·M·L
+  `<script>´ element with `@type="text/lesml"´ into the corresponding
+  H·T·M·L representation of the text it contains.
+This forms the “canonical” definition of the Les·M·L syntax;
+  no features will be added to the language which are not feasible to
+  be implemented here.
+</!-->
 
-This Source Code Form is subject to the terms of the Mozilla Public License, v 2.0.
-If a copy of the M·P·L was not distributed with this file, You can obtain one at <https://mozilla.org/MPL/2.0/>.
--->
 <!DOCTYPE transform [
        <!ENTITY LesML "urn:fdc:ladys.computer:20240512:LesML">
-       <!ENTITY pilcrow-atts "(local-name()='id' or local-name()='lang') and namespace-uri()='' or local-name()='lang' and namespace-uri()='http://www.w3.org/XML/1998/namespace'">
-       <!ENTITY sigiled-text "(string-length($text)=1 or substring($text, 2, 1)=' ' or substring($text, 2, 1)='¶')">
-       <!ENTITY unsigiled-text "concat(translate(substring($text, 2, 1), ' ', ''), substring($text, 3, string-length($text)-2))">
+       <!ENTITY block-types "self::html:blockquote or self::html:footer or self::html:li or self::html:dt or self::html:dd or self::html:section or &paragraph-types;">
+       <!ENTITY paragraph-types "self::html:p or self::html:pre or self::html:h1 or self::html:h2 or self::html:h3 or self::html:h4">
+       <!ENTITY level-pi "processing-instruction()[local-name()='LesML-Level']">
        <!ENTITY xhtml "http://www.w3.org/1999/xhtml">
 ]>
 <transform
        xmlns="http://www.w3.org/1999/XSL/Transform"
-       xmlns:LesML="urn:fdc:ladys.computer:20240512:LesML"
+       xmlns:LesML="&LesML;"
        xmlns:exsl="http://exslt.org/common"
        xmlns:exsldyn="http://exslt.org/dynamic"
        xmlns:exslfunc="http://exslt.org/functions"
        xmlns:exslset="http://exslt.org/sets"
        xmlns:exslstr="http://exslt.org/strings"
-       xmlns:html="http://www.w3.org/1999/xhtml"
+       xmlns:html="&xhtml;"
        xmlns:书社="urn:fdc:ladys.computer:20231231:Shu1She4"
        exclude-result-prefixes="LesML"
        extension-element-prefixes="exsl exsldyn exslfunc exslset exslstr"
        version="1.0"
 >
        <书社:id>urn:fdc:ladys.computer:20240512:LesML:parser.xslt</书社:id>
-       <param name="LESML_SECTION_BREAK_CHARS" select="'*-.=_~·․‥…⁂⋯─━┄┅┈┉╌╍═╴╶╸╺☙❧ ・*-.=_~'"/>
+       <param name="LESML_SECTION_BREAK_CHARS" select="'*-.=_~ ·․‥…⁂⁠⋯─━┄┅┈┉╌╍═╴╶╸╺☙❧ ・*-.=_~'"/>
        <exslfunc:function name="LesML:split">
                <param name="source" select="string()"/>
                <param name="separator" select="'&#xA;'"/>
@@ -65,16 +77,24 @@ If a copy of the M·P·L was not distributed with this file, You can obtain one
                        </otherwise>
                </choose>
        </template>
-       <template name="LesML:break-and-unescape">
+       <template name="LesML:comment-out">
                <param name="source"/>
-               <for-each select="LesML:split($source)">
-                       <call-template name="LesML:unescape">
-                               <with-param name="source" select="string()"/>
-                       </call-template>
-                       <if test="position()!=last()">
-                               <element name="br" namespace="&xhtml;"/>
-                       </if>
-               </for-each>
+               <comment>
+                       <for-each select="LesML:split($source, '--')">
+                               <if test="string()='' or starts-with(., '‐')">
+                                       <text>&#x034F;</text>
+                               </if>
+                               <value-of select="."/>
+                               <if test="substring(., string-length(.), 1)='‐'">
+                                       <text>&#x034F;</text>
+                               </if>
+                               <choose>
+                                       <when test="position()!=last()">
+                                               <text>-&#x034F;-</text>
+                                       </when>
+                               </choose>
+                       </for-each>
+               </comment>
        </template>
        <template name="LesML:unescape">
                <param name="source"/>
@@ -121,115 +141,476 @@ If a copy of the M·P·L was not distributed with this file, You can obtain one
                        </otherwise>
                </choose>
        </template>
-       <template name="LesML:comment-out">
-               <param name="source"/>
-               <comment>
-                       <for-each select="LesML:split($source, '--')">
-                               <if test="string()='' or starts-with(., '‐')">
-                                       <text>&#x034F;</text>
-                               </if>
-                               <value-of select="."/>
-                               <if test="substring(., string-length(.), 1)='‐'">
-                                       <text>&#x034F;</text>
-                               </if>
+       <template name="LesML:expand-sigils">
+               <param name="sigils" select="/.."/>
+               <param name="level" select="0"/>
+               <param name="partype"/>
+               <param name="syntax"/>
+               <param name="lines" select="/.."/>
+               <choose>
+                       <when test="$sigils">
+                               <variable name="wrapper-frag">
+                                       <choose>
+                                               <when test="$sigils[1]='•'">
+                                                       <element name="li" namespace="&xhtml;">
+                                                               <attribute name="class">
+                                                                       <text>unordered</text>
+                                                               </attribute>
+                                                       </element>
+                                               </when>
+                                               <when test="$sigils[1]='№'">
+                                                       <element name="li" namespace="&xhtml;">
+                                                               <attribute name="class">
+                                                                       <text>ordered</text>
+                                                               </attribute>
+                                                       </element>
+                                               </when>
+                                               <when test="$sigils[1]='※'">
+                                                       <element name="section" namespace="&xhtml;">
+                                                               <attribute name="role">
+                                                                       <text>note</text>
+                                                               </attribute>
+                                                               <attribute name="class">
+                                                                       <text>note</text>
+                                                               </attribute>
+                                                       </element>
+                                               </when>
+                                               <when test="$sigils[1]='⯑'">
+                                                       <element name="section" namespace="&xhtml;">
+                                                               <attribute name="role">
+                                                                       <text>note</text>
+                                                               </attribute>
+                                                               <attribute name="class">
+                                                                       <text>query</text>
+                                                               </attribute>
+                                                       </element>
+                                               </when>
+                                               <when test="$sigils[1]='∫'">
+                                                       <element name="section" namespace="&xhtml;">
+                                                               <attribute name="role">
+                                                                       <text>doc-abstract</text>
+                                                               </attribute>
+                                                               <attribute name="class">
+                                                                       <text>abstract</text>
+                                                               </attribute>
+                                                       </element>
+                                               </when>
+                                               <when test="$sigils[1]='☡'">
+                                                       <element name="section" namespace="&xhtml;">
+                                                               <attribute name="role">
+                                                                       <text>doc-notice</text>
+                                                               </attribute>
+                                                               <attribute name="class">
+                                                                       <text>caution</text>
+                                                               </attribute>
+                                                       </element>
+                                               </when>
+                                               <when test="$sigils[1]='⚠'">
+                                                       <element name="section" namespace="&xhtml;">
+                                                               <attribute name="role">
+                                                                       <text>doc-notice</text>
+                                                               </attribute>
+                                                               <attribute name="class">
+                                                                       <text>warning</text>
+                                                               </attribute>
+                                                       </element>
+                                               </when>
+                                               <when test="$sigils[1]='🛈'">
+                                                       <element name="section" namespace="&xhtml;">
+                                                               <attribute name="role">
+                                                                       <text>doc-tip</text>
+                                                               </attribute>
+                                                               <attribute name="class">
+                                                                       <text>info</text>
+                                                               </attribute>
+                                                       </element>
+                                               </when>
+                                               <when test="$sigils[1]='💡'">
+                                                       <element name="section" namespace="&xhtml;">
+                                                               <attribute name="role">
+                                                                       <text>doc-tip</text>
+                                                               </attribute>
+                                                               <attribute name="class">
+                                                                       <text>tip</text>
+                                                               </attribute>
+                                                       </element>
+                                               </when>
+                                               <when test="$sigils[1]='»'">
+                                                       <element name="blockquote" namespace="&xhtml;"/>
+                                               </when>
+                                               <when test="$sigils[1]='∎'">
+                                                       <element name="footer" namespace="&xhtml;"/>
+                                               </when>
+                                               <otherwise>
+                                                       <message terminate="yes">
+                                                               <text>Unrecognized sigil: </text>
+                                                               <value-of select="$sigils[1]"/>
+                                                               <text>.</text>
+                                                       </message>
+                                               </otherwise>
+                                       </choose>
+                               </variable>
+                               <for-each select="exsl:node-set($wrapper-frag)/*">
+                                       <copy>
+                                               <copy-of select="@*|node()"/>
+                                               <processing-instruction name="LesML-Level">
+                                                       <value-of select="$level"/>
+                                               </processing-instruction>
+                                               <call-template name="LesML:expand-sigils">
+                                                       <with-param name="sigils" select="$sigils[position()>1]"/>
+                                                       <with-param name="level" select="$level+1"/>
+                                                       <with-param name="partype" select="$partype"/>
+                                                       <with-param name="syntax" select="$syntax"/>
+                                                       <with-param name="lines" select="$lines"/>
+                                               </call-template>
+                                       </copy>
+                               </for-each>
+                       </when>
+                       <when test="$partype='comment'">
+                               <element name="div" namespace="&xhtml;">
+                                       <processing-instruction name="LesML-Level">
+                                               <value-of select="$level"/>
+                                       </processing-instruction>
+                                       <call-template name="LesML:comment-out">
+                                               <with-param name="source">
+                                                       <for-each select="$lines">
+                                                               <value-of select="."/>
+                                                               <if test="position()!=count($lines)">
+                                                                       <text>&#xA;</text>
+                                                               </if>
+                                                       </for-each>
+                                               </with-param>
+                                       </call-template>
+                               </element>
+                       </when>
+                       <otherwise>
+                               <variable name="firstchar" select="substring(translate($lines[1], ' &#x9;', ''), 1, 1)"/>
+                               <variable name="firstline">
+                                       <choose>
+                                               <when test="$firstchar!=''">
+                                                       <value-of select="concat($firstchar, substring-after($lines[1], $firstchar))"/>
+                                               </when>
+                                       </choose>
+                               </variable>
+                               <variable name="firsttokens" select="exslstr:tokenize($firstline, ' &#x9;')"/>
+                               <variable name="id-and-lang">
+                                       <if test="$firstchar='¶'">
+                                               <value-of select="$firsttokens[1]"/>
+                                       </if>
+                               </variable>
+                               <variable name="restofline">
+                                       <choose>
+                                               <when test="$id-and-lang!=''">
+                                                       <variable name="nosp" select="substring($firsttokens[1+number($id-and-lang!='')], 1, 1)"/>
+                                                       <if test="$nosp!=''">
+                                                               <value-of select="concat($nosp, substring-after(substring-after($firstline, $id-and-lang), $nosp))"/>
+                                                       </if>
+                                               </when>
+                                               <otherwise>
+                                                       <value-of select="$lines[1]"/>
+                                               </otherwise>
+                                       </choose>
+                               </variable>
+                               <variable name="maybe-langtag">
+                                       <if test="substring($id-and-lang, string-length($id-and-lang), 1)='$' and contains($id-and-lang, '@')">
+                                               <value-of select="LesML:split(substring($id-and-lang, 2, string-length($id-and-lang)-2), '@')[last()]"/>
+                                       </if>
+                               </variable>
+                               <variable name="langtag">
+                                       <if test="translate($maybe-langtag, '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-', '')=''">
+                                               <value-of select="$maybe-langtag"/>
+                                       </if>
+                               </variable>
+                               <variable name="id">
+                                       <choose>
+                                               <when test="string($langtag)!=''">
+                                                       <value-of select="substring($id-and-lang, 2, string-length($id-and-lang)-(string-length($langtag)+3))"/>
+                                               </when>
+                                               <otherwise>
+                                                       <value-of select="substring($id-and-lang, 2)"/>
+                                               </otherwise>
+                                       </choose>
+                               </variable>
+                               <variable name="newline">
+                                       <choose>
+                                               <when test="$partype='pre' or $partype='pre code'">
+                                                       <element name="br" namespace="&xhtml;"/>
+                                               </when>
+                                               <otherwise>
+                                                       <text>&#xA;</text>
+                                               </otherwise>
+                                       </choose>
+                               </variable>
+                               <variable name="text">
+                                       <if test="$id-and-lang='' or $restofline!=''">
+                                               <value-of select="$restofline"/>
+                                               <if test="count($lines)>1">
+                                                       <copy-of select="$newline"/>
+                                               </if>
+                                       </if>
+                                       <for-each select="$lines[position()>1]">
+                                               <value-of select="."/>
+                                               <if test="position()!=count($lines[position()>1])">
+                                                       <copy-of select="$newline"/>
+                                               </if>
+                                       </for-each>
+                               </variable>
+                               <variable name="parwrapper">
+                                       <choose>
+                                               <when test="$partype='pre code'">
+                                                       <text>pre</text>
+                                               </when>
+                                               <when test="$partype='footnote'">
+                                                       <text>p</text>
+                                               </when>
+                                               <when test="$partype!=''">
+                                                       <value-of select="$partype"/>
+                                               </when>
+                                               <otherwise>
+                                                       <text>p</text>
+                                               </otherwise>
+                                       </choose>
+                               </variable>
+                               <element name="div" namespace="&xhtml;">
+                                       <if test="$id!=''">
+                                               <attribute name="id">
+                                                       <value-of select="$id"/>
+                                               </attribute>
+                                       </if>
+                                       <if test="$langtag!=''">
+                                               <attribute name="lang">
+                                                       <value-of select="$langtag"/>
+                                               </attribute>
+                                               <attribute name="xml:lang">
+                                                       <value-of select="$langtag"/>
+                                               </attribute>
+                                       </if>
+                                       <if test="$partype='footnote'">
+                                               <processing-instruction name="LesML-Footnote">
+                                                       <value-of select="$id"/>
+                                               </processing-instruction>
+                                       </if>
+                                       <processing-instruction name="LesML-Level">
+                                               <value-of select="$level"/>
+                                       </processing-instruction>
+                                       <element name="{$parwrapper}" namespace="&xhtml;">
+                                               <if test="$parwrapper='pre'">
+                                                       <attribute name="role">
+                                                               <text>paragraph</text>
+                                                       </attribute>
+                                               </if>
+                                               <if test="$partype='pre code'">
+                                                       <attribute name="class">
+                                                               <text>code</text>
+                                                       </attribute>
+                                               </if>
+                                               <if test="$syntax!=''">
+                                                       <attribute name="data-syntax">
+                                                               <value-of select="$syntax"/>
+                                                       </attribute>
+                                               </if>
+                                               <choose>
+                                                       <when test="$parwrapper='hr'">
+                                                               <attribute name="data-source-text">
+                                                                       <value-of select="$text"/>
+                                                               </attribute>
+                                                       </when>
+                                                       <otherwise>
+                                                               <copy-of select="$text"/>
+                                                       </otherwise>
+                                               </choose>
+                                       </element>
+                               </element>
+                       </otherwise>
+               </choose>
+       </template>
+       <template name="LesML:chunk">
+               <param name="lines" select="/.."/>
+               <variable name="last-lines" select="$lines[normalize-space()!='' and (normalize-space(following-sibling::*[1])='' or position()=last())]"/>
+               <for-each select="$last-lines">
+                       <variable name="position" select="position()"/>
+                       <variable name="prev-last" select="$last-lines[($position)-1]"/>
+                       <variable name="linespans" select="(exslset:intersection(exslset:trailing($lines, $prev-last), exslset:leading($lines, .))|.)[normalize-space()!='']"/>
+                       <variable name="nospace" select="translate($linespans[1], ' &#x9;', '')"/>
+                       <variable name="firstnolevel" select="substring(translate($nospace, '⋮', ''), 1, 1)"/>
+                       <variable name="levelling">
                                <choose>
-                                       <when test="position()!=last()">
-                                               <text>-&#x034F;-</text>
+                                       <when test="$firstnolevel!=''">
+                                               <value-of select="substring-before($linespans[1], $firstnolevel)"/>
                                        </when>
+                                       <otherwise>
+                                               <value-of select="$linespans[1]"/>
+                                       </otherwise>
                                </choose>
-                       </for-each>
-               </comment>
-       </template>
-       <template name="LesML:id-and-contents">
-               <param name="source"/>
-               <variable name="id-and-lang">
-                       <if test="starts-with($source, '¶')">
+                       </variable>
+                       <variable name="nested-bullet">
+                               <choose>
+                                       <when test="contains('◦▪⁃', $firstnolevel)">
+                                               <value-of select="$firstnolevel"/>
+                                       </when>
+                                       <otherwise/>
+                               </choose>
+                       </variable>
+                       <variable name="level-modifier">
                                <choose>
-                                       <when test="contains($source, ' ')">
-                                               <value-of select="substring-before(substring-after($source, '¶'), ' ')"/>
+                                       <when test="$nested-bullet='◦'">
+                                               <text>1</text>
+                                       </when>
+                                       <when test="$nested-bullet='▪'">
+                                               <text>2</text>
+                                       </when>
+                                       <when test="$nested-bullet='⁃'">
+                                               <text>3</text>
                                        </when>
                                        <otherwise>
-                                               <value-of select="substring-after($source, '¶')"/>
+                                               <text>0</text>
                                        </otherwise>
                                </choose>
-                       </if>
-               </variable>
-               <variable name="restoftext">
-                       <choose>
-                               <when test="starts-with($source, '¶') and contains($source, ' ')">
-                                       <value-of select="substring-after($source, ' ')"/>
-                               </when>
-                               <when test="starts-with($source, '¶')"/>
-                               <otherwise>
-                                       <value-of select="$source"/>
-                               </otherwise>
-                       </choose>
-               </variable>
-               <variable name="maybe-langtag">
-                       <if test="substring($id-and-lang, string-length($id-and-lang), 1)='$' and contains($id-and-lang, '@')">
-                               <value-of select="LesML:split(substring($id-and-lang, 2, string-length($id-and-lang)-2), '@')[last()]"/>
-                       </if>
-               </variable>
-               <variable name="langtag">
-                       <if test="translate($maybe-langtag, '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-', '')=''">
-                               <value-of select="$maybe-langtag"/>
-                       </if>
-               </variable>
-               <variable name="id">
-                       <choose>
-                               <when test="string($langtag)!=''">
-                                       <value-of select="substring($id-and-lang, 1, string-length($id-and-lang)-(string-length($langtag)+2))"/>
-                               </when>
-                               <otherwise>
-                                       <value-of select="$id-and-lang"/>
-                               </otherwise>
-                       </choose>
-               </variable>
-               <if test="string($id)!=''">
-                       <attribute name="id">
-                               <value-of select="$id"/>
-                       </attribute>
-               </if>
-               <if test="string($langtag)!=''">
-                       <attribute name="lang">
-                               <value-of select="$langtag"/>
-                       </attribute>
-                       <attribute name="xml:lang">
-                               <value-of select="$langtag"/>
-                       </attribute>
-               </if>
-               <value-of select="$restoftext"/>
-       </template>
-       <template name="LesML:parse">
-               <param name="lines" select="/.."/>
-               <param name="parent-params" select="/.."/>
-               <variable name="first-line" select="$lines[1]"/>
-               <variable name="shebang">
-                       <if test="starts-with($first-line, '#!lesml')">
-                               <value-of select="$first-line"/>
-                       </if>
-               </variable>
-               <variable name="params-string">
-                       <choose>
-                               <when test="starts-with($shebang, '#!lesml@')">
-                                       <value-of select="substring-after($shebang, '$')"/>
-                               </when>
+                       </variable>
+                       <variable name="nobullet">
+                               <choose>
+                                       <when test="$nested-bullet!=''">
+                                               <value-of select="substring-after($nospace, $nested-bullet)"/>
+                                       </when>
+                                       <otherwise>
+                                               <value-of select="concat($firstnolevel, substring-after($nospace, $firstnolevel))"/>
+                                       </otherwise>
+                               </choose>
+                       </variable>
+                       <variable name="firstnosigil" select="substring(translate($nobullet, '•№※⯑∫☡⚠🛈💡»∎', ''), 1, 1)"/>
+                       <variable name="sigilling">
+                               <choose>
+                                       <when test="$firstnosigil!=''">
+                                               <value-of select="substring-before(substring-after($linespans[1], concat($levelling, $nested-bullet)), $firstnosigil)"/>
+                                       </when>
+                                       <otherwise>
+                                               <value-of select="substring-after($linespans[1], concat($levelling, $nested-bullet))"/>
+                                       </otherwise>
+                               </choose>
+                       </variable>
+                       <variable name="nosigil">
+                               <value-of select="concat($firstnosigil, substring-after($nobullet, $firstnosigil))"/>
+                       </variable>
+                       <variable name="ruled" select="count($linespans)=1 and $nosigil!='' and translate($nosigil, $LESML_SECTION_BREAK_CHARS, '')=''"/>
+                       <variable name="prefix">
+                               <choose>
+                                       <when test="not($ruled) and $firstnosigil='|'">
+                                               <variable name="postpipe" select="substring-after($linespans[1], '|')"/>
+                                               <choose>
+                                                       <when test="substring(translate($postpipe, '0123456789abcdefghijklmnopqrstuvwxyz/.-', ''), 1, 1)='$'">
+                                                               <value-of select="concat(substring-before($linespans[1], '$'), '$')"/>
+                                                       </when>
+                                                       <otherwise>
+                                                               <value-of select="concat(substring-before($linespans[1], '|'), '|')"/>
+                                                       </otherwise>
+                                               </choose>
+                                       </when>
+                                       <when test="not($ruled) and contains('⁌§❦✠#^', $firstnosigil)">
+                                               <value-of select="concat(substring-before($linespans[1], $firstnosigil), $firstnosigil)"/>
+                                       </when>
+                                       <when test="$firstnosigil!=''">
+                                               <value-of select="substring-before($linespans[1], $firstnosigil)"/>
+                                       </when>
+                                       <otherwise>
+                                               <value-of select="$linespans[1]"/>
+                                       </otherwise>
+                               </choose>
+                       </variable>
+                       <variable name="preformatted" select="$firstnosigil='|' and not($linespans[not(starts-with(., $prefix))])"/>
+                       <variable name="inner-lines-fragment">
+                               <for-each select="$linespans">
+                                       <choose>
+                                               <when test="$preformatted or position()=1">
+                                                       <html:span>
+                                                               <value-of select="substring-after(., $prefix)"/>
+                                                       </html:span>
+                                               </when>
+                                               <otherwise>
+                                                       <copy-of select="."/>
+                                               </otherwise>
+                                       </choose>
+                               </for-each>
+                       </variable>
+                       <variable name="inner-lines" select="exsl:node-set($inner-lines-fragment)/node()"/>
+                       <call-template name="LesML:expand-sigils">
+                               <with-param name="sigils" select="exslstr:tokenize(translate(concat($nested-bullet, $sigilling), '◦▪⁃ &#x9;', '•••'), '')"/>
+                               <with-param name="level" select="string-length(translate($levelling, ' &#x9;', ''))+$level-modifier"/>
+                               <with-param name="partype">
+                                       <choose>
+                                               <when test="$preformatted">
+                                                       <text>pre</text>
+                                                       <if test="contains($prefix, '$')">
+                                                               <text> code</text>
+                                                       </if>
+                                               </when>
+                                               <when test="$ruled">
+                                                       <text>hr</text>
+                                               </when>
+                                               <when test="$firstnosigil='#'">
+                                                       <text>comment</text>
+                                               </when>
+                                               <when test="$firstnosigil='^'">
+                                                       <text>footnote</text>
+                                               </when>
+                                               <when test="$firstnosigil='⁌'">
+                                                       <text>h1</text>
+                                               </when>
+                                               <when test="$firstnosigil='§'">
+                                                       <text>h2</text>
+                                               </when>
+                                               <when test="$firstnosigil='❦'">
+                                                       <text>h3</text>
+                                               </when>
+                                               <when test="$firstnosigil='✠'">
+                                                       <text>h4</text>
+                                               </when>
+                                               <otherwise/>
+                                       </choose>
+                               </with-param>
+                               <with-param name="syntax" select="substring-before(substring-after($prefix, '|'), '$')"/>
+                               <with-param name="lines" select="$inner-lines"/>
+                       </call-template>
+               </for-each>
+       </template>
+       <template name="LesML:paragraphize">
+               <param name="lines" select="/.."/>
+               <variable name="chunked-fragment">
+                       <call-template name="LesML:chunk">
+                               <with-param name="lines" select="$lines"/>
+                       </call-template>
+               </variable>
+               <apply-templates select="exsl:node-set($chunked-fragment)" mode="LesML:blockify"/>
+       </template>
+       <template name="LesML:parse">
+               <param name="lines" select="/.."/>
+               <param name="parent-params" select="/.."/>
+               <variable name="first-line" select="$lines[1]"/>
+               <variable name="shero">
+                       <if test="starts-with($first-line, '#?lesml')">
+                               <value-of select="$first-line"/>
+                       </if>
+               </variable>
+               <variable name="params-string">
+                       <choose>
+                               <when test="starts-with($shero, '#?lesml@')">
+                                       <value-of select="substring-after($shero, '$')"/>
+                               </when>
                                <otherwise>
-                                       <value-of select="substring-after($shebang, '#!lesml')"/>
+                                       <value-of select="substring-after($shero, '#?lesml')"/>
                                </otherwise>
                        </choose>
                </variable>
                <variable name="params-fragment">
                        <choose>
-                               <when test="$shebang!=''">
+                               <when test="$shero!=''">
                                        <html:dl>
-                                               <if test="starts-with($shebang, '#!lesml@') and contains($shebang, '$')">
+                                               <if test="starts-with($shero, '#?lesml@') and contains($shero, '$')">
                                                        <html:div>
                                                                <html:dt>
                                                                        <text> LANG </text>
                                                                </html:dt>
                                                                <html:dd>
-                                                                       <value-of select="substring-before(substring-after($shebang, '#!lesml@'), '$')"/>
+                                                                       <value-of select="substring-before(substring-after($shero, '#?lesml@'), '$')"/>
                                                                </html:dd>
                                                        </html:div>
                                                </if>
@@ -265,10 +646,10 @@ If a copy of the M·P·L was not distributed with this file, You can obtain one
                                </otherwise>
                        </choose>
                </variable>
-               <variable name="params" select="exsl:node-set($params-fragment)/*"/>
-               <variable name="noshebang" select="$lines[position()>1 or not(starts-with(., '#!lesml') or starts-with(., '##'))]"/>
-               <variable name="docsep" select="$noshebang[starts-with(., '#!lesml') or starts-with(., '##')][1]"/>
-               <variable name="doclines" select="exslset:leading($noshebang, $docsep)"/>
+               <variable name="params" select="exsl:node-set($params-fragment)/node()"/>
+               <variable name="noshero" select="$lines[position()>1 or not(starts-with(., '#?lesml') or starts-with(., '##'))]"/>
+               <variable name="docsep" select="$noshero[starts-with(., '#?lesml') or starts-with(., '##')][1]"/>
+               <variable name="doclines" select="exslset:leading($noshero, $docsep)"/>
                <if test="starts-with($first-line, '##') and $first-line!='##'">
                        <call-template name="LesML:comment-out">
                                <with-param name="source" select="substring-after($first-line, '##')"/>
@@ -281,27 +662,28 @@ If a copy of the M·P·L was not distributed with this file, You can obtain one
                                        <with-param name="lines" select="exslset:trailing($doclines, $record-separators[last()])"/>
                                </call-template>
                        </variable>
-                       <variable name="paragraphized" select="exsl:node-set($paragraphized-fragment)"/>
+                       <variable name="paragraphized-document" select="exsl:node-set($paragraphized-fragment)"/>
                        <variable name="filtered-ordered-footnotes-fragment">
-                               <for-each select="$paragraphized//html:li[processing-instruction()[local-name()='LesML-Footnote'] and html:p[position()=1 and @id]]">
-                                       <sort select="count(($paragraphized//html:a[@role='doc-noteref' and @href=concat('#', string(current()/html:p[1]/@id))])[1]/preceding::*)" data-type="number"/>
-                                       <if test="$paragraphized//html:a[@role='doc-noteref' and @href=concat('#', string(current()/html:p[1]/@id))]">
-                                               <copy-of select="."/>
+                               <for-each select="$paragraphized-document//processing-instruction()[local-name()='LesML-Footnote']">
+                                       <sort select="count(($paragraphized-document//html:a[@role='doc-noteref' and @href=concat('#', string(current()))])[1]/preceding::*)" data-type="number"/>
+                                       <if test="$paragraphized-document//html:a[@role='doc-noteref' and @href=concat('#', string(current()))]">
+                                               <copy-of select=".."/>
                                        </if>
                                </for-each>
                        </variable>
                        <variable name="used-footnotes-fragment">
                                <for-each select="exsl:node-set($filtered-ordered-footnotes-fragment)/node()">
-                                       <variable name="links" select="$paragraphized//html:a[@role='doc-noteref' and @href=concat('#', string(current()/html:p[1]/@id))]"/>
+                                       <variable name="footnote-id" select="processing-instruction()[local-name()='LesML-Footnote']"/>
+                                       <variable name="links" select="$paragraphized-document//html:a[@role='doc-noteref' and @href=concat('#', string($footnote-id))]"/>
                                        <element name="li" namespace="&xhtml;">
                                                <variable name="numero" select="position()"/>
                                                <attribute name="id">
-                                                       <value-of select="html:p[1]/@id"/>
+                                                       <value-of select="$footnote-id"/>
                                                </attribute>
                                                <attribute name="value">
                                                        <value-of select="$numero"/>
                                                </attribute>
-                                               <copy-of select="@*|node()"/>
+                                               <copy-of select="node()[not(self::processing-instruction() and local-name()='LesML-Footnote')]"/>
                                                <element name="footer" namespace="&xhtml;">
                                                        <attribute name="class">
                                                                <text>backlinks</text>
@@ -325,7 +707,7 @@ If a copy of the M·P·L was not distributed with this file, You can obtain one
                                        </element>
                                </for-each>
                        </variable>
-                       <variable name="used-footnote-nodes" select="exsl:node-set($used-footnotes-fragment)/node()"/>
+                       <variable name="used-footnotes" select="exsl:node-set($used-footnotes-fragment)/node()"/>
                        <element name="article" namespace="&xhtml;">
                                <for-each select="$params/html:div/html:dt[string()=' LANG ']">
                                        <attribute name="lang">
@@ -340,7 +722,7 @@ If a copy of the M·P·L was not distributed with this file, You can obtain one
                                                <value-of select="following-sibling::html:dd"/>
                                        </attribute>
                                </for-each>
-                               <if test="$record-separators[preceding-sibling::*[normalize-space()!='']]">
+                               <if test="$record-separators and exslset:difference(exslset:leading($doclines, $record-separators[last()]), $record-separators)[normalize-space()!='']">
                                        <element name="footer" namespace="&xhtml;">
                                                <attribute name="class">
                                                        <text>head</text>
@@ -348,14 +730,15 @@ If a copy of the M·P·L was not distributed with this file, You can obtain one
                                                <for-each select="$record-separators">
                                                        <variable name="position" select="position()"/>
                                                        <variable name="prev-separator" select="$record-separators[($position)-1]"/>
-                                                       <variable name="fields" select="exslset:leading(exslset:trailing($doclines, $prev-separator), .)"/>
+                                                       <variable name="fields" select="exslset:leading(exslset:trailing($doclines, $prev-separator), .)[normalize-space()!='']"/>
                                                        <if test="$fields">
                                                                <element name="dl" namespace="&xhtml;">
                                                                        <for-each select="$fields">
+                                                                               <variable name="following" select="exslset:intersection($fields, following-sibling::*)"/>
                                                                                <choose>
-                                                                                       <when test="starts-with(., ' ') and exslset:leading($fields, .)"/>
+                                                                                       <when test="contains(' &#x9;', substring(., 1, 1)) and exslset:has-same-node($fields, preceding-sibling::*[1])"/>
                                                                                        <otherwise>
-                                                                                               <variable name="next" select="exslset:intersection(following-sibling::*[not(starts-with(., ' '))][1], $fields)"/>
+                                                                                               <variable name="next" select="$following[not(contains(' &#x9;', substring(., 1, 1)))][1]"/>
                                                                                                <element name="div" namespace="&xhtml;">
                                                                                                        <element name="dt" namespace="&xhtml;">
                                                                                                                <value-of select="normalize-space(substring-before(., ':'))"/>
@@ -372,17 +755,17 @@ If a copy of the M·P·L was not distributed with this file, You can obtain one
                                                                                                                        </choose>
                                                                                                                </variable>
                                                                                                                <choose>
-                                                                                                                       <when test="substring($firstline, string-length($firstline))='\' and following-sibling::*[position()=1 and starts-with(., ' ')]">
+                                                                                                                       <when test="substring($firstline, string-length($firstline))='\' and $following[position()=1 and contains(' &#x9;', substring(., 1, 1))]">
                                                                                                                                <value-of select="substring($firstline, 1, string-length($firstline)-1)"/>
                                                                                                                        </when>
                                                                                                                        <otherwise>
                                                                                                                                <value-of select="$firstline"/>
                                                                                                                        </otherwise>
                                                                                                                </choose>
-                                                                                                               <for-each select="exslset:intersection(following-sibling::*[starts-with(., ' ')], exslset:leading($fields, $next))">
+                                                                                                               <for-each select="exslset:leading($following, $next)[contains(' &#x9;', substring(., 1, 1))]">
                                                                                                                        <variable name="nextline" select="normalize-space(.)"/>
                                                                                                                        <choose>
-                                                                                                                               <when test="substring($nextline, string-length($nextline))='\' and following-sibling::*[position()=1 and starts-with(., ' ')]">
+                                                                                                                               <when test="substring($nextline, string-length($nextline))='\' and exslset:intersection($fields, following-sibling::*)[position()=1 and contains(' &#x9;', substring(., 1, 1))]">
                                                                                                                                        <value-of select="substring($nextline, 1, string-length($nextline)-1)"/>
                                                                                                                                </when>
                                                                                                                                <otherwise>
@@ -397,7 +780,7 @@ If a copy of the M·P·L was not distributed with this file, You can obtain one
                                                                        </for-each>
                                                                </element>
                                                        </if>
-                                                       <if test=".!='%%'">
+                                                       <if test="substring-after(., '%%')!=''">
                                                                <call-template name="LesML:comment-out">
                                                                        <with-param name="source" select="substring-after(., '%%')"/>
                                                                </call-template>
@@ -409,18 +792,18 @@ If a copy of the M·P·L was not distributed with this file, You can obtain one
                                        <attribute name="class">
                                                <text>body</text>
                                        </attribute>
-                                       <apply-templates select="$paragraphized/node()" mode="LesML:finalize">
-                                               <with-param name="used-footnotes" select="$used-footnote-nodes"/>
+                                       <apply-templates select="$paragraphized-document/node()" mode="LesML:finalize">
+                                               <with-param name="used-footnotes" select="$used-footnotes"/>
                                        </apply-templates>
                                </element>
-                               <if test="$used-footnote-nodes">
+                               <if test="$used-footnotes">
                                        <element name="section" namespace="&xhtml;">
                                                <attribute name="role">
                                                        <text>doc-endnotes</text>
                                                </attribute>
                                                <element name="ol" namespace="&xhtml;">
-                                                       <apply-templates select="$used-footnote-nodes" mode="LesML:finalize">
-                                                               <with-param name="used-footnotes" select="$used-footnote-nodes"/>
+                                                       <apply-templates select="$used-footnotes" mode="LesML:finalize">
+                                                               <with-param name="used-footnotes" select="$used-footnotes"/>
                                                        </apply-templates>
                                                </element>
                                        </element>
@@ -434,389 +817,6 @@ If a copy of the M·P·L was not distributed with this file, You can obtain one
                        </call-template>
                </if>
        </template>
-       <template name="LesML:block">
-               <param name="lines" select="/.."/>
-               <variable name="last-lines" select="$lines[normalize-space()!='' and (normalize-space(following-sibling::*[1])='' or position()=last())]"/>
-               <for-each select="$last-lines">
-                       <variable name="position" select="position()"/>
-                       <variable name="prev-last" select="$last-lines[($position)-1]"/>
-                       <variable name="linespans" select="(exslset:intersection(exslset:trailing($lines, $prev-last), exslset:leading($lines, .))|.)[normalize-space()!='']"/>
-                       <variable name="prefix" select="substring(translate(., ' &#x9;', ''), 1, 1)"/>
-                       <variable name="all-prefixed" select="not($linespans[not(starts-with(translate(., ' &#x9;', ''), $prefix))])"/>
-                       <variable name="quoted" select="$all-prefixed and $prefix='»'"/>
-                       <variable name="bracketed" select="$all-prefixed and $prefix=']'"/>
-                       <choose>
-                               <when test="count($linespans)=1 and translate(., concat($LESML_SECTION_BREAK_CHARS, ' &#x9;'), '')=''">
-                                       <element name="hr" namespace="&xhtml;"/>
-                               </when>
-                               <when test="$quoted or $bracketed">
-                                       <variable name="innerlines-fragment">
-                                               <for-each select="$linespans">
-                                                       <variable name="inner">
-                                                               <value-of select="substring-after(., $prefix)"/>
-                                                       </variable>
-                                                       <copy>
-                                                               <choose>
-                                                                       <when test="starts-with($inner, ' ') or starts-with($inner, '&#x9;')">
-                                                                               <value-of select="substring($inner, 1+count(exslstr:tokenize($inner, '')[normalize-space()='' and not(preceding-sibling::*[normalize-space()!=''])]))"/>
-                                                                       </when>
-                                                                       <otherwise>
-                                                                               <value-of select="$inner"/>
-                                                                       </otherwise>
-                                                               </choose>
-                                                       </copy>
-                                               </for-each>
-                                       </variable>
-                                       <variable name="blocked-fragment">
-                                               <call-template name="LesML:block">
-                                                       <with-param name="lines" select="exsl:node-set($innerlines-fragment)/*"/>
-                                               </call-template>
-                                       </variable>
-                                       <variable name="innerpars" select="exsl:node-set($blocked-fragment)/node()"/>
-                                       <choose>
-                                               <when test="$quoted and $innerpars[position()=last() and self::html:footer]">
-                                                       <element name="figure" namespace="&xhtml;">
-                                                               <element name="blockquote" namespace="&xhtml;">
-                                                                       <copy-of select="$innerpars[position()!=last()]"/>
-                                                               </element>
-                                                               <element name="figcaption" namespace="&xhtml;">
-                                                                       <copy-of select="$innerpars[last()]/node()"/>
-                                                               </element>
-                                                       </element>
-                                               </when>
-                                               <when test="$quoted">
-                                                       <element name="blockquote" namespace="&xhtml;">
-                                                               <copy-of select="$innerpars"/>
-                                                       </element>
-                                               </when>
-                                               <when test="$bracketed">
-                                                       <element name="footer" namespace="&xhtml;">
-                                                               <copy-of select="$innerpars"/>
-                                                       </element>
-                                               </when>
-                                       </choose>
-                               </when>
-                               <otherwise>
-                                       <variable name="preformatted" select="$all-prefixed and $prefix='|'"/>
-                                       <variable name="text">
-                                               <for-each select="$linespans">
-                                                       <choose>
-                                                               <when test="$preformatted">
-                                                                       <value-of select="substring-after(., '|')"/>
-                                                               </when>
-                                                               <otherwise>
-                                                                       <value-of select="normalize-space()"/>
-                                                               </otherwise>
-                                                       </choose>
-                                                       <if test="position()!=count($linespans)">
-                                                               <choose>
-                                                                       <when test="$preformatted">
-                                                                               <text>&#xA;</text>
-                                                                       </when>
-                                                                       <otherwise>
-                                                                               <text> </text>
-                                                                       </otherwise>
-                                                               </choose>
-                                                       </if>
-                                               </for-each>
-                                       </variable>
-                                       <choose>
-                                               <when test="$preformatted">
-                                                       <element name="pre" namespace="&xhtml;">
-                                                               <call-template name="LesML:id-and-contents">
-                                                                       <with-param name="source" select="$text"/>
-                                                               </call-template>
-                                                       </element>
-                                               </when>
-                                               <when test="string($text)=''"/>
-                                               <when test="starts-with($text, '⁌') and &sigiled-text;">
-                                                       <element name="h1" namespace="&xhtml;">
-                                                               <call-template name="LesML:id-and-contents">
-                                                                       <with-param name="source" select="&unsigiled-text;"/>
-                                                               </call-template>
-                                                       </element>
-                                               </when>
-                                               <when test="starts-with($text, '§') and &sigiled-text;">
-                                                       <element name="h2" namespace="&xhtml;">
-                                                               <call-template name="LesML:id-and-contents">
-                                                                       <with-param name="source" select="&unsigiled-text;"/>
-                                                               </call-template>
-                                                       </element>
-                                               </when>
-                                               <when test="starts-with($text, '❦') and &sigiled-text;">
-                                                       <element name="h3" namespace="&xhtml;">
-                                                               <call-template name="LesML:id-and-contents">
-                                                                       <with-param name="source" select="&unsigiled-text;"/>
-                                                               </call-template>
-                                                       </element>
-                                               </when>
-                                               <when test="starts-with($text, '✠') and &sigiled-text;">
-                                                       <element name="h4" namespace="&xhtml;">
-                                                               <call-template name="LesML:id-and-contents">
-                                                                       <with-param name="source" select="&unsigiled-text;"/>
-                                                               </call-template>
-                                                       </element>
-                                               </when>
-                                               <when test="starts-with($text, '•') and &sigiled-text;">
-                                                       <element name="li" namespace="&xhtml;">
-                                                               <attribute name="class">
-                                                                       <text>unordered</text>
-                                                               </attribute>
-                                                               <attribute name="data-level">
-                                                                       <text>1</text>
-                                                               </attribute>
-                                                               <element name="p" namespace="&xhtml;">
-                                                                       <call-template name="LesML:id-and-contents">
-                                                                               <with-param name="source" select="&unsigiled-text;"/>
-                                                                       </call-template>
-                                                               </element>
-                                                       </element>
-                                               </when>
-                                               <when test="starts-with($text, '🔢') and &sigiled-text;">
-                                                       <element name="li" namespace="&xhtml;">
-                                                               <attribute name="class">
-                                                                       <text>ordered</text>
-                                                               </attribute>
-                                                               <attribute name="data-level">
-                                                                       <text>1</text>
-                                                               </attribute>
-                                                               <element name="p" namespace="&xhtml;">
-                                                                       <call-template name="LesML:id-and-contents">
-                                                                               <with-param name="source" select="&unsigiled-text;"/>
-                                                                       </call-template>
-                                                               </element>
-                                                       </element>
-                                               </when>
-                                               <when test="starts-with($text, '◦') and &sigiled-text;">
-                                                       <element name="li" namespace="&xhtml;">
-                                                               <attribute name="class">
-                                                                       <text>unordered</text>
-                                                               </attribute>
-                                                               <attribute name="data-level">
-                                                                       <text>2</text>
-                                                               </attribute>
-                                                               <element name="p" namespace="&xhtml;">
-                                                                       <call-template name="LesML:id-and-contents">
-                                                                               <with-param name="source" select="&unsigiled-text;"/>
-                                                                       </call-template>
-                                                               </element>
-                                                       </element>
-                                               </when>
-                                               <when test="starts-with($text, '🔠') and &sigiled-text;">
-                                                       <element name="li" namespace="&xhtml;">
-                                                               <attribute name="class">
-                                                                       <text>ordered</text>
-                                                               </attribute>
-                                                               <attribute name="data-level">
-                                                                       <text>2</text>
-                                                               </attribute>
-                                                               <element name="p" namespace="&xhtml;">
-                                                                       <call-template name="LesML:id-and-contents">
-                                                                               <with-param name="source" select="&unsigiled-text;"/>
-                                                                       </call-template>
-                                                               </element>
-                                                       </element>
-                                               </when>
-                                               <when test="starts-with($text, '▪') and &sigiled-text;">
-                                                       <element name="li" namespace="&xhtml;">
-                                                               <attribute name="class">
-                                                                       <text>unordered</text>
-                                                               </attribute>
-                                                               <attribute name="data-level">
-                                                                       <text>3</text>
-                                                               </attribute>
-                                                               <element name="p" namespace="&xhtml;">
-                                                                       <call-template name="LesML:id-and-contents">
-                                                                               <with-param name="source" select="&unsigiled-text;"/>
-                                                                       </call-template>
-                                                               </element>
-                                                       </element>
-                                               </when>
-                                               <when test="starts-with($text, '🔡') and &sigiled-text;">
-                                                       <element name="li" namespace="&xhtml;">
-                                                               <attribute name="class">
-                                                                       <text>ordered</text>
-                                                               </attribute>
-                                                               <attribute name="data-level">
-                                                                       <text>3</text>
-                                                               </attribute>
-                                                               <element name="p" namespace="&xhtml;">
-                                                                       <call-template name="LesML:id-and-contents">
-                                                                               <with-param name="source" select="&unsigiled-text;"/>
-                                                                       </call-template>
-                                                               </element>
-                                                       </element>
-                                               </when>
-                                               <when test="starts-with($text, '⁃') and &sigiled-text;">
-                                                       <element name="li" namespace="&xhtml;">
-                                                               <attribute name="class">
-                                                                       <text>unordered</text>
-                                                               </attribute>
-                                                               <attribute name="data-level">
-                                                                       <text>4</text>
-                                                               </attribute>
-                                                               <element name="p" namespace="&xhtml;">
-                                                                       <call-template name="LesML:id-and-contents">
-                                                                               <with-param name="source" select="&unsigiled-text;"/>
-                                                                       </call-template>
-                                                               </element>
-                                                       </element>
-                                               </when>
-                                               <when test="starts-with($text, '🔣') and &sigiled-text;">
-                                                       <element name="li" namespace="&xhtml;">
-                                                               <attribute name="class">
-                                                                       <text>ordered</text>
-                                                               </attribute>
-                                                               <attribute name="data-level">
-                                                                       <text>4</text>
-                                                               </attribute>
-                                                               <element name="p" namespace="&xhtml;">
-                                                                       <call-template name="LesML:id-and-contents">
-                                                                               <with-param name="source" select="&unsigiled-text;"/>
-                                                                       </call-template>
-                                                               </element>
-                                                       </element>
-                                               </when>
-                                               <when test="starts-with($text, '※') and &sigiled-text;">
-                                                       <element name="section" namespace="&xhtml;">
-                                                               <attribute name="role">
-                                                                       <text>note</text>
-                                                               </attribute>
-                                                               <attribute name="class">
-                                                                       <text>note</text>
-                                                               </attribute>
-                                                               <element name="p" namespace="&xhtml;">
-                                                                       <call-template name="LesML:id-and-contents">
-                                                                               <with-param name="source" select="&unsigiled-text;"/>
-                                                                       </call-template>
-                                                               </element>
-                                                       </element>
-                                               </when>
-                                               <when test="starts-with($text, '☡') and &sigiled-text;">
-                                                       <element name="section" namespace="&xhtml;">
-                                                               <attribute name="role">
-                                                                       <text>note</text>
-                                                               </attribute>
-                                                               <attribute name="class">
-                                                                       <text>caution</text>
-                                                               </attribute>
-                                                               <element name="p" namespace="&xhtml;">
-                                                                       <call-template name="LesML:id-and-contents">
-                                                                               <with-param name="source" select="&unsigiled-text;"/>
-                                                                       </call-template>
-                                                               </element>
-                                                       </element>
-                                               </when>
-                                               <when test="starts-with($text, '⯑') and &sigiled-text;">
-                                                       <element name="section" namespace="&xhtml;">
-                                                               <attribute name="role">
-                                                                       <text>note</text>
-                                                               </attribute>
-                                                               <attribute name="class">
-                                                                       <text>query</text>
-                                                               </attribute>
-                                                               <element name="p" namespace="&xhtml;">
-                                                                       <call-template name="LesML:id-and-contents">
-                                                                               <with-param name="source" select="&unsigiled-text;"/>
-                                                                       </call-template>
-                                                               </element>
-                                                       </element>
-                                               </when>
-                                               <when test="starts-with($text, '@') and &sigiled-text;">
-                                                       <element name="section" namespace="&xhtml;">
-                                                               <attribute name="role">
-                                                                       <text>doc-abstract</text>
-                                                               </attribute>
-                                                               <element name="p" namespace="&xhtml;">
-                                                                       <call-template name="LesML:id-and-contents">
-                                                                               <with-param name="source" select="&unsigiled-text;"/>
-                                                                       </call-template>
-                                                               </element>
-                                                       </element>
-                                               </when>
-                                               <when test="starts-with($text, '🛈') and &sigiled-text;">
-                                                       <element name="section" namespace="&xhtml;">
-                                                               <attribute name="role">
-                                                                       <text>doc-tip</text>
-                                                               </attribute>
-                                                               <element name="p" namespace="&xhtml;">
-                                                                       <call-template name="LesML:id-and-contents">
-                                                                               <with-param name="source" select="&unsigiled-text;"/>
-                                                                       </call-template>
-                                                               </element>
-                                                       </element>
-                                               </when>
-                                               <when test="starts-with($text, '⚠') and &sigiled-text;">
-                                                       <element name="section" namespace="&xhtml;">
-                                                               <attribute name="role">
-                                                                       <text>doc-notice</text>
-                                                               </attribute>
-                                                               <element name="p" namespace="&xhtml;">
-                                                                       <call-template name="LesML:id-and-contents">
-                                                                               <with-param name="source" select="&unsigiled-text;"/>
-                                                                       </call-template>
-                                                               </element>
-                                                       </element>
-                                               </when>
-                                               <when test="starts-with($text, '⋯') and &sigiled-text;">
-                                                       <element name="div" namespace="&xhtml;">
-                                                               <processing-instruction name="LesML-Continuation"/>
-                                                               <element name="p" namespace="&xhtml;">
-                                                                       <call-template name="LesML:id-and-contents">
-                                                                               <with-param name="source" select="&unsigiled-text;"/>
-                                                                       </call-template>
-                                                               </element>
-                                                       </element>
-                                               </when>
-                                               <when test="starts-with($text, '^') and &sigiled-text;">
-                                                       <element name="li" namespace="&xhtml;">
-                                                               <attribute name="class">
-                                                                       <text>ordered footnote</text>
-                                                               </attribute>
-                                                               <attribute name="data-level">
-                                                                       <text>1</text>
-                                                               </attribute>
-                                                               <processing-instruction name="LesML-Footnote"/>
-                                                               <element name="p" namespace="&xhtml;">
-                                                                       <call-template name="LesML:id-and-contents">
-                                                                               <with-param name="source" select="&unsigiled-text;"/>
-                                                                       </call-template>
-                                                               </element>
-                                                       </element>
-                                               </when>
-                                               <when test="starts-with($text, '#') and &sigiled-text;">
-                                                       <call-template name="LesML:comment-out">
-                                                               <with-param name="source" select="&unsigiled-text;"/>
-                                                       </call-template>
-                                               </when>
-                                               <otherwise>
-                                                       <element name="p" namespace="&xhtml;">
-                                                               <call-template name="LesML:id-and-contents">
-                                                                       <with-param name="source" select="$text"/>
-                                                               </call-template>
-                                                       </element>
-                                               </otherwise>
-                                       </choose>
-                               </otherwise>
-                       </choose>
-               </for-each>
-       </template>
-       <template name="LesML:paragraphize">
-               <param name="lines" select="/.."/>
-               <variable name="blocked-fragment">
-                       <call-template name="LesML:block">
-                               <with-param name="lines" select="$lines"/>
-                       </call-template>
-               </variable>
-               <variable name="blocked" select="exsl:node-set($blocked-fragment)"/>
-               <variable name="footnote-ids" select="$blocked//html:li[processing-instruction()[local-name()='LesML-Footnote']]/html:p[1]/@id"/>
-               <variable name="inlined-fragment">
-                       <apply-templates select="$blocked/node()" mode="LesML:comment">
-                               <with-param name="footnote-ids" select="$footnote-ids"/>
-                       </apply-templates>
-               </variable>
-               <apply-templates select="exsl:node-set($inlined-fragment)/node()" mode="LesML:finalize-tree"/>
-       </template>
        <template match="html:script[@type='text/lesml']">
                <variable name="source">
                        <for-each select=".//text()">
@@ -829,294 +829,142 @@ If a copy of the M·P·L was not distributed with this file, You can obtain one
                        </call-template>
                </element>
        </template>
-       <template match="html:ol[processing-instruction()[local-name()='LesML-Footnote']]|processing-instruction()[local-name()='LesML-Footnote']" mode="LesML:finalize" priority="1"/>
-       <template match="html:a[@role='doc-noteref']" mode="LesML:finalize">
-               <param name="used-footnotes" select="/.."/>
-               <variable name="matching-note" select="$used-footnotes[concat('#', @id)=current()/@href]"/>
-               <copy>
-                       <apply-templates select="@*" mode="LesML:finalize">
-                               <with-param name="used-footnotes" select="$used-footnotes"/>
-                       </apply-templates>
-                       <choose>
-                               <when test="$matching-note">
-                                       <value-of select="$matching-note/@value"/>
-                               </when>
-                               <otherwise>
-                                       <apply-templates select="node()" mode="LesML:finalize">
-                                               <with-param name="used-footnotes" select="$used-footnotes"/>
-                                       </apply-templates>
-                               </otherwise>
-                       </choose>
-               </copy>
+       <template match="/" mode="LesML:blockify">
+               <variable name="nested-fragment">
+                       <apply-templates mode="LesML:nesting-blocks"/>
+               </variable>
+               <variable name="nested-document" select="exsl:node-set($nested-fragment)"/>
+               <apply-templates select="exsl:node-set($nested-fragment)/node()" mode="LesML:inlinify">
+                       <with-param name="footnote-ids" select="$nested-document/*/processing-instruction()[local-name()='LesML-Footnote']"/>
+               </apply-templates>
        </template>
-       <template match="html:p[position()=1 and ancestor::*]" mode="LesML:finalize">
-               <param name="used-footnotes" select="/.."/>
-               <variable name="content" select="@*[not(&pilcrow-atts;)]|node()"/>
-               <if test="$content">
+       <template match="*[&level-pi;]" mode="LesML:nesting-blocks">
+               <variable name="current-level" select="number(&level-pi;)"/>
+               <if test="not(preceding-sibling::*[&level-pi; and $current-level>&level-pi;])">
+                       <variable name="notatlevel" select="following-sibling::*[$current-level>=&level-pi;][1]"/>
+                       <variable name="contents-fragment">
+                               <copy-of select="node()"/>
+                               <copy-of select="exslset:leading(following-sibling::*, $notatlevel)"/>
+                       </variable>
                        <copy>
-                               <apply-templates select="$content" mode="LesML:finalize">
-                                       <with-param name="used-footnotes" select="$used-footnotes"/>
-                               </apply-templates>
+                               <copy-of select="@*"/>
+                               <apply-templates select="exsl:node-set($contents-fragment)/node()" mode="LesML:nesting-blocks"/>
                        </copy>
                </if>
        </template>
-       <template match="html:section" mode="LesML:finalize">
-               <param name="used-footnotes" select="/.."/>
-               <variable name="notinsection" select="following-sibling::node()[not(html:li/@data-level>1)][1]"/>
+       <template match="node()" mode="LesML:nesting-blocks" priority="-1">
                <copy>
-                       <apply-templates select="@*|html:p[1]/@*[&pilcrow-atts;]" mode="LesML:finalize"/>
-                       <apply-templates select="node()" mode="LesML:finalize"/>
-                       <for-each select="exslset:leading(following-sibling::node(), $notinsection)">
-                               <copy>
-                                       <apply-templates select="@*|node()" mode="LesML:finalize">
-                                               <with-param name="used-footnotes" select="$used-footnotes"/>
-                                       </apply-templates>
-                               </copy>
-                       </for-each>
+                       <copy-of select="@*"/>
+                       <apply-templates select="node()" mode="LesML:nesting-blocks"/>
                </copy>
        </template>
-       <template match="html:*[html:li/@data-level>1]" mode="LesML:finalize">
-               <param name="used-footnotes" select="/.."/>
-               <if test="not(preceding-sibling::node()[not(html:li/@data-level>1)][1]/self::html:section)">
-                       <copy>
-                               <apply-templates select="@*|node()" mode="LesML:finalize">
-                                       <with-param name="used-footnotes" select="$used-footnotes"/>
-                               </apply-templates>
-                       </copy>
-               </if>
-       </template>
-       <template match="text()" mode="LesML:finalize">
-               <call-template name="LesML:break-and-unescape">
-                       <with-param name="source" select="string()"/>
-               </call-template>
-       </template>
-       <template match="@*|node()" mode="LesML:finalize" priority="-1">
-               <param name="used-footnotes" select="/.."/>
+       <template match="*[&level-pi;]" mode="LesML:inlinify">
+               <param name="footnote-ids" select="/.."/>
                <copy>
-                       <apply-templates select="@*|html:p[1]/@*[&pilcrow-atts;]" mode="LesML:finalize"/>
-                       <apply-templates select="node()" mode="LesML:finalize">
-                               <with-param name="used-footnotes" select="$used-footnotes"/>
+                       <copy-of select="@*"/>
+                       <apply-templates mode="LesML:inlinify">
+                               <with-param name="footnote-ids" select="$footnote-ids|*/processing-instruction()[local-name()='LesML-Footnote']"/>
                        </apply-templates>
                </copy>
        </template>
-       <template match="node()" mode="LesML:finalize-attributes">
-               <variable name="notattr" select="following-sibling::node()[not(self::text() and translate(., ' &#x9;', '')='' or self::LesML:attribute)]"/>
-               <for-each select="(.|exslset:leading(following-sibling::node(), $notattr)[self::LesML:attribute])/@*">
-                       <copy-of select="."/>
-                       <if test="local-name()='lang' and namespace-uri()=''">
-                               <attribute name="xml:lang">
-                                       <value-of select="."/>
-                               </attribute>
-                       </if>
-               </for-each>
-       </template>
-       <template match="html:li" mode="LesML:finalize-list">
-               <param name="parent-level" select="0"/>
-               <variable name="current-class" select="string(@class)"/>
-               <variable name="current-level" select="number(@data-level)"/>
-               <variable name="wrapper">
-                       <choose>
-                               <when test="contains(concat(' ', @class, ' '), ' ordered ')">
-                                       <text>ol</text>
-                               </when>
-                               <otherwise>
-                                       <text>ul</text>
-                               </otherwise>
-                       </choose>
-               </variable>
-               <variable name="notinlist" select="following-sibling::node()[not(self::html:div/processing-instruction()[local-name()='LesML-Continuation'] or self::html:li and (@data-level>$current-level or @data-level=$current-level and @class=$current-class))][1]"/>
-               <element name="{$wrapper}" namespace="&xhtml;">
-                       <if test="contains(concat(' ', @class, ' '), ' footnote ')">
-                               <processing-instruction name="LesML-Footnote"/>
-                       </if>
-                       <for-each select=".|exslset:leading(following-sibling::node(), $notinlist)[self::html:li and @data-level=$current-level]">
-                               <variable name="notcontinuation" select="following-sibling::node()[not(self::html:div/processing-instruction()[local-name()='LesML-Continuation'])][1]"/>
-                               <copy>
-                                       <apply-templates select="@*|node()" mode="LesML:finalize-tree"/>
-                                       <for-each select="exslset:leading(following-sibling::node(), $notcontinuation)">
-                                               <apply-templates select="node()" mode="LesML:finalize-tree"/>
-                                       </for-each>
-                                       <if test="$notcontinuation/self::html:li[@data-level>$current-level]">
-                                               <apply-templates select="$notcontinuation" mode="LesML:finalize-list">
-                                                       <with-param name="parent-level" select="$current-level"/>
-                                               </apply-templates>
-                                       </if>
-                               </copy>
-                       </for-each>
-               </element>
-               <if test="$notinlist/self::html:li[@data-level>$parent-level]">
-                       <apply-templates select="$notinlist" mode="LesML:finalize-list">
-                               <with-param name="parent-level" select="$parent-level"/>
-                       </apply-templates>
-               </if>
-       </template>
-       <template match="LesML:attribute[preceding-sibling::node()[position()=1 and (self::text() or self::*)]]|text()[preceding-sibling::node()[position()=1 and self::*] and following-sibling::node()[position()=1 and self::LesML:attribute] and translate(., ' &#x9;', '')='']" mode="LesML:finalize-tree" priority="2"/>
-       <template match="LesML:attribute|text()[following-sibling::node()[position()=1 and self::LesML:attribute]]" mode="LesML:finalize-tree" priority="1">
-               <element name="span" namespace="&xhtml;">
-                       <apply-templates select="." mode="LesML:finalize-attributes"/>
-                       <if test="self::text()">
-                               <copy/>
-                       </if>
-               </element>
-       </template>
-       <template match="html:div" mode="LesML:finalize-tree">
-               <if test="not(processing-instruction()[local-name()='LesML-Continuation']) or not(preceding-sibling::node()[position()=1 and self::html:* and contains(' div h1 h2 h3 h4 h5 h6 li section ', local-name())])">
-                       <variable name="notcontinuation" select="following-sibling::node()[not(self::html:div/processing-instruction()[local-name()='LesML-Continuation'])][1]"/>
-                       <choose>
-                               <when test="processing-instruction()[local-name()='LesML-Continuation'] and $notcontinuation[self::html:* and contains(' h1 h2 h3 h4 h5 h6 ', local-name())]">
-                                       <variable name="notcontinuation2" select="$notcontinuation/following-sibling::node()[not(self::html:div/processing-instruction()[local-name()='LesML-Continuation'])][1]"/>
-                                       <element name="hgroup" namespace="&xhtml;">
-                                               <apply-templates select="node()" mode="LesML:finalize-tree"/>
-                                               <for-each select="exslset:leading(following-sibling::node(), $notcontinuation2)">
-                                                       <choose>
-                                                               <when test="self::html:div">
-                                                                       <apply-templates select="node()" mode="LesML:finalize-tree"/>
-                                                               </when>
-                                                               <otherwise>
-                                                                       <copy>
-                                                                               <apply-templates select="@*|node()" mode="LesML:finalize-tree"/>
-                                                                       </copy>
-                                                               </otherwise>
-                                                       </choose>
-                                               </for-each>
-                                       </element>
-                               </when>
-                               <when test="not(processing-instruction()[local-name()='LesML-Continuation'])">
-                                       <copy>
-                                               <apply-templates select="@*|node()" mode="LesML:finalize-tree"/>
-                                               <for-each select="exslset:leading(following-sibling::node(), $notcontinuation)">
-                                                       <apply-templates select="node()" mode="LesML:finalize-tree"/>
-                                               </for-each>
-                                       </copy>
-                               </when>
-                               <otherwise>
-                                       <apply-templates select="node()" mode="LesML:finalize-tree"/>
-                                       <for-each select="exslset:leading(following-sibling::node(), $notcontinuation)">
-                                               <apply-templates select="node()" mode="LesML:finalize-tree"/>
-                                       </for-each>
-                               </otherwise>
-                       </choose>
-               </if>
+       <template match="node()" mode="LesML:inlinify" priority="-1">
+               <param name="footnote-ids" select="/.."/>
+               <apply-templates select="." mode="LesML:comment">
+                       <with-param name="footnote-ids" select="$footnote-ids"/>
+               </apply-templates>
        </template>
-       <template match="html:h1|html:h2|html:h3|html:h4|html:h5|html:h6" mode="LesML:finalize-tree">
-               <if test="not(preceding-sibling::node()[position()=1 and self::html:div/processing-instruction()[local-name()='LesML-Continuation']]) or preceding-sibling::node()[not(self::html:div/processing-instruction()[local-name()='LesML-Continuation'])][position()=1 and self::html:* and contains(' h1 h2 h3 h4 h5 h6 ', local-name())]">
+       <template match="node()" mode="LesML:comment">
+               <param name="footnote-ids" select="/.."/>
+               <variable name="result-fragment">
                        <choose>
-                               <when test="following-sibling::node()[position()=1 and self::html:div]/processing-instruction()[local-name()='LesML-Continuation']">
-                                       <variable name="notcontinuation" select="following-sibling::node()[not(self::html:div/processing-instruction()[local-name()='LesML-Continuation'])][1]"/>
-                                       <element name="hgroup" namespace="&xhtml;">
-                                               <copy>
-                                                       <apply-templates select="@*|node()" mode="LesML:finalize-tree"/>
-                                               </copy>
-                                               <for-each select="exslset:leading(following-sibling::node(), $notcontinuation)">
-                                                       <apply-templates select="node()" mode="LesML:finalize-tree"/>
-                                               </for-each>
-                                       </element>
-                               </when>
-                               <otherwise>
-                                       <copy>
-                                               <apply-templates select="@*|node()" mode="LesML:finalize-tree"/>
-                                       </copy>
-                               </otherwise>
-                       </choose>
-               </if>
-       </template>
-       <template match="html:li" mode="LesML:finalize-tree">
-               <if test="not(preceding-sibling::node()) or preceding-sibling::node()[not(preceding-sibling::* and self::html:div/processing-instruction()[local-name()='LesML-Continuation'])][position()=1 and not(self::html:li)]">
-                       <apply-templates select="." mode="LesML:finalize-list"/>
-               </if>
-       </template>
-       <template match="html:section" mode="LesML:finalize-tree">
-               <variable name="notcontinuation" select="following-sibling::node()[not(self::html:div/processing-instruction()[local-name()='LesML-Continuation'])][1]"/>
-               <copy>
-                       <apply-templates select="@*|node()" mode="LesML:finalize-tree"/>
-                       <for-each select="exslset:leading(following-sibling::node(), $notcontinuation)">
-                               <apply-templates select="node()" mode="LesML:finalize-tree"/>
-                       </for-each>
-               </copy>
-       </template>
-       <template match="processing-instruction()[local-name()='LesML-Continuation']" mode="LesML:finalize-tree"/>
-       <template match="processing-instruction()[local-name()='LesML-Token-Escape']" mode="LesML:finalize-tree">
-               <value-of select="."/>
-       </template>
-       <template match="@*|node()" mode="LesML:finalize-tree" priority="-1">
-               <copy>
-                       <apply-templates select="." mode="LesML:finalize-attributes"/>
-                       <apply-templates select="node()" mode="LesML:finalize-tree"/>
-               </copy>
-       </template>
-       <template match="node()" mode="LesML:inline">
-               <param name="element-name"/>
-               <param name="element-namespace" select="'http://www.w3.org/1999/xhtml'"/>
-               <param name="start-sigil"/>
-               <param name="end-sigil"/>
-               <param name="class"/>
-               <param name="role"/>
-               <choose>
-                       <when test="self::*">
-                               <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" select="LesML:split($start-node, $start-sigil)"/>
-                                               <variable name="wrapped">
+                               <when test="self::*">
+                                       <variable name="start-node" select="text()[contains(., '⌦')][1]"/>
+                                       <variable name="after-start">
+                                               <if test="$start-node">
+                                                       <value-of select="substring-after($start-node, '⌦')"/>
+                                               </if>
+                                       </variable>
+                                       <variable name="has-end-node" select="contains($after-start, '⌫') or $start-node/following-sibling::text()[contains(., '⌫')]"/>
+                                       <choose>
+                                               <when test="$start-node and $has-end-node">
+                                                       <variable name="following">
+                                                               <value-of select="$after-start"/>
+                                                               <copy-of select="$start-node/following-sibling::node()"/>
+                                                       </variable>
+                                                       <variable name="end-node" select="exsl:node-set($following)/text()[contains(., '⌫')][last()]"/>
+                                                       <variable name="comment">
+                                                               <for-each select="$end-node/preceding-sibling::node()">
+                                                                       <value-of select="."/>
+                                                               </for-each>
+                                                               <value-of select="substring-before($end-node, '⌫')"/>
+                                                       </variable>
+                                                       <variable name="rest-fragment">
+                                                               <element name="span" namespace="&xhtml;">
+                                                                       <value-of select="substring-after($end-node, '⌫')"/>
+                                                                       <copy-of select="$end-node/following-sibling::node()"/>
+                                                               </element>
+                                                       </variable>
+                                                       <variable name="commented-rest-fragment">
+                                                               <apply-templates select="exsl:node-set($rest-fragment)/node()" mode="LesML:comment">
+                                                                       <with-param name="footnote-ids" select="$footnote-ids"/>
+                                                               </apply-templates>
+                                                       </variable>
+                                                       <copy>
+                                                               <copy-of select="@*"/>
+                                                               <copy-of select="$start-node/preceding-sibling::node()"/>
+                                                               <copy-of select="substring-before($start-node, '⌦')"/>
+                                                               <call-template name="LesML:comment-out">
+                                                                       <with-param name="source" select="string($comment)"/>
+                                                               </call-template>
+                                                               <copy-of select="exsl:node-set($commented-rest-fragment)/*/node()"/>
+                                                       </copy>
+                                               </when>
+                                               <when test="$start-node">
+                                                       <variable name="rest-fragment">
+                                                               <element name="div" namespace="&xhtml;">
+                                                                       <value-of select="substring-after($after-start, '⌦')"/>
+                                                                       <copy-of select="$start-node/following-sibling::node()"/>
+                                                               </element>
+                                                       </variable>
+                                                       <variable name="commented-rest-fragment">
+                                                               <apply-templates select="exsl:node-set($rest-fragment)/node()" mode="LesML:comment">
+                                                                       <with-param name="footnote-ids" select="$footnote-ids"/>
+                                                               </apply-templates>
+                                                       </variable>
                                                        <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-of select="substring-before($start-node, '⌦')"/>
+                                                               <text>^</text>
+                                                               <copy-of select="exsl:node-set($commented-rest-fragment)/*/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="@*"/>
-                                                       <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"/>
-                                                       </apply-templates>
-                                               </copy>
-                                       </otherwise>
-                               </choose>
-                       </when>
-                       <otherwise>
-                               <copy-of select="."/>
-                       </otherwise>
-               </choose>
+                                               </when>
+                                               <otherwise>
+                                                       <copy>
+                                                               <copy-of select="@*"/>
+                                                               <apply-templates select="node()" mode="LesML:comment">
+                                                                       <with-param name="footnote-ids" select="$footnote-ids"/>
+                                                               </apply-templates>
+                                                       </copy>
+                                               </otherwise>
+                                       </choose>
+                               </when>
+                               <when test="self::text()[contains(., '⌧')]">
+                                       <for-each select="LesML:split(., '⌧')">
+                                               <value-of select="."/>
+                                               <if test="position()!=last()">
+                                                       <call-template name="LesML:comment-out"/>
+                                               </if>
+                                       </for-each>
+                               </when>
+                               <otherwise>
+                                       <copy-of select="."/>
+                               </otherwise>
+                       </choose>
+               </variable>
+               <apply-templates select="exsl:node-set($result-fragment)/node()" mode="LesML:attrify">
+                       <with-param name="footnote-ids" select="$footnote-ids"/>
+               </apply-templates>
        </template>
        <template match="*" mode="LesML:partition">
                <param name="start-sigil"/>
@@ -1132,11 +980,20 @@ If a copy of the M·P·L was not distributed with this file, You can obtain one
                                        <value-of select="substring-before($end-node, $end-sigil)"/>
                                </variable>
                                <variable name="preceding" select="exsl:node-set($preceding-fragment)/node()"/>
-                               <variable name="start-node" select="($preceding[self::text() and contains(., $start-sigil)])[last()][not($ncname-keys) or not(following-sibling::* or following-sibling::comment())]"/>
+                               <variable name="start-node" select="($preceding[self::text() and contains(., $start-sigil)])[last()][not($ncname-keys) or not(following-sibling::*[not(self::html:br)] or following-sibling::comment())]"/>
                                <variable name="start-tokens" select="LesML:split($start-node, $start-sigil)"/>
                                <variable name="innards-fragment">
                                        <value-of select="$start-tokens[last()]"/>
-                                       <copy-of select="$start-node/following-sibling::node()"/>
+                                       <for-each select="$start-node/following-sibling::node()">
+                                               <choose>
+                                                       <when test="$ncname-keys and self::html:br">
+                                                               <text>&#xA;</text>
+                                                       </when>
+                                                       <otherwise>
+                                                               <copy-of select="."/>
+                                                       </otherwise>
+                                               </choose>
+                                       </for-each>
                                </variable>
                                <variable name="innards" select="exsl:node-set($innards-fragment)/node()"/>
                                <variable name="separator-node" select="($innards[self::text() and contains(., $separator)])[not($ncname-keys)*(last()-1)+1]"/>
@@ -1278,102 +1135,9 @@ If a copy of the M·P·L was not distributed with this file, You can obtain one
                        </otherwise>
                </choose>
        </template>
-       <template match="node()" mode="LesML:comment">
-               <param name="footnote-ids" select="/.."/>
-               <variable name="result">
-                       <choose>
-                               <when test="self::*">
-                                       <variable name="start-node" select="text()[contains(., '⌦')][1]"/>
-                                       <variable name="after-start">
-                                               <if test="$start-node">
-                                                       <value-of select="substring-after($start-node, '⌦')"/>
-                                               </if>
-                                       </variable>
-                                       <variable name="has-end-node" select="contains($after-start, '⌫') or $start-node/following-sibling::text()[contains(., '⌫')]"/>
-                                       <choose>
-                                               <when test="$start-node and $has-end-node">
-                                                       <variable name="following">
-                                                               <value-of select="$after-start"/>
-                                                               <copy-of select="$start-node/following-sibling::node()"/>
-                                                       </variable>
-                                                       <variable name="end-node" select="exsl:node-set($following)/text()[contains(., '⌫')][last()]"/>
-                                                       <variable name="comment">
-                                                               <for-each select="$end-node/preceding-sibling::node()">
-                                                                       <value-of select="."/>
-                                                               </for-each>
-                                                               <value-of select="substring-before($end-node, '⌫')"/>
-                                                       </variable>
-                                                       <variable name="rest-fragment">
-                                                               <element name="span" namespace="&xhtml;">
-                                                                       <value-of select="substring-after($end-node, '⌫')"/>
-                                                                       <copy-of select="$end-node/following-sibling::node()"/>
-                                                               </element>
-                                                       </variable>
-                                                       <variable name="commented-rest-fragment">
-                                                               <apply-templates select="exsl:node-set($rest-fragment)/node()" mode="LesML:comment">
-                                                                       <with-param name="footnote-ids" select="$footnote-ids"/>
-                                                               </apply-templates>
-                                                       </variable>
-                                                       <copy>
-                                                               <copy-of select="@*"/>
-                                                               <copy-of select="$start-node/preceding-sibling::node()"/>
-                                                               <copy-of select="substring-before($start-node, '⌦')"/>
-                                                               <call-template name="LesML:comment-out">
-                                                                       <with-param name="source" select="string($comment)"/>
-                                                               </call-template>
-                                                               <copy-of select="exsl:node-set($commented-rest-fragment)/*/node()"/>
-                                                       </copy>
-                                               </when>
-                                               <when test="$start-node">
-                                                       <variable name="rest-fragment">
-                                                               <element name="div" namespace="&xhtml;">
-                                                                       <value-of select="substring-after($after-start, '⌦')"/>
-                                                                       <copy-of select="$start-node/following-sibling::node()"/>
-                                                               </element>
-                                                       </variable>
-                                                       <variable name="commented-rest-fragment">
-                                                               <apply-templates select="exsl:node-set($rest-fragment)/node()" mode="LesML:comment">
-                                                                       <with-param name="footnote-ids" select="$footnote-ids"/>
-                                                               </apply-templates>
-                                                       </variable>
-                                                       <copy>
-                                                               <copy-of select="@*"/>
-                                                               <copy-of select="$start-node/preceding-sibling::node()"/>
-                                                               <copy-of select="substring-before($start-node, '⌦')"/>
-                                                               <text>^</text>
-                                                               <copy-of select="exsl:node-set($commented-rest-fragment)/*/node()"/>
-                                                       </copy>
-                                               </when>
-                                               <otherwise>
-                                                       <copy>
-                                                               <copy-of select="@*"/>
-                                                               <apply-templates select="node()" mode="LesML:comment">
-                                                                       <with-param name="footnote-ids" select="$footnote-ids"/>
-                                                               </apply-templates>
-                                                       </copy>
-                                               </otherwise>
-                                       </choose>
-                               </when>
-                               <when test="self::text()[contains(., '⌧')]">
-                                       <for-each select="LesML:split(., '⌧')">
-                                               <value-of select="."/>
-                                               <if test="position()!=last()">
-                                                       <call-template name="LesML:comment-out"/>
-                                               </if>
-                                       </for-each>
-                               </when>
-                               <otherwise>
-                                       <copy-of select="."/>
-                               </otherwise>
-                       </choose>
-               </variable>
-               <apply-templates select="exsl:node-set($result)/node()" mode="LesML:attrify">
-                       <with-param name="footnote-ids" select="$footnote-ids"/>
-               </apply-templates>
-       </template>
        <template match="node()" mode="LesML:attrify">
                <param name="footnote-ids" select="/.."/>
-               <variable name="result">
+               <variable name="result-fragment">
                        <choose>
                                <when test="self::*">
                                        <variable name="partitioned-fragment">
@@ -1429,13 +1193,13 @@ If a copy of the M·P·L was not distributed with this file, You can obtain one
                                </otherwise>
                        </choose>
                </variable>
-               <apply-templates select="exsl:node-set($result)/node()" mode="LesML:linkify">
+               <apply-templates select="exsl:node-set($result-fragment)/node()" mode="LesML:linkify">
                        <with-param name="footnote-ids" select="$footnote-ids"/>
                </apply-templates>
        </template>
        <template match="node()" mode="LesML:linkify">
                <param name="footnote-ids" select="/.."/>
-               <variable name="result">
+               <variable name="result-fragment">
                        <choose>
                                <when test="processing-instruction()[local-name()='LesML-All-Done']">
                                        <copy>
@@ -1504,13 +1268,87 @@ If a copy of the M·P·L was not distributed with this file, You can obtain one
                                </otherwise>
                        </choose>
                </variable>
-               <apply-templates select="exsl:node-set($result)/node()" mode="LesML:strikethrough">
+               <apply-templates select="exsl:node-set($result-fragment)/node()" mode="LesML:strikethrough">
                        <with-param name="footnote-ids" select="$footnote-ids"/>
                </apply-templates>
        </template>
+       <template match="node()" mode="LesML:inline">
+               <param name="element-name"/>
+               <param name="element-namespace" select="'http://www.w3.org/1999/xhtml'"/>
+               <param name="start-sigil"/>
+               <param name="end-sigil"/>
+               <param name="class"/>
+               <param name="role"/>
+               <choose>
+                       <when test="self::*">
+                               <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" select="LesML:split($start-node, $start-sigil)"/>
+                                               <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="@*"/>
+                                                       <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"/>
+                                                       </apply-templates>
+                                               </copy>
+                                       </otherwise>
+                               </choose>
+                       </when>
+                       <otherwise>
+                               <copy-of select="."/>
+                       </otherwise>
+               </choose>
+       </template>
        <template match="node()" mode="LesML:strikethrough">
                <param name="footnote-ids" select="/.."/>
-               <variable name="result">
+               <variable name="result-fragment">
                        <apply-templates select="." mode="LesML:inline">
                                <with-param name="element-name" select="'s'"/>
                                <with-param name="element-namespace" select="'&xhtml;'"/>
@@ -1518,13 +1356,13 @@ If a copy of the M·P·L was not distributed with this file, You can obtain one
                                <with-param name="end-sigil" select="'⸡'"/>
                        </apply-templates>
                </variable>
-               <apply-templates select="exsl:node-set($result)/node()" mode="LesML:underline">
+               <apply-templates select="exsl:node-set($result-fragment)/node()" mode="LesML:underline">
                        <with-param name="footnote-ids" select="$footnote-ids"/>
                </apply-templates>
        </template>
        <template match="node()" mode="LesML:underline">
                <param name="footnote-ids" select="/.."/>
-               <variable name="result">
+               <variable name="result-fragment">
                        <apply-templates select="." mode="LesML:inline">
                                <with-param name="element-name" select="'u'"/>
                                <with-param name="element-namespace" select="'&xhtml;'"/>
@@ -1532,13 +1370,13 @@ If a copy of the M·P·L was not distributed with this file, You can obtain one
                                <with-param name="end-sigil" select="'⸥'"/>
                        </apply-templates>
                </variable>
-               <apply-templates select="exsl:node-set($result)/node()" mode="LesML:noted">
+               <apply-templates select="exsl:node-set($result-fragment)/node()" mode="LesML:noted">
                        <with-param name="footnote-ids" select="$footnote-ids"/>
                </apply-templates>
        </template>
        <template match="node()" mode="LesML:noted">
                <param name="footnote-ids" select="/.."/>
-               <variable name="result">
+               <variable name="result-fragment">
                        <apply-templates select="." mode="LesML:inline">
                                <with-param name="element-name" select="'small'"/>
                                <with-param name="element-namespace" select="'&xhtml;'"/>
@@ -1547,13 +1385,13 @@ If a copy of the M·P·L was not distributed with this file, You can obtain one
                                <with-param name="role" select="'note'"/>
                        </apply-templates>
                </variable>
-               <apply-templates select="exsl:node-set($result)/node()" mode="LesML:parenthetical">
+               <apply-templates select="exsl:node-set($result-fragment)/node()" mode="LesML:parenthetical">
                        <with-param name="footnote-ids" select="$footnote-ids"/>
                </apply-templates>
        </template>
        <template match="node()" mode="LesML:parenthetical">
                <param name="footnote-ids" select="/.."/>
-               <variable name="result">
+               <variable name="result-fragment">
                        <apply-templates select="." mode="LesML:inline">
                                <with-param name="element-name" select="'small'"/>
                                <with-param name="element-namespace" select="'&xhtml;'"/>
@@ -1561,13 +1399,13 @@ If a copy of the M·P·L was not distributed with this file, You can obtain one
                                <with-param name="end-sigil" select="'⸩'"/>
                        </apply-templates>
                </variable>
-               <apply-templates select="exsl:node-set($result)/node()" mode="LesML:code">
+               <apply-templates select="exsl:node-set($result-fragment)/node()" mode="LesML:code">
                        <with-param name="footnote-ids" select="$footnote-ids"/>
                </apply-templates>
        </template>
        <template match="node()" mode="LesML:code">
                <param name="footnote-ids" select="/.."/>
-               <variable name="result">
+               <variable name="result-fragment">
                        <apply-templates select="." mode="LesML:inline">
                                <with-param name="element-name" select="'code'"/>
                                <with-param name="element-namespace" select="'&xhtml;'"/>
@@ -1575,13 +1413,13 @@ If a copy of the M·P·L was not distributed with this file, You can obtain one
                                <with-param name="end-sigil" select="'´'"/>
                        </apply-templates>
                </variable>
-               <apply-templates select="exsl:node-set($result)/node()" mode="LesML:titled">
+               <apply-templates select="exsl:node-set($result-fragment)/node()" mode="LesML:titled">
                        <with-param name="footnote-ids" select="$footnote-ids"/>
                </apply-templates>
        </template>
        <template match="node()" mode="LesML:titled">
                <param name="footnote-ids" select="/.."/>
-               <variable name="result">
+               <variable name="result-fragment">
                        <apply-templates select="." mode="LesML:inline">
                                <with-param name="element-name" select="'cite'"/>
                                <with-param name="element-namespace" select="'&xhtml;'"/>
@@ -1589,13 +1427,13 @@ If a copy of the M·P·L was not distributed with this file, You can obtain one
                                <with-param name="end-sigil" select="'⟫'"/>
                        </apply-templates>
                </variable>
-               <apply-templates select="exsl:node-set($result)/node()" mode="LesML:named">
+               <apply-templates select="exsl:node-set($result-fragment)/node()" mode="LesML:named">
                        <with-param name="footnote-ids" select="$footnote-ids"/>
                </apply-templates>
        </template>
        <template match="node()" mode="LesML:named">
                <param name="footnote-ids" select="/.."/>
-               <variable name="result">
+               <variable name="result-fragment">
                        <apply-templates select="." mode="LesML:inline">
                                <with-param name="element-name" select="'u'"/>
                                <with-param name="element-namespace" select="'&xhtml;'"/>
@@ -1604,13 +1442,13 @@ If a copy of the M·P·L was not distributed with this file, You can obtain one
                                <with-param name="class" select="'name'"/>
                        </apply-templates>
                </variable>
-               <apply-templates select="exsl:node-set($result)/node()" mode="LesML:offset">
+               <apply-templates select="exsl:node-set($result-fragment)/node()" mode="LesML:offset">
                        <with-param name="footnote-ids" select="$footnote-ids"/>
                </apply-templates>
        </template>
        <template match="node()" mode="LesML:offset">
                <param name="footnote-ids" select="/.."/>
-               <variable name="result">
+               <variable name="result-fragment">
                        <apply-templates select="." mode="LesML:inline">
                                <with-param name="element-name" select="'i'"/>
                                <with-param name="element-namespace" select="'&xhtml;'"/>
@@ -1618,13 +1456,13 @@ If a copy of the M·P·L was not distributed with this file, You can obtain one
                                <with-param name="end-sigil" select="'⟩'"/>
                        </apply-templates>
                </variable>
-               <apply-templates select="exsl:node-set($result)/node()" mode="LesML:bolded">
+               <apply-templates select="exsl:node-set($result-fragment)/node()" mode="LesML:bolded">
                        <with-param name="footnote-ids" select="$footnote-ids"/>
                </apply-templates>
        </template>
        <template match="node()" mode="LesML:bolded">
                <param name="footnote-ids" select="/.."/>
-               <variable name="result">
+               <variable name="result-fragment">
                        <apply-templates select="." mode="LesML:inline">
                                <with-param name="element-name" select="'b'"/>
                                <with-param name="element-namespace" select="'&xhtml;'"/>
@@ -1632,13 +1470,13 @@ If a copy of the M·P·L was not distributed with this file, You can obtain one
                                <with-param name="end-sigil" select="'⦄'"/>
                        </apply-templates>
                </variable>
-               <apply-templates select="exsl:node-set($result)/node()" mode="LesML:important">
+               <apply-templates select="exsl:node-set($result-fragment)/node()" mode="LesML:important">
                        <with-param name="footnote-ids" select="$footnote-ids"/>
                </apply-templates>
        </template>
        <template match="node()" mode="LesML:important">
                <param name="footnote-ids" select="/.."/>
-               <variable name="result">
+               <variable name="result-fragment">
                        <apply-templates select="." mode="LesML:inline">
                                <with-param name="element-name" select="'strong'"/>
                                <with-param name="element-namespace" select="'&xhtml;'"/>
@@ -1646,13 +1484,13 @@ If a copy of the M·P·L was not distributed with this file, You can obtain one
                                <with-param name="end-sigil" select="'☜'"/>
                        </apply-templates>
                </variable>
-               <apply-templates select="exsl:node-set($result)/node()" mode="LesML:emphasized">
+               <apply-templates select="exsl:node-set($result-fragment)/node()" mode="LesML:emphasized">
                        <with-param name="footnote-ids" select="$footnote-ids"/>
                </apply-templates>
        </template>
        <template match="node()" mode="LesML:emphasized">
                <param name="footnote-ids" select="/.."/>
-               <variable name="result">
+               <variable name="result-fragment">
                        <apply-templates select="." mode="LesML:inline">
                                <with-param name="element-name" select="'em'"/>
                                <with-param name="element-namespace" select="'&xhtml;'"/>
@@ -1660,87 +1498,294 @@ If a copy of the M·P·L was not distributed with this file, You can obtain one
                                <with-param name="end-sigil" select="'⹑'"/>
                        </apply-templates>
                </variable>
-               <apply-templates select="exsl:node-set($result)/node()" mode="LesML:footnoted">
+               <apply-templates select="exsl:node-set($result-fragment)/node()" mode="LesML:footnoted">
                        <with-param name="footnote-ids" select="$footnote-ids"/>
                </apply-templates>
        </template>
        <template match="node()" mode="LesML:footnoted">
                <param name="footnote-ids" select="/.."/>
+               <variable name="result-fragment">
+                       <choose>
+                               <when test="self::*">
+                                       <variable name="start-node" select="text()[contains(., '[^')][1]"/>
+                                       <variable name="after-start" select="substring-after($start-node, '[^')"/>
+                                       <variable name="footnote-id" select="substring-before($after-start, ']')"/>
+                                       <variable name="matched-footnote" select="$footnote-ids[.=$footnote-id]"/>
+                                       <choose>
+                                               <when test="$footnote-id!='' and $matched-footnote">
+                                                       <variable name="rest-fragment">
+                                                               <element name="div" namespace="&xhtml;">
+                                                                       <value-of select="substring-after($after-start, ']')"/>
+                                                                       <copy-of select="$start-node/following-sibling::node()"/>
+                                                               </element>
+                                                       </variable>
+                                                       <variable name="footnoted-rest-fragment">
+                                                               <apply-templates select="exsl:node-set($rest-fragment)/node()" mode="LesML:footnoted">
+                                                                       <with-param name="footnote-ids" select="$footnote-ids"/>
+                                                               </apply-templates>
+                                                       </variable>
+                                                       <copy>
+                                                               <copy-of select="@*"/>
+                                                               <copy-of select="$start-node/preceding-sibling::node()"/>
+                                                               <copy-of select="substring-before($start-node, '[^')"/>
+                                                               <element name="a" namespace="&xhtml;">
+                                                                       <attribute name="id">
+                                                                               <text>LesML.fnref.</text>
+                                                                               <value-of select="generate-id($start-node)"/>
+                                                                       </attribute>
+                                                                       <attribute name="role">
+                                                                               <text>doc-noteref</text>
+                                                                       </attribute>
+                                                                       <attribute name="href">
+                                                                               <text>#</text>
+                                                                               <value-of select="$footnote-id"/>
+                                                                       </attribute>
+                                                                       <attribute name="target">
+                                                                               <text>_self</text>
+                                                                       </attribute>
+                                                               </element>
+                                                               <copy-of select="exsl:node-set($footnoted-rest-fragment)/*/node()"/>
+                                                       </copy>
+                                               </when>
+                                               <when test="$start-node">
+                                                       <variable name="rest-fragment">
+                                                               <element name="div" namespace="&xhtml;">
+                                                                       <value-of select="substring-after($start-node, '[^')"/>
+                                                                       <copy-of select="$start-node/following-sibling::node()"/>
+                                                               </element>
+                                                       </variable>
+                                                       <variable name="footnoted-rest-fragment">
+                                                               <apply-templates select="exsl:node-set($rest-fragment)/node()" mode="LesML:footnoted">
+                                                                       <with-param name="footnote-ids" select="$footnote-ids"/>
+                                                               </apply-templates>
+                                                       </variable>
+                                                       <copy>
+                                                               <copy-of select="@*"/>
+                                                               <copy-of select="$start-node/preceding-sibling::node()"/>
+                                                               <copy-of select="substring-before($start-node, '[^')"/>
+                                                               <text>[^</text>
+                                                               <copy-of select="exsl:node-set($footnoted-rest-fragment)/*/node()"/>
+                                                       </copy>
+                                               </when>
+                                               <otherwise>
+                                                       <copy>
+                                                               <copy-of select="@*"/>
+                                                               <apply-templates select="node()" mode="LesML:footnoted">
+                                                                       <with-param name="footnote-ids" select="$footnote-ids"/>
+                                                               </apply-templates>
+                                                       </copy>
+                                               </otherwise>
+                                       </choose>
+                               </when>
+                               <otherwise>
+                                       <copy-of select="."/>
+                               </otherwise>
+                       </choose>
+               </variable>
+               <apply-templates select="exsl:node-set($result-fragment)/node()" mode="LesML:finish"/>
+       </template>
+       <template match="processing-instruction()[local-name()='LesML-All-Done']" mode="LesML:finish"/>
+       <template match="processing-instruction()[local-name()='LesML-Token-Escape']" mode="LesML:finish">
+               <value-of select="."/>
+       </template>
+       <template match="@*|node()" mode="LesML:finish" priority="-1">
+               <copy>
+                       <apply-templates select="@*|node()" mode="LesML:finish"/>
+               </copy>
+       </template>
+
+<!-->
+❦ Templates in the `LesML:finalize´ and related modes
+
+These templates finalize the resulting tree.
+</!-->
+
+       <template match="node()" mode="LesML:finalize-attributes">
+               <param name="exclude-level" select="false()"/>
+               <variable name="notattr" select="following-sibling::node()[not(self::LesML:attribute)]"/>
+               <variable name="div" select="self::html:div|self::*[&block-types;]/node()[not(self::processing-instruction())][position()=1 and self::html:div]"/>
+               <variable name="para" select="self::html:p|$div/node()[not(self::processing-instruction())][position()=1 and self::html:p]"/>
+               <variable name="emptypara" select="$para[not(@*|node()[not(self::processing-instruction())])]"/>
+               <for-each select="(.|$emptypara/..|exslset:leading(following-sibling::node(), $notattr)[self::LesML:attribute])/@*">
+                       <copy-of select="."/>
+                       <if test="local-name()='lang' and namespace-uri()=''">
+                               <attribute name="xml:lang">
+                                       <value-of select="."/>
+                               </attribute>
+                       </if>
+               </for-each>
+               <if test="not($exclude-level) and &level-pi;!=0">
+                       <attribute name="data-level">
+                               <value-of select="&level-pi;"/>
+                       </attribute>
+               </if>
+       </template>
+       <template match="html:li" mode="LesML:finalize-list">
+               <param name="used-footnotes" select="/.."/>
+               <variable name="current-class" select="string(@class)"/>
+               <variable name="current-level" select="number(&level-pi;)"/>
+               <variable name="wrapper">
+                       <choose>
+                               <when test="contains(concat(' ', @class, ' '), ' ordered ')">
+                                       <text>ol</text>
+                               </when>
+                               <otherwise>
+                                       <text>ul</text>
+                               </otherwise>
+                       </choose>
+               </variable>
+               <variable name="notinlist" select="following-sibling::node()[not(self::html:li and &level-pi;=$current-level and @class=$current-class)][1]"/>
+               <element name="{$wrapper}" namespace="&xhtml;">
+                       <for-each select=".|exslset:leading(following-sibling::node(), $notinlist)">
+                               <copy>
+                                       <apply-templates select="." mode="LesML:finalize-attributes"/>
+                                       <apply-templates select="node()" mode="LesML:finalize">
+                                               <with-param name="used-footnotes" select="$used-footnotes"/>
+                                       </apply-templates>
+                               </copy>
+                       </for-each>
+               </element>
+               <if test="$notinlist/self::html:li">
+                       <apply-templates select="$notinlist" mode="LesML:finalize-list">
+                               <with-param name="used-footnotes" select="$used-footnotes"/>
+                       </apply-templates>
+               </if>
+       </template>
+       <template match="*[processing-instruction()[local-name()='LesML-Footnote']]|html:div/node()[not(self::processing-instruction())][position()=1 and self::html:p and not(@*|node())]|LesML:attribute[preceding-sibling::node()[position()=1 and (self::text() or self::*)]]|processing-instruction()" mode="LesML:finalize" priority="2"/>
+       <template match="html:div[not((node()[not(self::processing-instruction())])/following-sibling::node()[not(self::processing-instruction())])]" mode="LesML:finalize" priority="1">
+               <param name="used-footnotes" select="/.."/>
+               <variable name="context" select="."/>
+               <variable name="result-fragment">
+                       <apply-templates select="node()" mode="LesML:finalize">
+                               <with-param name="used-footnotes" select="$used-footnotes"/>
+                       </apply-templates>
+               </variable>
+               <variable name="result" select="exsl:node-set($result-fragment)/node()"/>
                <choose>
-                       <when test="self::*">
-                               <variable name="start-node" select="text()[contains(., '^')][1]"/>
-                               <variable name="after-start" select="substring-after($start-node, '^')"/>
-                               <variable name="footnote-id" select="substring-before($after-start, '.')"/>
-                               <variable name="matched-footnote" select="$footnote-ids[.=$footnote-id]"/>
-                               <choose>
-                                       <when test="$footnote-id!='' and $matched-footnote">
-                                               <variable name="rest-fragment">
-                                                       <element name="div" namespace="&xhtml;">
-                                                               <value-of select="substring-after($after-start, '.')"/>
-                                                               <copy-of select="$start-node/following-sibling::node()"/>
-                                                       </element>
-                                               </variable>
-                                               <variable name="footnoted-rest-fragment">
-                                                       <apply-templates select="exsl:node-set($rest-fragment)/node()" mode="LesML:footnoted">
-                                                               <with-param name="footnote-ids" select="$footnote-ids"/>
-                                                       </apply-templates>
-                                               </variable>
-                                               <copy>
-                                                       <copy-of select="@*"/>
-                                                       <copy-of select="$start-node/preceding-sibling::node()"/>
-                                                       <copy-of select="substring-before($start-node, '^')"/>
-                                                       <element name="a" namespace="&xhtml;">
-                                                               <attribute name="id">
-                                                                       <text>LesML.fnref.</text>
-                                                                       <value-of select="generate-id($start-node)"/>
-                                                               </attribute>
-                                                               <attribute name="role">
-                                                                       <text>doc-noteref</text>
-                                                               </attribute>
-                                                               <attribute name="href">
-                                                                       <text>#</text>
-                                                                       <value-of select="$footnote-id"/>
-                                                               </attribute>
-                                                               <attribute name="target">
-                                                                       <text>_self</text>
-                                                               </attribute>
-                                                       </element>
-                                                       <copy-of select="exsl:node-set($footnoted-rest-fragment)/*/node()"/>
-                                               </copy>
-                                       </when>
-                                       <when test="$start-node">
-                                               <variable name="rest-fragment">
-                                                       <element name="div" namespace="&xhtml;">
-                                                               <value-of select="substring-after($after-start, '^')"/>
-                                                               <copy-of select="$start-node/following-sibling::node()"/>
-                                                       </element>
-                                               </variable>
-                                               <variable name="footnoted-rest-fragment">
-                                                       <apply-templates select="exsl:node-set($rest-fragment)/node()" mode="LesML:footnoted">
-                                                               <with-param name="footnote-ids" select="$footnote-ids"/>
-                                                       </apply-templates>
-                                               </variable>
-                                               <copy>
-                                                       <copy-of select="@*"/>
-                                                       <copy-of select="$start-node/preceding-sibling::node()"/>
-                                                       <copy-of select="substring-before($start-node, '^')"/>
-                                                       <text>^</text>
-                                                       <copy-of select="exsl:node-set($footnoted-rest-fragment)/*/node()"/>
-                                               </copy>
-                                       </when>
-                                       <otherwise>
-                                               <copy>
-                                                       <copy-of select="@*"/>
-                                                       <apply-templates select="node()" mode="LesML:footnoted">
-                                                               <with-param name="footnote-ids" select="$footnote-ids"/>
-                                                       </apply-templates>
-                                               </copy>
-                                       </otherwise>
-                               </choose>
+                       <when test="not(../&level-pi;) and &level-pi;!=0 or ../&level-pi; and &level-pi;-1!=../&level-pi;">
+                               <copy>
+                                       <apply-templates select="$context" mode="LesML:finalize-attributes"/>
+                                       <copy-of select="$result"/>
+                               </copy>
                        </when>
-                       <otherwise>
-                               <copy-of select="."/>
-                       </otherwise>
+                       <when test="$result">
+                               <for-each select="$result">
+                                       <copy>
+                                               <apply-templates select="$context" mode="LesML:finalize-attributes">
+                                                       <with-param name="exclude-level" select="true()"/>
+                                               </apply-templates>
+                                               <copy-of select="@*|node()"/>
+                                       </copy>
+                               </for-each>
+                       </when>
+                       <when test="* and (preceding-sibling::node()[not(self::processing-instruction())] or not(ancestor::*))">
+                               <for-each select="*">
+                                       <copy>
+                                               <apply-templates select="$context" mode="LesML:finalize-attributes">
+                                                       <with-param name="exclude-level" select="true()"/>
+                                               </apply-templates>
+                                               <copy-of select="@*|node()"/>
+                                       </copy>
+                               </for-each>
+                       </when>
+                       <otherwise/>
                </choose>
        </template>
+       <template match="LesML:attribute|text()[following-sibling::node()[position()=1 and self::LesML:attribute]]" mode="LesML:finalize" priority="1">
+               <element name="span" namespace="&xhtml;">
+                       <apply-templates select="." mode="LesML:finalize-attributes"/>
+                       <if test="self::text()">
+                               <copy/>
+                       </if>
+               </element>
+       </template>
+       <template match="html:blockquote[node()[position()=last() and self::html:footer]]" mode="LesML:finalize">
+               <param name="used-footnotes" select="/.."/>
+               <element name="figure" namespace="&xhtml;">
+                       <copy>
+                               <apply-templates select="." mode="LesML:finalize-attributes"/>
+                               <apply-templates select="node()[position()!=last()]" mode="LesML:finalize">
+                                       <with-param name="used-footnotes" select="$used-footnotes"/>
+                               </apply-templates>
+                       </copy>
+                       <for-each select="node()[last()]">
+                               <element name="figcaption" namespace="&xhtml;">
+                                       <apply-templates select="." mode="LesML:finalize-attributes">
+                                               <with-param name="exclude-level" select="true()"/>
+                                       </apply-templates>
+                                       <apply-templates select="node()" mode="LesML:finalize">
+                                               <with-param name="used-footnotes" select="$used-footnotes"/>
+                                       </apply-templates>
+                               </element>
+                       </for-each>
+               </element>
+       </template>
+       <template match="html:div[(self::*|html:div)[html:h1 or html:h2 or html:h3 or html:h4 or html:h5 or html:h6]]" mode="LesML:finalize">
+               <param name="used-footnotes" select="/.."/>
+               <element name="hgroup" namespace="&xhtml;">
+                       <apply-templates select="." mode="LesML:finalize-attributes"/>
+                       <apply-templates select="node()" mode="LesML:finalize">
+                               <with-param name="used-footnotes" select="$used-footnotes"/>
+                       </apply-templates>
+               </element>
+       </template>
+       <template match="html:li" mode="LesML:finalize">
+               <param name="used-footnotes" select="/.."/>
+               <if test="not(preceding-sibling::node()) or preceding-sibling::node()[position()=1 and not(self::html:li)]">
+                       <apply-templates select="." mode="LesML:finalize-list">
+                               <with-param name="used-footnotes" select="$used-footnotes"/>
+                       </apply-templates>
+               </if>
+       </template>
+       <template match="html:pre" mode="LesML:finalize">
+               <param name="used-footnotes" select="/.."/>
+               <copy>
+                       <apply-templates select="." mode="LesML:finalize-attributes"/>
+                       <choose>
+                               <when test="@class='code'">
+                                       <element name="code" namespace="&xhtml;">
+                                               <apply-templates select="node()" mode="LesML:finalize">
+                                                       <with-param name="used-footnotes" select="$used-footnotes"/>
+                                               </apply-templates>
+                                       </element>
+                               </when>
+                               <otherwise>
+                                       <apply-templates select="node()" mode="LesML:finalize">
+                                               <with-param name="used-footnotes" select="$used-footnotes"/>
+                                       </apply-templates>
+                               </otherwise>
+                       </choose>
+               </copy>
+       </template>
+       <template match="html:a[@role='doc-noteref']" mode="LesML:finalize">
+               <param name="used-footnotes" select="/.."/>
+               <variable name="matching-note" select="$used-footnotes[concat('#', @id)=current()/@href]"/>
+               <copy>
+                       <apply-templates select="." mode="LesML:finalize-attributes"/>
+                       <choose>
+                               <when test="$matching-note">
+                                       <value-of select="$matching-note/@value"/>
+                               </when>
+                               <otherwise>
+                                       <apply-templates select="node()" mode="LesML:finalize">
+                                               <with-param name="used-footnotes" select="$used-footnotes"/>
+                                       </apply-templates>
+                               </otherwise>
+                       </choose>
+               </copy>
+       </template>
+       <template match="text()" mode="LesML:finalize">
+               <call-template name="LesML:unescape">
+                       <with-param name="source" select="string()"/>
+               </call-template>
+       </template>
+       <template match="node()" mode="LesML:finalize" priority="-1">
+               <param name="used-footnotes" select="/.."/>
+               <copy>
+                       <apply-templates select="." mode="LesML:finalize-attributes"/>
+                       <apply-templates select="node()" mode="LesML:finalize">
+                               <with-param name="used-footnotes" select="$used-footnotes"/>
+                       </apply-templates>
+               </copy>
+       </template>
 </transform>
This page took 0.167679 seconds and 4 git commands to generate.