<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:date="http://exslt.org/dates-and-times"
  xmlns:crypto="http://exslt.org/crypto"
  exclude-result-prefixes="xsl crypto date">

<xsl:output method="xml"
            version="1.0"
            encoding="utf-8"
            doctype-public="-//IETF//DTD XCAL//iCalendar XML//EN"
            doctype-system="DTD/xcal.dtd"
            indent="yes"/>

  <xsl:include href="date.xsl"/>

  <xsl:variable name="baseurl">https://www.c3d2.de/</xsl:variable>

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

  <xsl:template match="news">
    <iCalendar>
      <vcalendar method="PUBLISH" version="2.0" prodid="-//C3D2-Web//event iCal-Export//DE">
        <xsl:for-each select="newsfile">
          <xsl:sort select="date:seconds(document(.)/item/@date)" data-type="number" order="descending" />

          <xsl:if test="document(.)/item/event">
            <xsl:apply-templates select="document(.)/item">
              <xsl:with-param name="newsfile" select="."/>
            </xsl:apply-templates>
          </xsl:if>

        </xsl:for-each>
      </vcalendar>
    </iCalendar>
  </xsl:template>

  <xsl:template match="item">
    <xsl:param name="newsfile"/>

    <vevent>

      <uid><xsl:value-of select="crypto:sha1(concat(@title,@date))"/>@www.c3d2.de</uid>

      <dtstamp>
        <xsl:call-template name="date-to-hevent">
          <xsl:with-param name="date" select="@date"/>
        </xsl:call-template>
      </dtstamp>

      <dtstart>
        <xsl:call-template name="date-to-hevent">
          <xsl:with-param name="date" select="event/start"/>
        </xsl:call-template>
      </dtstart>

      <xsl:if test="event/end">
        <dtend>
          <xsl:call-template name="date-to-hevent">
            <xsl:with-param name="date" select="event/end"/>
          </xsl:call-template>
        </dtend>
      </xsl:if>

      <summary><xsl:value-of select="@title"/></summary>

      <location><xsl:value-of select="event/location"/></location>

      <categories>
        <xsl:choose>
          <xsl:when test="starts-with($newsfile, 'content/news/ta-')">
            <item>Themenabend</item>
          </xsl:when>
          <xsl:when test="starts-with($newsfile, 'content/news/event-')">
            <item>Event</item>
          </xsl:when>
          <xsl:otherwise>
            <item>Misc</item>
          </xsl:otherwise>
        </xsl:choose>
      </categories>

    </vevent>
  </xsl:template>
</xsl:stylesheet>
