<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
                xmlns:h='http://www.w3.org/1999/xhtml'
                xmlns='http://www.w3.org/1999/xhtml'
                version='1.0'>

  <xsl:output method='xml' encoding='UTF-8'
              doctype-public='-//W3C//DTD XHTML 1.0 Transitional//EN'
              doctype-system='http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'/>

  <xsl:param name='chapters'/>
  <xsl:param name='publish'/>

  <xsl:template match='/h:html/h:body'>
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:apply-templates/>
      <xsl:call-template name='do-chapters'>
        <xsl:with-param name='cs' select='concat($chapters, " ")'/>
      </xsl:call-template>
      <script src='style/expanders.js'><xsl:text> </xsl:text></script>
    </xsl:copy>
  </xsl:template>

  <xsl:template match='h:div[@class="header" or @class="footer" or @class="header top" or @class="header bottom"]'/>

  <xsl:template match="h:head">
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:apply-templates/>
      <xsl:call-template name='do-chapters-style'>
        <xsl:with-param name='cs' select='concat($chapters, " ")'/>
      </xsl:call-template>
    </xsl:copy>
  </xsl:template>

  <xsl:template match='@id'>
    <xsl:param name='c'/>
    <xsl:attribute name='id'>
      <xsl:value-of select='concat($c, .)'/>
    </xsl:attribute>
  </xsl:template>

  <xsl:template match='h:a/@name'>
    <xsl:param name='c'/>
    <xsl:attribute name='name'>
      <xsl:value-of select='concat($c, .)'/>
    </xsl:attribute>
  </xsl:template>

  <xsl:template match='h:a/@href'>
    <xsl:param name='c'/>
    <xsl:attribute name='href'>
      <xsl:choose>
        <xsl:when test='starts-with(., "#")'>
          <xsl:value-of select='concat("#", $c, substring(., 2))'/>
        </xsl:when>
        <xsl:when test='substring-after(substring-before(., "#"), ".") = "html" and not(contains(., "/"))'>
          <xsl:value-of select='concat("#", substring-before(., ".html"), "-", substring-after(., "#"))'/>
        </xsl:when>
        <xsl:when test='contains(concat(" ", $chapters, " "), concat(" ", substring-before(., ".html"), " "))'>
          <xsl:value-of select='concat("#chapter-", substring-before(., ".html"))'/>
        </xsl:when>
        <xsl:when test='contains(concat(" ", $chapters, " "), concat(" ", ., " "))'>
          <xsl:value-of select='concat("#chapter-", .)'/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select='.'/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:attribute>
  </xsl:template>

  <xsl:template match='h:script[@src="style/expanders.js"]'/>

  <xsl:template match='node()|@*'>
    <xsl:param name='c'/>
    <xsl:copy>
      <xsl:apply-templates select="@*">
        <xsl:with-param name='c' select='$c'/>
      </xsl:apply-templates>
      <xsl:apply-templates>
        <xsl:with-param name='c' select='$c'/>
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>

  <xsl:template name='do-chapters'>
    <xsl:param name='cs'/>
    <xsl:variable name='c' select='concat(substring-before($cs, " "), "-")'/>
    <xsl:if test='$c != "" and $c != "-"'>
      <hr class='chapter-divider'/>
      <xsl:text>&#xa;</xsl:text>
      <div id='chapter-{substring($c, 1, string-length($c) - 1)}'>
        <xsl:apply-templates select='document(concat($publish, "/", substring($c, 1, string-length($c) - 1), ".html"))/h:html/h:body/node()'>
          <xsl:with-param name='c' select='$c'/>
        </xsl:apply-templates>
      </div>
      <xsl:call-template name='do-chapters'>
        <xsl:with-param name='cs' select='substring-after($cs, " ")'/>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>

  <xsl:template name='do-chapters-style'>
    <xsl:param name='cs'/>
    <xsl:variable name='c' select='concat(substring-before($cs, " "), "-")'/>
    <xsl:if test='$c != "" and $c != "-"'>
      <xsl:apply-templates select='document(concat($publish, "/", substring($c, 1, string-length($c) - 1), ".html"))/h:html/h:head/h:style'/>
      <xsl:call-template name='do-chapters-style'>
        <xsl:with-param name='cs' select='substring-after($cs, " ")'/>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>

