<!--

make-rss-transform.xsl
Copyright (c) 2004-2005, L. David Baron

vim: set noet ts=4 sw=4 :

process using:
xsltproc http://dbaron.org/log/make-rss-transform.xsl http://dbaron.org/log/ > rss1.rss
(or with make-rss.pl, which does the same thing in a slightly more complex way)

  -->
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:html="http://www.w3.org/1999/xhtml"
                xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
                xmlns:dc="http://purl.org/dc/elements/1.1/"
                xmlns:rss="http://purl.org/rss/1.0/"
                xmlns:content="http://purl.org/rss/1.0/modules/content/"
                exclude-result-prefixes="html"
                >
<xsl:output method="xml" encoding="UTF-8" />

<!-- process http://dbaron.org/log/ -->
<xsl:template match="/">
	<rdf:RDF>
		<rss:channel rdf:about="http://dbaron.org/log/">
			<rss:title>David Baron's Weblog</rss:title>
			<rss:link>http://dbaron.org/log/</rss:link>
			<rss:description>David Baron's weblog</rss:description>
			<dc:creator>David Baron</dc:creator>
			<dc:language>en-US</dc:language>
			<rss:items>
				<rdf:Seq>
					<xsl:call-template name="main">
						<xsl:with-param name="header" select="true()" />
					</xsl:call-template>
				</rdf:Seq>
			</rss:items>
		</rss:channel>
		<xsl:call-template name="main">
			<xsl:with-param name="header" select="false()" />
		</xsl:call-template>
	</rdf:RDF>
</xsl:template>

<xsl:template name="main">
	<xsl:param name="header" />
	<xsl:for-each select="document(.//html:body/html:ol/html:li/html:a/@href)//html:*[@class='entry']">
		<xsl:choose>
			<xsl:when test="count(self::html:body) = 1">
				<xsl:variable name="entryurl" select="concat('http://dbaron.org/log/', ./ancestor::html:html//html:title/following-sibling::comment()[position()=1])" />
				<xsl:variable name="date" select="./html:h3/html:span[@class='date']" />
				<xsl:variable name="time" select="./html:h3/html:span[@class='time']" />
				<xsl:variable name="timezone" select="./html:h3/html:span[@class='timezone']" />
				<xsl:variable name="datetime" select="concat($date, 'T', $time, ':00', substring($timezone, 1, 3), ':', substring($timezone, 4, 2))" />
				<xsl:choose>
					<xsl:when test="$header">
						<rdf:li rdf:resource="{$entryurl}" />
					</xsl:when>
					<xsl:otherwise>
						<rss:item rdf:about="{$entryurl}">
							<rss:title>
								<xsl:value-of select="./html:h2[position()=1]" />
							</rss:title>
							<rss:link>
								<xsl:value-of select="$entryurl" />
							</rss:link>
							<dc:date>
								<xsl:value-of select="$datetime" />
							</dc:date>
							<dc:creator>David Baron</dc:creator>
							<xsl:if test="position()&lt;=5">
								<content:encoded>
									<xsl:text disable-output-escaping="yes">&lt;![CDATA[</xsl:text>
									<div xmlns="http://www.w3.org/1999/xhtml">
										<xsl:apply-templates select="./html:div[@class='entrybody']/node()" mode="fixhtml" />
									</div>
									<xsl:text disable-output-escaping="yes">]]&gt;</xsl:text>
								</content:encoded>
							</xsl:if>
						</rss:item>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:when>
			<xsl:otherwise>
				<xsl:variable name="fragid" select="./*[position()=1 and (self::html:h3 or self::html:h2)]/html:a/@href" />
				<xsl:variable name="year"  select="substring($fragid, 3, 4)" />
				<xsl:variable name="month" select="substring($fragid, 7, 2)" />
				<xsl:variable name="newstyle" select="number($year)>2003 or (number($year)=2003 and number($month)>=9)" />
				<xsl:variable name="entryurl" select="concat('http://dbaron.org/log/', $year, '-', $month, $fragid)" />
				<xsl:choose>
					<xsl:when test="$header">
						<rdf:li rdf:resource="{$entryurl}" />
					</xsl:when>
					<xsl:otherwise>
						<rss:item rdf:about="{$entryurl}">
							<rss:title>
								<xsl:choose>
									<xsl:when test="$newstyle">
										<xsl:value-of select="./html:h3[position()=1]/html:a" />
									</xsl:when>
									<xsl:otherwise>
										<xsl:value-of select="concat(normalize-space(substring(./html:h2[position()=1]/following-sibling::*, 0, 40)), '...')" />
									</xsl:otherwise>
								</xsl:choose>
							</rss:title>
							<rss:link>
								<xsl:value-of select="$entryurl" />
							</rss:link>
							<dc:date>
								<xsl:variable name="date" select="substring(./*[position()=1 and (self::html:h2 or self::html:h3)]/html:a/@href, 3, 8)" />
								<xsl:value-of select="concat(substring($date, 1, 4),
															 '-',
															 substring($date, 5, 2),
															 '-',
															 substring($date, 7, 2))" />
								<xsl:if test="$newstyle">
									<xsl:variable name="time" select="substring(./html:h3/html:a/following-sibling::text(), 3, 11)" />
									<xsl:value-of select="concat('T',
																 substring($time, 1, 5),
																 ':00-',
																 substring($time, 8, 2),
																 ':00')" />
								</xsl:if>
							</dc:date>
							<dc:creator>David Baron</dc:creator>
							<xsl:if test="position()&lt;=5">
								<content:encoded>
									<xsl:text disable-output-escaping="yes">&lt;![CDATA[</xsl:text>
									<div xmlns="http://www.w3.org/1999/xhtml">
										<xsl:apply-templates select="./*[position()=1 and (self::html:h2 or self::html:h3)]/following-sibling::node()" mode="fixhtml" />
									</div>
									<xsl:text disable-output-escaping="yes">]]&gt;</xsl:text>
								</content:encoded>
							</xsl:if>
						</rss:item>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:for-each>
</xsl:template>

<xsl:template match="@*|node()" mode="fixhtml">
	<xsl:copy>
		<xsl:apply-templates select="@*|node()" mode="fixhtml" />
	</xsl:copy>
</xsl:template>

<xsl:template match="html:a[not(ancestor::html:map)]/@shape" mode="fixhtml" />

</xsl:stylesheet>
