<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xhtml="http://www.w3.org/1999/xhtml"
	xmlns:str="http://exslt.org/strings"
    xmlns="http://www.w3.org/1999/xhtml"
    exclude-result-prefixes="xhtml"
	extension-element-prefixes="str">

    <xsl:param name="xml" />
    <xsl:param name="xsl" />
    <xsl:param name="mode" />
    <xsl:param name="suboutline" />
    <xsl:param name="stylesheet" select="'opml-to-xoxo.xsl'" />
    <xsl:param name="css"        select="'opml-to-xoxo.css'" />

    <!--
        Global transformations.
    -->

    <!-- expansionState will probably be munged by transformations here, so excise it. -->
    <xsl:template match="/opml/head/expansionState"></xsl:template>

    <xsl:template match="/">
        
        <!-- Is there a client-side XSL supplied? If so, inject it.  -->
        <xsl:if test="$mode!='toc'">
            <xsl:processing-instruction name="xml-stylesheet">type="text/xsl" href="<xsl:value-of select="$stylesheet" />"</xsl:processing-instruction><xsl:text>
</xsl:text>
        </xsl:if>

        <!-- Master mode switch. -->
        <xsl:choose>
            <xsl:when test="$mode='toc'"><xsl:apply-templates mode="toc" /></xsl:when>
            <xsl:when test="$mode='readinglist'"><xsl:apply-templates mode="readinglist" /></xsl:when>
            <xsl:otherwise><xsl:apply-templates mode="passthrough" /></xsl:otherwise>
        </xsl:choose>
    
    </xsl:template>

    <!-- Default node pass-through mode -->
    <xsl:template mode="passthrough" match="node() | @*">
        <xsl:copy><xsl:apply-templates mode="passthrough" select="node() | @*"/></xsl:copy>
    </xsl:template>

    <!--
        TOC processing.
    -->
    <xsl:template mode="toc" match="/opml">
        <html>
            <head>
                <title><xsl:value-of select="/opml/head/title/text()" /> - available reading lists</title>
                <link rel="stylesheet" type="text/css" href="{$css}" />
            </head>
            <body>
                <h1><xsl:value-of select="/opml/head/title/text()" /> - available reading lists</h1>
                <ul>
                    <xsl:for-each select="/opml/body/outline">
                        <li>
                            <a class="outline" href="?mode=readinglist&amp;suboutline={@text}&amp;xml={$xml}&amp;xsl={$xsl}">OPML</a>
                            &#8212;
                            <xsl:value-of select="@text" />
                        </li>
                    </xsl:for-each>
                </ul>
            </body>
        </html>
    </xsl:template>

    <!--
        Named reading list processing.
    -->

    <!-- Tweak the OPML title with respect to the suboutline -->
    <xsl:template mode="readinglist" match="/opml/head/title/text()">
        <xsl:value-of select="." /> - <xsl:value-of select="$suboutline" />
    </xsl:template>

    <!-- Filter out all outline nodes not matching given name -->
    <xsl:template mode="readinglist" match="/opml/body/outline[@text!=$suboutline]"></xsl:template>

    <!-- Under the desired sub-list, find and flatten out all type=rss nodes with xmlUrl defined -->
    <xsl:template mode="readinglist" match="/opml/body/outline[@text=$suboutline]">
        <xsl:for-each select=".//outline[@type='rss' and @xmlUrl!='']">
            <xsl:copy><xsl:apply-templates mode="readinglist" select="node() | @*"/></xsl:copy>
            <xsl:text>
                </xsl:text>
        </xsl:for-each>
    </xsl:template>

    <!-- Sometimes have problems with odd descriptions. -->
    <!-- TODO: Find something less destructive to do with descriptions. -->
    <xsl:template mode="readinglist" match="outline/@description"></xsl:template>

    <!-- Pass through all other nodes -->
    <xsl:template mode="readinglist" match="node() | @*">
        <xsl:copy><xsl:apply-templates mode="readinglist" select="node() | @*"/></xsl:copy>
    </xsl:template>

</xsl:stylesheet>