1 | <xsl:transform
|
---|
2 | xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
|
---|
3 | xmlns:xs="http://www.w3.org/2001/XMLSchema"
|
---|
4 | xmlns:x="http://purl.org/net/xml2rfc/ext"
|
---|
5 | version="2.0"
|
---|
6 | exclude-result-prefixes="xs">
|
---|
7 |
|
---|
8 | <xsl:output method="xml" omit-xml-declaration="yes" indent="no"/>
|
---|
9 |
|
---|
10 | <xsl:param name="abnf"/>
|
---|
11 |
|
---|
12 | <xsl:template match="/">
|
---|
13 | <xsl:variable name="src" select="."/>
|
---|
14 | <xsl:variable name="collected" select="unparsed-text($abnf)"/>
|
---|
15 |
|
---|
16 | <xsl:variable name="all-lines" as="xs:string*" select="tokenize($collected, '\r?\n')"/>
|
---|
17 | <xsl:variable name="lines" select="$all-lines[normalize-space(.)!='']"/>
|
---|
18 |
|
---|
19 | <section title="Collected ABNF" anchor="collected.abnf">
|
---|
20 | <xsl:text> </xsl:text>
|
---|
21 | <figure>
|
---|
22 | <xsl:text> </xsl:text>
|
---|
23 | <artwork type="abnf" name="{$abnf}">
|
---|
24 | <xsl:for-each select="$lines[substring(.,1,2)!='; ']">
|
---|
25 |
|
---|
26 | <!-- Group by start character -->
|
---|
27 | <xsl:variable name="lineno" select="position()"/>
|
---|
28 | <xsl:variable name="sc1" select="substring(.,1,1)"/>
|
---|
29 | <xsl:variable name="sc0" select="x:laststartchar($lines, $lineno - 1)"/>
|
---|
30 | <xsl:if test="$sc1!=' ' and $sc0!=' ' and $sc1!=$sc0">
|
---|
31 | <xsl:text> </xsl:text>
|
---|
32 | </xsl:if>
|
---|
33 |
|
---|
34 | <!-- Add cross-refs for terms -->
|
---|
35 | <xsl:analyze-string select="." regex='^([A-Za-z0-9\-]+) = ' flags="sm">
|
---|
36 | <xsl:matching-substring>
|
---|
37 | <xsl:variable name="term" select="regex-group(1)"/>
|
---|
38 | <xsl:choose>
|
---|
39 | <xsl:when test="$src//*[@anchor=$term] or $src//x:anchor-alias[@value=$term]">
|
---|
40 | <x:ref><xsl:value-of select="$term"/></x:ref>
|
---|
41 | </xsl:when>
|
---|
42 | <xsl:otherwise>
|
---|
43 | <xsl:value-of select="$term"/>
|
---|
44 | </xsl:otherwise>
|
---|
45 | </xsl:choose>
|
---|
46 | <xsl:text> = </xsl:text>
|
---|
47 | </xsl:matching-substring>
|
---|
48 | <xsl:non-matching-substring>
|
---|
49 | <xsl:value-of select="."/>
|
---|
50 | </xsl:non-matching-substring>
|
---|
51 | </xsl:analyze-string>
|
---|
52 |
|
---|
53 | <xsl:text> </xsl:text>
|
---|
54 | </xsl:for-each>
|
---|
55 | </artwork>
|
---|
56 | <xsl:text> </xsl:text>
|
---|
57 | </figure>
|
---|
58 | <xsl:text> </xsl:text>
|
---|
59 |
|
---|
60 | <figure>
|
---|
61 | <preamble>ABNF diagnostics:</preamble>
|
---|
62 | <artwork type="inline">
|
---|
63 | <xsl:text> </xsl:text>
|
---|
64 | <xsl:for-each select="$lines[substring(.,1,2)='; ']">
|
---|
65 | <xsl:value-of select="."/>
|
---|
66 | <xsl:text> </xsl:text>
|
---|
67 | </xsl:for-each>
|
---|
68 | </artwork></figure>
|
---|
69 | </section>
|
---|
70 |
|
---|
71 | <!-- check whether it's up-to-date... -->
|
---|
72 | <xsl:variable name="src">
|
---|
73 | <xsl:for-each select="//section[@anchor='collected.abnf']//artwork">
|
---|
74 | <xsl:value-of select="."/>
|
---|
75 | </xsl:for-each>
|
---|
76 | </xsl:variable>
|
---|
77 |
|
---|
78 | <xsl:if test="not(//section[@anchor='collected.abnf']) or normalize-space($src) != normalize-space($collected)">
|
---|
79 | <xsl:message>WARNING: appendix contained inside source document needs to be updated</xsl:message>
|
---|
80 | <!--<xsl:message>A: <xsl:value-of select="//section[@anchor='collected.abnf']//artwork"/></xsl:message>
|
---|
81 | <xsl:message>B: <xsl:value-of select="$collected"/></xsl:message>-->
|
---|
82 | </xsl:if>
|
---|
83 | </xsl:template>
|
---|
84 |
|
---|
85 | <xsl:function name="x:laststartchar">
|
---|
86 | <xsl:param name="lines"/>
|
---|
87 | <xsl:param name="position"/>
|
---|
88 |
|
---|
89 | <xsl:choose>
|
---|
90 | <xsl:when test="' '!=substring($lines[$position],1,1)">
|
---|
91 | <xsl:value-of select="substring($lines[$position],1,1)"/>
|
---|
92 | </xsl:when>
|
---|
93 | <xsl:otherwise>
|
---|
94 | <xsl:value-of select="x:laststartchar($lines, $position - 1)"/>
|
---|
95 | </xsl:otherwise>
|
---|
96 | </xsl:choose>
|
---|
97 | </xsl:function>
|
---|
98 |
|
---|
99 |
|
---|
100 | </xsl:transform> |
---|