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

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

tune the code that generated "ABNF diagnostics" so that they only show up for unexpected problems

File size: 6.0 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:otherwise>
77            <xsl:value-of select="."/>
78            <xsl:text>&#10;</xsl:text>
79          </xsl:otherwise>
80        </xsl:choose>
81      </xsl:for-each>
82    </xsl:variable>
83   
84    <xsl:if test="$diags!=''">
85      <figure>
86        <preamble>ABNF diagnostics:</preamble>
87        <artwork type="inline">
88          <xsl:text>&#10;</xsl:text>
89          <xsl:value-of select="$diags"/>
90      </artwork></figure>
91    </xsl:if>
92  </section></xsl:variable>
93
94  <xsl:copy-of select="$generated"/>
95
96  <!-- check whether it's up-to-date... -->
97  <xsl:variable name="src">
98    <xsl:for-each select="//section[@anchor='collected.abnf']//artwork">
99      <xsl:value-of select="."/>
100    </xsl:for-each>
101  </xsl:variable>
102 
103  <xsl:variable name="new">
104    <xsl:value-of select="$generated//artwork"/>
105  </xsl:variable>
106 
107  <xsl:if test="not(//section[@anchor='collected.abnf']) or normalize-space($src) != normalize-space($new)">
108    <xsl:message>WARNING: appendix contained inside source document needs to be updated</xsl:message>
109    <xsl:call-template name="showdiff">
110      <xsl:with-param name="actual" select="normalize-space($src)"/>
111      <xsl:with-param name="expected" select="normalize-space($new)"/>
112    </xsl:call-template>
113  </xsl:if>
114</xsl:template>
115
116<xsl:template name="showdiff">
117  <xsl:param name="actual"/>
118  <xsl:param name="expected"/>
119  <xsl:param name="prev"/>
120 
121  <xsl:choose>
122    <xsl:when test="$actual=''">
123      <!-- done -->
124    </xsl:when>
125    <xsl:when test="substring($actual,1,1)=substring($expected,1,1)">
126      <xsl:call-template name="showdiff">
127        <xsl:with-param name="actual" select="substring($actual,2)"/>
128        <xsl:with-param name="expected" select="substring($expected,2)"/>
129        <xsl:with-param name="prev" select="concat($prev,substring($actual,1,1))"/>
130      </xsl:call-template>
131    </xsl:when>
132    <xsl:otherwise>
133      <xsl:message>WARNING: at position <xsl:value-of select="string-length($prev)"/></xsl:message>
134      <xsl:message>WARNING: prefix <xsl:value-of select="$prev"/></xsl:message>
135      <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>
136    </xsl:otherwise>
137  </xsl:choose>
138</xsl:template>
139
140<xsl:function name="x:laststartchar">
141  <xsl:param name="lines"/>
142  <xsl:param name="position"/>
143 
144  <xsl:choose>
145    <xsl:when test="' '!=substring($lines[$position],1,1)">
146      <xsl:value-of select="substring($lines[$position],1,1)"/>
147    </xsl:when>
148    <xsl:otherwise>
149      <xsl:value-of select="x:laststartchar($lines, $position - 1)"/>
150    </xsl:otherwise>
151  </xsl:choose>
152</xsl:function>
153 
154 
155</xsl:transform>
Note: See TracBrowser for help on using the repository browser.