<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

  <xsl:template match="/">
    <html>
      <head>
	<title>Jessica Perry Hekman: Readings List</title>
      </head>
      <body background="../images/chalk.jpg" link="005500" vlink="000088"
	alink="000000">
	<center>
	  <h1><a href='../'>Jessica Perry Hekman</a>: Readings List</h1>
	</center>
        <p>See also my <a href="reviews.html">reviews</a> of some of the books in this list. Lately, instead of maintaining thoughts on various books as reviews, I've been keeping a <a href="http://pendaran.arborius.net/moveabletype/content/">blog</a>.</p>

        <p>Note: items are displayed in reverse chronological order, since I suspect that's how people would rather see them.</p>

	<!-- FIXME: breaks if no milestones but DTD doesn't require them -->

        <p><hr /></p>

        <xsl:apply-templates select="/readinglist/readings/milestone" mode="list">
          <xsl:sort select="position()" order="descending" data-type="number"/>
        </xsl:apply-templates>

      </body>
    </html>

  </xsl:template>

  <xsl:template match="milestone" mode="list">

    <ul>
      <xsl:choose>
        <xsl:when test="position() = 1">
          <xsl:apply-templates select="following-sibling::*" mode="last">
            <xsl:sort select="position()" order="descending"
              data-type="number"/>
          </xsl:apply-templates>
        </xsl:when>
        <xsl:otherwise>
          <xsl:apply-templates select="following-sibling::milestone[1]" mode="skin"/>
        </xsl:otherwise>
      </xsl:choose>
    </ul>
    <h2><xsl:apply-templates /></h2>
  </xsl:template>

  <xsl:template match="milestone" mode="skin" priority="1">
    <xsl:apply-templates
      select="preceding-sibling::*[1][not(name() = 'milestone')]" mode="skin"/>
  </xsl:template>

  <xsl:template match="readings/*" mode="skin">
    <li>
      <xsl:apply-templates select="." />
    </li>

    <xsl:if test="not(name(preceding-sibling::*[1]) = 'milestone')">
      <xsl:apply-templates select="preceding-sibling::*[1]" mode="skin" />
    </xsl:if>
  </xsl:template>

  <xsl:template match="readings/*" mode="last">
    <li>
      <xsl:apply-templates select="." />
    </li>
  </xsl:template>

  <xsl:template match="readings/article">
    <xsl:variable name="title">
      <xsl:choose>
	<xsl:when test="title">
	  <xsl:apply-templates select="title" />
	</xsl:when>
	<xsl:otherwise>
	  <i>untitled article,</i>
	</xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <!-- make a template with subtitle -->

  <xsl:choose>
      <xsl:when test="url">
	<xsl:element name="a">
	  <xsl:attribute name="href">
	    <xsl:value-of select="url"/>
	  </xsl:attribute>
	  <xsl:value-of select="$title"/>
	</xsl:element>
      </xsl:when>
      <xsl:otherwise>
	<xsl:value-of select="$title"/>
      </xsl:otherwise>
    </xsl:choose>

    <xsl:text>&#x20;</xsl:text>
    <xsl:apply-templates select="author[1]" mode="dont-skip"/>

    <xsl:apply-templates select="journal" />
    <xsl:text>&#x20;</xsl:text>
    <xsl:apply-templates select="notes" />

  </xsl:template>

  <xsl:template match="article/journal">
    <xsl:text> in </xsl:text>
    <xsl:choose>
      <xsl:when test="url">
	<xsl:element name="a">
	  <xsl:attribute name="href">
	    <xsl:value-of select="url"/>
	  </xsl:attribute>
	  <xsl:apply-templates select="title"/>
	</xsl:element>
      </xsl:when>
      <xsl:otherwise>
	  <xsl:apply-templates select="title"/>
	</xsl:otherwise>
      </xsl:choose>
    <xsl:text>, </xsl:text>
    <xsl:apply-templates select="issue"/>
    <xsl:apply-templates select="note"/>
  </xsl:template>

  <!-- todo "complete" -->
    <!-- for all title templates, add subtitle processing, and add url -->
      <xsl:template match="readings/book|readings/play|readings/graphic-novel|readings/comic-compilation|readings/journal">
      <xsl:apply-templates select="title" />
    <xsl:text>&#x20;</xsl:text>
      <xsl:apply-templates select="author[1]" mode="dont-skip"/>
    <xsl:apply-templates select="note" />
    <xsl:if test="review">
      <xsl:text> </xsl:text>
      <a>
        <xsl:attribute name="href">
          <xsl:text>reviews.html#r</xsl:text>
          <xsl:value-of select="count(preceding::book|play|graphic-novel|comic-compilation|journal) + 1" />
        </xsl:attribute>
        <xsl:text>[Review]</xsl:text>
      </a>
    </xsl:if>
  </xsl:template>

  <xsl:template match="readings/story">
    <xsl:variable name="title">
    <!-- make title a template and handle subtitle -->
      <xsl:choose>
	<xsl:when test="title">
	  <xsl:apply-templates select="title" />
	</xsl:when>
	<xsl:otherwise>
	  <i>untitled story</i>
	</xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <xsl:choose>
      <xsl:when test="url">
	<xsl:element name="a">
	  <xsl:attribute name="href">
	    <xsl:value-of select="url"/>
	  </xsl:attribute>
	  <xsl:value-of select="$title"/>
	</xsl:element>
      </xsl:when>
      <xsl:otherwise>
	<xsl:value-of select="$title"/>
      </xsl:otherwise>
    </xsl:choose>
    <xsl:text>&#x20;</xsl:text>

    <!-- number -->

      <xsl:apply-templates select="author[1]" mode="dont-skip"/>
    <xsl:text>&#x20;</xsl:text>
    <xsl:apply-templates select="note" />
    </xsl:template>


    <xsl:template match="author" />

    <!-- TODO -->
    <!-- check "tag" (as in "x with Y,") where Y has "tag='with'" -->
    <!-- sort by title when calling (??)-->
    <!-- don't walk through one by one (??) -->
    <!-- only check if the next/prev are authors with the same title;
else treat it like a new list -->

  <xsl:template match="author" mode="dont-skip">
    <xsl:variable name="preceding" select="preceding-sibling::*[1]" />
    <xsl:variable name="following" select="following-sibling::*[1]" />

    <!--
      if the previous sibling is an author too
      if its type is different than ours, give our id string
      if there is one after us, put a comma before us
      if there is not one after us, put an and before us

      if the previous sibling isn't, give our id string

    -->
    <xsl:choose>
      <xsl:when test="name($preceding[1]) = 'author'">
	<xsl:choose>
	  <xsl:when test="$preceding[1]/@title != @title">
	    <xsl:text>;&#x20;</xsl:text>
	    <xsl:apply-templates select="." mode="author-title" />
	  </xsl:when>
	  <xsl:otherwise>
	    <xsl:choose>
	      <xsl:when test="name($following) = 'author'">
		<xsl:text>,&#x20;</xsl:text>
	      </xsl:when>
	      <xsl:otherwise> <!-- we are the last author in this list -->
		<!-- HERE , and and don't work; search for bull -->
		<xsl:text>&#x20;and&#x20;</xsl:text>
	      </xsl:otherwise>
	    </xsl:choose>
	  </xsl:otherwise>
	</xsl:choose>
      </xsl:when> <!-- preceding is also author -->

      <xsl:otherwise> <!-- we are the first in this list -->
	<xsl:apply-templates select="." mode="author-title" />
      </xsl:otherwise>
    </xsl:choose>

    <xsl:apply-templates />

    <xsl:if test="name($following[1]) = 'author'">
      <xsl:apply-templates select="$following[1]" mode="dont-skip"/>
    </xsl:if>

    </xsl:template>

    <xsl:template match="author" mode="author-title">
      <xsl:choose>
      <xsl:when test="@title='editor'">
	<xsl:text>edited by&#x20;</xsl:text>
      </xsl:when>
      <xsl:when test="@title='illustrator'">
	<xsl:text>illustrated by&#x20;</xsl:text>
      </xsl:when>
      <xsl:when test="@title='translator'">
	<xsl:text>translated by&#x20;</xsl:text>
      </xsl:when>
      <xsl:otherwise>
	<xsl:text>by&#x20;</xsl:text>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

    <!--
<!ELEMENT readings (milestone|article|book|comic-compilation|journal|proceedings|script|specification)* >
-->

  <xsl:template match="post">
    <xsl:apply-templates select="title"/>
    <xsl:text> a post </xsl:text>
    <xsl:apply-templates select="author[1]" mode="dont-skip"/>
    <xsl:text>&#x20;</xsl:text>
    <xsl:apply-templates select="forum" />
    <xsl:apply-templates select="date" />
  </xsl:template>

  <xsl:template match="newsletter|magazine">
    <i><xsl:apply-templates select="title"/></i>
    <xsl:if test="issue">
      <xsl:text>, </xsl:text>
      <xsl:apply-templates select="issue" />
    </xsl:if>
    <xsl:if test="date">
      <xsl:text>, </xsl:text>
      <xsl:apply-templates select="date" />
    </xsl:if>
    <xsl:apply-templates select="note" />
  </xsl:template>

  <xsl:template match="essay">
    <xsl:apply-templates select="title"/>
    <xsl:text>&#x20;</xsl:text>
    <xsl:apply-templates select="author[1]" mode="dont-skip"/>
    <xsl:text>&#x20;</xsl:text>
    <xsl:apply-templates select="url"/>
  </xsl:template>

  <xsl:template match="article/title|essay/title|post/title|story/title|specification/title">
    <xsl:text>"</xsl:text>
    <xsl:apply-templates />
    <xsl:apply-templates select="following-sibling::subtitle"
      mode="dont-skip"/>
    <xsl:text>,</xsl:text>
    <xsl:text>"</xsl:text>
  </xsl:template>

  <xsl:template match="subtitle" />

  <xsl:template match="subtitle" mode="dont-skip">
    <xsl:text>:&#x20;</xsl:text>
    <xsl:apply-templates />
    <xsl:if test="not(name(preceding-sibling::*[1]) = 'title')">
      <xsl:message terminate="yes">
	<xsl:text>subtitle must follow title&#x20;</xsl:text>
	<xsl:text>&#xa;&#x20;Element:&#x20;</xsl:text>
	<xsl:value-of select="name(parent::*)"/>
	<xsl:text>&#xa;&#x20;Subtitle:&#x20;</xsl:text>
	<xsl:value-of select="."/>
	<xsl:text>&#xa;&#x20;Author:&#x20;</xsl:text>
	<xsl:value-of select="parent::*/author"/>
	<xsl:text>&#xa;&#x20;Preceding-sibling:&#x20;</xsl:text>
	<xsl:value-of select="name(preceding-sibling::*[1])"/>
	<xsl:text>&#x20;</xsl:text>
	<xsl:value-of select="preceding-sibling::*[1]"/>
      </xsl:message>
    </xsl:if>
  </xsl:template>



  <xsl:template match="book/title|graphic-novel/title|play/title|comic-compilation/title|journal/title">
    <i>
      <xsl:apply-templates />
      <xsl:apply-templates select="following-sibling::subtitle"
	mode="dont-skip"/>
      <xsl:if test="parent::*/author and not(parent::*/number)">
	<xsl:text>,</xsl:text>
      </xsl:if>
    </i>

    <xsl:apply-templates select="parent::*/number" />
    <xsl:if test="parent::*/author and parent::*/number">
      <xsl:text>,</xsl:text>
    </xsl:if>
  </xsl:template>

  <xsl:template match="note">
    <xsl:text> [</xsl:text>
    <i>
      <xsl:apply-templates />
    </i>
    <xsl:text>]</xsl:text>

  </xsl:template>

  <!-- TODO handle this differently when type=volume -->
  <xsl:template match="graphic-novel/number">
    <xsl:choose>
      <xsl:when test="../@type = 'volume'">
	<xsl:text> Volume </xsl:text>
      </xsl:when>
      <xsl:otherwise>
	<xsl:text> #</xsl:text>
      </xsl:otherwise>
    </xsl:choose>
    <xsl:choose>
      <xsl:when test="@begin">
	<xsl:value-of select="@begin" />-<xsl:value-of select="@end" />
      </xsl:when>
      <xsl:otherwise>
	<xsl:value-of select="@no" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="forum">
    <xsl:text> to </xsl:text>
    <xsl:if test="@type">
      <xsl:text>the </xsl:text>
    </xsl:if>
    <xsl:apply-templates />
    <xsl:text>&#x20;</xsl:text>
    <xsl:value-of select="@type" />
  </xsl:template>


<xsl:template match="post/date">
    <xsl:text> on </xsl:text>
    <xsl:apply-templates />
  </xsl:template>

  <xsl:template match="url">
    <xsl:text>(</xsl:text>
    <xsl:element name="a">
      <xsl:attribute name="href">
	<xsl:value-of select="."/>
      </xsl:attribute>
      <xsl:value-of select="."/>
    </xsl:element>
    <xsl:text>)</xsl:text>
  </xsl:template>

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

  <xsl:template match="review" />

</xsl:stylesheet>