<?xml version="1.0" encoding="euc-jp"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
		xmlns:h="http://www.horobi.com/2004/xhatenaml/0.91">

<!--
xh2html.xsl: convert XHatenaML document into HTML 
version 0.9.3
-->

<!-- .... configuration ........................................... -->
<xsl:variable name="theme">http://d.hatena.ne.jp/theme/metal/metal.css</xsl:variable>
<xsl:variable name="style">rna.css</xsl:variable>
<xsl:variable name="kw-path">http://d.hatena.ne.jp/keyword/</xsl:variable>
<xsl:variable name="asin-path">http://d.hatena.ne.jp/asin/</xsl:variable>
<xsl:variable name="isbn-path">http://d.hatena.ne.jp/asin/</xsl:variable>
<xsl:variable name="d-path">http://d.hatena.ne.jp/</xsl:variable>
<xsl:variable name="a-path">http://a.hatena.ne.jp/</xsl:variable>
<xsl:variable name="g-domain">g.hatena.ne.jp</xsl:variable>
<xsl:variable name="google-path">http://www.google.co.jp/search?ie=euc-jp&amp;q=</xsl:variable>
<xsl:variable name="amazon-path">http://www.amazon.co.jp/exec/obidos/external-search?mode=blended&amp;encoding-string-jp=日本語&amp;keyword=</xsl:variable>
<!-- .............................................................. -->

<xsl:output method="html" encoding="euc-jp"/>

<xsl:template match="/">
  <xsl:variable name="title" select="h:diary/h:head/h:title"/>
  <html>
    <head>
      <meta http-equiv="Content-Style-Type" content="text/css"/>
      <meta http-equiv="Content-Script-Type" content="text/javascript"/>
      <title><xsl:value-of select="$title"/></title>
      <link rel="stylesheet" href="{$theme}" type="text/css" media="all"/>
      <link rel="stylesheet" href="{$style}" type="text/css" media="all"/>
      <style type="text/css">
	.not-implemented { background-color: red; text-decoration: blink; }
      </style>
    </head>
    <body>
      <h1><xsl:value-of select="$title"/></h1>
      <div class="hatena-body">
	<div class="main">
	  <xsl:apply-templates select="h:diary/h:day"/>
	</div>
      </div>
    </body>
  </html>
</xsl:template>

<xsl:template match="h:day">
  <xsl:variable name="date" select="@date"/>
  <xsl:variable name="date-id">
    <xsl:call-template name="date-id"/>
  </xsl:variable>
  <div class="day">
    <h2>
      <a href="./{$date-id}">
	<span class="date"><xsl:value-of select="$date"/></span>
      </a>
      <xsl:text> </xsl:text>
      <xsl:apply-templates select="h:title" mode="title"/>
    </h2>
    <div class="body">
      <xsl:apply-templates select="*"/>
      <xsl:if test=".//h:note">
	<div class="footnote">
	  <xsl:apply-templates select=".//h:note" mode="footnote"/>
	</div>
      </xsl:if>
    </div>
  </div>
</xsl:template>

<xsl:template match="h:section">
  <xsl:variable name="id">
    <xsl:choose>
      <xsl:when test="@id">
	<xsl:value-of select="@id"/>
      </xsl:when>
      <xsl:otherwise>
	<xsl:text>p</xsl:text>
	<xsl:value-of select="position()"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:variable name="date-id">
    <xsl:call-template name="date-id"/>
  </xsl:variable>

  <div class="section">
    <h3>
      <a href="./{$date-id}#{$id}" name="{$id}">
	<span class="sanchor">■</span>
      </a>
      <xsl:text> </xsl:text>
      <xsl:apply-templates select="h:c" mode="title"/>
      <xsl:value-of select="h:title"/>
    </h3>
    <xsl:apply-templates select="*"/>
  </div>
</xsl:template>

<xsl:template match="h:c" mode="title">
  <xsl:text>[</xsl:text>
  <a href="" class="sectioncategory"><xsl:value-of select="."/></a>
  <xsl:text>]</xsl:text>
</xsl:template>

<xsl:template match="h:title" mode="title">
  <xsl:apply-templates />
</xsl:template>

<xsl:template match="h:title|h:c"/>

<xsl:template match="h:*">
  <xsl:element name="{local-name(.)}">
    <xsl:apply-templates select="node()|@*"/>
  </xsl:element>
</xsl:template>

<xsl:template match="@*">
  <xsl:attribute name="{local-name(.)}">
    <xsl:apply-templates/>
  </xsl:attribute>
</xsl:template>

<xsl:template match="h:tex">
  <span class="not-implemented"> **未実装** </span>
</xsl:template>


<xsl:template match="h:kw">
  <a href="{$kw-path}{.}"><xsl:value-of select="."/></a>
</xsl:template>

<xsl:template match="h:kw[@g]">
  <a href="http://{@g}.{$g-domain}/keyword/{.}">
    <xsl:value-of select="."/>
  </a>
</xsl:template>

<xsl:template match="h:url">
  <a href="{.}"><xsl:value-of select="."/></a>
</xsl:template>

<xsl:template match="h:ref[@asin]">
  <a href="{$asin-path}{@asin}">asin:<xsl:value-of select="@asin"/></a>
</xsl:template>

<xsl:template match="h:ref[@isbn]">
  <a href="{$isbn-path}{@isbn}"
     >isbn:<xsl:value-of select="translate(@isbn, '-', '')"/></a>
</xsl:template>

<xsl:template match="h:ref[@asin][@image]">
  <xsl:call-template name="asin-img">
    <xsl:with-param name="number" select="@asin"/>
    <xsl:with-param name="image" select="@image"/>
    <xsl:with-param name="path" select="$asin-path"/>
  </xsl:call-template>
</xsl:template>

<xsl:template match="h:ref[@isbn][@image]">
  <xsl:call-template name="asin-img">
    <xsl:with-param name="number" select="translate(@isbn, '-', '')"/>
    <xsl:with-param name="image" select="@image"/>
    <xsl:with-param name="path" select="$isbn-path"/>
  </xsl:call-template>
</xsl:template>

<xsl:template name="asin-img">
  <xsl:param name="number"/>
  <xsl:param name="image"/>
  <xsl:param name="path"/>

  <xsl:variable name="asin">
    <xsl:value-of select="translate($number,
		  'abcdefghijklmnopqrstuvwxyz',
		  'ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>
  </xsl:variable>
  <xsl:variable name="suffix">
    <xsl:choose>
      <xsl:when test="$image='small'">THUMBZZZ</xsl:when>
      <xsl:when test="$image='medium'">MZZZZZZZ</xsl:when>
      <xsl:when test="$image='large'">LZZZZZZZ</xsl:when>
    </xsl:choose>
  </xsl:variable>
  <a href="{$path}{$asin}">
    <img src="http://images-jp.amazon.com/images/P/{$asin}.09.{$suffix}.jpg"/>
  </a>
</xsl:template>

<xsl:template match="h:ref[@id][@g]">
  <a href="http://{@g}.{$g-domain}/{@id}/{@ref}">
    <xsl:call-template name="diary-ref-string"/>
  </a>
</xsl:template>

<xsl:template match="h:ref[@id][not(@g)]">
  <a href="{$d-path}{@id}/{@ref}">
    <xsl:call-template name="diary-ref-string"/>
  </a>
</xsl:template>

<xsl:template match="h:ref[@g][not(@id)]">
  <a href="http://{@g}.{$g-domain}/">
    <xsl:call-template name="diary-ref-string"/>
  </a>
</xsl:template>

<xsl:template name="diary-ref-string">
  <xsl:if test="@g">
    <xsl:text>g:</xsl:text>
    <xsl:value-of select="@g"/>
  </xsl:if>
  <xsl:if test="@id">
    <xsl:text>:</xsl:text>
    <xsl:text>id:</xsl:text>
    <xsl:value-of select="@id"/>
  </xsl:if>
  <xsl:if test="@ref">
    <xsl:text>:</xsl:text>
    <xsl:value-of select="@ref"/>
  </xsl:if>
</xsl:template>

<xsl:template match="h:ref[@a]">
  <a href="{$a-path}{@a}/">a:id:<xsl:value-of select="@a"/></a>
</xsl:template>

<xsl:template match="h:sf[@type='google']">
  <a href="{$google-path}{.}">google:<xsl:value-of select="."/></a>
</xsl:template>

<xsl:template match="h:sf[@type='amazon']">
  <a href="{$amazon-path}{.}">amazon:<xsl:value-of select="."/></a>
</xsl:template>


<xsl:template match="h:note">
  <xsl:variable name="date"><xsl:call-template name="date-id"/></xsl:variable>
  <xsl:variable name="n"><xsl:call-template name="fn"/></xsl:variable>
  <span class="footnote">
    <a title="{.}" href="#{$date}f{$n}" name="{$date}fn{$n}">
      <xsl:text>*</xsl:text>
      <xsl:value-of select="$n"/>
    </a>
  </span>
</xsl:template>

<xsl:template match="h:note" mode="footnote">
  <xsl:variable name="date"><xsl:call-template name="date-id"/></xsl:variable>
  <xsl:variable name="n"><xsl:call-template name="fn"/></xsl:variable>

  <p class="footnote">
    <a href="#{$date}fn{$n}" name="{$date}f{$n}">
      <xsl:text>*</xsl:text>
      <xsl:value-of select="$n"/>
    </a>
    <xsl:text>:</xsl:text>
    <xsl:apply-templates />
  </p>
</xsl:template>

<xsl:template name="date-id">
  <xsl:value-of select="translate(ancestor-or-self::h:day/@date, '-', '')"/>
</xsl:template>

<xsl:template name="fn">
  <xsl:number level="any" from="h:day" count="h:note"/>
</xsl:template>
<!--
TBD:
- 自分の日記への参照をローカルファイルへの参照に?
-->
</xsl:stylesheet>

