<xsl:transform version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:exsl="http://exslt.org/common"
    xmlns:func="http://exslt.org/functions"
    xmlns:my="#"
    extension-element-prefixes="exsl func my">

<!-- ================================================================== -->

<xsl:template match="blog-idx">
  <xsl:message>Rebuilding blog ...</xsl:message>

  <xsl:message>* generating fragments and pages for individual posts</xsl:message>
  <xsl:for-each select="//post-idx">
    <xsl:variable name="post" select="my:dateTime2post(@dateTime)"/>
    <exsl:document
        indent="yes"
        href="{my:dateTime2postPath(@dateTime, 'post.page.xml')}"
        method="xml"
        encoding="UTF-8">
      <page title="{$post/@title}">
        <body>
    <div class="blog-date">
      <xsl:value-of select="my:dateTime2post(@dateTime)/@day"/>
      <xsl:text> </xsl:text>
      <xsl:value-of select="my:dateTime2dateStr(@dateTime)"/>
      </div>
    <fragment id="/blog/{my:dateTime2postPath(@dateTime,'post.frag.xml')}"/>
          </body>
        </page>
      </exsl:document>
    <exsl:document
        indent="yes"
        href="{my:dateTime2postPath(@dateTime, 'post.frag.xml')}"
        method="xml"
        encoding="UTF-8">
    <xsl:call-template name="post-fragment">
      <xsl:with-param name="post" select="$post"/>
      </xsl:call-template>
      </exsl:document>
    </xsl:for-each>

  <xsl:message>* generating current blog</xsl:message>
  <exsl:document
      indent="yes"
      href="current.frag.xml"
      method="xml"
      encoding="UTF-8">
    <fragment>
      <xsl:for-each select="//post-idx">
        <xsl:sort select="@dateTime" order="descending"/>
	<xsl:if test="position() &lt;= 10">

        <xsl:if test="generate-id(..) != generate-id(following::post-idx[1]/..)">
          <div class="blog-date">
            <xsl:value-of select="my:dateTime2post(@dateTime)/@day"/>
	    <xsl:text> </xsl:text>
            <xsl:value-of select="my:dateTime2dateStr(@dateTime)"/>
            </div>
          </xsl:if>
        <fragment
	    id="/blog/{my:dateTime2postPath(@dateTime,'post.frag.xml')}"/>

	</xsl:if>
        </xsl:for-each>
      </fragment>
    </exsl:document>
  <exsl:document
      indent="yes"
      href="recent-lst.frag.xml"
      method="xml"
      encoding="UTF-8">
    <fragment>
      <div style="float: right">
      <a href="recent.rss"><img border="0px" src="/images/rss.png" alt="rss feed"/></a>
        </div>

      <div class="side-title">Recent Posts</div>

      <ul>
      <xsl:for-each select="//post-idx">
        <xsl:sort select="@dateTime" order="descending"/>
	<xsl:if test="position() &lt;= 10">

        <xsl:variable name="post" select="my:dateTime2post(@dateTime)"/>
        <li><a href="/index.html#blog{@dateTime}">
          <xsl:value-of select="$post/@title"/>
          </a></li>

	</xsl:if>
        </xsl:for-each>
	</ul>
      <br/>
      </fragment>
    </exsl:document>

  <xsl:message>* generating wondergeek feed</xsl:message>
  <exsl:document
      indent="yes"
      href="wg.txt"
      method="text"
      encoding="UTF-8">
    <xsl:variable name="latest" select="(/blog-idx/year/month/day/post-idx)[last()]"/>

    <!-- Author -->
    <xsl:text>Crutcher</xsl:text>
    <xsl:text>&#xa;</xsl:text>

    <!-- Link -->
    <xsl:text>http://monket.samedi-studios.com/index.html#blog</xsl:text>
    <xsl:value-of select="$latest/@dateTime"/>
    <xsl:text>&#xa;</xsl:text>

    <!-- Title -->
    <xsl:value-of select="my:dateTime2post($latest/@dateTime)/@title"/>
    <xsl:text>&#xa;</xsl:text>

    <!-- Exceprt -->
    <xsl:value-of
        select="normalize-space(substring(string(my:dateTime2post($latest/@dateTime)/body), 1, 200))"/>
    <xsl:text> ...</xsl:text>
    <xsl:text>&#xa;</xsl:text>

    <!-- Timestamp -->
    <xsl:value-of select="/blog-idx/@last"/>
    <xsl:text>&#xa;</xsl:text>
    </exsl:document>

  <xsl:message>* generating rss9.1 feed</xsl:message>
  <exsl:document
      indent="yes"
      href="recent.rss"
      method="xml"
      encoding="UTF-8">
    <rss version="0.91">
    <channel>
      <title>Crutcher's Blog</title>
      <link>http://monket.samedi-studios.com</link>
      <language>en-US</language>
      <description>Crutcher's incessant rambling on subjects of linguistics, boozahol, and awesome.</description>

      <lastBuildDate>
        <xsl:value-of
	  select="my:dateTime2rfc822(//post-idx[last()]/@dateTime)"/>
        </lastBuildDate>

      <xsl:for-each select="//post-idx">
        <xsl:sort select="@dateTime" order="descending"/>
	<xsl:if test="position() &lt;= 10">

        <xsl:variable name="post" select="my:dateTime2post(@dateTime)"/>
	<item>
	  <title>
	    <xsl:value-of select="$post/@title"/>
	    </title>
	  <link>
            <xsl:text>http://monket.samedi-studios.com/</xsl:text>
            <xsl:value-of select="my:permalink($post)"/>
	    </link>
	  <description>
	    <xsl:text disable-output-escaping="yes">&lt;![CDATA[</xsl:text>
	    <xsl:copy-of select="$post/body/node()"/>
	    <xsl:text disable-output-escaping="yes">]]&gt;</xsl:text>
	    </description>
	  </item>

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

      </channel>
      </rss>
    </exsl:document>

  <xsl:message>* generating yearly indexes</xsl:message>
  <xsl:for-each select="year">
    <exsl:document
        indent="yes"
        href="{@id}/index.page.xml"
        method="xml"
        encoding="UTF-8">
      <page title="Blog Archive">
        <body>
          <fragment id="year.frag.xml"/>
          </body>
        </page>
      </exsl:document>

    <exsl:document
        indent="yes"
        href="{@id}/year.frag.xml"
        method="xml"
        encoding="UTF-8">
      <fragment>
        <div class="blog-title">
	  <xsl:text>Year: </xsl:text>
          <xsl:value-of select="@id"/>
          </div>
        <xsl:for-each select="month">
          - <a href="{@id}/index.html">
            <xsl:value-of select="my:num2month(@id)"/>
	    </a><br/>
          </xsl:for-each>
        </fragment>
      </exsl:document>
    </xsl:for-each>

  <xsl:message>* generating monthly indexes</xsl:message>
  <xsl:for-each select="year">
    <xsl:for-each select="month">
      <exsl:document
          indent="yes"
          href="{../@id}/{@id}/index.page.xml"
          method="xml"
          encoding="UTF-8">
	<page title="Blog Archive">
	  <body>
	    <fragment id="month.frag.xml"/>
	    </body>
	  </page>
        </exsl:document>

      <exsl:document
          indent="yes"
          href="{../@id}/{@id}/month.frag.xml"
          method="xml"
          encoding="UTF-8">
        <fragment>
          <div class="blog-title">
            <xsl:value-of select="my:num2month(@id)"/>
            </div>
          <xsl:for-each select=".//post-idx">
            <div class="blog-date">
              <xsl:value-of select="my:dateTime2dateStr(@dateTime)"/>
              </div>
            <a href="/blog/{my:dateTime2postPath(@dateTime,'post.html')}">
              <xsl:value-of select="my:dateTime2post(@dateTime)/@title"/>
              </a><br/>
            </xsl:for-each>
          </fragment>
	</exsl:document>
      </xsl:for-each>
    </xsl:for-each>

  <xsl:message>* generating blog index</xsl:message>
  <exsl:document
      indent="yes"
      href="index.frag.xml"
      method="xml"
      encoding="UTF-8">
    <fragment>

<div class="pg-section">
<div class="pg-subsection-title">Posts By Year</div>
  <xsl:for-each select="year">
    <fragment id="{@id}/year.frag.xml"/>
    </xsl:for-each>
  </div>

<div class="pg-section">
<div class="pg-subsection-title">Latest Posts</div>
  <fragment id="current.frag.xml"/>
  </div>

      </fragment>
    </exsl:document>

  </xsl:template>

<!-- ================================================================== -->

<func:function name="my:num2month">
<xsl:param name="num"/>
<xsl:variable name="months">
  <month>Janurary</month>
  <month>February</month>
  <month>March</month>
  <month>April</month>
  <month>May</month>
  <month>June</month>
  <month>July</month>
  <month>August</month>
  <month>September</month>
  <month>October</month>
  <month>November</month>
  <month>December</month>
  </xsl:variable>
<func:result select="string(exsl:node-set($months)/month[number($num)])"/>
  </func:function>

<func:function name="my:num2monthAbrev">
<xsl:param name="num"/>
<xsl:variable name="months">
  <month>Jan</month>
  <month>Feb</month>
  <month>Mar</month>
  <month>Apr</month>
  <month>May</month>
  <month>Jun</month>
  <month>Jul</month>
  <month>Aug</month>
  <month>Sep</month>
  <month>Oct</month>
  <month>Nov</month>
  <month>Dec</month>
  </xsl:variable>
<func:result select="string(exsl:node-set($months)/month[number($num)])"/>
  </func:function>

<!-- ================================================================== -->

<xsl:template name="post-fragment">
<xsl:param name="post" select="."/>
  <fragment>
    <div class="blog-post">
      <a name="blog{$post/@dateTime}"/>
      <div class="blog-title">
        <xsl:value-of select="$post/@title"/>
        </div>
      <div class="blog-body">
        <xsl:copy-of select="$post/body/node()"/>
	<xsl:if test="$post/link">
	  <a href="{$post/link/@href}">Link</a>
	  <xsl:if test="$post/link/node()">
	    <i>
	      <xsl:text> (</xsl:text>
	      <xsl:copy-of select="$post/link/node()"/>
	      <xsl:text>)</xsl:text>
	      </i>
	    </xsl:if>
	  </xsl:if>
        </div>
      <div class="blog-footer">
        <xsl:text>posted at </xsl:text>
        <xsl:value-of select="substring-after(@dateTime, 'T')"/>
        <xsl:text>&#xa0;</xsl:text>
        <a href="{my:permalink($post)}">permalink</a>

        <xsl:text>&#xa0;|&#xa0;</xsl:text>
        <xsl:variable name="surl">
          <xsl:text>http://www.technorati.com/cosmos/search.html?</xsl:text>
          <xsl:text>rank=&amp;</xsl:text>
          <xsl:text>sub=mtcosmos&amp;</xsl:text>
          <xsl:text>url=http://monket.samedi-studios.com</xsl:text>
          <xsl:value-of select="my:permalink($post)"/>
          </xsl:variable>
        <a href="{$surl}">
          <xsl:text>trackback</xsl:text>
          </a>
        </div>
      </div>
    </fragment>
  </xsl:template>

<!-- ================================================================== -->

<func:function name="my:permalink">
<xsl:param name="post" select="."/>
  <func:result
    select="concat('/blog/', my:dateTime2postPath($post/@dateTime, 'post.html'))"/>
  </func:function>

<func:function name="my:dateTime2post">
<xsl:param name="dt"/>
  <func:result
    select="document(my:dateTime2postPath($dt, 'post.xml'))/post"/>
  </func:function>

<func:function name="my:dateTime2dateStr">
<xsl:param name="dt"/>
  <func:result select="substring-before($dt, 'T')"/>
  </func:function>

<func:function name="my:dateTime2postPath">
<xsl:param name="dt"/>
<xsl:param name="file"/>

<xsl:variable name="date" select="substring-before($dt, 'T')"/>
<xsl:variable name="y" select="substring-before($date,'-')"/>
<xsl:variable name="m"
  select="substring-before(substring-after($date, '-'),'-')"/>
<xsl:variable name="d"
  select="substring-after(substring-after($date,'-'),'-')"/>

<xsl:variable name="t" select="substring-after($dt, 'T')"/>
<xsl:variable name="h" select="substring-before($t, ':')"/>
<xsl:variable name="min" select="substring-after($t, ':')"/>

  <func:result>
    <xsl:value-of select="$y"/>
    <xsl:text>/</xsl:text>
    <xsl:value-of select="$m"/>
    <xsl:text>/</xsl:text>
    <xsl:value-of select="$d"/>
    <xsl:text>/</xsl:text>
    <xsl:value-of select="$h"/>
    <xsl:text>.</xsl:text>
    <xsl:value-of select="$min"/>

    <xsl:if test="$file">
      <xsl:text>/</xsl:text>
      <xsl:value-of select="$file"/>
      </xsl:if>
    </func:result>
  </func:function>

<func:function name="my:dateTime2rfc822">
<xsl:param name="dt"/>

<xsl:variable name="date" select="substring-before($dt, 'T')"/>
<xsl:variable name="y" select="substring-before($date,'-')"/>
<xsl:variable name="m"
  select="substring-before(substring-after($date, '-'),'-')"/>
<xsl:variable name="d"
  select="substring-after(substring-after($date,'-'),'-')"/>

<xsl:variable name="t" select="substring-after($dt, 'T')"/>
<xsl:variable name="h" select="substring-before($t, ':')"/>
<xsl:variable name="min" select="substring-after($t, ':')"/>

  <func:result>
    <xsl:value-of select="number($d)"/>
    <xsl:text> </xsl:text>

    <xsl:value-of select="my:num2monthAbrev(number($m))"/>
    <xsl:text> </xsl:text>

    <xsl:value-of select="$y"/>
    <xsl:text> </xsl:text>

    <xsl:value-of select="$t"/>
    <xsl:text> CST</xsl:text>
    </func:result>
  </func:function>
 
<!-- ================================================================== -->
  </xsl:transform>
