<?xml version="1.0" ?>
<xsl:stylesheet
    version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns="http://www.w3.org/2005/Atom"
    xmlns:h="http://www.w3.org/1999/xhtml"
    xml:lang="en">

    <xsl:output omit-xml-declaration="no" encoding="utf-8" method="xml" indent='yes'/>

    <xsl:variable name='filename'>
      <xsl:value-of select='substring-after(/h:html/h:body/h:a[1]/@href, "#")'/>
    </xsl:variable>

    <xsl:variable name='cvsuri'>
      <xsl:text>http://dev.w3.org</xsl:text>
      <xsl:value-of select='substring-before(/h:html/h:body/h:a[1]/@href, "#")'/>
      <xsl:value-of select='$filename'/>
    </xsl:variable>

    <xsl:template match="/">
      <xsl:apply-templates select='.//h:body'/>
    </xsl:template>

    <xsl:template match="h:body">
      <feed>
	<id>
	  <xsl:value-of select='$cvsuri'/>
	</id>
	<updated>
	  <xsl:call-template name='extract-date'>
	    <xsl:with-param name='date'
			    select='h:i[1]'/>
	  </xsl:call-template>
	</updated>
	<title>	  
	  <xsl:value-of select='$filename'/>
	  <xsl:text> CVS log</xsl:text>
	</title>
	<author>
	  <name>World Wide Web Consortium</name>
	  <uri>
	    <xsl:value-of select='$cvsuri'/>
	  </uri>
	</author>
	<link rel="alternate" type="text/html"
	      href="{$cvsuri}" />
	<link rel="self"
	      href="{$cvsuri}" />
	<rights>World Wide Web Consortium</rights>
	<xsl:apply-templates  select='.//h:a[contains(@id, "rev")]'/>
      </feed>
    </xsl:template>

    <xsl:template match="h:a">
      <xsl:variable name='version'>
	<xsl:value-of select='substring-after(@id, "rev")'/>
      </xsl:variable>

      <xsl:variable name='checkout'>
	<xsl:text>http://dev.w3.org</xsl:text>
	<xsl:variable name='s' select='replace(following-sibling::h:a[@href][2]/@href, "iso-8859-1", "utf-8")'/>
	<xsl:choose>
	  <xsl:when test='contains($filename, ".xml")'>
	    <xsl:value-of select='replace($s, "text/plain", "application/xml")'/>
	  </xsl:when>
	  <xsl:otherwise>
	    <xsl:value-of select='$s'/>
	  </xsl:otherwise>
	</xsl:choose>
      </xsl:variable>

      <xsl:variable name='contentType'>
	<xsl:value-of select='replace(substring-after($checkout, "content-type="), "%20", " ")'/>
      </xsl:variable>

      <xsl:variable name='date'>
	<xsl:value-of select='following-sibling::h:i[1]'/>
      </xsl:variable>

      <xsl:variable name='author'>
	<xsl:value-of select='following-sibling::h:i[2]'/>
      </xsl:variable>

      <entry>
	<title>
	  <xsl:value-of select='$filename'/>
	  <xsl:text> revision </xsl:text>
	  <xsl:value-of select='$version'/>
	</title>
	<id>
	  <xsl:value-of select='$cvsuri'/>
	  <xsl:text>#rev</xsl:text>
	  <xsl:value-of select='$version'/>
	</id>
	<published>
	  <xsl:call-template name='extract-date'>
	    <xsl:with-param name='date'
			    select='$date'/>
	  </xsl:call-template>
	</published>
	<author>
	  <name><xsl:value-of select='$author'/></name>
	</author>
	<link rel="alternate" type="text/html">
	  <xsl:attribute name='href'>
	    <xsl:value-of select='$cvsuri'/>
	    <xsl:text>#rev</xsl:text>
	    <xsl:value-of select='$version'/>
	  </xsl:attribute>
	</link>
	<link rel="enclosure" href="{$checkout}" type='{$contentType}'>
	  <xsl:attribute name='title'>
	    <xsl:text>revision </xsl:text>
	    <xsl:value-of select='$version'/>
	  </xsl:attribute>
	</link>
	<category scheme='{$cvsuri}' term="{$version}">
	  <xsl:attribute name='label'>
	    <xsl:text>revision </xsl:text>
	    <xsl:value-of select='$version'/>
	  </xsl:attribute>
	</category>
	<content type="xhtml" xml:lang='en'>
	  <h:div>
	    <xsl:apply-templates select='following-sibling::h:pre[1]'/>

	    <h:p><xsl:text>Download </xsl:text>
	    <a href='{$checkout}'>
	      <xsl:value-of select='$filename'/>
	      <xsl:text> revision </xsl:text>
	      <xsl:value-of select='$version'/>
	    </a>
	    <xsl:text>.</xsl:text>
	    </h:p>
	  </h:div>
	</content>	
      </entry>
    </xsl:template>

    <xsl:template match="h:pre">
      <h:pre style='border: 1px solid #ccc;'>
	<xsl:apply-templates select="@*|node()"/>
      </h:pre>
    </xsl:template>

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

    <xsl:template name='extract-date'>
      <xsl:param name='date'/>
      
      <xsl:variable name='tdate'
		    select='tokenize($date, " ")'/>

      <xsl:value-of
	  select='$tdate[5]'/>
      <xsl:text>-</xsl:text>
      <xsl:variable name='month'
	  select='$tdate[2]'/>
      <xsl:choose>
	<xsl:when test='starts-with($month, "Ja")'>
	  <xsl:text>01</xsl:text>
	</xsl:when>
	<xsl:when test='starts-with($month, "Fe")'>
	  <xsl:text>02</xsl:text>
	</xsl:when>
	<xsl:when test='starts-with($month, "Mar")'>
	  <xsl:text>03</xsl:text>
	</xsl:when>
	<xsl:when test='starts-with($month, "Ap")'>
	  <xsl:text>04</xsl:text>
	</xsl:when>
	<xsl:when test='starts-with($month, "May")'>
	  <xsl:text>05</xsl:text>
	</xsl:when>
	<xsl:when test='starts-with($month, "Jun")'>
	  <xsl:text>06</xsl:text>
	</xsl:when>
	<xsl:when test='starts-with($month, "Jul")'>
	  <xsl:text>07</xsl:text>
	</xsl:when>
	<xsl:when test='starts-with($month, "Aug")'>
	  <xsl:text>08</xsl:text>
	</xsl:when>
	<xsl:when test='starts-with($month, "Se")'>
	  <xsl:text>09</xsl:text>
	</xsl:when>
	<xsl:when test='starts-with($month, "Oc")'>
	  <xsl:text>10</xsl:text>
	</xsl:when>
	<xsl:when test='starts-with($month, "No")'>
	  <xsl:text>11</xsl:text>
	</xsl:when>
	<xsl:when test='starts-with($month, "De")'>
	  <xsl:text>12</xsl:text>
	</xsl:when>
	<xsl:otherwise>
	  <xsl:text>01</xsl:text>
	</xsl:otherwise>
      </xsl:choose>
      <xsl:text>-</xsl:text>
      <xsl:variable name='day'>
	<xsl:value-of
	  select='$tdate[3]'/>
      </xsl:variable>
      <xsl:if test='string-length($day)=1'>
	<xsl:text>0</xsl:text>
      </xsl:if>
      <xsl:value-of select='$day'/>

      <xsl:text>T</xsl:text>
      <xsl:value-of select='$tdate[4]'/>
      <xsl:text>Z</xsl:text>
    </xsl:template>
</xsl:stylesheet>
