1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
|
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!--
Break up a list of nodes at each top-level <br>, and turn each
sublist into a <dt>...</dt> node. Initially nfirst is empty and
nlist has the remaining nodes; if nlist doesn't start with a
<br> node, then shift one node from nlist to nfirst and recurse;
nlist is empty or starts with <br>, then output nfirst, empty
it, and recurse with the remainder of nlist. (We also eat a
whitespace-only text node after the <br> for purely aesthetic
reasons.)
-->
<xsl:template name="dl-br-dt">
<xsl:param name="nfirst" />
<xsl:param name="nlist" />
<xsl:choose>
<xsl:when test="not($nfirst or $nlist)" />
<xsl:when test="$nlist[1][self::br] or not($nlist)">
<dt>
<xsl:apply-templates select="$nfirst" />
</dt>
<xsl:call-template name="dl-br-dt">
<xsl:with-param name="nfirst" select="$nfirst[false()]" />
<xsl:with-param name="nlist" select="$nlist[position() > 1 and (position() > 2 or * or normalize-space())]" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="dl-br-dt">
<xsl:with-param name="nfirst" select="$nfirst | $nlist[1]" />
<xsl:with-param name="nlist" select="$nlist[position() > 1]" />
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template mode="dl-br" match="li[child::br]">
<xsl:call-template name="dl-br-dt">
<xsl:with-param name="nfirst" select="*[false()]" />
<xsl:with-param name="nlist" select="node()[following-sibling::br]" />
</xsl:call-template>
<xsl:if test="child::br[position()=last() and (following-sibling::* or normalize-space(following-sibling::text()))]">
<dd>
<xsl:apply-templates select="node()[not(following-sibling::br or self::br)]" />
</dd>
</xsl:if>
</xsl:template>
<xsl:template mode="dl-p" match="li[child::p]">
<dt>
<xsl:apply-templates select="p[1]/node()" />
</dt>
<xsl:if test="child::p[following-sibling::p]">
<dd>
<xsl:apply-templates select="p[position() != 1]|node()[not(self::p)]" />
</dd>
</xsl:if>
</xsl:template>
<xsl:template mode="dl-br" match="*|@*">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template mode="dl-p" match="*|@*">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="ul[child::li[1][child::br] and not(parent::div[@class='no-dl-fudge'])]">
<dl>
<xsl:apply-templates mode="dl-br" select="node()" />
</dl>
</xsl:template>
<xsl:template match="ul[child::li[1][child::p] and not(parent::div[@class='no-dl-fudge'])]">
<dl>
<xsl:apply-templates mode="dl-p" select="node()" />
</dl>
</xsl:template>
<xsl:template match="*|@*">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="code[not(ancestor::pre)]">
<xsl:copy>
<xsl:choose>
<xsl:when test="string-length() < 40">
<xsl:attribute name="class">shortcode</xsl:attribute>
</xsl:when>
<xsl:otherwise>
<xsl:attribute name="class">longcode</xsl:attribute>
</xsl:otherwise>
</xsl:choose>
<xsl:apply-templates select="node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="pre[child::code]">
<xsl:copy>
<xsl:attribute name="class">codeblock</xsl:attribute>
<xsl:apply-templates select="node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="h1">
<xsl:variable name="n" select="1 + count(preceding-sibling::h1)" />
<xsl:copy>
<xsl:attribute name="id"><xsl:value-of select="concat('S',$n)" /></xsl:attribute>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="h2">
<xsl:variable name="n1" select="count(preceding-sibling::h1)" />
<xsl:variable name="n2" select="1 + count(preceding-sibling::h2) - count(preceding-sibling::h1[1]/preceding-sibling::h2)" />
<xsl:copy>
<xsl:attribute name="id"><xsl:value-of select="concat('S',$n1,'.',$n2)" /></xsl:attribute>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template name="toc2">
<xsl:variable name="n1" select="count(preceding-sibling::h1)" />
<xsl:variable name="n2" select="1 + count(preceding-sibling::h2) - count(preceding-sibling::h1[1]/preceding-sibling::h2)" />
<li value="{$n2}">
<a href="#S{concat($n1,'.',$n2)}">
<xsl:value-of select="." />
</a>
</li>
</xsl:template>
<xsl:template name="toc1">
<xsl:variable name="n" select="1 + count(preceding-sibling::h1)" />
<li value="{$n}">
<a href="#S{$n}">
<xsl:value-of select="." />
</a>
<xsl:if test="following-sibling::h2[count(preceding-sibling::h1) = $n]">
<ol>
<xsl:for-each select="following-sibling::h2[count(preceding-sibling::h1) = $n]">
<xsl:call-template name="toc2" />
</xsl:for-each>
</ol>
</xsl:if>
</li>
</xsl:template>
<xsl:template match="/">
<html>
<head>
<title>PL/Lua Documentation</title>
<xsl:copy-of select="//body/style" />
</head>
<body>
<div id="headContainer">
<h1>Contents</h1>
<ol>
<xsl:for-each select="//body/h1">
<xsl:call-template name="toc1" />
</xsl:for-each>
</ol>
</div>
<div id="bodyContainer">
<xsl:apply-templates select="//body/node()[not(self::style) and not(@id='footerContainer')]" />
</div>
<xsl:apply-templates select="//body/div[@id='footerContainer']" />
</body>
</html>
</xsl:template>
</xsl:stylesheet>
|