]> Lady’s Gitweb - LesML/blobdiff - parser.xslt
Better handle id‐less pilcrows
[LesML] / parser.xslt
index f1ade04f126864d162b63879b59716c699134d2b..2f028c2036f24f1dea89455a7fed05185663c4b5 100644 (file)
@@ -12,7 +12,9 @@ This Source Code Form is subject to the terms of the Mozilla Public License, v 2
 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 section-break '#*-.=_~·․‥…⁂⋯─━┄┅┈┉╌╍═╴╶╸╺☙❧ ・*-.=_~'>
+       <!ENTITY section-break "*-.=_~·․‥…⁂⋯─━┄┅┈┉╌╍═╴╶╸╺☙❧ ・*-.=_~">
+       <!ENTITY sigiled-text "(string-length($text)=1 or substring($text, 2, 1)=' ')">
+       <!ENTITY unsigiled-text "substring($text, 3, string-length($text)-2)">
 ]>
 <transform
        xmlns="http://www.w3.org/1999/XSL/Transform"
@@ -103,9 +105,12 @@ If a copy of the M·P·L was not distributed with this file, You can obtain one
                        <when test="starts-with($source, '¶')">
                                <choose>
                                        <when test="contains($source, ' ')">
-                                               <attribute name="id">
-                                                       <value-of select="substring-before(substring-after($source, '¶'), ' ')"/>
-                                               </attribute>
+                                               <variable name="id" select="substring-before(substring-after($source, '¶'), ' ')"/>
+                                               <if test="$id!=''">
+                                                       <attribute name="id">
+                                                               <value-of select="$id"/>
+                                                       </attribute>
+                                               </if>
                                                <value-of select="substring-after($source, ' ')"/>
                                        </when>
                                        <otherwise>
@@ -129,7 +134,6 @@ If a copy of the M·P·L was not distributed with this file, You can obtain one
                                <value-of select="$first-line"/>
                        </if>
                </variable>
-               <variable name="noshebang" select="$lines[position()>1 or not(starts-with(., '#!lesml') or starts-with(., '##'))]"/>
                <variable name="params-string">
                        <choose>
                                <when test="starts-with($shebang, '#!lesml@')">
@@ -141,120 +145,151 @@ If a copy of the M·P·L was not distributed with this file, You can obtain one
                        </choose>
                </variable>
                <variable name="params-fragment">
-                       <html:dl>
-                               <if test="starts-with($shebang, '#!lesml@') and contains($shebang, '$')">
-                                       <html:div>
-                                               <html:dt>
-                                                       <text> LANG </text>
-                                               </html:dt>
-                                               <html:dd>
-                                                       <value-of select="substring-before(substring-after($shebang, '#!lesml@'), '$')"/>
-                                               </html:dd>
-                                       </html:div>
-                               </if>
-                               <for-each select="exslstr:tokenize($params-string)">
-                                       <choose>
-                                               <when test="contains(., '=')">
+                       <choose>
+                               <when test="$shebang!=''">
+                                       <html:dl>
+                                               <if test="starts-with($shebang, '#!lesml@') and contains($shebang, '$')">
                                                        <html:div>
                                                                <html:dt>
-                                                                       <value-of select="substring-before(., '=')"/>
+                                                                       <text> LANG </text>
                                                                </html:dt>
                                                                <html:dd>
-                                                                       <value-of select="substring-after(., '=')"/>
+                                                                       <value-of select="substring-before(substring-after($shebang, '#!lesml@'), '$')"/>
                                                                </html:dd>
                                                        </html:div>
-                                               </when>
-                                               <otherwise>
-                                                       <html:div>
-                                                               <html:dt>
-                                                                       <value-of select="."/>
-                                                               </html:dt>
-                                                               <html:dd/>
-                                                       </html:div>
-                                               </otherwise>
-                                       </choose>
-                               </for-each>
-                       </html:dl>
+                                               </if>
+                                               <for-each select="exslstr:tokenize($params-string)">
+                                                       <choose>
+                                                               <when test="contains(., '=')">
+                                                                       <html:div>
+                                                                               <html:dt>
+                                                                                       <value-of select="substring-before(., '=')"/>
+                                                                               </html:dt>
+                                                                               <html:dd>
+                                                                                       <value-of select="substring-after(., '=')"/>
+                                                                               </html:dd>
+                                                                       </html:div>
+                                                               </when>
+                                                               <otherwise>
+                                                                       <html:div>
+                                                                               <html:dt>
+                                                                                       <value-of select="."/>
+                                                                               </html:dt>
+                                                                               <html:dd/>
+                                                                       </html:div>
+                                                               </otherwise>
+                                                       </choose>
+                                               </for-each>
+                                       </html:dl>
+                               </when>
+                               <when test="$parent-params">
+                                       <copy-of select="$parent-params"/>
+                               </when>
+                               <otherwise>
+                                       <html:dl/>
+                               </otherwise>
+                       </choose>
                </variable>
                <variable name="params" select="exsl:node-set($params-fragment)/*"/>
-               <variable name="record-separators" select="$noshebang[starts-with(., '%%')]"/>
-               <html:article>
-                       <for-each select="$params/html:div/html:dt[string()=' LANG ']">
-                               <attribute name="lang">
-                                       <value-of select="following-sibling::html:dd"/>
-                               </attribute>
-                               <attribute name="xml:lang">
-                                       <value-of select="following-sibling::html:dd"/>
-                               </attribute>
-                       </for-each>
-                       <for-each select="$params/html:div/html:dt[string()='profile']">
-                               <attribute name="data-lesml-profile">
-                                       <value-of select="following-sibling::html:dd"/>
-                               </attribute>
-                       </for-each>
-                       <if test="count($record-separators)>1">
-                               <html:footer class="head">
-                                       <for-each select="$record-separators">
-                                               <variable name="position" select="position()"/>
-                                               <variable name="prev-separator" select="$record-separators[($position)-1]"/>
-                                               <variable name="fields" select="$noshebang[following-sibling::*[generate-id()=generate-id(current())] and (not($prev-separator) or preceding-sibling::*[generate-id()=generate-id($prev-separator)])]"/>
-                                               <if test="$fields">
-                                                       <html:dl>
-                                                               <for-each select="$fields">
-                                                                       <choose>
-                                                                               <when test="starts-with(., ' ') and $fields[generate-id()=generate-id(current()/preceding-sibling::*[1])]"/>
-                                                                               <otherwise>
-                                                                                       <variable name="next" select="following-sibling::*[not(starts-with(., ' '))]"/>
-                                                                                       <html:div>
-                                                                                               <html:dt>
-                                                                                                       <value-of select="normalize-space(substring-before(., ':'))"/>
-                                                                                               </html:dt>
-                                                                                               <html:dd>
-                                                                                                       <variable name="firstline">
-                                                                                                               <choose>
-                                                                                                                       <when test="contains(., ':')">
-                                                                                                                               <value-of select="normalize-space(substring-after(., ':'))"/>
-                                                                                                                       </when>
-                                                                                                                       <otherwise>
-                                                                                                                               <value-of select="normalize-space(.)"/>
-                                                                                                                       </otherwise>
-                                                                                                               </choose>
-                                                                                                       </variable>
-                                                                                                       <choose>
-                                                                                                               <when test="substring($firstline, string-length($firstline))='\' and following-sibling::*[position()=1 and starts-with(., ' ')]">
-                                                                                                                       <value-of select="substring($firstline, 1, string-length($firstline)-1)"/>
-                                                                                                               </when>
-                                                                                                               <otherwise>
-                                                                                                                       <value-of select="$firstline"/>
-                                                                                                               </otherwise>
-                                                                                                       </choose>
-                                                                                                       <for-each select="following-sibling::*[starts-with(., ' ') and not(preceding-sibling::*[generate-id()=generate-id($next)])]">
-                                                                                                               <variable name="nextline" select="normalize-space(.)"/>
+               <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="$noshebang[not($docsep) or following-sibling::*[generate-id()=generate-id($docsep)]]"/>
+               <if test="starts-with($first-line, '##') and $first-line!='##'">
+                       <comment>
+                               <value-of select="substring-after($first-line, '##')"/>
+                       </comment>
+               </if>
+               <if test="$shebang!='' or $doclines[normalize-space()!='']">
+                       <variable name="record-separators" select="$doclines[starts-with(., '%%')]"/>
+                       <html:article>
+                               <for-each select="$params/html:div/html:dt[string()=' LANG ']">
+                                       <attribute name="lang">
+                                               <value-of select="following-sibling::html:dd"/>
+                                       </attribute>
+                                       <attribute name="xml:lang">
+                                               <value-of select="following-sibling::html:dd"/>
+                                       </attribute>
+                               </for-each>
+                               <for-each select="$params/html:div/html:dt[string()='profile']">
+                                       <attribute name="data-lesml-profile">
+                                               <value-of select="following-sibling::html:dd"/>
+                                       </attribute>
+                               </for-each>
+                               <if test="$record-separators[preceding-sibling::*[normalize-space()!='']]">
+                                       <html:footer class="head">
+                                               <for-each select="$record-separators">
+                                                       <variable name="position" select="position()"/>
+                                                       <variable name="prev-separator" select="$record-separators[($position)-1]"/>
+                                                       <variable name="fields" select="$noshebang[following-sibling::*[generate-id()=generate-id(current())] and (not($prev-separator) or preceding-sibling::*[generate-id()=generate-id($prev-separator)])]"/>
+                                                       <if test="$fields">
+                                                               <html:dl>
+                                                                       <for-each select="$fields">
+                                                                               <choose>
+                                                                                       <when test="starts-with(., ' ') and $fields[generate-id()=generate-id(current()/preceding-sibling::*[1])]"/>
+                                                                                       <otherwise>
+                                                                                               <variable name="next" select="following-sibling::*[not(starts-with(., ' '))]"/>
+                                                                                               <html:div>
+                                                                                                       <html:dt>
+                                                                                                               <value-of select="normalize-space(substring-before(., ':'))"/>
+                                                                                                       </html:dt>
+                                                                                                       <html:dd>
+                                                                                                               <variable name="firstline">
+                                                                                                                       <choose>
+                                                                                                                               <when test="contains(., ':')">
+                                                                                                                                       <value-of select="normalize-space(substring-after(., ':'))"/>
+                                                                                                                               </when>
+                                                                                                                               <otherwise>
+                                                                                                                                       <value-of select="normalize-space(.)"/>
+                                                                                                                               </otherwise>
+                                                                                                                       </choose>
+                                                                                                               </variable>
                                                                                                                <choose>
-                                                                                                                       <when test="substring($nextline, string-length($nextline))='\' and following-sibling::*[position()=1 and starts-with(., ' ')]">
-                                                                                                                               <value-of select="substring($nextline, 1, string-length($nextline)-1)"/>
+                                                                                                                       <when test="substring($firstline, string-length($firstline))='\' and following-sibling::*[position()=1 and starts-with(., ' ')]">
+                                                                                                                               <value-of select="substring($firstline, 1, string-length($firstline)-1)"/>
                                                                                                                        </when>
                                                                                                                        <otherwise>
-                                                                                                                               <value-of select="$nextline"/>
+                                                                                                                               <value-of select="$firstline"/>
                                                                                                                        </otherwise>
                                                                                                                </choose>
-                                                                                                       </for-each>
-                                                                                               </html:dd>
-                                                                                       </html:div>
-                                                                               </otherwise>
-                                                                       </choose>
-                                                               </for-each>
-                                                       </html:dl>
-                                               </if>
-                                       </for-each>
-                               </html:footer>
-                       </if>
-                       <html:div class="body">
-                               <call-template name="LesML:paragraphize">
-                                       <with-param name="lines" select="$noshebang[not($record-separators) or preceding-sibling::*[generate-id()=generate-id($record-separators[last()])]]"/>
-                               </call-template>
-                       </html:div>
-               </html:article>
+                                                                                                               <for-each select="following-sibling::*[starts-with(., ' ') and not(preceding-sibling::*[generate-id()=generate-id($next)])]">
+                                                                                                                       <variable name="nextline" select="normalize-space(.)"/>
+                                                                                                                       <choose>
+                                                                                                                               <when test="substring($nextline, string-length($nextline))='\' and following-sibling::*[position()=1 and starts-with(., ' ')]">
+                                                                                                                                       <value-of select="substring($nextline, 1, string-length($nextline)-1)"/>
+                                                                                                                               </when>
+                                                                                                                               <otherwise>
+                                                                                                                                       <value-of select="$nextline"/>
+                                                                                                                               </otherwise>
+                                                                                                                       </choose>
+                                                                                                               </for-each>
+                                                                                                       </html:dd>
+                                                                                               </html:div>
+                                                                                       </otherwise>
+                                                                               </choose>
+                                                                       </for-each>
+                                                               </html:dl>
+                                                       </if>
+                                                       <if test=".!='%%'">
+                                                               <comment>
+                                                                       <value-of select="substring-after(., '%%')"/>
+                                                               </comment>
+                                                       </if>
+                                               </for-each>
+                                       </html:footer>
+                               </if>
+                               <html:div class="body">
+                                       <call-template name="LesML:paragraphize">
+                                               <with-param name="lines" select="$doclines[not($record-separators) or preceding-sibling::*[generate-id()=generate-id($record-separators[last()])]]"/>
+                                       </call-template>
+                               </html:div>
+                       </html:article>
+               </if>
+               <if test="$docsep">
+                       <call-template name="LesML:parse">
+                               <with-param name="lines" select="$docsep|$lines[preceding-sibling::*[generate-id()=generate-id($docsep)]]"/>
+                               <with-param name="parent-params" select="$params"/>
+                       </call-template>
+               </if>
        </template>
        <template name="LesML:paragraphize">
                <param name="lines" select="/.."/>
@@ -276,160 +311,165 @@ If a copy of the M·P·L was not distributed with this file, You can obtain one
                                <if test="string($text)!=''">
                                        <variable name="par">
                                                <choose>
-                                                       <when test="starts-with($text, '⁌ ')">
+                                                       <when test="starts-with($text, '⁌') and &sigiled-text;">
                                                                <html:h1>
                                                                        <call-template name="LesML:id-and-contents">
-                                                                               <with-param name="source" select="substring-after($text, '⁌ ')"/>
+                                                                               <with-param name="source" select="&unsigiled-text;"/>
                                                                        </call-template>
                                                                </html:h1>
                                                        </when>
-                                                       <when test="starts-with($text, '§ ')">
+                                                       <when test="starts-with($text, '§') and &sigiled-text;">
                                                                <html:h2>
                                                                        <call-template name="LesML:id-and-contents">
-                                                                               <with-param name="source" select="substring-after($text, '§ ')"/>
+                                                                               <with-param name="source" select="&unsigiled-text;"/>
                                                                        </call-template>
                                                                </html:h2>
                                                        </when>
-                                                       <when test="starts-with($text, '❦ ')">
+                                                       <when test="starts-with($text, '❦') and &sigiled-text;">
                                                                <html:h3>
                                                                        <call-template name="LesML:id-and-contents">
-                                                                               <with-param name="source" select="substring-after($text, '❦ ')"/>
+                                                                               <with-param name="source" select="&unsigiled-text;"/>
                                                                        </call-template>
                                                                </html:h3>
                                                        </when>
-                                                       <when test="starts-with($text, '✠ ')">
+                                                       <when test="starts-with($text, '✠') and &sigiled-text;">
                                                                <html:h4>
                                                                        <call-template name="LesML:id-and-contents">
-                                                                               <with-param name="source" select="substring-after($text, '✠ ')"/>
+                                                                               <with-param name="source" select="&unsigiled-text;"/>
                                                                        </call-template>
                                                                </html:h4>
                                                        </when>
-                                                       <when test="starts-with($text, '• ')">
+                                                       <when test="starts-with($text, '•') and &sigiled-text;">
                                                                <html:li class="unordered" data-level="1">
                                                                        <html:p>
                                                                                <call-template name="LesML:id-and-contents">
-                                                                                       <with-param name="source" select="substring-after($text, '• ')"/>
+                                                                                       <with-param name="source" select="&unsigiled-text;"/>
                                                                                </call-template>
                                                                        </html:p>
                                                                </html:li>
                                                        </when>
-                                                       <when test="starts-with($text, '🔢 ')">
+                                                       <when test="starts-with($text, '🔢') and &sigiled-text;">
                                                                <html:li class="ordered" data-level="1">
                                                                        <html:p>
                                                                                <call-template name="LesML:id-and-contents">
-                                                                                       <with-param name="source" select="substring-after($text, '🔢 ')"/>
+                                                                                       <with-param name="source" select="&unsigiled-text;"/>
                                                                                </call-template>
                                                                        </html:p>
                                                                </html:li>
                                                        </when>
-                                                       <when test="starts-with($text, '◦ ')">
+                                                       <when test="starts-with($text, '◦') and &sigiled-text;">
                                                                <html:li class="unordered" data-level="2">
                                                                        <html:p>
                                                                                <call-template name="LesML:id-and-contents">
-                                                                                       <with-param name="source" select="substring-after($text, '◦ ')"/>
+                                                                                       <with-param name="source" select="&unsigiled-text;"/>
                                                                                </call-template>
                                                                        </html:p>
                                                                </html:li>
                                                        </when>
-                                                       <when test="starts-with($text, '🔠 ')">
+                                                       <when test="starts-with($text, '🔠') and &sigiled-text;">
                                                                <html:li class="ordered" data-level="2">
                                                                        <html:p>
                                                                                <call-template name="LesML:id-and-contents">
-                                                                                       <with-param name="source" select="substring-after($text, '🔠 ')"/>
+                                                                                       <with-param name="source" select="&unsigiled-text;"/>
                                                                                </call-template>
                                                                        </html:p>
                                                                </html:li>
                                                        </when>
-                                                       <when test="starts-with($text, '▪ ')">
+                                                       <when test="starts-with($text, '▪') and &sigiled-text;">
                                                                <html:li class="unordered" data-level="3">
                                                                        <html:p>
                                                                                <call-template name="LesML:id-and-contents">
-                                                                                       <with-param name="source" select="substring-after($text, '▪ ')"/>
+                                                                                       <with-param name="source" select="&unsigiled-text;"/>
                                                                                </call-template>
                                                                        </html:p>
                                                                </html:li>
                                                        </when>
-                                                       <when test="starts-with($text, '🔡 ')">
+                                                       <when test="starts-with($text, '🔡') and &sigiled-text;">
                                                                <html:li class="ordered" data-level="3">
                                                                        <html:p>
                                                                                <call-template name="LesML:id-and-contents">
-                                                                                       <with-param name="source" select="substring-after($text, '🔡 ')"/>
+                                                                                       <with-param name="source" select="&unsigiled-text;"/>
                                                                                </call-template>
                                                                        </html:p>
                                                                </html:li>
                                                        </when>
-                                                       <when test="starts-with($text, '⁃ ')">
+                                                       <when test="starts-with($text, '⁃') and &sigiled-text;">
                                                                <html:li class="unordered" data-level="4">
                                                                        <html:p>
                                                                                <call-template name="LesML:id-and-contents">
-                                                                                       <with-param name="source" select="substring-after($text, '⁃ ')"/>
+                                                                                       <with-param name="source" select="&unsigiled-text;"/>
                                                                                </call-template>
                                                                        </html:p>
                                                                </html:li>
                                                        </when>
-                                                       <when test="starts-with($text, '🔣 ')">
+                                                       <when test="starts-with($text, '🔣') and &sigiled-text;">
                                                                <html:li class="ordered" data-level="4">
                                                                        <html:p>
                                                                                <call-template name="LesML:id-and-contents">
-                                                                                       <with-param name="source" select="substring-after($text, '🔣 ')"/>
+                                                                                       <with-param name="source" select="&unsigiled-text;"/>
                                                                                </call-template>
                                                                        </html:p>
                                                                </html:li>
                                                        </when>
-                                                       <when test="starts-with($text, '🛈 ')">
+                                                       <when test="starts-with($text, '🛈') and &sigiled-text;">
                                                                <html:div role="note" class="info">
                                                                        <html:p>
                                                                                <call-template name="LesML:id-and-contents">
-                                                                                       <with-param name="source" select="substring-after($text, '🛈 ')"/>
+                                                                                       <with-param name="source" select="&unsigiled-text;"/>
                                                                                </call-template>
                                                                        </html:p>
                                                                </html:div>
                                                        </when>
-                                                       <when test="starts-with($text, '⯑ ')">
+                                                       <when test="starts-with($text, '⯑') and &sigiled-text;">
                                                                <html:div role="note" class="query">
                                                                        <html:p>
                                                                                <call-template name="LesML:id-and-contents">
-                                                                                       <with-param name="source" select="substring-after($text, '⯑ ')"/>
+                                                                                       <with-param name="source" select="&unsigiled-text;"/>
                                                                                </call-template>
                                                                        </html:p>
                                                                </html:div>
                                                        </when>
-                                                       <when test="starts-with($text, '⚠︎ ')">
+                                                       <when test="starts-with($text, '⚠︎') and &sigiled-text;">
                                                                <html:div role="note" class="warn">
                                                                        <html:p>
                                                                                <call-template name="LesML:id-and-contents">
-                                                                                       <with-param name="source" select="substring-after($text, '⚠︎ ')"/>
+                                                                                       <with-param name="source" select="&unsigiled-text;"/>
                                                                                </call-template>
                                                                        </html:p>
                                                                </html:div>
                                                        </when>
-                                                       <when test="starts-with($text, '※ ')">
+                                                       <when test="starts-with($text, '※') and &sigiled-text;">
                                                                <html:div role="note" class="note">
                                                                        <html:p>
                                                                                <call-template name="LesML:id-and-contents">
-                                                                                       <with-param name="source" select="substring-after($text, '※ ')"/>
+                                                                                       <with-param name="source" select="&unsigiled-text;"/>
                                                                                </call-template>
                                                                        </html:p>
                                                                </html:div>
                                                        </when>
-                                                       <when test="starts-with($text, '☡ ')">
+                                                       <when test="starts-with($text, '☡') and &sigiled-text;">
                                                                <html:div role="note" class="caution">
                                                                        <html:p>
                                                                                <call-template name="LesML:id-and-contents">
-                                                                                       <with-param name="source" select="substring-after($text, '☡ ')"/>
+                                                                                       <with-param name="source" select="&unsigiled-text;"/>
                                                                                </call-template>
                                                                        </html:p>
                                                                </html:div>
                                                        </when>
-                                                       <when test="starts-with($text, '⋯ ')">
+                                                       <when test="starts-with($text, '⋯') and &sigiled-text;">
                                                                <html:div class="continuation">
                                                                        <html:p>
                                                                                <call-template name="LesML:id-and-contents">
-                                                                                       <with-param name="source" select="substring-after($text, '⋯ ')"/>
+                                                                                       <with-param name="source" select="&unsigiled-text;"/>
                                                                                </call-template>
                                                                        </html:p>
                                                                </html:div>
                                                        </when>
+                                                       <when test="starts-with($text, '#') and &sigiled-text;">
+                                                               <comment>
+                                                                       <value-of select="&unsigiled-text;"/>
+                                                               </comment>
+                                                       </when>
                                                        <otherwise>
                                                                <html:p>
                                                                        <call-template name="LesML:id-and-contents">
This page took 0.100759 seconds and 4 git commands to generate.