<?xml version='1.0'?>

<xsl:stylesheet 
  version='1.0' 
  xmlns='http://www.w3.org/1999/xhtml'
  xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
  xmlns:ss='http://simp.mitre.org/simple-slides'
  exclude-result-prefixes="ss"
  >

<!--
     Transformer for Simple Slides

     Version History

     * 2004-May-10

       Initial version

     * 2004-May-21

       Added simple-slides processing instruction for setting css source

     * 2004-May-24

       Switched from XHTML Basic to XHTML 1.1
-->

<!-- get css attribute from the simple-slides processing instruction -->

<xsl:variable name="pi-css"
    select="substring-after(
        /processing-instruction('simple-slides')[contains(.,'css=')],
        'css=')"/>

<xsl:param name="css">
  <xsl:choose>
    <xsl:when test="$pi-css">
      <xsl:value-of select="$pi-css"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of 
           select="'http://simp.mitre.org/simple-slides/simple-slides.css'"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:param>

<xsl:output 
  method='xml' 
  indent='yes'
  doctype-public='-//W3C//DTD XHTML 1.1//EN'
  doctype-system='http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd'
  encoding='UTF-8'
  media-type='text/xhtml'
  version='1.0'/>

<xsl:template match='/'>
<xsl:apply-templates select='ss:slides'/>
</xsl:template>

<xsl:template match='ss:slides'>
<html xml:lang='en'>
<head>
<meta http-equiv="Content-Style-Type" content="text/css"/>
<link rel="stylesheet" title="simple slides" type="text/css">
  <xsl:attribute name="href"><xsl:value-of select='$css'/></xsl:attribute>
</link>
<title><xsl:value-of select='@title'/></title>
</head>

<body>
<xsl:apply-templates select='ss:slide|ss:note'/>
<xsl:call-template name='TOC'/>
</body>
</html>
</xsl:template>

<!-- The note template -->
<xsl:template match='ss:note'>

<div class="note">
<h1><xsl:value-of select='@title'/></h1>
<xsl:apply-templates/>
</div>

</xsl:template>

<!-- The slide template -->
<xsl:template match='ss:slide'>
<!-- The variable 'page' contains the slide number. -->
<xsl:variable name='page' select='count(preceding-sibling::ss:slide)+1'/>

<div class="slide">
<!-- JavaScript mouse click hack -->
<xsl:choose>
<xsl:when test='position() = last()'>
<xsl:attribute name="onclick">window.location.replace('#toc')</xsl:attribute>
</xsl:when>
<xsl:otherwise>
<xsl:attribute name="onclick">window.location.replace('#slide-<xsl:value-of select='$page + 1'/>')</xsl:attribute>
</xsl:otherwise>
</xsl:choose>
<!-- Create navigation links -->
<p class='nav'>
<a><xsl:attribute name="id">slide-<xsl:value-of select='$page'/></xsl:attribute></a>
<xsl:choose>
<xsl:when test='position() = last()'>
[<a><xsl:attribute name="href">#slide-<xsl:value-of select='$page - 1'/></xsl:attribute>previous</a>]
[<a href="#toc">up</a>]
[next]
</xsl:when>
<xsl:when test='$page = 1'>
[previous]
[<a href="#toc">up</a>]
[<a><xsl:attribute name="href">#slide-<xsl:value-of select='$page + 1'/></xsl:attribute>next</a>]
</xsl:when>
<xsl:otherwise>
[<a><xsl:attribute name="href">#slide-<xsl:value-of select='$page - 1'/></xsl:attribute>previous</a>]
[<a href="#toc">up</a>]
[<a><xsl:attribute name="href">#slide-<xsl:value-of select='$page + 1'/></xsl:attribute>next</a>]
</xsl:otherwise>
</xsl:choose>
</p>
<!-- Create slide content -->
<h1><xsl:value-of select='@title'/></h1>
<xsl:apply-templates/>
</div>

</xsl:template>

<!-- Create table of contents -->
<xsl:template name="TOC">

<div class="index" onclick="window.location.replace('#slide-1')">
<p class="nav">
<a id="toc"></a>
[previous]
[<a href="index.html">up</a>]
[<a href="#slide-1">next</a>]</p>

<h1>Table of Contents</h1>

<ol>
<xsl:for-each select="ss:slide">

<li><a>
<xsl:attribute name="href">#slide-<xsl:value-of select='position()'/></xsl:attribute>
<xsl:value-of select="@title"/>
</a></li>
</xsl:for-each>

</ol>

</div>

</xsl:template>

<!-- copy from source to result tree -->

<xsl:template match="*">
  <xsl:element name="{name()}">
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates />
  </xsl:element>
</xsl:template>

</xsl:stylesheet>
