<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:dc="http://purl.org/dc/elements/1.1/"
                xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
		xmlns="http://www.w3.org/2005/Atom"
                xmlns:date="http://exslt.org/dates-and-times"
                version="1.0">

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

<!-- date:format-date is specified in EXSLT. If your XSLT processor
implements it, you can suppress that line (I cannot do it myself,
<xsl:if> is not authorized as a top-level element). -->
<xsl:include href="date.format-date.template.xsl"/>

<xsl:param name="writeHTML" select="0"/>

<!-- Name of the Subversion repository (for display only)-->
<xsl:param name="repository_name">UNKNOWN NAME</xsl:param>

<!-- Subtitle of the ATOM feed --> 
<xsl:param name="subtitle"/>

<!-- URL of the Subversion repository -->
<xsl:param name="url">http://UNKNOWN/</xsl:param>

<!-- A tag for the feed. Typically just a domain name or email address with a date. 
  See RFC 4151 -->
<xsl:param name="tag">example.org,2005-11-03</xsl:param>

<!-- Maintainer of the feed -->
<xsl:param name="maintainer">Mr Unknown</xsl:param>

<!-- Prefix for a Web access (the revision number is added at the end). With ViewCVS, 
  it is something like "http://viewcvs.example.org/viewcvs.cgi/?rev=". -->
<xsl:param name="prefix">http://UNKNOWN/subversion/r</xsl:param>
  
<!-- Prefix for creating a direct link to files. With ViewCVS, 
  it is something like "http://viewcvs.example.org/viewcvs.cgi/" (and
  "?view=markup" for endlinktofiles). If
  it is empty, no link will be added. If you include REVISION_NUM in endlinktofiles, 
  it will be replaced by the revision number. -->
<xsl:param name="linktofiles"></xsl:param>
<xsl:param name="endlinktofiles"></xsl:param>
  
<rdf:Description rdf:about=''>
  <!-- This version has been hacked by Stephane Bortzmeyer, AFNIC -->
  <rdf:type rdf:resource="http://norman.walsh.name/knows/taxonomy#XSL"/>
  <dc:type rdf:resource='http://purl.org/dc/dcmitype/Text'/>
  <dc:format>application/xsl+xml</dc:format>
  <dc:title>Subversion log to Atom Stylesheet</dc:title>
  <dc:date>2005-02-11T17:19:51-05:00</dc:date>
  <dc:creator rdf:resource='http://norman.walsh.name/knows/who#norman-walsh'/>
  <dc:rights>Copyright © 2005 Norman Walsh. All rights reserved.</dc:rights>
  <dc:description>Convert XML Subversion log to Atom.</dc:description>
</rdf:Description>

<xsl:template match="log">
  <xsl:processing-instruction name="xml-stylesheet"> type='text/xsl' href='/style/atom.xsl'</xsl:processing-instruction>
  <feed>
    <title>Subversion Log of <xsl:value-of select="$repository_name"/></title>
    <xsl:if test="$subtitle">
      <subtitle><xsl:value-of select="$subtitle"/></subtitle>    
    </xsl:if>
    <link rel="alternate" type="text/html"><xsl:attribute name="href"><xsl:value-of select="$url"/></xsl:attribute></link>
    <!-- We use RFC 4151 -->
    <id>tag:<xsl:value-of select="$tag"/></id>
    <updated>
      <xsl:value-of select="logentry[1]/date"/>
    </updated>
    <author><name><xsl:value-of select="$maintainer"/></name></author>

    <xsl:apply-templates select="logentry">
      <xsl:sort order="descending" data-type="number" select="@revision"/>
    </xsl:apply-templates>
  </feed>
</xsl:template>

<xsl:template name="print-date">
   <xsl:param name="date"/>
   <xsl:call-template name="date:format-date">
     <xsl:with-param name="date-time" select="$date" />
     <xsl:with-param name="pattern" select="'dd MMMM yyyy'" />
   </xsl:call-template>
</xsl:template>

<xsl:template name="print-time">
   <xsl:param name="time"/>
   <xsl:call-template name="date:format-date">
     <xsl:with-param name="date-time" select="$time" />
     <xsl:with-param name="pattern" select="'HH:mm'" />
   </xsl:call-template>
</xsl:template>

<xsl:template match="logentry">
  <entry>
    <title>r<xsl:value-of select="@revision"/><xsl:text>: </xsl:text><xsl:value-of select="msg"/></title>
    <link rel="alternate" type="text/html">
      <xsl:attribute name="href">
        <xsl:value-of select="$prefix"/>
        <xsl:value-of select="@revision"/>
      </xsl:attribute>
      </link>
    <id>
      <xsl:text>tag:</xsl:text><xsl:value-of select="$tag"/><xsl:text>:</xsl:text><xsl:value-of select="$repository_name"/><xsl:text>:r</xsl:text>
      <xsl:value-of select="@revision"/>
    </id>
    <updated>
      <xsl:value-of select="date"/>
    </updated>
    <author>
      <name>
	<xsl:value-of select="author"/>
      </name>
    </author>
    <summary type="text">
      <xsl:value-of select="msg"/>
      <xsl:text> (</xsl:text>
      <xsl:choose>
	<xsl:when test="count(paths/path) = 0">No files</xsl:when>
	<xsl:when test="count(paths/path) = 1">1 file</xsl:when>
	<xsl:otherwise>
	  <xsl:value-of select="count(paths/path)"/>
	  <xsl:text> files</xsl:text>
	</xsl:otherwise>
      </xsl:choose>
      <xsl:text>)</xsl:text>
    </summary>
    <xsl:if test="$writeHTML = 0">
      <content type="xhtml">
	<xsl:call-template name="htmlSummary"/>
      </content>
    </xsl:if>
  </entry>

  <xsl:if test="$writeHTML != 0">
    <xsl:call-template name="write.chunk">
      <xsl:with-param name="filename" select="concat($root,
					             '/Subversion/R',
						     @revision,
						     '.html')"/>
      <xsl:with-param name="quiet" select="1"/>
      <xsl:with-param name="method" select="'xml'"/>
      <xsl:with-param name="encoding" select="'utf-8'"/>
      <xsl:with-param name="indent" select="'no'"/>
      <xsl:with-param name="content">
	<html xmlns="http://www.w3.org/1999/xhtml">
	  <head>
	    <title>Revision <xsl:value-of select="@revision"/></title>
	  </head>
	  <body>
	    <xsl:call-template name="htmlSummary"/>
	  </body>
	</html>
      </xsl:with-param>
    </xsl:call-template>
  </xsl:if>
</xsl:template>

<xsl:template name="htmlSummary">
  <div xmlns="http://www.w3.org/1999/xhtml">
    <h1>Revisions to <xsl:value-of select="$url"/></h1>
    <h3>
      <xsl:text>Revision </xsl:text>
      <xsl:value-of select="@revision"/>
      <xsl:text> by </xsl:text>
      <xsl:value-of select="author"/>
      <xsl:text> on </xsl:text>
      <xsl:call-template name="print-date">
	<xsl:with-param name="date" select="date"/>
      </xsl:call-template> 
      <xsl:text> at </xsl:text>
      <xsl:call-template name="print-time">
	<xsl:with-param name="time" select="substring(date,12)"/>
      </xsl:call-template> 
      <xsl:text> UT</xsl:text>
    </h3>

    <p><xsl:value-of select="msg"/></p>

    <p>Detailed list of changes:</p>
    
    <ul>
      <xsl:for-each select="paths/path">
	<li>
	  <xsl:choose>
	    <xsl:when test="@action = 'A'">Added </xsl:when>
	    <xsl:when test="@action = 'D'">Deleted </xsl:when>
	    <xsl:when test="@action = 'M'">Modified </xsl:when>
	    <xsl:when test="@action = 'R'">Restored </xsl:when>
	    <xsl:otherwise>
	      <xsl:value-of select="@action"/>
	      <xsl:text>??? </xsl:text>
	      <xsl:message>
		<xsl:text>Unexpected subversion action: </xsl:text>
		<xsl:value-of select="@action"/>
	      </xsl:message>
	    </xsl:otherwise>
	  </xsl:choose>
	  <code class="filename">
            <xsl:choose>
            <xsl:when test="$linktofiles != &quot;&quot;">
              <a>
                <xsl:attribute name="href">
                  <xsl:value-of select="$linktofiles"/>
                  <xsl:value-of select="."/>
                  <xsl:choose>
                    <!-- TODO: also test substring-after because the placeholder could 
                         be at the beginning -->
                    <xsl:when test="substring-before($endlinktofiles, &quot;REVISION_NUM&quot;) = &quot;&quot;">
                      <xsl:value-of select="$endlinktofiles"/>
                    </xsl:when>
                    <xsl:otherwise>
                      <xsl:value-of select="substring-before($endlinktofiles, &quot;REVISION_NUM&quot;)"/>
                      <xsl:value-of select="../../@revision"/>
                      <xsl:value-of select="substring-after($endlinktofiles, &quot;REVISION_NUM&quot;)"/>
                    </xsl:otherwise>
                  </xsl:choose>
                </xsl:attribute>
                <xsl:value-of select="."/>
              </a>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="."/>
            </xsl:otherwise>
            </xsl:choose>            
          </code>
	</li>
      </xsl:for-each>
    </ul>
  </div>
</xsl:template>

</xsl:stylesheet>



