<?xml version="1.0"?>
<!--

Author: Ben West
Website: bewest.wordpress.com and siliconllama.com
Email: bewest at gmail.com

rss2hp.xsl
============
This will transform an rss version 2 document into opml suitable for
hyperscope.

Acknowledgements:
l. m. orchards  - this is based heavily on his xoxo2hp


TODO:
	* add rss version 1.
-->
<xsl:stylesheet version="1.0" 
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:hs="http://www.hyperscope.org/hyperscope/opml/public/2006/05/09"
	>
    
	<!-- TODO: is this system dependent? -->
	<xsl:param name="HS_JS_URI" select="'/hyperscope/src/client/lib/hs/xslt/hyperscope.xsl'" />
    
<xsl:output indent="yes" method="xml" />

<xsl:template match="/">
<!-- This outputs a stylesheet instruction so hyperscope can render it. -->				
<xsl:processing-instruction 
	name="xml-stylesheet">type="text/xsl" href="<xsl:value-of select="$HS_JS_URI" />"
</xsl:processing-instruction><xsl:text>
</xsl:text>
	<opml hs:version="1.0" version="2.0">
	<xsl:comment>
		<!-- channel variables -->
	</xsl:comment>
		<xsl:variable name="title" select="/rss/channel/title" />
		<xsl:variable name="updated" select="/rss/channel/lastBuildDate" />
		<xsl:variable name="link" select="/rss/channel/link" />
		<xsl:variable name="pubDate" select="/rss/channel/pupDate" />
		<xsl:variable name="description" select="/rss/channel/description" />
		<xsl:variable name="lastBuildDate" select="/rss/channel/lastBuildDate" />
		<head>
			<title><xsl:value-of select="$title" /></title>
			<xsl:if test="$pubDate">
				<dateCreated><xsl:value-of select="$pubDate" /></dateCreated>
			</xsl:if>
			<xsl:if test="$pubDate">
				<dateModified><xsl:value-of select="$lastBuildDate" /></dateModified>
			</xsl:if>
		</head>
		<body>
			<outline hs:label="Title">
				<xsl:attribute name="text">
					<xsl:value-of select="$title" />
				</xsl:attribute>
			</outline>
			<outline hs:label="Description">
				<xsl:attribute name="text">
					<xsl:value-of select="$description" />
				</xsl:attribute>
			</outline>
			<!-- TODO: add number of posts -->
			<outline hs:label="Posts" text="Posts">
				<xsl:for-each select="/rss/channel/item">
						<xsl:call-template name="entries" />
				</xsl:for-each>
			</outline>
		</body>
	</opml>
</xsl:template>

<xsl:template name="entries">
	<xsl:comment>
		<!-- Entry Variables -->
	</xsl:comment>
	<xsl:variable name="title" select="./title" />
	<xsl:variable name="link" select="./link" />
	<xsl:variable name="content" select="./content:encoded" />
	<xsl:variable name="description" select="./description" />
	<xsl:comment>
	<!-- yay... rss uses the same date type -->
	</xsl:comment>
	<xsl:variable name="pubDate" select="./pubDate" />
	<xsl:variable name="guid" select="./guid" />
	<outline>
		<xsl:attribute name="hs:label">
			<xsl:value-of select="$title" />
		</xsl:attribute>

		<xsl:attribute name="hs:createdOn">
			<xsl:value-of select="$pubDate" />
		</xsl:attribute>
		<!-- TODO: how do links work in hyperscope? -->
		<xsl:attribute name="text">
			<xsl:value-of select="$link" />
		</xsl:attribute>

		<outline>
			<xsl:attribute name="hs:label">
				<xsl:value-of select="$description" />
			</xsl:attribute>
			<xsl:attribute name="text">
				<xsl:value-of select="$content" />
			</xsl:attribute>
			</outline>
	</outline>
</xsl:template>
</xsl:stylesheet>
