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

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

Collected ABNF section: explain ABNF list expansion (see #436)

File size: 6.3 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      <xsl:if test="not(starts-with($abnf,'p1-'))"><t>
22  In the collected ABNF below, list rules are expanded as per <xref target='Part1' x:rel='#notation'/>.
23</t></xsl:if>
24      <figure>
25      <xsl:text>&#10;</xsl:text>
26      <artwork type="abnf" name="{$abnf}">
27        <xsl:for-each select="$lines[substring(.,1,2)!='; ']">
28       
29          <!-- Group by start character -->
30          <xsl:variable name="lineno" select="position()"/>
31          <xsl:variable name="sc1" select="substring(.,1,1)"/>
32          <xsl:variable name="sc0" select="x:laststartchar($lines, $lineno - 1)"/>
33          <xsl:if test="$sc1!=' ' and $sc0!=' ' and $sc1!=$sc0">
34            <xsl:text>&#10;</xsl:text>
35          </xsl:if>
36
37          <!-- Add cross-refs for terms -->
38          <xsl:analyze-string select="." regex='^([A-Za-z0-9\-]+) = ' flags="sm">
39            <xsl:matching-substring>
40              <xsl:variable name="term" select="regex-group(1)"/>
41              <xsl:choose>
42                <xsl:when test="$src//*[@anchor=$term] or $src//x:anchor-alias[@value=$term]">
43                  <x:ref><xsl:value-of select="$term"/></x:ref>
44                </xsl:when>
45                <xsl:otherwise>
46                  <xsl:value-of select="$term"/>
47                </xsl:otherwise>
48              </xsl:choose>
49              <xsl:text> = </xsl:text>
50            </xsl:matching-substring>
51            <xsl:non-matching-substring>
52              <xsl:value-of select="."/>
53            </xsl:non-matching-substring>
54          </xsl:analyze-string>
55
56          <xsl:text>&#10;</xsl:text>
57        </xsl:for-each>
58      </artwork>
59      <xsl:text>&#10;</xsl:text>
60    </figure> 
61    <xsl:text>&#10;</xsl:text>
62   
63    <xsl:variable name="diags">
64      <xsl:for-each select="$lines[substring(.,1,2)='; ']">
65        <xsl:variable name="prod" select="substring-before(substring-after(.,'; '),' defined but not used')"/>
66        <xsl:choose>
67          <xsl:when test="$prod!='' and $src//iref[@item='Header Fields' and @subitem=$prod]">
68            <!-- header field; expected not to be reference -->
69          </xsl:when>
70          <xsl:when test="$prod!='' and $src//iref[@item='Grammar' and @subitem=$prod and comment()='terminal production']">
71            <!-- known to be a terminal production -->
72          </xsl:when>
73          <xsl:when test="$prod!='' and $src//iref[@item='Grammar' and @subitem=$prod and comment()='exported production']">
74            <!-- known to be used in another part -->
75          </xsl:when>
76          <xsl:when test="$prod!='' and $src//iref[@item='Grammar' and @subitem=$prod and comment()='unused production']">
77            <!-- known to be unused, but we want to include it anyway -->
78          </xsl:when>
79          <xsl:when test="$prod!='' and $src//x:ref=$prod">
80            <!-- referenced otherwise -->
81          </xsl:when>
82          <xsl:otherwise>
83            <xsl:value-of select="."/>
84            <xsl:text>&#10;</xsl:text>
85          </xsl:otherwise>
86        </xsl:choose>
87      </xsl:for-each>
88    </xsl:variable>
89   
90    <xsl:if test="$diags!=''">
91      <figure>
92        <preamble>ABNF diagnostics:</preamble>
93        <artwork type="inline">
94          <xsl:text>&#10;</xsl:text>
95          <xsl:value-of select="$diags"/>
96      </artwork></figure>
97    </xsl:if>
98  </section></xsl:variable>
99
100  <xsl:copy-of select="$generated"/>
101
102  <!-- check whether it's up-to-date... -->
103  <xsl:variable name="src">
104    <xsl:for-each select="//section[@anchor='collected.abnf']//artwork">
105      <xsl:value-of select="."/>
106    </xsl:for-each>
107  </xsl:variable>
108 
109  <xsl:variable name="new">
110    <xsl:value-of select="$generated//artwork"/>
111  </xsl:variable>
112 
113  <xsl:if test="not(//section[@anchor='collected.abnf']) or normalize-space($src) != normalize-space($new)">
114    <xsl:message>WARNING: appendix contained inside source document needs to be updated</xsl:message>
115    <xsl:call-template name="showdiff">
116      <xsl:with-param name="actual" select="normalize-space($src)"/>
117      <xsl:with-param name="expected" select="normalize-space($new)"/>
118    </xsl:call-template>
119  </xsl:if>
120</xsl:template>
121
122<xsl:template name="showdiff">
123  <xsl:param name="actual"/>
124  <xsl:param name="expected"/>
125  <xsl:param name="prev"/>
126 
127  <xsl:choose>
128    <xsl:when test="$actual=''">
129      <!-- done -->
130    </xsl:when>
131    <xsl:when test="substring($actual,1,1)=substring($expected,1,1)">
132      <xsl:call-template name="showdiff">
133        <xsl:with-param name="actual" select="substring($actual,2)"/>
134        <xsl:with-param name="expected" select="substring($expected,2)"/>
135        <xsl:with-param name="prev" select="concat($prev,substring($actual,1,1))"/>
136      </xsl:call-template>
137    </xsl:when>
138    <xsl:otherwise>
139      <xsl:message>WARNING: at position <xsl:value-of select="string-length($prev)"/></xsl:message>
140      <xsl:message>WARNING: prefix <xsl:value-of select="$prev"/></xsl:message>
141      <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>
142    </xsl:otherwise>
143  </xsl:choose>
144</xsl:template>
145
146<xsl:function name="x:laststartchar">
147  <xsl:param name="lines"/>
148  <xsl:param name="position"/>
149 
150  <xsl:choose>
151    <xsl:when test="' '!=substring($lines[$position],1,1)">
152      <xsl:value-of select="substring($lines[$position],1,1)"/>
153    </xsl:when>
154    <xsl:otherwise>
155      <xsl:value-of select="x:laststartchar($lines, $position - 1)"/>
156    </xsl:otherwise>
157  </xsl:choose>
158</xsl:function>
159 
160 
161</xsl:transform>
Note: See TracBrowser for help on using the repository browser.