-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathmeiLint.xsl
687 lines (417 loc) · 19.8 KB
/
meiLint.xsl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:math="http://www.w3.org/2005/xpath-functions/math" xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl" xmlns:mei="http://www.music-encoding.org/ns/mei" xmlns="http://www.music-encoding.org/ns/mei" exclude-result-prefixes="xs math xd" version="3.0">
<xd:doc scope="stylesheet">
<xd:desc>
<xd:p><xd:b>Created on:</xd:b> 2023–2025</xd:p>
<xd:p><xd:b>Author:</xd:b> Benjamin W. Bohl</xd:p>
<xd:p>This XSLT is intended for linting Music Encoding Initiative flavoured XML files.</xd:p>
</xd:desc>
</xd:doc>
<xd:doc scope="component">
<xd:desc>
<xd:p>The output format definition.</xd:p>
</xd:desc>
</xd:doc>
<xsl:output media-type="text/xml" method="xml" indent="false" encoding="UTF-8" omit-xml-declaration="no"/>
<!-- ======================================================================= -->
<!-- PARAMETERS -->
<!-- ======================================================================= -->
<xd:doc scope="component">
<xd:desc>
<xd:p>A static parameter to define the output mode.</xd:p>
<xd:p>Options are:</xd:p>
<xd:ul>
<xd:li><xd:b>documentation</xd:b> this will return the original file with added documentation of applying meiLint.xsl.</xd:li>
<xd:li><xd:b>clean</xd:b> this will return the file as single-line, stripped from linebreaks and indentation whitespace.</xd:li>
<xd:li><xd:b>lint</xd:b> this will first clean (as by the above option) and afterwards indent the file.</xd:li>
</xd:ul>
</xd:desc>
</xd:doc>
<xsl:param name="output-mode" static="yes" select="'lint'" as="xs:string"/>
<xd:doc>
<xd:desc>
<xd:p>A character sequence used for indentation</xd:p>
</xd:desc>
</xd:doc>
<xsl:param name="indentation-characters" select="' '" as="xs:string"/>
<xd:doc scope="component">
<xd:desc>
<xd:p>Sequence of element local-names that should be preceded by a newline character and indentation.</xd:p>
</xd:desc>
</xd:doc>
<xsl:param name="elements.break-before" select="('addrLine', 'lb', 'mei', 'meiHead', 'pb')" as="xs:string*"/>
<xd:doc scope="component">
<xd:desc>
<xd:p>Sequence od element local-names that should never be preceded by a newline character and indentation.</xd:p>
</xd:desc>
</xd:doc>
<xsl:param name="elements.no-break" select="('rend')" as="xs:string*"/>
<xd:doc scope="component">
<xd:desc>
<xd:p>Sequence of element local-names, the contents of which should not be indented.</xd:p>
</xd:desc>
</xd:doc>
<xsl:param name="elements.do-not-break-contents" select="('addrLine', 'desc', 'dir', 'head', 'p', 'rend', 'title')" as="xs:string*"/>
<!-- ======================================================================= -->
<!-- GLOBAL VARIABLES -->
<!-- ======================================================================= -->
<xd:doc scope="component">
<xd:desc>URL of this XSLT</xd:desc>
</xd:doc>
<xsl:variable name="linter-url" as="xs:anyURI">https://github.com/music-encoding/encoding-tools/blob/main/meiLint/meiLint.xsl</xsl:variable>
<xd:doc scope="component">
<xd:desc>Version of this XSLT</xd:desc>
</xd:doc>
<xsl:variable name="linter-version" as="xs:string">v1.0.0</xsl:variable>
<xd:doc scope="component">
<xd:desc>Linter ID</xd:desc>
</xd:doc>
<xsl:variable name="linter-id" select="xs:ID('meiLint_' || $linter-version)" as="xs:ID"/>
<xd:doc scope="component">
<xd:desc>
<xd:p>Variable with new line character.</xd:p>
</xd:desc>
</xd:doc>
<xsl:variable name="nl" as="item()">
<xsl:text>
</xsl:text>
</xsl:variable>
<!-- ======================================================================= -->
<!-- MAIN OUTPUT TEMPLATE -->
<!-- ======================================================================= -->
<xd:doc scope="component">
<xd:desc>
<xd:p>The root template. Creates documentation, clean, and lint variables. Returns one of these, depending on the value of the 'output-mode' parameter.</xd:p>
</xd:desc>
</xd:doc>
<xsl:template match="/">
<xsl:variable name="documentation" as="node()+">
<xsl:apply-templates select="node() | @*" mode="documentation"/>
</xsl:variable>
<xsl:variable name="clean" as="node()+">
<xsl:apply-templates select="$documentation" mode="clean"/>
</xsl:variable>
<xsl:variable name="lint" use-when="$output-mode = 'lint'" as="node()+">
<xsl:apply-templates select="$clean" mode="lint"/>
</xsl:variable>
<xsl:copy-of select="$documentation" use-when="$output-mode = 'documentation'"/>
<xsl:copy-of select="$clean" use-when="$output-mode = 'clean'"/>
<xsl:copy-of select="$lint" use-when="$output-mode = 'lint'"/>
</xsl:template>
<!-- ======================================================================= -->
<!-- NAMED TEMPLATES -->
<!-- ======================================================================= -->
<xd:doc scope="component">
<xd:desc>
<xd:p>Template for text nodes to get rid of whitespace an newline characters.</xd:p>
</xd:desc>
</xd:doc>
<xsl:template name="normalize_space-break">
<xsl:analyze-string select="." regex="[\n]+">
<xsl:matching-substring/>
<xsl:non-matching-substring>
<xsl:analyze-string select="." regex="^[\n\s]+$">
<xsl:matching-substring/>
<xsl:non-matching-substring>
<xsl:analyze-string select="." regex="^\s\s+">
<xsl:matching-substring>
<xsl:text> </xsl:text>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:analyze-string select="." regex="^\s+|\s+$">
<xsl:matching-substring/>
<xsl:non-matching-substring>
<xsl:copy/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:template>
<xd:doc scope="component">
<xd:desc>
<xd:p>Application documentation template.</xd:p>
</xd:desc>
<xd:return>An mei:application element describing this XSLT.</xd:return>
</xd:doc>
<xsl:template name="documentation-application">
<xsl:choose>
<xsl:when test="id($linter-id)">
<!-- do nothing if an xml:id with the value of $linter-id can be found in the document --> </xsl:when>
<xsl:otherwise>
<!-- create an mei:application element -->
<xsl:element name="application">
<xsl:attribute name="version" select="$linter-version"/>
<xsl:attribute name="xml:id" select="$linter-id"/>
<xsl:element name="name">
<xsl:value-of select="tokenize($linter-url, '/')[last()]"/>
</xsl:element>
<xsl:element name="ptr">
<xsl:attribute name="target" select="$linter-url"/>
</xsl:element>
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xd:doc scope="component">
<xd:desc>
<xd:p>Change documentation template.</xd:p>
</xd:desc>
<xd:param name="n">The value for the n attribute of the generate mei:change element.</xd:param>
<xd:return>An mei:change element.</xd:return>
<xd:param name="n">The desired value for change/@n.</xd:param>
</xd:doc>
<xsl:template name="documentation-change">
<xsl:param name="n" required="true" as="xs:NMTOKEN"/>
<xsl:element name="change">
<xsl:attribute name="n" select="$n"/>
<xsl:attribute name="resp" select="'#' || $linter-id"/>
<xsl:element name="changeDesc">
<xsl:element name="p">Applied <xsl:value-of select="$output-mode"/> mode.</xsl:element>
</xsl:element>
<xsl:element name="date">
<xsl:attribute name="isodate" select="format-date(current-date(), '[Y]-[M02]-[D02]')"/>
</xsl:element>
</xsl:element>
</xsl:template>
<!-- ======================================================================= -->
<!-- DOCUMENTATION-MODE TEMPLATES -->
<!-- ======================================================================= -->
<xd:doc scope="component">
<xd:desc>
<xd:p>
<xd:b>documentation: root-element pre-ckeck for meiHead.</xd:b>
</xd:p>
<xd:p>Matches root elements that either are mei:meiHead or have a direct child mei:meiHead.</xd:p>
</xd:desc>
</xd:doc>
<xsl:template match="/mei:*[self::mei:meiHead or mei:meiHead]" mode="documentation">
<xsl:copy>
<xsl:apply-templates select="@* | node()" mode="documentation"/>
</xsl:copy>
</xsl:template>
<xd:doc scope="component">
<xd:desc>
<xd:p>Documentation for files without top-level mei:meiHead.</xd:p>
<xd:p>Matches root elements that are not mei:meiHead and do not contain an mei:meiHead element as direct child.</xd:p>
</xd:desc>
<xd:return>Inserts XML-comments for documentation then processes the XML-tree.</xd:return>
</xd:doc>
<xsl:template match="/mei:*[not(mei:meiHead) and not(self::mei:meiHead)]" mode="documentation">
<xsl:variable name="documentation-change" as="element()">
<xsl:call-template name="documentation-change">
<xsl:with-param name="n" select="xs:NMTOKEN(1)"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="documentation-application" as="element()">
<xsl:call-template name="documentation-application"/>
</xsl:variable>
<xsl:comment>
<xsl:value-of select="$documentation-change/mei:date/@isodate, $documentation-change/mei:changeDesc/mei:p" separator=" – "/>
<xsl:text> Agent: </xsl:text>
<xsl:value-of select="$documentation-application/mei:ptr/@target, $documentation-application/@version" separator=" – "/>
</xsl:comment>
<xsl:apply-templates select="." mode="clone"/>
</xsl:template>
<xd:doc scope="component">
<xd:desc>
<xd:p>documentation: Insert documentation into top mei:meiHead.</xd:p>
</xd:desc>
</xd:doc>
<xsl:template match="mei:meiHead[count(ancestor::*) le 1]" mode="documentation">
<xsl:variable name="exists-appInfo" select="exists(mei:encodingDesc/mei:appInfo)" as="xs:boolean"/>
<xsl:variable name="exists-revisionDesc" select="exists(mei:reivisionDesc)" as="xs:boolean"/>
<xsl:variable name="nodes-before-encodingDesc" select="mei:fileDesc/preceding-sibling::node(), mei:fileDesc" as="node()*"/>
<xsl:variable name="nodes-after-encodingDesc" select="mei:fileDesc/following-sibling::node() except (mei:encodingDesc)" as="node()*"/>
<xsl:variable name="nodes-after-appInfo" select="
if ($exists-appInfo) then
mei:encodingDesc/mei:appInfo/following-sibling::node()
else
mei:encodingDesc/node() except (mei:head)" as="node()*"/>
<xsl:copy>
<xsl:apply-templates select="@*" mode="documentation"/>
<xsl:apply-templates select="$nodes-before-encodingDesc" mode="documentation"/>
<xsl:element name="encodingDesc">
<xsl:apply-templates select="mei:encodingDesc/@*" mode="documentation"/>
<xsl:apply-templates select="mei:encodingDesc/mei:head" mode="documentation"/>
<xsl:element name="appInfo">
<xsl:apply-templates select="mei:encodingDesc/mei:appInfo/@* | mei:encodingDesc/mei:appInfo/node()" mode="documentation"/>
<xsl:call-template name="documentation-application"/>
</xsl:element>
<xsl:apply-templates select="$nodes-after-appInfo" mode="documentation"/>
</xsl:element>
<xsl:apply-templates select="$nodes-after-encodingDesc except (mei:revisionDesc)" mode="documentation"/>
<xsl:element name="revisionDesc">
<xsl:variable name="count-change" select="xs:nonNegativeInteger(count(mei:revisionDesc/mei:change))" as="xs:nonNegativeInteger"/>
<xsl:apply-templates select="mei:revisionDesc/@* | mei:revisionDesc/node()" mode="documentation"/>
<xsl:call-template name="documentation-change">
<xsl:with-param name="n" select="xs:NMTOKEN($count-change + 1)"/>
</xsl:call-template>
</xsl:element>
</xsl:copy>
</xsl:template>
<!-- ========== CLEAN TEMPLATES ========== -->
<xd:doc scope="component">
<xd:desc>
<xd:p>Overwrite XSLT default templates to copy every node.</xd:p>
</xd:desc>
</xd:doc>
<xsl:template match="node() | @*" mode="clean clone documentation">
<xsl:copy>
<xsl:apply-templates select="@* | node()" mode="#current"/>
</xsl:copy>
</xsl:template>
<xd:doc scope="component">
<xd:desc>
<xd:p>Cleanup namespace declarations on elements.</xd:p>
</xd:desc>
</xd:doc>
<xsl:template match="*" mode="clean">
<xsl:variable name="name.new" as="xs:Name">
<xsl:choose>
<xsl:when test="namespace-uri() = 'http://www.music-encoding.org/ns/mei'">
<xsl:copy-of select="xs:NCName(local-name())"/>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="xs:Name(name())"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:element name="{$name.new}" namespace="{namespace-uri()}">
<xsl:variable name="current-element" select="." as="element()"/>
<xsl:for-each select="namespace::*">
<xsl:variable name="prefix" select="name()" as="xs:string"/>
<xsl:if test="$current-element/descendant::*[(namespace-uri() = current() and substring-before(name(), ':') = $prefix) or @*[substring-before(name(), ':') = $prefix]]">
<xsl:copy-of select=".[. != 'http://www.music-encoding.org/ns/mei']"/>
</xsl:if>
</xsl:for-each>
<xsl:apply-templates select="@* | node()" mode="clean"/>
</xsl:element>
</xsl:template>
<xd:doc scope="component">
<xd:desc>
<xd:p>Template for processing text nodes.</xd:p>
<xd:p>Removes leading space character and trailing space or newline characters.</xd:p>
</xd:desc>
</xd:doc>
<xsl:template match="text()" mode="clean">
<xsl:variable name="number-of-nodes-in-parent" select="xs:nonNegativeInteger(count(parent::mei:*/node()))" as="xs:nonNegativeInteger"/>
<xsl:variable name="index-in-parent" select="xs:nonNegativeInteger(position())" as="xs:nonNegativeInteger"/>
<xsl:message>Info: <xsl:text>node number: </xsl:text><xsl:value-of select="$index-in-parent"/> of <xsl:value-of select="$number-of-nodes-in-parent"/> nodes in parent.</xsl:message>
<xsl:choose>
<xsl:when test="$index-in-parent = 1">
<xsl:analyze-string select="." regex="^\s">
<xsl:matching-substring/>
<xsl:non-matching-substring>
<xsl:call-template name="normalize_space-break"/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:when>
<xsl:when test="$index-in-parent = $number-of-nodes-in-parent">
<xsl:analyze-string select="." regex="[\s\n]+$">
<xsl:matching-substring/>
<xsl:non-matching-substring>
<xsl:call-template name="normalize_space-break"/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:when>
<xsl:when test=". = ' '">
<xsl:copy/>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="normalize_space-break"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- ======================================================================= -->
<!-- LINT-MODE TEMPLATES -->
<!-- ======================================================================= -->
<xd:doc scope="component">
<xd:desc>
<xd:p>lint: Default template.</xd:p>
</xd:desc>
</xd:doc>
<xsl:template match="node() | @*" mode="lint">
<xsl:message>========================================</xsl:message>
<xsl:variable name="is-attribute" select="
if (count(. | ../@*) = count(../@*)) then
xs:boolean('true')
else
xs:boolean('false')" as="xs:boolean"/>
<xsl:variable name="is-element" select="
if (self::*) then
xs:boolean('true')
else
xs:boolean('false')" as="xs:boolean"/>
<xsl:variable name="is-text" select="
if (self::text()) then
xs:boolean('true')
else
xs:boolean('false')" as="xs:boolean"/>
<xsl:variable name="node-type" as="xs:string">
<xsl:choose>
<xsl:when test="$is-attribute">attribute</xsl:when>
<xsl:when test="$is-element">element</xsl:when>
<xsl:when test="$is-text">text</xsl:when>
<xsl:otherwise>undetermined</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:message>LINT-MODE PROCESSING <xsl:value-of select="$node-type"/> NODE: <xsl:value-of select="local-name()"/> at: <xsl:value-of select="
string-join((for $e in ancestor-or-self::*
return
local-name($e)), '/')"/></xsl:message>
<xsl:message>is text: <xsl:value-of select="$is-text"/></xsl:message>
<xsl:variable name="has.no-break-ancestor" as="xs:boolean">
<xsl:choose>
<xsl:when test="local-name(parent::*) = $elements.do-not-break-contents">true</xsl:when>
<xsl:otherwise>false</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="nesting-depth" select="xs:nonNegativeInteger(count(ancestor::*))" as="xs:nonNegativeInteger"/>
<xsl:variable name="indentation-start" select="
if ($nesting-depth gt 0) then
string-join(for $i in 1 to $nesting-depth return ' ')
else ()" as="xs:string?"/>
<xsl:variable name="indentation-end" select="
if ($nesting-depth gt 0) then
string-join(for $i in 1 to $nesting-depth - 1 return ' ')
else ()" as="xs:string?"/>
<xsl:message>is element: <xsl:value-of select="$is-element"/></xsl:message>
<xsl:if test="self::comment() or (self::* and not(local-name(.) = $elements.no-break) and not($has.no-break-ancestor)) or local-name(.) = $elements.break-before">
<xsl:message>nesting-depth: <xsl:value-of select="$nesting-depth"/></xsl:message>
<xsl:message>indentation start: ›<xsl:value-of select="$indentation-start"/>‹</xsl:message>
<xsl:message>indentation end: ›<xsl:value-of select="$indentation-end"/>‹</xsl:message>
<xsl:copy-of select="$nl, $indentation-start"/>
</xsl:if>
<xsl:choose>
<xsl:when test="self::text()">
<xsl:next-match/>
</xsl:when>
<xsl:when test="self::comment()">
<xsl:comment select="concat(' ', normalize-space(.), ' ')"/>
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:apply-templates select="@* | node()" mode="lint"/>
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
<xsl:if test="(self::* and not(local-name(.) = $elements.no-break))">
<xsl:variable name="nesting-depth" select="xs:nonNegativeInteger(count(ancestor::*))" as="xs:nonNegativeInteger"/>
<xsl:if test="not($has.no-break-ancestor) and not(following-sibling::node())">
<xsl:copy-of select="$nl"/>
<xsl:copy-of select="$indentation-end"/>
</xsl:if>
</xsl:if>
</xsl:template>
<xd:doc scope="component">
<xd:desc>
<xd:p>lint: Assure xml-model processing instruction are preceded by a newline character.</xd:p>
</xd:desc>
</xd:doc>
<xsl:template match="processing-instruction('xml-model')" mode="lint">
<xsl:copy-of select="$nl, ."/>
</xsl:template>
</xsl:stylesheet>