<?xml version="1.0" encoding="utf-8" ?>

<!DOCTYPE xsl:stylesheet [
  <!ENTITY w3c-logo SYSTEM "w3c.svg" >
]>

<!-- TODO: get font/colour data from w3ctalk-640w.css -->

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:fo="http://www.w3.org/1999/XSL/Format"
                xmlns:html="http://www.w3.org/1999/xhtml"
                xmlns:svg="http://www.w3.org/2000/svg"
                version="1.0">

<!-- STYLESHEET ATTRIBUTE SETS -->

  <xsl:attribute-set name="h1">
    <xsl:attribute name="font-size">30px</xsl:attribute>
    <xsl:attribute name="font-weight">bold</xsl:attribute>
    <xsl:attribute name="color">navy</xsl:attribute>
    <xsl:attribute name="space-before">.5em</xsl:attribute>
  </xsl:attribute-set>

  <xsl:attribute-set name="h2">
    <xsl:attribute name="font-size">24px</xsl:attribute>
    <xsl:attribute name="font-weight">bold</xsl:attribute>
    <xsl:attribute name="space-before">5px</xsl:attribute>
    <xsl:attribute name="space-after">10px</xsl:attribute>

    <!-- FOP BUG: doesnt do keep-with-next -->
    <xsl:attribute name="keep-with-next">always</xsl:attribute>
  </xsl:attribute-set>

  <xsl:attribute-set name="body"> 
    <xsl:attribute name="font-size">20px</xsl:attribute>
    <xsl:attribute name="space-before">.5em</xsl:attribute>
    <xsl:attribute name="space-after">.5em</xsl:attribute>
  </xsl:attribute-set>

  <xsl:attribute-set name="footer">
    <xsl:attribute name="font-size">15px</xsl:attribute>
  </xsl:attribute-set>

  <xsl:attribute-set name="a">
    <xsl:attribute name="color">blue</xsl:attribute>
    <xsl:attribute name="background-color">yellow</xsl:attribute>
    <xsl:attribute name="font-size">20px</xsl:attribute>
  </xsl:attribute-set>

  <xsl:attribute-set name="toc">
    <xsl:attribute name="font-size">15px</xsl:attribute>
  </xsl:attribute-set>

<!-- ************************* -->

  <xsl:output method="xml" indent="yes"/>
  
  <xsl:template match="/">
    <fo:root>
      <fo:layout-master-set>

        <!-- ***** LAYOUT of TOC SLIDES ***** -->
        <fo:simple-page-master master-name="toc"
          page-height="480px" page-width="640px"
          margin-top="20px"  margin-bottom="20px" 
          margin-left="20px" margin-right="20px">
          <fo:region-body region-name="main" column-count="2"
            margin-top="200px" margin-bottom="50px"/>
          <fo:region-before region-name="header" extent="250px"/>
          <fo:region-after region-name="footer" extent="50px"/>
        </fo:simple-page-master>

        <!-- ***** LAYOUT of SLIDES ***** -->
        <fo:simple-page-master master-name="slide"
          page-height="480px" page-width="640px"
          margin-left="20px" margin-right="20px">
          <fo:region-body region-name="main"
            margin-top="80px" margin-bottom="50px"/>
          <fo:region-before region-name="header" extent="60px"/>
          <fo:region-after region-name="footer" extent="50px"/>
        </fo:simple-page-master>
      </fo:layout-master-set>

      <!--********************* table of contents *************************-->

      <fo:page-sequence master-name="toc">
        <xsl:comment>*******TABLE OF CONTENTS********</xsl:comment>
        <fo:static-content flow-name="header">
            <!-- w3c logo -->
            <fo:block>
              <!-- fop formats external-graphic as block, not inline -->
              <!-- this text should be next to the graphic -->
<!--              <xsl:text>BIG TITLE</xsl:text> -->

              <fo:external-graphic src="file:w3c.png" 
                height="30px" content-height="30px"/>
          </fo:block>


          <fo:block xsl:use-attribute-sets="h1">
            <xsl:value-of select="/html:html/html:head/html:title"/>
          </fo:block>
          <fo:block xsl:use-attribute-sets="h2">
            <fo:block>
              <xsl:value-of select="/html:html/html:head/html:meta[@name='author']/@content"/>
            </fo:block>
            <fo:block>
              <xsl:value-of select="/html:html/html:head/html:meta[@name='event']/@content"/>
            </fo:block>
            <fo:block>
              <xsl:value-of select="/html:html/html:head/html:meta[@name='date']/@content"/>

            </fo:block>
            <fo:block space-after.optimum="10px">
            <fo:leader leader-pattern="rule" leader-length="600px"
              rule-thickness="2px"/>
          </fo:block>

          </fo:block>
        </fo:static-content>

        <fo:flow flow-name="main">
          <xsl:comment>List containing TOC</xsl:comment>
          <fo:list-block start-indent="10mm">
            <xsl:apply-templates select="/html:html/html:body/html:h1" 
              mode="toc"/>
          </fo:list-block>
        </fo:flow>
        <xsl:comment>*****End of TOC***********************</xsl:comment>
      </fo:page-sequence>

      <!--**********************************************-->

      <!-- slides -->

        <xsl:comment>*****Start of Slides***********************</xsl:comment>

      <xsl:apply-templates select="html:html/html:body/html:h1"/>
    </fo:root>
  </xsl:template>

<!-- ************************** -->

  <!-- table of contents template for h1 -->
  <xsl:template match="html:h1" mode="toc">
    <fo:list-item xsl:use-attribute-sets="toc">
      <fo:list-item-label end-indent="label-end()"><fo:block>
      <fo:inline font-family="ZapfDingbats" font-size="15px">&#x25CF;</fo:inline></fo:block>
      </fo:list-item-label>
      <fo:list-item-body start-indent="body-start()">
        <!-- FOP bug: the attrib-set should be on top-level fo:list-block -->
        <fo:block>
          <fo:basic-link internal-destination="{generate-id()}" color="blue">
            <xsl:value-of select="."/>
          </fo:basic-link>
        </fo:block>
      </fo:list-item-body>
    </fo:list-item>
  </xsl:template>

<!-- ************************** -->

  <!-- for each h1 in the source document, generate a new slide -->

  <xsl:template match="html:h1">
    <fo:page-sequence master-name="slide">
      <!-- FOP BUG:  id="{generate-id()}" should be here -->

      <!-- SLIDE HEADER: w3c logo -->

      <fo:static-content flow-name="header">
        <fo:block>

<!--          <fo:block> -->
<!--            <fo:external-graphic src="file:w3c.png" content-height="40px" -->
<!--              height="40px"/> -->
<!--          </fo:block> -->

          <!-- FOP doesn't grok this svg file -->
<!--            <fo:instream-foreign-object> -->
<!--              &w3c-logo; -->
<!--            </fo:instream-foreign-object> -->

        <fo:block xsl:use-attribute-sets="h1">
          <xsl:copy-of select="@id"/>
          <xsl:apply-templates/>
        </fo:block>

        <!-- rule -->
        <fo:block>
          <fo:leader leader-pattern="rule" leader-length="600px"
            rule-thickness="2px"/>
        </fo:block>
      </fo:block>
      </fo:static-content>

      <!-- slide footer: Title, author and page -->
      <fo:static-content flow-name="footer">
        <fo:block xsl:use-attribute-sets="footer">
          <fo:leader leader-pattern="rule" leader-length="600px"
            rule-thickness="2px" space-before="0px"/>

        <!-- put the three footer items (title, author, page) in a -->
        <!-- 1-row table to handle length overflows -->

        <!-- FOP doesn't do table-layout="auto" -->

        <!-- latest CVS crashed on this table -->
        <fo:table>
          <fo:table-column column-width="242px"/>
          <fo:table-column column-width="242px"/>
          <fo:table-column column-width="92px"/>
          <fo:table-body>
            <fo:table-row>
              <fo:table-cell>
                <fo:block text-align="start">
                  <xsl:value-of select="/html:html/html:head/html:title"/>
                </fo:block>
              </fo:table-cell>
              <fo:table-cell>
                <fo:block text-align="center">
                  <xsl:value-of select="/html:html/html:head/html:meta[@name='author']/@content"/>
                </fo:block>
              </fo:table-cell>
              <fo:table-cell>
                <fo:block text-align="end">
                  <xsl:number/>
                  <xsl:text> of </xsl:text>
                  <xsl:value-of 
                    select="count(/html:html/html:body/html:h1)"/>
                </fo:block>
              </fo:table-cell>
            </fo:table-row>
          </fo:table-body>
        </fo:table>
        </fo:block>
      </fo:static-content>

      <fo:flow flow-name="main" xsl:use-attribute-sets="body"  >
        <!-- include in this slide what's after the h1 until next h1 -->

            <!-- FOP doesn't grok this svg file -->
<!--              <fo:block border-style="solid" border-width="1pt" border-color="blue"> -->
<!--              <fo:instream-foreign-object> -->
<!--                &w3c-logo; -->
<!--              </fo:instream-foreign-object> -->
<!--            </fo:block> -->


        <!-- dummy block necessary as otherwise two consecutive h1s -->
        <!-- produce an empty flow here -->

        <!-- generate id should be in enclosing page-sequence -->
        <!-- but FOP chokes on that -->

        <!-- FOP BUG: id="{generate-id()}" shouldn't be here but on -->
        <!-- enclosing page sequence -->
        <fo:block id="{generate-id()}">
          <xsl:apply-templates select="following-sibling::*[not(name()='h1')][preceding-sibling::html:h1[1] = current()]"/>
        </fo:block>
    </fo:flow>
  </fo:page-sequence>
  </xsl:template>

  <xsl:template match="html:h2">
    <fo:block xsl:use-attribute-sets="h2">
      <xsl:apply-templates/>
    </fo:block>
  </xsl:template>

  <xsl:template match="html:p">
    <fo:block xsl:use-attribute-sets="body">
      <xsl:apply-templates/>
    </fo:block>
  </xsl:template>

<xsl:template match="html:a[@href]">
  <fo:basic-link xsl:use-attribute-sets="a">
    <xsl:choose>
      <xsl:when test="substring(@href,1,1)='#'">
        <xsl:attribute name="internal-destination">
          <xsl:value-of select="substring-after(@href,'#')"/>
        </xsl:attribute>
        <xsl:apply-templates/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:attribute name="external-destination">
          <xsl:value-of select="@href"/>
        </xsl:attribute>
        <xsl:apply-templates/>
      </xsl:otherwise>
    </xsl:choose>
  </fo:basic-link>
</xsl:template>

<xsl:template match="html:a[@name]">
  <fo:wrapper id="{@name}">
    <xsl:apply-templates/>
  </fo:wrapper>
</xsl:template>



  <xsl:template match="html:ul | html:ol">
    <fo:list-block start-indent="10mm">
      <xsl:apply-templates/>
    </fo:list-block>
  </xsl:template>

  <xsl:template match="html:li/html:ul | html:li/html:ol">
    <fo:list-block font-size="90%" start-indent="10mm">
      <xsl:apply-templates/>
    </fo:list-block>
  </xsl:template>

  <xsl:template match="html:li">
    <fo:list-item space-after="10px">
      <fo:list-item-label end-indent="label-end()">
        <fo:block>
          <xsl:choose>
            <xsl:when test="../../html:ol">
              <fo:block text-align="end"><xsl:number/>. </fo:block>
            </xsl:when>
            <xsl:otherwise>

<fo:inline font-family="ZapfDingbats" font-size="15px">&#x25CF;</fo:inline>
<!-- FOP bug: should be fo:character above -->

            </xsl:otherwise>
          </xsl:choose>
        </fo:block>
      </fo:list-item-label>
      <fo:list-item-body start-indent="body-start()">
        <fo:block><xsl:apply-templates/></fo:block>
      </fo:list-item-body>
    </fo:list-item>
  </xsl:template>

  <xsl:template match="html:pre">

    <!-- fop doesn't do linefeed-treatment -->
    <fo:block color="navy"  
              font-family="monospace"
              font-weight="bold"
              font-size="14px"
              wrap-option="no-wrap"
              overflow="hidden"
              space-after.optimum=".5em"
              linefeed-treatment="preserve"
              white-space-collapse="false">
      <xsl:apply-templates/>
    </fo:block>
  </xsl:template>

  <xsl:template match="html:br">
    <fo:character character="&#x0A;"/>
  </xsl:template>

  <xsl:template match="html:code">
    <fo:inline font-family="monospace">
      <xsl:apply-templates/>
    </fo:inline>
  </xsl:template>

  <!-- make an image fill the slide @@ use scale-to-fit -->
  <!-- @@ make all graphics SVG -->
  <xsl:template match="html:img">
    <fo:block text-align="center">
      <fo:external-graphic src="file:{@src}"
        content-height="{@height}px" 
        height="{@height}px"/>

    </fo:block>
  </xsl:template>

  <xsl:template match="html:strong">
    <fo:inline font-weight="bold"><xsl:apply-templates/></fo:inline>
  </xsl:template>

  <xsl:template match="html:q">
    <fo:inline font-style="italic">"<xsl:apply-templates/>"</fo:inline>
  </xsl:template>


  <!-- copy any svg in the source straight to output -->
  <xsl:template match="svg:svg">
    <fo:instream-foreign-object>
      <xsl:copy-of select="."/>
    </fo:instream-foreign-object>
  </xsl:template>

  <!-- kbd is reserved for annotations in source so ignore it -->
  <xsl:template match="html:kbd"/>
  <xsl:template match="html:p[html:kbd]"/>

</xsl:stylesheet>
