<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
  xmlns:atom="http://www.w3.org/2005/Atom"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output
    method="html"
    media-type="text/html"
    indent="yes"
    encoding="UTF-8"/>
    
  <!-- Revised Mon Feb 19 22:48:47 UTC 2007 -->

  <!-- Change these to display in different language -->
  <xsl:variable name="ToC_title"   select="'Atom Feed Entries'"/>
  <xsl:variable name="ToC_link"    select="'Top'"/>
  <xsl:variable name="Email_link"  select="'Email'"/>
  <xsl:variable name="Link_link"   select="'Link'"/>
  <xsl:variable name="title"       select="'Title'"/>
  <xsl:variable name="subtitle"    select="'Subtitle'"/>
  <xsl:variable name="author"      select="'Author'"/>
  <xsl:variable name="category"    select="'Category'"/>
  <xsl:variable name="contributor" select="'Contributor'"/>
  <xsl:variable name="published"   select="'Published'"/>
  <xsl:variable name="updated"     select="'Updated'"/>
  <xsl:variable name="summary"     select="'Summary'"/>
  <xsl:variable name="content"     select="'Content'"/>
  <xsl:variable name="order"       select="'Order'"/>
  <xsl:variable name="resort"      select="'Re-sort Listing'"/>
  <xsl:variable name="main_title"  select="'Sorted List of Entries.'"/>

  <!-- Change these to alter display appearance -->
  <xsl:variable name="page_background_color" select="'white'"/>
  <xsl:variable name="dark_background_color" select="'moccasin'"/>
  <xsl:variable name="lite_background_color" select="'cornsilk'"/>
  <xsl:variable name="border_color"    select="'darkred'"/>
  <xsl:variable name="text_bold_color" select="'maroon'"/>
  <xsl:variable name="text_note_color" select="'teal'"/>
  <xsl:variable name="text_warn_color" select="'red'"/>
  <xsl:variable name="text_cmd_color"  select="'black'"/>
  <xsl:variable name="background_image_left" 
     select="'/images/border_brn_lh.png'"/>

  <xsl:variable name="border_style">
    <xsl:text>border: 5px double </xsl:text>
    <xsl:value-of select="$border_color"/>
    <xsl:text>;</xsl:text>
  </xsl:variable>

  <xsl:variable name="button_style">
    <xsl:text>
      border: 2px outset darkred;
      color: black;
      background: moccasin;
      font-size: 80%; font-family: monospace;
      position: relative;
      bottom: .1em;
    </xsl:text>
  </xsl:variable>

  <xsl:variable name="button_hover_style">
    <xsl:text>
      border: 2px inset black;
      color: white;
      background: firebrick;
    </xsl:text>
  </xsl:variable>

  <xsl:variable name="button_space">
    <xsl:text>&#160;&#160;</xsl:text>
  </xsl:variable>
  
  <!-- END xsl:variable DEFINITIONS -->
  <!-- ===================================================================== -->
  <!-- BEGIN Atom Sorting VARIABLE -->  
  
  <!--
     NOTE: The variable 'sort_order' triggers how entries will sort
     in both the main list and in the Table of Contents. It must be
     defined as either one or the other of just these choices. Cut and
     paste from one of these three...

     'Title 1st, Category 2nd'
     'Category 1st, Title 2nd'
     'Updated'

     ...substituting nothing else whatsoever or else you will get only
     a header and not any entries nor yet a Table of Contents.
  -->

  <xsl:variable name="sort_order">
    <!-- A by no means ideal, switch function for entry sorting-order -->
    <xsl:choose>
      <xsl:when test="contains(/atom:feed/atom:link, 'sort_order=Title')">
        <xsl:text>Title 1st, Category 2nd</xsl:text>
      </xsl:when>                                  
      <xsl:when test="contains(/atom:feed/atom:link, 'sort_order=Category')">
        <xsl:text>Category 1st, Title 2nd</xsl:text>
      </xsl:when>   
      <xsl:otherwise> 
        <xsl:text>Updated</xsl:text>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  
  <!-- END Atom Sorting VARIABLE -->  
  <!-- ===================================================================== -->
  <!-- BEGIN Perl/CGI Sorting TEMPLATES -->
  
  <!-- Used only when Perl/CGI has inserted its flag when served -->
    
  <xsl:template name="cgi_bin">
    <xsl:choose>
      <xsl:when test="contains(/atom:feed/atom:id, 'cgi-bin')">
        <a class="button" >
          <xsl:attribute name="href">
            <xsl:text>../cgi-bin/gus_atom_xsl.pl</xsl:text>
            <xsl:text>?atom=</xsl:text>
            <xsl:value-of select="
              substring-before(
                substring-after(
                  substring-after(/atom:feed/atom:id,'http://'),
                  '/'
                ),
              '_cgi-bin')"/>
            <xsl:text>.xml!sort_order=</xsl:text>
            <xsl:call-template name="sort_order_rotate"/>
          </xsl:attribute>          
          <xsl:attribute name="title">
            <xsl:value-of select="$resort"/>
          </xsl:attribute>
          <xsl:text>&#160;</xsl:text>
          <xsl:call-template name="ToC_order"/> 
          <xsl:text>&#160;</xsl:text>
        </a>
      </xsl:when>   
      <xsl:otherwise> 
        <span class="subtitle">
          <xsl:call-template name="ToC_order"/>
        </span>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
  <xsl:template name="sort_order_rotate">
    <xsl:choose>
      <xsl:when test="starts-with($sort_order, 'Title')">
        <xsl:text>Category</xsl:text>
      </xsl:when>                                  
      <xsl:when test="starts-with($sort_order, 'Category')">
        <xsl:text>Updated</xsl:text>
      </xsl:when>   
      <xsl:otherwise> 
        <xsl:text>Title</xsl:text>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>   
  
  <!-- END Perl/CGI Sorting TEMPLATES -->
  <!-- ===================================================================== -->
  <!-- BEGIN match="foo" TEMPLATES -->
  
  <!-- Nodes as yet undefined for display as HTML -->
  <xsl:template match="atom:id"></xsl:template>  
  <xsl:template match="atom:icon"></xsl:template>
  <xsl:template match="atom:logo"></xsl:template>
  <xsl:template match="atom:rights"></xsl:template>
  <xsl:template match="atom:generator"></xsl:template>

  <xsl:template match="/">
    <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
      <xsl:call-template name="html_head"/>
      <body>
        <xsl:call-template name="ToC"/>
        <xsl:apply-templates select="atom:feed"/>
        <ul style="list-style:none inside; padding:0; margin:0;">
          <xsl:call-template name="sort_list"/>
        </ul>
      </body>
    </html>
  </xsl:template>
  
  <xsl:template match="atom:feed">
    <!-- Creates the topmost sectoion, the header. -->
    <xsl:apply-templates select="*[not(atom:id) and not(atom:entry)]"/>
    <xsl:call-template name="build_validator_link"/>
    <xsl:call-template name="about_atom"/>
    <hr/>
    <h1 style="text-align:center"><xsl:value-of select="$main_title"/></h1>
  </xsl:template>  

  <xsl:template match="atom:author">
    <b><xsl:value-of select="$author"/>:&#160;</b>
    <span style="font-weight:700;">
      <xsl:value-of select="./atom:name"/>            
    </span>
    <xsl:apply-templates select="atom:uri"/>
    <xsl:apply-templates select="atom:email"/>
    <br/>                                    
  </xsl:template>

  <xsl:template match="atom:contributor">
    <xsl:if test="./atom:name!=''">
      <b><xsl:value-of select="$contributor"/>:&#160;</b>
      <xsl:value-of select="./atom:name"/>     
      <br/>
    </xsl:if>               
  </xsl:template>

  <xsl:template match="atom:uri">
    <xsl:if test="node()!=''">
      <xsl:value-of select="$button_space"/>
      <a class="button"
        href="{node()}"
        title="{node()}">
        <xsl:text>&#160;</xsl:text>
        <xsl:value-of select="$Link_link"/>
        <xsl:text>&#160;</xsl:text>
      </a>           
    </xsl:if>               
  </xsl:template>

  <xsl:template match="atom:email">
    <xsl:if test="node()!=''">
      <xsl:value-of select="$button_space"/>
      <a class="button"
        href="mailto:{node()}"
        title="{node()}">
        <xsl:text>&#160;</xsl:text>
        <xsl:value-of select="$Email_link"/>
        <xsl:text>&#160;</xsl:text>
      </a>           
    </xsl:if>               
  </xsl:template>

  <xsl:template match="atom:published">
    <xsl:if test="node()!='' and node()!=../atom:updated">
      <b><xsl:value-of select="$published"/>:&#160;</b>
      <span style="color:gray;">
        <xsl:value-of select="."/>  
      </span>
      <xsl:value-of select="$button_space"/>
      <xsl:value-of select="$button_space"/> 
    </xsl:if>               
  </xsl:template>

  <xsl:template match="atom:updated">
    <xsl:if test="node()!=''">
      <b><xsl:value-of select="$updated"/>:&#160;</b>
      <span style="color:gray;">
        <xsl:value-of select="."/>  
      </span>
      <br/>
    </xsl:if>               
  </xsl:template>

  <xsl:template match="atom:category">     
    <xsl:if test="./@term!=''">
      <b><xsl:value-of select="$category"/>:&#160;</b>
      <xsl:value-of select="./@term"/> 
      <br/>
    </xsl:if>               
  </xsl:template>

  <xsl:template match="atom:summary">
    <xsl:if test="node()!=''">
      <b><xsl:value-of select="$summary"/>:&#160;</b>
      <xsl:value-of select="."/>     
      <br/>
    </xsl:if>               
  </xsl:template>

  <xsl:template match="atom:content">
    <xsl:if test="node()!=''">
      <b><xsl:value-of select="$content"/>:&#160;</b>
      <xsl:value-of select="."/>     
      <br/>
    </xsl:if>               
  </xsl:template>

  <xsl:template match="atom:feed/atom:title">
    <h1><xsl:value-of select="."/></h1>
  </xsl:template>

  <xsl:template match="atom:entry/atom:link">     
    <xsl:if test="@href!=''">
      <a class="button"
        href="{@href}"
        title="{@href}">
        <xsl:text>&#160;</xsl:text>
        <xsl:value-of select="$Link_link"/>
        <xsl:text>&#160;</xsl:text>
      </a>
    </xsl:if>             
  </xsl:template>
  
  <xsl:template match="atom:feed/atom:entry/atom:title">
    <b><xsl:value-of select="$title"/>:&#160;</b>
    <b style="color:black;line-height:250%;">
    <xsl:value-of select="."/></b> 
    <xsl:value-of select="$button_space"/>
    <xsl:apply-templates select="../atom:link"/>
    <xsl:value-of select="$button_space"/>
    <xsl:call-template name="ToC_uplink"/>
    <br/>
  </xsl:template>

  <xsl:template match="atom:feed/atom:entry">
    <hr/>
    <li>
      <xsl:call-template name="ToC_downlink"/>
    </li>
    <xsl:apply-templates select="*[not(self::atom:link) and not(self::atom:id)]"/>
  </xsl:template>
  
  <!-- ALL name="foo" TEMPLATES ABOVE HERE -->
  <!-- ===================================================================== -->
  <!-- ONLY name="foo" TEMPLATES BELOW HERE TO EOF -->

  <xsl:template name="about_atom">
    <br/>
    <b>Protocol: </b>
    <span style="font-weight:700;color:gray;">
      <xsl:value-of select="'Atom 1.0'"/>
    </span>
    <xsl:value-of select="$button_space"/>
    <xsl-text>This is an Atom feed being displayed as ordinary HTML in your browser thorugh the magic of XSLT.</xsl-text>
    <xsl-if test="contains(/atom:feed/atom:link/@href, 'cgi-bin')">
      <xsl-text> Thanks to Perl/CGI it is also re-sortable via clicking a button in the Table of Contents.</xsl-text>
    </xsl-if>
    <xsl-text> You might also like to try viewing feeds like this one in an Atom/RSS feed-reader. Here are links to two such:</xsl-text>
    <xsl:value-of select="$button_space"/>
    <a class="button"
      href="http://www.google.com/search?hl=en&amp;q=%22atom+reader%22&amp;btnG=Google+Search"
      title="Atom Readers on GOOGLE">
      <xsl:text>&#160;</xsl:text>
      <xsl:value-of select="$Link_link"/>
      <xsl:text>&#160;</xsl:text>
    </a>
    <xsl:value-of select="$button_space"/>
    <xsl:text>&amp;</xsl:text>
    <xsl:value-of select="$button_space"/>
    <a class="button"
      href="http://AtomEnabled.org"
      title="http://AtomEnabled.org">
      <xsl:text>&#160;</xsl:text>
      <xsl:value-of select="$Link_link"/>
      <xsl:text>&#160;</xsl:text>
    </a>
  </xsl:template>
  
  <xsl:template name="html_head">
    <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
      <meta name="author" content="Gan Uesli Starling"/>
      <meta name="copyright" content="2007, Gan Uesli Starling"/>
      <style type="text/css">
        <xsl:call-template name="css_styles"/>
      </style>
    </head>
  </xsl:template>

  <xsl:template name="css_styles">
          body {
            font-family: serif;
            font-size: 100%;
            line-height: 150%;
            color: #000000;
            margin-left: 60px;
            margin-right: 20px;
            margin-top: 20px;
            margin-bottom: 20px;  
            background: 
              <xsl:value-of select="$page_background_color"/> 
              url(<xsl:value-of select="$background_image_left"/>);
            background-image: url(<xsl:value-of select="$background_image_left"/>);
            background-repeat: repeat-y;
            background-attachment: fixed;
          }
          h1 {
            font-family: sans-serif;
            font-weight: 700;
            font-size: 125%;
            color: <xsl:value-of select="$text_bold_color"/>;
          }
          h2 {
            font-family: sans-serif;
            font-weight: 600;
            font-size: 115%;
            color: <xsl:value-of select="$text_bold_color"/>;
          }
          h3 {
            font-family: sans-serif;
            font-weight: 500;
            font-size: 100%;
            color: <xsl:value-of select="$text_bold_color"/>;
          }
          h4 {
            font-family: sans-serif;
            font-weight: 400;
            font-size: 90%;
            color: black;
          }
          p { text-indent: 0%; text-align: left; }
          b { color: <xsl:value-of select="$text_bold_color"/>; }
          hr {
            height: 5px;
            color: <xsl:value-of select="$dark_background_color"/>;
            background-color: <xsl:value-of select="$dark_background_color"/>;
            border: 2px solid <xsl:value-of select="$border_color"/>;
            clear: left;
          }
          .subtitle {
            font-family: sans-serif;
            font-size: 80%;
            line-height: 80%;
            text-align: center;
            color: <xsl:value-of select="$text_bold_color"/>;
          }
          a.button { <xsl:value-of select="$button_style"/> }
          a:link { color: #0000bb; text-decoration: none; }
          a:visited { color: #0000bb; text-decoration: none; }
          a:active {  color: #800000; text-decoration: none; }
          a:hover { <xsl:value-of select="$button_hover_style"/> }
  </xsl:template>

  <xsl:template name="atom_updated">
    <b>Updated: </b>
    <span style="font-weight:700;color:gray;">
      <xsl:value-of select="atom:feed/atom:updated"/>
    </span>
  </xsl:template>
  
  <!-- Sort by category, title, or updated -->
  <xsl:template name="sort_list">
    <xsl:if test="$sort_order='Category 1st, Title 2nd'">
      <xsl:apply-templates select="atom:feed/atom:entry">
        <xsl:sort select="atom:category/@term"/>
        <xsl:sort select="atom:title"/>
      </xsl:apply-templates>
    </xsl:if>
    <xsl:if test="$sort_order='Title 1st, Category 2nd'">
      <xsl:apply-templates select="atom:feed/atom:entry">
        <xsl:sort select="atom:title"/>
        <xsl:sort select="atom:category/@term"/>
      </xsl:apply-templates>
    </xsl:if>
    <xsl:if test="$sort_order='Updated'">
      <xsl:apply-templates select="atom:feed/atom:entry">      
        <xsl:sort select="atom:updated" data-type="text" order="descending"/>
      </xsl:apply-templates>
    </xsl:if>        
  </xsl:template>
  
  <xsl:template name="build_validator_link">
    <b>Validate: </b>
    <span style="font-weight:700;color:gray;">
      <xsl:choose>
        <!-- Because Perl/CGI URLs are too long to fit and won't wrap. -->
        <xsl:when test="contains(/atom:feed/atom:link/@href, 'cgi-bin')">
          <xsl:text>This Current Feed</xsl:text>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="/atom:feed/atom:link/@href"/>
        </xsl:otherwise>
      </xsl:choose>
    </span>
    <xsl:value-of select="$button_space"/>

    <xsl:variable name="filtered_url">
      <xsl:call-template name="string_replace">
        <xsl:with-param name="input" select="/atom:feed/atom:link/@href"/>
        <xsl:with-param name="search" select="'http://'"/>
        <xsl:with-param name="replace" select="''"/>
      </xsl:call-template>
    </xsl:variable>

    <a class="button"
      title="Feed Validator">
      <xsl:attribute name="href">
        <xsl:value-of select="'http://feedvalidator.org/check.cgi?url='"/>
        <xsl:value-of select="$filtered_url"/>
      </xsl:attribute>
      <xsl:text>&#160;</xsl:text>
      <xsl:value-of select="$Link_link"/>
      <xsl:text>&#160;</xsl:text>
    </a>
  </xsl:template>

  <!-- http://feedvalidator.org/check.cgi?url= -->

  <xsl:template name="string_replace">
    <xsl:param name="input"/>
    <xsl:param name="search"/>
    <xsl:param name="replace"/>
    <xsl:choose>
      <xsl:when test="contains($input, $search)">
        <xsl:value-of select="substring-before($input, $search)"/>
        <xsl:value-of select="$replace"/>
        <xsl:call-template name="string_replace">
          <xsl:with-param name="input" select="substring-after($input, $search)"/>
          <xsl:with-param name="search" select="$search"/>
          <xsl:with-param name="replace" select="$replace"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$input"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
  <!-- TEMPLATES FOR ToC HERE TO EOF -->

  <xsl:template name="ToC_back_link">
    <a>
      <xsl:attribute name="id">
        <xsl:value-of select="$ToC_link"/>
      </xsl:attribute>
    </a>
  </xsl:template>

  <xsl:template name="ToC">
    <xsl:call-template name="ToC_back_link"/>
    <div>
      <xsl:attribute name="style">
        <xsl:text>
          float:right;
          margin: 0em 0em 1em 1em;
          padding: 0em 1em .2em 1em;
          background-color:
        </xsl:text>
        <xsl:value-of select="$lite_background_color"/>
        <xsl:text>;</xsl:text>
        <xsl:value-of select="$border_style"/>
      </xsl:attribute>
      <xsl:call-template name="ToC_list"/>
  </div>
  </xsl:template>

  <!-- A note in ToC about sorting order -->
  <xsl:template name="ToC_order">
    <xsl:if test="$sort_order='Category 1st, Title 2nd'">
      <xsl:value-of select="$order"/>
      <xsl:text>: </xsl:text>
      <xsl:value-of select="$category"/>
      <xsl:text>, </xsl:text>
      <xsl:value-of select="$title"/>
    </xsl:if>
    <xsl:if test="$sort_order='Title 1st, Category 2nd'">
      <xsl:value-of select="$order"/>
      <xsl:text>: </xsl:text>
      <xsl:value-of select="$title"/>
      <xsl:text>, </xsl:text>
      <xsl:value-of select="$category"/>
    </xsl:if>
    <xsl:if test="$sort_order='Updated'">
      <xsl:value-of select="$order"/>
      <xsl:text>: </xsl:text>
      <xsl:value-of select="$updated"/>
    </xsl:if>
  </xsl:template>

  <xsl:template name="ToC_list">
    <h3 style="text-align:center;">
        <xsl:value-of select="$ToC_title"/>
    </h3>
    <p style="text-align:center">
      <xsl:call-template name="cgi_bin"/>
    </p>
    <xsl:call-template name="ToC_entries"/>
  </xsl:template>

  <xsl:template name="ToC_entries">
    <ol style="font-size:85%;font-height:120%;">

      <xsl:if test="$sort_order='Category 1st, Title 2nd'">
        <xsl:for-each select="atom:feed/atom:entry">
          <xsl:sort select="atom:category/@term"/>
          <xsl:sort select="atom:title"/>
          <xsl:call-template name="ToC_entry"/>
        </xsl:for-each>
      </xsl:if>

      <xsl:if test="$sort_order='Title 1st, Category 2nd'">
        <xsl:for-each select="atom:feed/atom:entry">
          <xsl:sort select="atom:title"/>
          <xsl:sort select="atom:category/@term"/>
          <xsl:call-template name="ToC_entry"/>
        </xsl:for-each>
      </xsl:if>

      <xsl:if test="$sort_order='Updated'">
        <xsl:for-each select="atom:feed/atom:entry">
          <xsl:sort select="atom:updated" data-type="text" order="descending"/>
          <xsl:call-template name="ToC_entry"/>
        </xsl:for-each>
      </xsl:if>

    </ol>
  </xsl:template>

  <xsl:template name="ToC_entry">
    <li>
      <a>
        <xsl:attribute name="href">
          <xsl:text>#GUS-</xsl:text>
          <xsl:value-of select="position()"/>
        </xsl:attribute>
        <xsl:attribute name="title">
            <xsl:value-of select="$category"/>
            <xsl:text>: </xsl:text>
            <xsl:value-of select="atom:category/@term"/>
            <xsl:text> &#8212; </xsl:text>
            <xsl:value-of select="$updated"/>
            <xsl:text>: </xsl:text>
            <xsl:value-of select="atom:updated"/>
        </xsl:attribute>
        <xsl:value-of select="atom:title"/>
      </a>
    </li>
  </xsl:template>

  <xsl:template name="ToC_downlink">
    <a>
      <xsl:attribute name="id">
        <xsl:text>GUS-</xsl:text>
        <xsl:value-of select="position()"/>
      </xsl:attribute>
    </a>
  </xsl:template>

  <xsl:template name="ToC_uplink">
    <a class="button">
      <xsl:attribute name="href">
        <xsl:text>#</xsl:text>
        <xsl:value-of select="$ToC_link"/>
      </xsl:attribute>
      <xsl:text>&#160;</xsl:text>
      <xsl:value-of select="$ToC_link"/>
      <xsl:text>&#160;</xsl:text>
    </a>
  </xsl:template>

</xsl:stylesheet>

<!--

  <atom:entry>
    <atom:title></atom:title>
    <atom:category term=""/>
    <atom:id></atom:id>
    <atom:updated>2007-02-04T03:43:00Z</atom:updated>
    <atom:link href=""/>
    <atom:summary></atom:summary>
    <atom:content>
    </atom:content>
  </atom:entry>

-->


