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

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

<xsl:output method="xml"
            version="1.0"
            encoding="utf-8"
            indent="yes"/>

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

  <xsl:template match="news">
    <feed xml:lang="de">
      <!-- Datum des neuesten item für /feed/updated holen -->
      <xsl:for-each select="newsfile">
        <xsl:sort select="date:seconds(document(.)/item/@date)" data-type="number" order="descending" />
        <xsl:if test="position() = 1">
          <updated><xsl:value-of select="document(.)/item/@date"/>Z</updated>
        </xsl:if>
      </xsl:for-each>

      <!-- Allgemeines -->
      <title>www.c3d2.de Newsfeed</title>
      <link rel="self" href="https://www.c3d2.de/news-atom.xml" />
      <id>http://www.c3d2.de/</id>
      <subtitle>Neues aus dem Chaos Computer Club Dresden</subtitle>

      <!-- Alle item zu atom-entry umformen -->
      <xsl:for-each select="newsfile">
        <xsl:sort select="date:seconds(document(.)/item/@date)" data-type="number" order="descending" />
          <xsl:if test="position() &lt;= 10">
            <xsl:variable name="newsfile" select="."/>
            
            <xsl:for-each select="document(.)/item">
              <xsl:call-template name="atom-entry">
                <xsl:with-param name="newsfile" select="$newsfile"/>
              </xsl:call-template>
            </xsl:for-each>

          </xsl:if>
      </xsl:for-each>
    </feed>
  </xsl:template>

  <xsl:template name="atom-entry">
    <xsl:param name="newsfile"/>
    <entry>
      <xsl:variable name="href">
        <xsl:value-of select="$baseurl"/>
        <xsl:value-of select="substring-before(substring-after($newsfile, 'content/'), '.xml')"/>
        <xsl:text>.html</xsl:text>
      </xsl:variable>
      <id><xsl:value-of select="$href"/></id>
      <title><xsl:value-of select="@title"/></title>
      <link href="{$href}"/>
      <updated><xsl:value-of select="@date"/>Z</updated>
      <author>
        <name><xsl:value-of select="@author"/></name>
      </author>
      <category>
        <xsl:attribute name="term"><xsl:choose>
          <xsl:when test="starts-with($newsfile, 'content/news/ta-')">Themenabend</xsl:when>
          <xsl:when test="starts-with($newsfile, 'content/news/event-')">Event</xsl:when>
          <xsl:otherwise>Misc</xsl:otherwise>
        </xsl:choose></xsl:attribute>
      </category>
      <content type="xhtml">
        <div xmlns="http://www.w3.org/1999/xhtml">
          <xsl:apply-templates/>
        </div>
      </content>
      <xsl:for-each select="resource">
        <link
          rel="enclosure"
          href="{translate(., ' ', '+')}"
          type="{@type}"
          length="{@size}"
          />
      </xsl:for-each>
    </entry>
  </xsl:template>

  <xsl:template match="item/image" xmlns="http://www.w3.org/1999/xhtml">
    <img>
      <xsl:choose>
        <xsl:when test="contains(., '://')">
          <xsl:attribute name="src"><xsl:value-of select="."/></xsl:attribute>
        </xsl:when>
        <xsl:otherwise>
          <xsl:attribute name="src"><xsl:value-of select="concat($baseurl, 'images/news/', .)"/></xsl:attribute>
        </xsl:otherwise>
      </xsl:choose>

      <xsl:attribute name="title"><xsl:value-of select="@title"/></xsl:attribute>
      <xsl:attribute name="alt"><xsl:value-of select="title"/></xsl:attribute>
    </img>
  </xsl:template>
</xsl:stylesheet>
