<!-- Convert HTML extracted from ebay saved search e-mail by ebmail2html.pl
	  to an Atom 1.0 file.

For demo purposes only. No warrantee expressed or implied.
-->

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
					 xmlns:a="http://www.w3.org/2005/Atom"
					 xmlns:date="http://exslt.org/dates-and-times"
					 xmlns:h="http://www.w3.org/1999/xhtml"
					 xmlns:exsl="http://exslt.org/common"
					 extension-element-prefixes="date exsl"
					 exclude-result-prefixes="a h"
					 version="1.0">

  <!-- Minimum number of entries to keep in feed file. -->
  <xsl:variable name="entryCount">30</xsl:variable>

  <!-- How many entries are in source tree. -->
  <xsl:variable name="sourceItemCount" select="count(feed/entry)"/>

  <!-- How many entries we'll need from the old file. -->
  <xsl:variable name="oldEntriesToKeep"
					 select="$entryCount - $sourceItemCount"/>

  <xsl:param name="RSSFilePath">.</xsl:param>

  <!-- Name of atom file to store (and to pull padding entries from). -->
  <xsl:variable name="RSSFilename">
    <xsl:value-of select="$RSSFilePath"/>
    <xsl:text>/</xsl:text>
    <xsl:value-of select="/html/head/meta[@name='filename']/@content"/>
    <xsl:text>.atom</xsl:text>
  </xsl:variable>

  <xsl:output indent="yes"/>

  <!-- ~~~~~~~~~~~~~~~~ Template rules. ~~~~~~~~~~~~~~ -->

  <xsl:template match="html">
	 <xsl:variable name="searchString"
						select="//meta[@name='searchString']/@content"/>

	 <!-- Save document in file named $RSSFilename. -->
    <exsl:document href="{$RSSFilename}" indent="yes">
	 <feed xmlns="http://www.w3.org/2005/Atom">
		<a:title><xsl:value-of select="head/title"/></a:title>
		<a:subtitle>
		  <xsl:text>Latest results for eBay search on "</xsl:text>
		  <xsl:value-of select="translate($searchString,'+',' ')"/>
		  <xsl:text>"</xsl:text>
		</a:subtitle>
		<a:link rel="alternate" type="text/html"
				href="http://search.ebay.com/{$searchString}" />
		
		<a:link rel="self" type="application/atom+xml" href="http://www.snee.com/rss/{head/meta[@name='filename']/@content}.xml" />

		<a:id>
		  <xsl:text>http://www.snee.com/rss/ebayalerts/</xsl:text>
		  <xsl:value-of select="$searchString"/>
		</a:id>

		<a:updated><xsl:value-of select="date:date-time()"/></a:updated>
		<a:generator uri="http://www.snee.com/rss/bin/ebayhtml2atom.xsl/">
		ebayhtml2atom</a:generator>


		<xsl:apply-templates/>
		<xsl:apply-templates mode="oldEntries"
			 select="document($RSSFilename)/a:feed/
						a:entry[position()&lt; $oldEntriesToKeep]" /> 
	 </feed>
	 </exsl:document>
  </xsl:template>


  <!-- If this td element has a meta element for an item id, we want it. -->
  <xsl:template match="td[font/meta[@name='id']]">
	 <xsl:variable name="id"
						select="font/meta[@name='id']/@content"/>
	 <xsl:text>

	 </xsl:text>
		<a:entry>
		  <a:link rel="alternate" href="{font/a/@href}"/>
		  <a:id>
			 <xsl:text>http://www.snee.com/id/ebay</xsl:text>
			 <xsl:value-of select="$id"/>
		  </a:id>
		  <a:updated><xsl:value-of select="date:date-time()"/></a:updated>
		  <xsl:apply-templates select="font[2]"/><!--  the title -->
		  <a:content type="xhtml">
			 <div xmlns="http://www.w3.org/1999/xhtml">
				<h:img src="http://thumbs.ebay.com/pict/{$id}.jpg"/>
				<xsl:apply-templates select="font[position() &gt; 2]"/>
			 </div>
		  </a:content>
		</a:entry>
  </xsl:template>

  <xsl:template match="td[table]|a:feed">
	 <xsl:apply-templates/>
  </xsl:template>


  <xsl:template match="td"/>


  <!-- Second font element is title unless it has a font child. -->
  <xsl:template match="font[position() = 2][count(font) = 0]">
		<a:title>
		  <xsl:apply-templates select="node()"/>
		</a:title>
  </xsl:template>


  <!-- Most text we want, outside of title, is in font elements that
       we'll convert to content/h:div/h:p elements. -->
  <xsl:template match="font">
	 <!-- Skip the little messages we don't care about. -->
	 <xsl:if test="not(contains(@style,'font-size: x-small'))">
		<h:p>
		  <xsl:apply-templates select="text()|b"/>
		</h:p>
	 </xsl:if>
  </xsl:template>


  <!-- We don't want the first font element in each td element. -->
  <xsl:template match="font[position() = 1]"/>


  <!-- Copy all atom and xhtml elements read in with document() verbatim. -->
  <xsl:template match="@*|node()" mode="oldEntries">
	 <xsl:copy>
		<xsl:apply-templates select="@*|node()" mode="oldEntries"/>
	 </xsl:copy>
  </xsl:template>


  <!-- Suppress -->
  <xsl:template match="style | head/title | font/font | td[@nowrap='true'] "/>


</xsl:stylesheet>
