[420] | 1 | <xsl:transform
|
---|
| 2 | xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
|
---|
[427] | 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">
|
---|
[420] | 7 |
|
---|
[427] | 8 | <xsl:output method="xml" omit-xml-declaration="yes" indent="no"/>
|
---|
| 9 |
|
---|
[420] | 10 | <xsl:param name="abnf"/>
|
---|
| 11 |
|
---|
| 12 | <xsl:template match="/">
|
---|
[427] | 13 | <xsl:variable name="src" select="."/>
|
---|
[421] | 14 | <xsl:variable name="collected" select="unparsed-text($abnf)"/>
|
---|
[420] | 15 |
|
---|
[581] | 16 | <xsl:variable name="all-lines" as="xs:string*" select="tokenize($collected, '\r?\n')"/>
|
---|
| 17 | <xsl:variable name="lines" select="$all-lines[normalize-space(.)!='']"/>
|
---|
[427] | 18 |
|
---|
| 19 | <section title="Collected ABNF" anchor="collected.abnf">
|
---|
[425] | 20 | <xsl:text> </xsl:text>
|
---|
[427] | 21 | <figure>
|
---|
| 22 | <xsl:text> </xsl:text>
|
---|
| 23 | <artwork type="abnf" name="{$abnf}">
|
---|
[454] | 24 | <xsl:for-each select="$lines[substring(.,1,2)!='; ']">
|
---|
| 25 |
|
---|
| 26 | <!-- Group by start character -->
|
---|
[427] | 27 | <xsl:variable name="lineno" select="position()"/>
|
---|
| 28 | <xsl:variable name="sc1" select="substring(.,1,1)"/>
|
---|
[428] | 29 | <xsl:variable name="sc0" select="x:laststartchar($lines, $lineno - 1)"/>
|
---|
[427] | 30 | <xsl:if test="$sc1!=' ' and $sc0!=' ' and $sc1!=$sc0">
|
---|
| 31 | <xsl:text> </xsl:text>
|
---|
| 32 | </xsl:if>
|
---|
[454] | 33 |
|
---|
| 34 | <!-- Add cross-refs for terms -->
|
---|
[427] | 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>
|
---|
[454] | 52 |
|
---|
[427] | 53 | <xsl:text> </xsl:text>
|
---|
| 54 | </xsl:for-each>
|
---|
| 55 | </artwork>
|
---|
| 56 | <xsl:text> </xsl:text>
|
---|
| 57 | </figure>
|
---|
| 58 | <xsl:text> </xsl:text>
|
---|
[454] | 59 |
|
---|
| 60 | <figure>
|
---|
| 61 | <preamble>ABNF diagnostics:</preamble>
|
---|
| 62 | <artwork type="inline">
|
---|
[537] | 63 | <xsl:text> </xsl:text>
|
---|
[454] | 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>
|
---|
[421] | 69 | </section>
|
---|
[420] | 70 |
|
---|
[425] | 71 | <!-- check whether it's up-to-date... -->
|
---|
[454] | 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>
|
---|
[421] | 77 |
|
---|
[454] | 78 | <xsl:if test="not(//section[@anchor='collected.abnf']) or normalize-space($src) != normalize-space($collected)">
|
---|
[421] | 79 | <xsl:message>WARNING: appendix contained inside source document needs to be updated</xsl:message>
|
---|
[425] | 80 | <!--<xsl:message>A: <xsl:value-of select="//section[@anchor='collected.abnf']//artwork"/></xsl:message>
|
---|
[421] | 81 | <xsl:message>B: <xsl:value-of select="$collected"/></xsl:message>-->
|
---|
| 82 | </xsl:if>
|
---|
[420] | 83 | </xsl:template>
|
---|
[428] | 84 |
|
---|
| 85 | <xsl:function name="x:laststartchar">
|
---|
| 86 | <xsl:param name="lines"/>
|
---|
| 87 | <xsl:param name="position"/>
|
---|
[420] | 88 |
|
---|
[428] | 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>
|
---|
[420] | 98 |
|
---|
[428] | 99 |
|
---|
[420] | 100 | </xsl:transform> |
---|