]> Lady’s Gitweb - LesML/blob - parser.xslt
e726a9e9953b1f744e346f653c0218b679fcd1e3
[LesML] / parser.xslt
1 <?xml version="1.0"?>
2 <!--
3 SPDX-FileCopyrightText: 2024, 2025 Lady <https://www.ladys.computer/about/#lady>
4 SPDX-License-Identifier: MPL-2.0
5 -->
6 <!--
7 ⁌ 💄📝 Les·M·L ∷ parser.xslt
8
9 © 2024–2025 Lady [@ Ladys Computer]
10
11 This Source Code Form is subject to the terms of the Mozilla Public License, v 2.0.
12 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/>.
13 -->
14 <!DOCTYPE transform [
15 <!ENTITY section-break "*-.=_~·․‥…⁂⋯─━┄┅┈┉╌╍═╴╶╸╺☙❧ ・*-.=_~">
16 <!ENTITY sigiled-text "(string-length($text)=1 or substring($text, 2, 1)=' ')">
17 <!ENTITY unsigiled-text "substring($text, 3, string-length($text)-2)">
18 <!ENTITY xhtml "http://www.w3.org/1999/xhtml">
19 ]>
20 <transform
21 xmlns="http://www.w3.org/1999/XSL/Transform"
22 xmlns:LesML="urn:fdc:ladys.computer:20240512:LesML"
23 xmlns:exsl="http://exslt.org/common"
24 xmlns:exslset="http://exslt.org/sets"
25 xmlns:exslstr="http://exslt.org/strings"
26 xmlns:html="http://www.w3.org/1999/xhtml"
27 xmlns:书社="urn:fdc:ladys.computer:20231231:Shu1She4"
28 exclude-result-prefixes="LesML"
29 extension-element-prefixes="exsl exslstr"
30 version="1.0"
31 >
32 <书社:id>urn:fdc:ladys.computer:20240512:LesML:parser.xslt</书社:id>
33 <template name="LesML:split">
34 <param name="source"/>
35 <param name="separator" select="'&#xA;'"/>
36 <choose>
37 <when test="contains($source, $separator)">
38 <html:span>
39 <value-of select="substring-before($source, $separator)"/>
40 </html:span>
41 <call-template name="LesML:split">
42 <with-param name="source" select="substring-after($source, $separator)"/>
43 <with-param name="separator" select="$separator"/>
44 </call-template>
45 </when>
46 <otherwise>
47 <html:span>
48 <value-of select="$source"/>
49 </html:span>
50 </otherwise>
51 </choose>
52 </template>
53 <template name="LesML:break-and-unescape">
54 <param name="source"/>
55 <variable name="broken-fragment">
56 <call-template name="LesML:split">
57 <with-param name="source" select="$source"/>
58 <with-param name="separator" select="'&#xA;'"/>
59 </call-template>
60 </variable>
61 <variable name="broken" select="exsl:node-set($broken-fragment)/node()"/>
62 <for-each select="$broken">
63 <call-template name="LesML:unescape">
64 <with-param name="source" select="string()"/>
65 </call-template>
66 <if test="position()!=count($broken)">
67 <element name="br" namespace="&xhtml;"/>
68 </if>
69 </for-each>
70 </template>
71 <template name="LesML:unescape">
72 <param name="source"/>
73 <choose>
74 <when test="contains($source, '{U+')">
75 <variable name="after" select="substring-after($source, '{U+')"/>
76 <choose>
77 <when test="contains($after, '}')">
78 <variable name="inner" select="substring-before($after, '}')"/>
79 <variable name="components">
80 <call-template name="LesML:split">
81 <with-param name="source" select="$inner"/>
82 <with-param name="separator" select="'.'"/>
83 </call-template>
84 </variable>
85 <variable name="component-nodes" select="exsl:node-set($components)/node()"/>
86 <value-of select="substring-before($source, '{U+')"/>
87 <choose>
88 <when test="$component-nodes[string(.)='' or translate(., '0123456789ABCDEF', '')!='']">
89 <text>{U+</text>
90 <value-of select="$inner"/>
91 <text>}</text>
92 <call-template name="LesML:unescape">
93 <with-param name="source" select="substring-after($after, '}')"/>
94 </call-template>
95 </when>
96 <otherwise>
97 <for-each select="$component-nodes">
98 <text disable-output-escaping="yes">&amp;#x</text>
99 <value-of select="."/>
100 <text>;</text>
101 </for-each>
102 <call-template name="LesML:unescape">
103 <with-param name="source" select="substring-after($after, '}')"/>
104 </call-template>
105 </otherwise>
106 </choose>
107 </when>
108 <otherwise>
109 <value-of select="substring-before($source, '{U+')"/>
110 <text>{U+</text>
111 <call-template name="LesML:unescape">
112 <with-param name="source" select="$after"/>
113 </call-template>
114 </otherwise>
115 </choose>
116 </when>
117 <otherwise>
118 <value-of select="$source"/>
119 </otherwise>
120 </choose>
121 </template>
122 <template name="LesML:id-and-contents">
123 <param name="source"/>
124 <choose>
125 <when test="starts-with($source, '¶')">
126 <choose>
127 <when test="contains($source, ' ')">
128 <variable name="id" select="substring-before(substring-after($source, '¶'), ' ')"/>
129 <if test="$id!=''">
130 <attribute name="id">
131 <value-of select="$id"/>
132 </attribute>
133 </if>
134 <value-of select="substring-after($source, ' ')"/>
135 </when>
136 <otherwise>
137 <attribute name="id">
138 <value-of select="substring-after($source, '¶')"/>
139 </attribute>
140 </otherwise>
141 </choose>
142 </when>
143 <otherwise>
144 <value-of select="$source"/>
145 </otherwise>
146 </choose>
147 </template>
148 <template name="LesML:parse">
149 <param name="lines" select="/.."/>
150 <param name="parent-params" select="/.."/>
151 <variable name="first-line" select="$lines[1]"/>
152 <variable name="shebang">
153 <if test="starts-with($first-line, '#!lesml')">
154 <value-of select="$first-line"/>
155 </if>
156 </variable>
157 <variable name="params-string">
158 <choose>
159 <when test="starts-with($shebang, '#!lesml@')">
160 <value-of select="substring-after($shebang, '$')"/>
161 </when>
162 <otherwise>
163 <value-of select="substring-after($shebang, '#!lesml')"/>
164 </otherwise>
165 </choose>
166 </variable>
167 <variable name="params-fragment">
168 <choose>
169 <when test="$shebang!=''">
170 <html:dl>
171 <if test="starts-with($shebang, '#!lesml@') and contains($shebang, '$')">
172 <html:div>
173 <html:dt>
174 <text> LANG </text>
175 </html:dt>
176 <html:dd>
177 <value-of select="substring-before(substring-after($shebang, '#!lesml@'), '$')"/>
178 </html:dd>
179 </html:div>
180 </if>
181 <for-each select="exslstr:tokenize($params-string)">
182 <choose>
183 <when test="contains(., '=')">
184 <html:div>
185 <html:dt>
186 <value-of select="substring-before(., '=')"/>
187 </html:dt>
188 <html:dd>
189 <value-of select="substring-after(., '=')"/>
190 </html:dd>
191 </html:div>
192 </when>
193 <otherwise>
194 <html:div>
195 <html:dt>
196 <value-of select="."/>
197 </html:dt>
198 <html:dd/>
199 </html:div>
200 </otherwise>
201 </choose>
202 </for-each>
203 </html:dl>
204 </when>
205 <when test="$parent-params">
206 <copy-of select="$parent-params"/>
207 </when>
208 <otherwise>
209 <html:dl/>
210 </otherwise>
211 </choose>
212 </variable>
213 <variable name="params" select="exsl:node-set($params-fragment)/*"/>
214 <variable name="noshebang" select="$lines[position()>1 or not(starts-with(., '#!lesml') or starts-with(., '##'))]"/>
215 <variable name="docsep" select="$noshebang[starts-with(., '#!lesml') or starts-with(., '##')][1]"/>
216 <variable name="doclines" select="exslset:leading($noshebang, $docsep)"/>
217 <if test="starts-with($first-line, '##') and $first-line!='##'">
218 <comment>
219 <value-of select="substring-after($first-line, '##')"/>
220 </comment>
221 </if>
222 <if test="$shebang!='' or $doclines[normalize-space()!='']">
223 <variable name="record-separators" select="$doclines[starts-with(., '%%')]"/>
224 <element name="article" namespace="&xhtml;">
225 <for-each select="$params/html:div/html:dt[string()=' LANG ']">
226 <attribute name="lang">
227 <value-of select="following-sibling::html:dd"/>
228 </attribute>
229 <attribute name="xml:lang">
230 <value-of select="following-sibling::html:dd"/>
231 </attribute>
232 </for-each>
233 <for-each select="$params/html:div/html:dt[string()='profile']">
234 <attribute name="data-lesml-profile">
235 <value-of select="following-sibling::html:dd"/>
236 </attribute>
237 </for-each>
238 <if test="$record-separators[preceding-sibling::*[normalize-space()!='']]">
239 <element name="footer" namespace="&xhtml;">
240 <attribute name="class">
241 <text>head</text>
242 </attribute>
243 <for-each select="$record-separators">
244 <variable name="position" select="position()"/>
245 <variable name="prev-separator" select="$record-separators[($position)-1]"/>
246 <variable name="fields" select="exslset:leading(exslset:trailing($doclines, $prev-separator), .)"/>
247 <if test="$fields">
248 <element name="dl" namespace="&xhtml;">
249 <for-each select="$fields">
250 <choose>
251 <when test="starts-with(., ' ') and exslset:leading($fields, .)"/>
252 <otherwise>
253 <variable name="next" select="exslset:intersection(following-sibling::*[not(starts-with(., ' '))][1], $fields)"/>
254 <element name="div" namespace="&xhtml;">
255 <element name="dt" namespace="&xhtml;">
256 <value-of select="normalize-space(substring-before(., ':'))"/>
257 </element>
258 <element name="dd" namespace="&xhtml;">
259 <variable name="firstline">
260 <choose>
261 <when test="contains(., ':')">
262 <value-of select="normalize-space(substring-after(., ':'))"/>
263 </when>
264 <otherwise>
265 <value-of select="normalize-space(.)"/>
266 </otherwise>
267 </choose>
268 </variable>
269 <choose>
270 <when test="substring($firstline, string-length($firstline))='\' and following-sibling::*[position()=1 and starts-with(., ' ')]">
271 <value-of select="substring($firstline, 1, string-length($firstline)-1)"/>
272 </when>
273 <otherwise>
274 <value-of select="$firstline"/>
275 </otherwise>
276 </choose>
277 <for-each select="exslset:intersection(following-sibling::*[starts-with(., ' ')], exslset:leading($fields, $next))">
278 <variable name="nextline" select="normalize-space(.)"/>
279 <choose>
280 <when test="substring($nextline, string-length($nextline))='\' and following-sibling::*[position()=1 and starts-with(., ' ')]">
281 <value-of select="substring($nextline, 1, string-length($nextline)-1)"/>
282 </when>
283 <otherwise>
284 <value-of select="$nextline"/>
285 </otherwise>
286 </choose>
287 </for-each>
288 </element>
289 </element>
290 </otherwise>
291 </choose>
292 </for-each>
293 </element>
294 </if>
295 <if test=".!='%%'">
296 <comment>
297 <value-of select="substring-after(., '%%')"/>
298 </comment>
299 </if>
300 </for-each>
301 </element>
302 </if>
303 <element name="div" namespace="&xhtml;">
304 <attribute name="class">
305 <text>body</text>
306 </attribute>
307 <call-template name="LesML:paragraphize">
308 <with-param name="lines" select="exslset:trailing($doclines, $record-separators[last()])"/>
309 </call-template>
310 </element>
311 </element>
312 </if>
313 <if test="$docsep">
314 <call-template name="LesML:parse">
315 <with-param name="lines" select="$docsep|exslset:trailing($lines, $docsep)"/>
316 <with-param name="parent-params" select="$params"/>
317 </call-template>
318 </if>
319 </template>
320 <template name="LesML:paragraphize">
321 <param name="lines" select="/.."/>
322 <variable name="last-lines" select="$lines[normalize-space()!='' and normalize-space(following-sibling::*[1])='']|$lines[last()]"/>
323 <variable name="blocked">
324 <for-each select="$last-lines">
325 <variable name="position" select="position()"/>
326 <variable name="prev-last" select="$last-lines[($position)-1]"/>
327 <variable name="linespans" select="(exslset:intersection(exslset:trailing($lines, $prev-last), exslset:leading($lines, .))|.)[normalize-space()!='']"/>
328 <variable name="quoted" select="not($linespans[not(starts-with(., ' ') or starts-with(., '&#x9;'))])"/>
329 <variable name="preformatted" select="not($linespans[not(starts-with(normalize-space(), '|'))])"/>
330 <variable name="text">
331 <for-each select="$linespans">
332 <choose>
333 <when test="$preformatted">
334 <value-of select="substring-after(., '|')"/>
335 </when>
336 <otherwise>
337 <value-of select="normalize-space()"/>
338 </otherwise>
339 </choose>
340 <if test="position()!=count($linespans)">
341 <choose>
342 <when test="$preformatted">
343 <text>&#xA;</text>
344 </when>
345 <otherwise>
346 <text> </text>
347 </otherwise>
348 </choose>
349 </if>
350 </for-each>
351 </variable>
352 <if test="string($text)!=''">
353 <variable name="par">
354 <choose>
355 <when test="$preformatted">
356 <element name="pre" namespace="&xhtml;">
357 <call-template name="LesML:id-and-contents">
358 <with-param name="source" select="$text"/>
359 </call-template>
360 </element>
361 </when>
362 <when test="starts-with($text, '⁌') and &sigiled-text;">
363 <element name="h1" namespace="&xhtml;">
364 <call-template name="LesML:id-and-contents">
365 <with-param name="source" select="&unsigiled-text;"/>
366 </call-template>
367 </element>
368 </when>
369 <when test="starts-with($text, '§') and &sigiled-text;">
370 <element name="h2" namespace="&xhtml;">
371 <call-template name="LesML:id-and-contents">
372 <with-param name="source" select="&unsigiled-text;"/>
373 </call-template>
374 </element>
375 </when>
376 <when test="starts-with($text, '❦') and &sigiled-text;">
377 <element name="h3" namespace="&xhtml;">
378 <call-template name="LesML:id-and-contents">
379 <with-param name="source" select="&unsigiled-text;"/>
380 </call-template>
381 </element>
382 </when>
383 <when test="starts-with($text, '✠') and &sigiled-text;">
384 <element name="h4" namespace="&xhtml;">
385 <call-template name="LesML:id-and-contents">
386 <with-param name="source" select="&unsigiled-text;"/>
387 </call-template>
388 </element>
389 </when>
390 <when test="starts-with($text, '•') and &sigiled-text;">
391 <element name="li" namespace="&xhtml;">
392 <attribute name="class">
393 <text>unordered</text>
394 </attribute>
395 <attribute name="data-level">
396 <text>1</text>
397 </attribute>
398 <element name="p" namespace="&xhtml;">
399 <call-template name="LesML:id-and-contents">
400 <with-param name="source" select="&unsigiled-text;"/>
401 </call-template>
402 </element>
403 </element>
404 </when>
405 <when test="starts-with($text, '🔢') and &sigiled-text;">
406 <element name="li" namespace="&xhtml;">
407 <attribute name="class">
408 <text>ordered</text>
409 </attribute>
410 <attribute name="data-level">
411 <text>1</text>
412 </attribute>
413 <element name="p" namespace="&xhtml;">
414 <call-template name="LesML:id-and-contents">
415 <with-param name="source" select="&unsigiled-text;"/>
416 </call-template>
417 </element>
418 </element>
419 </when>
420 <when test="starts-with($text, '◦') and &sigiled-text;">
421 <element name="li" namespace="&xhtml;">
422 <attribute name="class">
423 <text>unordered</text>
424 </attribute>
425 <attribute name="data-level">
426 <text>2</text>
427 </attribute>
428 <element name="p" namespace="&xhtml;">
429 <call-template name="LesML:id-and-contents">
430 <with-param name="source" select="&unsigiled-text;"/>
431 </call-template>
432 </element>
433 </element>
434 </when>
435 <when test="starts-with($text, '🔠') and &sigiled-text;">
436 <element name="li" namespace="&xhtml;">
437 <attribute name="class">
438 <text>ordered</text>
439 </attribute>
440 <attribute name="data-level">
441 <text>2</text>
442 </attribute>
443 <element name="p" namespace="&xhtml;">
444 <call-template name="LesML:id-and-contents">
445 <with-param name="source" select="&unsigiled-text;"/>
446 </call-template>
447 </element>
448 </element>
449 </when>
450 <when test="starts-with($text, '▪') and &sigiled-text;">
451 <element name="li" namespace="&xhtml;">
452 <attribute name="class">
453 <text>unordered</text>
454 </attribute>
455 <attribute name="data-level">
456 <text>3</text>
457 </attribute>
458 <element name="p" namespace="&xhtml;">
459 <call-template name="LesML:id-and-contents">
460 <with-param name="source" select="&unsigiled-text;"/>
461 </call-template>
462 </element>
463 </element>
464 </when>
465 <when test="starts-with($text, '🔡') and &sigiled-text;">
466 <element name="li" namespace="&xhtml;">
467 <attribute name="class">
468 <text>ordered</text>
469 </attribute>
470 <attribute name="data-level">
471 <text>3</text>
472 </attribute>
473 <element name="p" namespace="&xhtml;">
474 <call-template name="LesML:id-and-contents">
475 <with-param name="source" select="&unsigiled-text;"/>
476 </call-template>
477 </element>
478 </element>
479 </when>
480 <when test="starts-with($text, '⁃') and &sigiled-text;">
481 <element name="li" namespace="&xhtml;">
482 <attribute name="class">
483 <text>unordered</text>
484 </attribute>
485 <attribute name="data-level">
486 <text>4</text>
487 </attribute>
488 <element name="p" namespace="&xhtml;">
489 <call-template name="LesML:id-and-contents">
490 <with-param name="source" select="&unsigiled-text;"/>
491 </call-template>
492 </element>
493 </element>
494 </when>
495 <when test="starts-with($text, '🔣') and &sigiled-text;">
496 <element name="li" namespace="&xhtml;">
497 <attribute name="class">
498 <text>ordered</text>
499 </attribute>
500 <attribute name="data-level">
501 <text>4</text>
502 </attribute>
503 <element name="p" namespace="&xhtml;">
504 <call-template name="LesML:id-and-contents">
505 <with-param name="source" select="&unsigiled-text;"/>
506 </call-template>
507 </element>
508 </element>
509 </when>
510 <when test="starts-with($text, '🛈') and &sigiled-text;">
511 <element name="div" namespace="&xhtml;">
512 <attribute name="role">
513 <text>note</text>
514 </attribute>
515 <attribute name="class">
516 <text>info</text>
517 </attribute>
518 <element name="p" namespace="&xhtml;">
519 <call-template name="LesML:id-and-contents">
520 <with-param name="source" select="&unsigiled-text;"/>
521 </call-template>
522 </element>
523 </element>
524 </when>
525 <when test="starts-with($text, '⯑') and &sigiled-text;">
526 <element name="div" namespace="&xhtml;">
527 <attribute name="role">
528 <text>note</text>
529 </attribute>
530 <attribute name="class">
531 <text>query</text>
532 </attribute>
533 <element name="p" namespace="&xhtml;">
534 <call-template name="LesML:id-and-contents">
535 <with-param name="source" select="&unsigiled-text;"/>
536 </call-template>
537 </element>
538 </element>
539 </when>
540 <when test="starts-with($text, '⚠︎') and &sigiled-text;">
541 <element name="div" namespace="&xhtml;">
542 <attribute name="role">
543 <text>note</text>
544 </attribute>
545 <attribute name="class">
546 <text>warn</text>
547 </attribute>
548 <element name="p" namespace="&xhtml;">
549 <call-template name="LesML:id-and-contents">
550 <with-param name="source" select="&unsigiled-text;"/>
551 </call-template>
552 </element>
553 </element>
554 </when>
555 <when test="starts-with($text, '※') and &sigiled-text;">
556 <element name="div" namespace="&xhtml;">
557 <attribute name="role">
558 <text>note</text>
559 </attribute>
560 <attribute name="class">
561 <text>note</text>
562 </attribute>
563 <element name="p" namespace="&xhtml;">
564 <call-template name="LesML:id-and-contents">
565 <with-param name="source" select="&unsigiled-text;"/>
566 </call-template>
567 </element>
568 </element>
569 </when>
570 <when test="starts-with($text, '☡') and &sigiled-text;">
571 <element name="div" namespace="&xhtml;">
572 <attribute name="role">
573 <text>note</text>
574 </attribute>
575 <attribute name="class">
576 <text>caution</text>
577 </attribute>
578 <element name="p" namespace="&xhtml;">
579 <call-template name="LesML:id-and-contents">
580 <with-param name="source" select="&unsigiled-text;"/>
581 </call-template>
582 </element>
583 </element>
584 </when>
585 <when test="starts-with($text, '⋯') and &sigiled-text;">
586 <element name="div" namespace="&xhtml;">
587 <attribute name="class">
588 <text>continuation</text>
589 </attribute>
590 <element name="p" namespace="&xhtml;">
591 <call-template name="LesML:id-and-contents">
592 <with-param name="source" select="&unsigiled-text;"/>
593 </call-template>
594 </element>
595 </element>
596 </when>
597 <when test="starts-with($text, '#') and &sigiled-text;">
598 <comment>
599 <value-of select="&unsigiled-text;"/>
600 </comment>
601 </when>
602 <otherwise>
603 <element name="p" namespace="&xhtml;">
604 <call-template name="LesML:id-and-contents">
605 <with-param name="source" select="$text"/>
606 </call-template>
607 </element>
608 </otherwise>
609 </choose>
610 </variable>
611 <choose>
612 <when test="translate(string($text), '&section-break; ', '')=''">
613 <element name="hr" namespace="&xhtml;"/>
614 </when>
615 <when test="$quoted">
616 <element name="blockquote" namespace="&xhtml;">
617 <copy-of select="$par"/>
618 </element>
619 </when>
620 <otherwise>
621 <copy-of select="$par"/>
622 </otherwise>
623 </choose>
624 </if>
625 </for-each>
626 </variable>
627 <variable name="inlined">
628 <apply-templates select="exsl:node-set($blocked)/node()" mode="LesML:linkify"/>
629 </variable>
630 <apply-templates select="exsl:node-set($inlined)/node()" mode="LesML:finalize-tree"/>
631 </template>
632 <template match="html:script[@type='text/lesml']">
633 <variable name="lines-fragment">
634 <call-template name="LesML:split">
635 <with-param name="source">
636 <for-each select=".//text()">
637 <value-of select="."/>
638 </for-each>
639 </with-param>
640 </call-template>
641 </variable>
642 <call-template name="LesML:parse">
643 <with-param name="lines" select="exsl:node-set($lines-fragment)/*"/>
644 </call-template>
645 </template>
646 <template match="html:blockquote" mode="LesML:finalize-tree">
647 <if test="not(preceding-sibling::node()) or preceding-sibling::node()[position()=1 and not(self::html:blockquote)]">
648 <variable name="notquote" select="following-sibling::node()[not(self::html:blockquote)][1]"/>
649 <variable name="contents">
650 <copy-of select="node()"/>
651 <for-each select="exslset:leading(following-sibling::node(), $notquote)">
652 <copy-of select="node()"/>
653 </for-each>
654 </variable>
655 <variable name="content-nodes" select="exsl:node-set($contents)/node()"/>
656 <variable name="laststarttext" select="$content-nodes[last()]/self::html:p[not(@class) and not(@role)]/node()[self::text() or self::*][position()=1 and self::text()]"/>
657 <choose>
658 <when test="starts-with($laststarttext, '— ')">
659 <variable name="caption">
660 <copy-of select="$laststarttext/preceding-sibling::node()"/>
661 <value-of select="substring-after($laststarttext, '— ')"/>
662 <copy-of select="$laststarttext/following-sibling::node()"/>
663 </variable>
664 <element name="figure" namespace="&xhtml;">
665 <copy>
666 <apply-templates select="@*|$content-nodes[position()!=last()]" mode="LesML:finalize-tree"/>
667 </copy>
668 <element name="figcaption" namespace="&xhtml;">
669 <for-each select="$content-nodes[last()]">
670 <copy>
671 <apply-templates select="@*|exsl:node-set($caption)/node()" mode="LesML:finalize-tree"/>
672 </copy>
673 </for-each>
674 </element>
675 </element>
676 </when>
677 <otherwise>
678 <copy>
679 <apply-templates select="@*|$content-nodes" mode="LesML:finalize-tree"/>
680 </copy>
681 </otherwise>
682 </choose>
683 </if>
684 </template>
685 <template match="html:div" mode="LesML:finalize-tree">
686 <if test="not(@class='continuation') or not(preceding-sibling::node()) or preceding-sibling::node()[position()=1 and not(self::html:div or self::html:li)]">
687 <variable name="notcontinuation" select="following-sibling::node()[not(self::html:div and @class='continuation')][1]"/>
688 <copy>
689 <apply-templates select="@*|node()" mode="LesML:finalize-tree"/>
690 <for-each select="exslset:leading(following-sibling::node(), $notcontinuation)">
691 <apply-templates select="node()" mode="LesML:finalize-tree"/>
692 </for-each>
693 </copy>
694 </if>
695 </template>
696 <template match="html:li" mode="LesML:finalize-tree">
697 <if test="not(preceding-sibling::node()) or preceding-sibling::node()[not(preceding-sibling::* and self::html:div and @class='continuation')][position()=1 and not(self::html:li)]">
698 <apply-templates select="." mode="LesML:finalize-list"/>
699 </if>
700 </template>
701 <template match="html:li" mode="LesML:finalize-list">
702 <param name="parent-level" select="0"/>
703 <variable name="current-class" select="string(@class)"/>
704 <variable name="current-level" select="number(@data-level)"/>
705 <variable name="wrapper">
706 <choose>
707 <when test="@class='ordered'">
708 <text>ol</text>
709 </when>
710 <otherwise>
711 <text>ul</text>
712 </otherwise>
713 </choose>
714 </variable>
715 <variable name="notinlist" select="following-sibling::node()[not(self::html:div and @class='continuation' or self::html:li and (@data-level>$current-level or @data-level=$current-level and @class=$current-class))][1]"/>
716 <element name="{$wrapper}" namespace="&xhtml;">
717 <for-each select=".|exslset:leading(following-sibling::node(), $notinlist)[self::html:li and @data-level=$current-level]">
718 <variable name="notcontinuation" select="following-sibling::node()[not(self::html:div and @class='continuation')][1]"/>
719 <copy>
720 <apply-templates select="@*|node()" mode="LesML:finalize-tree"/>
721 <for-each select="exslset:leading(following-sibling::node(), $notcontinuation)">
722 <apply-templates select="node()" mode="LesML:finalize-tree"/>
723 </for-each>
724 <if test="$notcontinuation/self::html:li[@data-level>$current-level]">
725 <apply-templates select="$notcontinuation" mode="LesML:finalize-list">
726 <with-param name="parent-level" select="$current-level"/>
727 </apply-templates>
728 </if>
729 </copy>
730 </for-each>
731 </element>
732 <if test="$notinlist/self::html:li[@data-level>$parent-level]">
733 <apply-templates select="$notinlist" mode="LesML:finalize-list">
734 <with-param name="parent-level" select="$parent-level"/>
735 </apply-templates>
736 </if>
737 </template>
738 <template match="processing-instruction()[local-name()='LesML-Link-Escape']" mode="LesML:finalize-tree">
739 <text>🔗</text>
740 </template>
741 <template match="text()" mode="LesML:finalize-tree">
742 <call-template name="LesML:break-and-unescape">
743 <with-param name="source" select="string(.)"/>
744 </call-template>
745 </template>
746 <template match="@*|node()" mode="LesML:finalize-tree" priority="-1">
747 <copy>
748 <apply-templates select="@*|node()" mode="LesML:finalize-tree"/>
749 </copy>
750 </template>
751 <template match="node()" mode="LesML:inline">
752 <param name="element-name"/>
753 <param name="element-namespace" select="'http://www.w3.org/1999/xhtml'"/>
754 <param name="start-sigil"/>
755 <param name="end-sigil"/>
756 <param name="role"/>
757 <param name="langtag-supported" select="false()"/>
758 <choose>
759 <when test="self::*">
760 <variable name="end-node" select="text()[contains(., $end-sigil)][1]"/>
761 <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))"/>
762 <choose>
763 <when test="$end-node and $has-start-node">
764 <variable name="preceding">
765 <copy-of select="$end-node/preceding-sibling::node()"/>
766 <value-of select="substring-before($end-node, $end-sigil)"/>
767 </variable>
768 <variable name="start-node" select="exsl:node-set($preceding)/text()[contains(., $start-sigil)][last()]"/>
769 <variable name="restoftext" select="substring-after($end-node, $end-sigil)"/>
770 <variable name="maybe-langtag">
771 <if test="$langtag-supported and starts-with($restoftext, '@') and contains($restoftext, '$')">
772 <value-of select="substring-before(substring-after($restoftext, '@'), '$')"/>
773 </if>
774 </variable>
775 <variable name="langtag">
776 <if test="translate($maybe-langtag, '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-', '')=''">
777 <value-of select="$maybe-langtag"/>
778 </if>
779 </variable>
780 <variable name="start-tokens-fragment">
781 <call-template name="LesML:split">
782 <with-param name="source" select="string($start-node)"/>
783 <with-param name="separator" select="$start-sigil"/>
784 </call-template>
785 </variable>
786 <variable name="start-tokens" select="exsl:node-set($start-tokens-fragment)/*"/>
787 <variable name="wrapped">
788 <copy>
789 <copy-of select="@*"/>
790 <copy-of select="$start-node/preceding-sibling::node()"/>
791 <for-each select="$start-tokens[position()!=last()]">
792 <value-of select="."/>
793 <if test="position()!=last()">
794 <value-of select="$start-sigil"/>
795 </if>
796 </for-each>
797 <element name="{$element-name}" namespace="{$element-namespace}">
798 <if test="string($role)!=''">
799 <attribute name="role">
800 <value-of select="$role"/>
801 </attribute>
802 </if>
803 <if test="string($langtag)!=''">
804 <if test="$element-namespace='http://www.w3.org/1999/xhtml'">
805 <attribute name="lang">
806 <value-of select="$langtag"/>
807 </attribute>
808 </if>
809 <attribute name="xml:lang">
810 <value-of select="$langtag"/>
811 </attribute>
812 </if>
813 <value-of select="$start-tokens[last()]"/>
814 <copy-of select="$start-node/following-sibling::node()"/>
815 </element>
816 <choose>
817 <when test="string($langtag)!=''">
818 <value-of select="substring-after($restoftext, '$')"/>
819 </when>
820 <otherwise>
821 <value-of select="$restoftext"/>
822 </otherwise>
823 </choose>
824 <copy-of select="$end-node/following-sibling::node()"/>
825 </copy>
826 </variable>
827 <apply-templates select="exsl:node-set($wrapped)/*" mode="LesML:inline">
828 <with-param name="element-name" select="$element-name"/>
829 <with-param name="element-namespace" select="$element-namespace"/>
830 <with-param name="start-sigil" select="$start-sigil"/>
831 <with-param name="end-sigil" select="$end-sigil"/>
832 <with-param name="role" select="$role"/>
833 <with-param name="langtag-supported" select="$langtag-supported"/>
834 </apply-templates>
835 </when>
836 <otherwise>
837 <copy>
838 <copy-of select="@*"/>
839 <apply-templates select="node()" mode="LesML:inline">
840 <with-param name="element-name" select="$element-name"/>
841 <with-param name="element-namespace" select="$element-namespace"/>
842 <with-param name="start-sigil" select="$start-sigil"/>
843 <with-param name="end-sigil" select="$end-sigil"/>
844 <with-param name="role" select="$role"/>
845 <with-param name="langtag-supported" select="$langtag-supported"/>
846 </apply-templates>
847 </copy>
848 </otherwise>
849 </choose>
850 </when>
851 <otherwise>
852 <copy-of select="."/>
853 </otherwise>
854 </choose>
855 </template>
856 <template match="node()" mode="LesML:linkify">
857 <variable name="result">
858 <choose>
859 <when test="self::*">
860 <variable name="end-node" select="text()[contains(., '>}')][1]"/>
861 <variable name="has-start-node" select="$end-node/preceding-sibling::text()[contains(., '{🔗') and not(following-sibling::*)] or string-length(substring-after($end-node, '{🔗'))>string-length(substring-after($end-node, '>}'))"/>
862 <choose>
863 <when test="$end-node and $has-start-node">
864 <variable name="preceding">
865 <copy-of select="$end-node/preceding-sibling::node()"/>
866 <value-of select="substring-before($end-node, '>}')"/>
867 </variable>
868 <variable name="start-node" select="exsl:node-set($preceding)/text()[contains(., '{🔗') and not(following-sibling::*)][last()]"/>
869 <variable name="start-tokens-fragment">
870 <call-template name="LesML:split">
871 <with-param name="source" select="string($start-node)"/>
872 <with-param name="separator" select="'{🔗'"/>
873 </call-template>
874 </variable>
875 <variable name="start-tokens" select="exsl:node-set($start-tokens-fragment)/*"/>
876 <variable name="hyperlink">
877 <value-of select="$start-tokens[last()]"/>
878 <for-each select="$start-node/following-sibling::node()">
879 <choose>
880 <when test="self::text()">
881 <value-of select="."/>
882 </when>
883 <when test="self::processing-instruction()[local-name()='LesML-Link-Escape']">
884 <text>🔗</text>
885 </when>
886 </choose>
887 </for-each>
888 </variable>
889 <choose>
890 <when test="contains($hyperlink, '&lt;')">
891 <variable name="ltcomponents-fragment">
892 <call-template name="LesML:split">
893 <with-param name="source" select="$hyperlink"/>
894 <with-param name="separator" select="'&lt;'"/>
895 </call-template>
896 </variable>
897 <variable name="ltcomponents" select="exsl:node-set($ltcomponents-fragment)/*"/>
898 <variable name="wrapped">
899 <copy>
900 <copy-of select="@*"/>
901 <copy-of select="$start-node/preceding-sibling::node()"/>
902 <for-each select="$start-tokens[position()!=last()]">
903 <value-of select="."/>
904 <if test="position()!=last()">
905 <text>{🔗</text>
906 </if>
907 </for-each>
908 <element name="a" namespace="&xhtml;">
909 <attribute name="href">
910 <value-of select="$ltcomponents[last()]"/>
911 </attribute>
912 <choose>
913 <when test="count($ltcomponents)>2 or normalize-space($ltcomponents[1])!=''">
914 <for-each select="$ltcomponents[position()!=last()]">
915 <value-of select="."/>
916 <if test="position()!=last()">
917 <text>&lt;</text>
918 </if>
919 </for-each>
920 </when>
921 <otherwise>
922 <value-of select="$ltcomponents[last()]"/>
923 </otherwise>
924 </choose>
925 </element>
926 <value-of select="substring-after($end-node, '>}')"/>
927 <copy-of select="$end-node/following-sibling::node()"/>
928 </copy>
929 </variable>
930 <apply-templates select="exsl:node-set($wrapped)/*" mode="LesML:linkify"/>
931 </when>
932 <otherwise>
933 <variable name="escaped">
934 <copy>
935 <copy-of select="@*"/>
936 <copy-of select="$start-node/preceding-sibling::node()"/>
937 <for-each select="$start-tokens[position()!=last()]">
938 <value-of select="."/>
939 <if test="position()!=last()">
940 <text>{🔗</text>
941 </if>
942 </for-each>
943 <text>{</text>
944 <processing-instruction name="LesML-Link-Escape"/>
945 <copy-of select="$hyperlink"/>
946 <text>>}</text>
947 <value-of select="substring-after($end-node, '>}')"/>
948 <copy-of select="$end-node/following-sibling::node()"/>
949 </copy>
950 </variable>
951 <apply-templates select="exsl:node-set($escaped)/*" mode="LesML:linkify"/>
952 </otherwise>
953 </choose>
954 </when>
955 <otherwise>
956 <copy>
957 <copy-of select="@*"/>
958 <apply-templates select="node()" mode="LesML:linkify"/>
959 </copy>
960 </otherwise>
961 </choose>
962 </when>
963 <otherwise>
964 <copy-of select="."/>
965 </otherwise>
966 </choose>
967 </variable>
968 <apply-templates select="exsl:node-set($result)/node()" mode="LesML:strikethrough"/>
969 </template>
970 <template match="node()" mode="LesML:strikethrough">
971 <variable name="result">
972 <apply-templates select="." mode="LesML:inline">
973 <with-param name="element-name" select="'s'"/>
974 <with-param name="element-namespace" select="'&xhtml;'"/>
975 <with-param name="start-sigil" select="'⸠'"/>
976 <with-param name="end-sigil" select="'⸡'"/>
977 </apply-templates>
978 </variable>
979 <apply-templates select="exsl:node-set($result)/node()" mode="LesML:underline"/>
980 </template>
981 <template match="node()" mode="LesML:underline">
982 <variable name="result">
983 <apply-templates select="." mode="LesML:inline">
984 <with-param name="element-name" select="'u'"/>
985 <with-param name="element-namespace" select="'&xhtml;'"/>
986 <with-param name="start-sigil" select="'⸤'"/>
987 <with-param name="end-sigil" select="'⸥'"/>
988 </apply-templates>
989 </variable>
990 <apply-templates select="exsl:node-set($result)/node()" mode="LesML:noted"/>
991 </template>
992 <template match="node()" mode="LesML:noted">
993 <variable name="result">
994 <apply-templates select="." mode="LesML:inline">
995 <with-param name="element-name" select="'small'"/>
996 <with-param name="element-namespace" select="'&xhtml;'"/>
997 <with-param name="start-sigil" select="'⟦'"/>
998 <with-param name="end-sigil" select="'⟧'"/>
999 <with-param name="role" select="'note'"/>
1000 </apply-templates>
1001 </variable>
1002 <apply-templates select="exsl:node-set($result)/node()" mode="LesML:parenthetical"/>
1003 </template>
1004 <template match="node()" mode="LesML:parenthetical">
1005 <variable name="result">
1006 <apply-templates select="." mode="LesML:inline">
1007 <with-param name="element-name" select="'small'"/>
1008 <with-param name="element-namespace" select="'&xhtml;'"/>
1009 <with-param name="start-sigil" select="'⸨'"/>
1010 <with-param name="end-sigil" select="'⸩'"/>
1011 </apply-templates>
1012 </variable>
1013 <apply-templates select="exsl:node-set($result)/node()" mode="LesML:code"/>
1014 </template>
1015 <template match="node()" mode="LesML:code">
1016 <variable name="result">
1017 <apply-templates select="." mode="LesML:inline">
1018 <with-param name="element-name" select="'code'"/>
1019 <with-param name="element-namespace" select="'&xhtml;'"/>
1020 <with-param name="start-sigil" select="'`'"/>
1021 <with-param name="end-sigil" select="'´'"/>
1022 </apply-templates>
1023 </variable>
1024 <apply-templates select="exsl:node-set($result)/node()" mode="LesML:titled"/>
1025 </template>
1026 <template match="node()" mode="LesML:titled">
1027 <variable name="result">
1028 <apply-templates select="." mode="LesML:inline">
1029 <with-param name="element-name" select="'cite'"/>
1030 <with-param name="element-namespace" select="'&xhtml;'"/>
1031 <with-param name="start-sigil" select="'⟪'"/>
1032 <with-param name="end-sigil" select="'⟫'"/>
1033 </apply-templates>
1034 </variable>
1035 <apply-templates select="exsl:node-set($result)/node()" mode="LesML:offset"/>
1036 </template>
1037 <template match="node()" mode="LesML:offset">
1038 <variable name="result">
1039 <apply-templates select="." mode="LesML:inline">
1040 <with-param name="element-name" select="'i'"/>
1041 <with-param name="element-namespace" select="'&xhtml;'"/>
1042 <with-param name="start-sigil" select="'⟨'"/>
1043 <with-param name="end-sigil" select="'⟩'"/>
1044 <with-param name="langtag-supported" select="true()"/>
1045 </apply-templates>
1046 </variable>
1047 <apply-templates select="exsl:node-set($result)/node()" mode="LesML:bolded"/>
1048 </template>
1049 <template match="node()" mode="LesML:bolded">
1050 <variable name="result">
1051 <apply-templates select="." mode="LesML:inline">
1052 <with-param name="element-name" select="'b'"/>
1053 <with-param name="element-namespace" select="'&xhtml;'"/>
1054 <with-param name="start-sigil" select="'⦃'"/>
1055 <with-param name="end-sigil" select="'⦄'"/>
1056 </apply-templates>
1057 </variable>
1058 <apply-templates select="exsl:node-set($result)/node()" mode="LesML:important"/>
1059 </template>
1060 <template match="node()" mode="LesML:important">
1061 <variable name="result">
1062 <apply-templates select="." mode="LesML:inline">
1063 <with-param name="element-name" select="'strong'"/>
1064 <with-param name="element-namespace" select="'&xhtml;'"/>
1065 <with-param name="start-sigil" select="'☞'"/>
1066 <with-param name="end-sigil" select="'☜'"/>
1067 </apply-templates>
1068 </variable>
1069 <apply-templates select="exsl:node-set($result)/node()" mode="LesML:emphasized"/>
1070 </template>
1071 <template match="node()" mode="LesML:emphasized">
1072 <apply-templates select="." mode="LesML:inline">
1073 <with-param name="element-name" select="'em'"/>
1074 <with-param name="element-namespace" select="'&xhtml;'"/>
1075 <with-param name="start-sigil" select="'⹐'"/>
1076 <with-param name="end-sigil" select="'⹑'"/>
1077 </apply-templates>
1078 </template>
1079 </transform>
This page took 0.102923 seconds and 3 git commands to generate.