<xsl:transform
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://www.w3.org/1999/xhtml"
    xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'
    xmlns:c="http://www.w3.org/2002/12/cal/icaltzd#"
    >
<!-- $Id: weekCal.xsl,v 1.2 2005/08/05 06:59:00 connolly Exp $ -->
<!-- initial test data: http://www.w3.org/2004/lambda/Sites/index.html
-->
<xsl:output method="xml" indent="yes" />

<xsl:template match="/">
  <rdf:RDF>
    <xsl:apply-templates />
  </rdf:RDF>
</xsl:template>

<xsl:template match='*[@class="vcalendar"]'>
  <c:Vcalendar>
    <xsl:apply-templates />
  </c:Vcalendar>
</xsl:template>

<xsl:template match='h:table[@class="weekly"]'>
  <!-- @@ make tbody optional -->

  <xsl:variable name="ymd1" select='normalize-space(.//*[@class="dtstart"])' />
  <xsl:variable name="y" select="number(substring($ymd1, 1, 4))" />
  <xsl:variable name="m" select="number(substring($ymd1, 6, 2))" />
  <xsl:variable name="d" select="number(substring($ymd1, 9, 2))" />

  <!-- @@absolutize -->
  <xsl:variable name="tz" select='h:caption/h:a[@class="tz"]/@href' />

  <xsl:for-each select="h:tbody/h:tr">
    <xsl:for-each select="h:td">
      <!-- pick hh:mm off start of cell -->
      <xsl:variable name="summary"
		    select='substring-after(normalize-space(.), " ")' />

      <xsl:if test="$summary">
	<xsl:variable name="dcol"
		      select="position() - 1" />

	<xsl:variable name="dow"
		      select='substring("MO TU WE TH FR SA SU",
			      $dcol * 3 + 1, 2)'
		      />

	<xsl:variable name="ymd2">
	  <xsl:call-template name="add-days">
	    <xsl:with-param name="y" select="$y" />
	    <xsl:with-param name="m" select="$m" />
	    <xsl:with-param name="d" select="$d" />
	    <xsl:with-param name="d-max" select="31" /> <!-- @@kludge -->
	    <xsl:with-param name="delta" select="$dcol" />
	  </xsl:call-template>
	</xsl:variable>

	<xsl:variable name="dtstart"
		      select='concat($ymd2, "T",
			      substring-before(normalize-space(.), " "))' />

	<c:component>
	  <!-- hmm... @id? -->
	  <c:Vevent>
	    <c:summary><xsl:value-of select="$summary" /></c:summary>
	    <!-- c:description -->
	    <c:dtstart rdf:datatype='{$tz}'>
	      <xsl:value-of select="$dtstart" />
	    </c:dtstart>

	    <!-- @@other durations -->
	    <c:duration rdf:parseType="Resource">
	      <c:duration>P1H</c:duration>
	    </c:duration>

	    <c:rrule rdf:parseType="Resource">
	      <c:freq>WEEKLY</c:freq>
	      <!-- @@ more than one day per week -->
	      <c:byday><xsl:value-of select="$dow" /></c:byday>
	    </c:rrule>
	  </c:Vevent>
	</c:component>
      </xsl:if>
    </xsl:for-each>
  </xsl:for-each>
</xsl:template>


<xsl:template name="add-days">
  <xsl:param name="y" />
  <xsl:param name="m" />
  <xsl:param name="d" />
  <xsl:param name="d-max" />
  <xsl:param name="delta" />

  <xsl:variable name="d-out">
    <xsl:choose>
      <xsl:when test='$d + $delta &gt; $d-max'>
	<xsl:value-of select='$d + $delta - $d-max' />
      </xsl:when>
      <xsl:otherwise>
	<xsl:value-of select='$d + $delta' />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:variable name="m2">
    <xsl:choose>
      <xsl:when test='$d + $delta &gt; $d-max'>
	<xsl:value-of select='$m + 1' />
      </xsl:when>
      <xsl:otherwise>
	<xsl:value-of select='$m' />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:variable name="m-out">
    <xsl:choose>
      <xsl:when test='$m2 &gt; 12'>
	<xsl:value-of select='1' />
      </xsl:when>
      <xsl:otherwise>
	<xsl:value-of select='$m2' />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:variable name="y-out">
    <xsl:choose>
      <xsl:when test='$m2 &gt; 12'>
	<xsl:value-of select='$y + 1' />
      </xsl:when>
      <xsl:otherwise>
	<xsl:value-of select='$y' />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:value-of select='concat(
			format-number($y-out, "0000"), "-",
			format-number($m-out, "00"), "-",
			format-number($d-out, "00"))' />
</xsl:template>


<!-- don't pass text thru -->
<xsl:template match="text()|@*">
</xsl:template>


</xsl:transform>
