source: draft-ietf-httpbis/latest/abnf2xml2rfc.xslt @ 2133

Last change on this file since 2133 was 1893, checked in by julian.reschke@…, 7 years ago

ABNF appendix generation now takes "unused", but referenced productions into account

File size: 6.1 KB
Line 
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  <xsl:variable name="generated"><section title="Collected ABNF" anchor="collected.abnf">
20    <xsl:text>&#10;</xsl:text>
21    <figure>
22      <xsl:text>&#10;</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>&#10;</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>&#10;</xsl:text>
54        </xsl:for-each>
55      </artwork>
56      <xsl:text>&#10;</xsl:text>
57    </figure> 
58    <xsl:text>&#10;</xsl:text>
59   
60    <xsl:variable name="diags">
61      <xsl:for-each select="$lines[substring(.,1,2)='; ']">
62        <xsl:variable name="prod" select="substring-before(substring-after(.,'; '),' defined but not used')"/>
63        <xsl:choose>
64          <xsl:when test="$prod!='' and $src//iref[@item='Header Fields' and @subitem=$prod]">
65            <!-- header field; expected not to be reference -->
66          </xsl:when>
67          <xsl:when test="$prod!='' and $src//iref[@item='Grammar' and @subitem=$prod and comment()='terminal production']">
68            <!-- known to be a terminal production -->
69          </xsl:when>
70          <xsl:when test="$prod!='' and $src//iref[@item='Grammar' and @subitem=$prod and comment()='exported production']">
71            <!-- known to be used in another part -->
72          </xsl:when>
73          <xsl:when test="$prod!='' and $src//iref[@item='Grammar' and @subitem=$prod and comment()='unused production']">
74            <!-- known to be unused, but we want to include it anyway -->
75          </xsl:when>
76          <xsl:when test="$prod!='' and $src//x:ref=$prod">
77            <!-- referenced otherwise -->
78          </xsl:when>
79          <xsl:otherwise>
80            <xsl:value-of select="."/>
81            <xsl:text>&#10;</xsl:text>
82          </xsl:otherwise>
83        </xsl:choose>
84      </xsl:for-each>
85    </xsl:variable>
86   
87    <xsl:if test="$diags!=''">
88      <figure>
89        <preamble>ABNF diagnostics:</preamble>
90        <artwork type="inline">
91          <xsl:text>&#10;</xsl:text>
92          <xsl:value-of select="$diags"/>
93      </artwork></figure>
94    </xsl:if>
95  </section></xsl:variable>
96
97  <xsl:copy-of select="$generated"/>
98
99  <!-- check whether it's up-to-date... -->
100  <xsl:variable name="src">
101    <xsl:for-each select="//section[@anchor='collected.abnf']//artwork">
102      <xsl:value-of select="."/>
103    </xsl:for-each>
104  </xsl:variable>
105 
106  <xsl:variable name="new">
107    <xsl:value-of select="$generated//artwork"/>
108  </xsl:variable>
109 
110  <xsl:if test="not(//section[@anchor='collected.abnf']) or normalize-space($src) != normalize-space($new)">
111    <xsl:message>WARNING: appendix contained inside source document needs to be updated</xsl:message>
112    <xsl:call-template name="showdiff">
113      <xsl:with-param name="actual" select="normalize-space($src)"/>
114      <xsl:with-param name="expected" select="normalize-space($new)"/>
115    </xsl:call-template>
116  </xsl:if>
117</xsl:template>
118
119<xsl:template name="showdiff">
120  <xsl:param name="actual"/>
121  <xsl:param name="expected"/>
122  <xsl:param name="prev"/>
123 
124  <xsl:choose>
125    <xsl:when test="$actual=''">
126      <!-- done -->
127    </xsl:when>
128    <xsl:when test="substring($actual,1,1)=substring($expected,1,1)">
129      <xsl:call-template name="showdiff">
130        <xsl:with-param name="actual" select="substring($actual,2)"/>
131        <xsl:with-param name="expected" select="substring($expected,2)"/>
132        <xsl:with-param name="prev" select="concat($prev,substring($actual,1,1))"/>
133      </xsl:call-template>
134    </xsl:when>
135    <xsl:otherwise>
136      <xsl:message>WARNING: at position <xsl:value-of select="string-length($prev)"/></xsl:message>
137      <xsl:message>WARNING: prefix <xsl:value-of select="$prev"/></xsl:message>
138      <xsl:message>WARNING: actual text is: '<xsl:value-of select="substring($actual,1,20)"/>...' expected was: '<xsl:value-of select="substring($expected,1,20)"/>...'</xsl:message>
139    </xsl:otherwise>
140  </xsl:choose>
141</xsl:template>
142
143<xsl:function name="x:laststartchar">
144  <xsl:param name="lines"/>
145  <xsl:param name="position"/>
146 
147  <xsl:choose>
148    <xsl:when test="' '!=substring($lines[$position],1,1)">
149      <xsl:value-of select="substring($lines[$position],1,1)"/>
150    </xsl:when>
151    <xsl:otherwise>
152      <xsl:value-of select="x:laststartchar($lines, $position - 1)"/>
153    </xsl:otherwise>
154  </xsl:choose>
155</xsl:function>
156 
157 
158</xsl:transform>
Note: See TracBrowser for help on using the repository browser.