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