<!--

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

vim: set noet ts=4 sw=4 :

process using:
xsltproc http://dbaron.org/log/make-atom-transform.xsl http://dbaron.org/log/ > atom-feed.atom
(or with make-atom.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:date="http://exslt.org/dates-and-times"
                xmlns:html="http://www.w3.org/1999/xhtml"
                xmlns:atom="http://www.w3.org/2005/Atom"
                xmlns="http://www.w3.org/2005/Atom"
                exclude-result-prefixes="html atom date"
                >
<xsl:output method="xml" encoding="UTF-8" />

<!-- process http://dbaron.org/log/ -->
<xsl:template match="/">
	<feed xml:lang="en-US">
		<title>David Baron's Weblog</title>
		<link rel="alternate" type="text/html" hreflang="en-US" href="http://dbaron.org/log/" />
		<link rel="self" type="application/atom+xml" hreflang="en-US" href="http://dbaron.org/log/atom-feed" />
		<author><name>David Baron</name></author>
		<!-- Probably better to use the most recent of the feed's
		updated times, but that's too much work. -->
		<updated><xsl:value-of select="date:date-time()" /></updated>
		<id>http://dbaron.org/log/atom-feed</id>
		<rights>Copyright &#xA9; <xsl:value-of select="substring(.//html:body/html:ol/html:li[position()=last()]/html:a/@href, 1, 4)" />-<xsl:value-of select="substring(.//html:body/html:ol/html:li[position()=1]/html:a/@href, 1, 4)" />, L. David Baron</rights>
		<xsl:for-each select="document(.//html:body/html:ol/html:li/html:a/@href)//html:*[@class='entry']">
			<xsl:text>&#x0A;</xsl:text>
			<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))" />
					<entry>
						<title>
							<xsl:value-of select="./html:h2[position()=1]" />
						</title>
						<link rel="alternate" type="text/html" href="{$entryurl}" />
						<id><xsl:value-of select="$entryurl" /></id>
						<updated><xsl:value-of select="$datetime" /></updated>
						<published><xsl:value-of select="$datetime" /></published>
						<author>
							<name>David Baron</name>
							<uri>http://dbaron.org/</uri>
							<email>dbaron@dbaron.org</email>
						</author>
						<xsl:if test="position()&lt;=5">
							<content type="xhtml" xml:base="{$entryurl}">
								<div xmlns="http://www.w3.org/1999/xhtml">
									<xsl:apply-templates select="./html:div[@class='entrybody']/node()" mode="fixhtml" />
								</div>
							</content>
						</xsl:if>
					</entry>
				</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:variable name="datetime">
						<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:choose>
							<xsl:when 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:when>
							<xsl:otherwise>
								<!-- Ugh.  Atom doesn't allow inexact times -->
								<xsl:value-of select="'T23:59:59-08:00'" />
							</xsl:otherwise>
						</xsl:choose>
					</xsl:variable>
					<entry>
						<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>
						</title>
						<link rel="alternate" type="text/html" href="{$entryurl}" />
						<id><xsl:value-of select="$entryurl" /></id>
						<updated><xsl:value-of select="$datetime" /></updated>
						<published><xsl:value-of select="$datetime" /></published>
						<author>
							<name>David Baron</name>
							<uri>http://dbaron.org/</uri>
							<email>dbaron@dbaron.org</email>
						</author>
						<xsl:if test="position()&lt;=5">
							<content type="xhtml" xml:base="{$entryurl}">
								<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>
							</content>
						</xsl:if>
					</entry>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:for-each>
	</feed>
</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>
