<?xml version="1.0" encoding="UTF-8"?>

<!-- ねこめしにっき用 XSL -->

<xsl:stylesheet version="1.0" 
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns="http://www.w3.org/1999/xhtml"
	xmlns:h="http://www.w3.org/1999/xhtml"
	xmlns:s="http://www.remus.dti.ne.jp/~a-satomi/"
	xmlns:js="http://www.remus.dti.ne.jp/~a-satomi/jsfunc"
	xmlns:ng="http://www.remus.dti.ne.jp/~a-satomi/nikki-genres"
	xmlns:rss="http://purl.org/rss/1.0/"
	xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" 
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/"
	xmlns:xalan="http://xml.apache.org/xalan"
	exclude-result-prefixes="h s js ng rss rdf dc trackback xalan">

<xsl:import href="common.xsl"/>
<xsl:import href="rss2html-item.xsl"/>

<!-- ================ 出力設定 ================  -->

<xsl:output method="xml"
	encoding="Shift_JIS"
	omit-xml-declaration="no"
	doctype-public="-//W3C//DTD XHTML 1.1//EN"
	doctype-system="http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"
	indent="yes"
	xalan:indent-amount="2"
	media-type="application/xhtml+xml"/>

<!-- ================ 各種設定 ================  -->

<xsl:param name="cssFile">nikki.css</xsl:param>

<!-- ================ メインテンプレ ================  -->

<xsl:template match="/h:html">
	<html xml:lang="ja">
		<xsl:apply-templates select="h:head"/>
	<body>
		<xsl:apply-templates select="h:h1/@*"/>
		<xsl:call-template name="navi"/>
		<xsl:apply-templates select="h:h1"/>
		<xsl:call-template name="navi"/>
		<xsl:call-template name="footer"/>
	</body>
	</html>
</xsl:template>

<!-- ================ にっき各ページの前記部分生成 ================  -->

<xsl:template match="h:h1">
	<xsl:choose>
		<!-- にっきトップページ・過去記事一覧ページは何もしない -->
		<xsl:when test="contains(@class, 'indexPage')">
			<xsl:apply-imports/>
		</xsl:when>
		<!-- それ以外（＝にっき各ページ） -->
		<xsl:otherwise>
			<div class="section">
				<h1><xsl:apply-templates/></h1>
				<xsl:call-template name="preface"/>
				<xsl:for-each select="following-sibling::h:h2[1]">
					<xsl:call-template name="section"/>
				</xsl:for-each>
			</div>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<xsl:template name="preface">
	<xsl:choose>
		<!-- ねこめしにっき各ページ -->
		<xsl:when test="contains(self::node(), 'ねこめしにっき')">
			<ul>
				<xsl:for-each select="document(concat($baseDir, $contentsPath))/h:html/h:h2[@id = 'mainItems']/following-sibling::h:dl[1]/h:dt">
					<li><xsl:apply-templates/></li>
				</xsl:for-each>
			</ul>
		</xsl:when>
		<!-- 古い「のうがき」各ページ -->
		<xsl:when test="contains(self::node(), 'のうがき')">
			<p>以前トップページにあった「今日ののうがき」の過去ログです。</p>
		</xsl:when>
		<xsl:otherwise/>
	</xsl:choose>
</xsl:template>

<!-- ======== s:headline 要素 (RSS からヘッドラインを生成) とその関連=========  -->

<!-- RSS からヘッドラインを生成 -->
<xsl:template match="s:headline">
	<xsl:apply-templates select="document(concat($baseDir, $rssPath))/rdf:RDF">
		<xsl:with-param name="item-num" select="@num"/><!-- リストアップする項目数 -->
	</xsl:apply-templates>
</xsl:template>

<!-- にっきトップページの h2.headines の s:datetime だけ処理変更 -->
<xsl:template match="s:datetime">
	<xsl:choose>
		<xsl:when test="$curFilePath = concat('/', $curDirName, '/', 'index.xml') and parent::h:h2/@id = 'headlines'">
			<span class="datetime">
				<xsl:text>(</xsl:text>
				<xsl:apply-templates select="document(concat($baseDir, $rssPath))/rdf:RDF/rss:channel/dc:date"/>
				<xsl:text> 現在)</xsl:text>
			</span>
		</xsl:when>
		<xsl:otherwise>
			<xsl:apply-imports/>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<!-- =========== s:genre 属性（所属ジャンル） ===========  -->

<xsl:template match="@s:genre">
	<xsl:param name="title" select="string(parent::node())"/>
	<xsl:param name="id" select="string(parent::node()/@id)"/>
	<xsl:param name="purl" select="js:replace(concat($baseURI, $curFilePath, '#', $id), '/(.+).xml#(.+)/', '$1.html#$2')"/>
	<xsl:param name="wb" select="string('http://alimika.alib.jp/nikki/writeback')"/>
	<xsl:param name="tb_id" select="js:replace(concat($curFilePath, '#', $id), '/.+\/(\d+)\/(\w+)\.xml#d(\d+)n(\d+)$/', '$1_$2_$3_$4')"/>

	<xsl:if test="string(self::node())">

		<ul class="infoDisplay">
			<!-- ジャンル表示を生成 -->
			<li class="genre">
				<xsl:text>Genre : </xsl:text>
				<xsl:call-template name="displayGenres"/>
			</li>
			<!-- WriteBack 一覧へのリンクを生成 -->
			<xsl:if test="js:match($id, '/^d(\d+)n(\d+)$/')"><!-- 暫定回避措置 -->
				<li class="writeback">
					<a title="「{$title}」へのご意見ご感想" href="{$wb}/list/{$tb_id}"><span class="comment">Comment</span>, <span class="trackback">TrackBack</span></a>
				</li>
			</xsl:if>
		</ul>

		<!-- TrackBack Auto Discovery 用の RDF 生成 -->
		<!--
		　本来これで無問題であるはずの記述
		　　<rdf:RDF>
		　　　<rdf:Description rdf:about="{$purl}" dc:identifier="{$purl}" dc:title="{$title}" trackback:ping="{$wb}/tb/{$tb_id}"/>
		　　</rdf:RDF>
		　・RDF の要素が埋め込まれている XHTML をうまく扱えない (ダウンロードになってしまう等の) ブラウザが多勢である
		　　　→ RDF 要素部分を丸ごとコメントにする ad hoc ソリューションがデファクト
		　・Trackback auto discovery の実装を見聞するに、DOM ツリーの走査から値を抽出する (望ましい) 処理なぞしておらず
		　　XHTML ソースを単に文字列として扱い、正規表現で抽出するというレガシーな手法が主流 (あるいは全て) っぽい。
		　　　→ デファクト的記述からなるべく逸脱しないソース形態を取るのが安全かもしれず。
		-->

		<xsl:comment>
			<xsl:text disable-output-escaping="yes">
          &lt;rdf:RDF xmlns:rdf=&quot;http://www.w3.org/1999/02/22-rdf-syntax-ns#&quot;
                   xmlns:dc=&quot;http://purl.org/dc/elements/1.1/&quot;
                   xmlns:trackback=&quot;http://madskills.com/public/xml/rss/module/trackback/&quot;&gt;
            &lt;rdf:Description
              rdf:about=&quot;</xsl:text>
			<xsl:value-of select="$purl"/>
			<xsl:text disable-output-escaping="yes">&quot;
              dc:identifier=&quot;</xsl:text>
			<xsl:value-of select="$purl"/>
			<xsl:text disable-output-escaping="yes">&quot;
              dc:title=&quot;</xsl:text>
			<xsl:value-of select="$title"/>
			<xsl:text disable-output-escaping="yes">&quot;
              trackback:ping=&quot;</xsl:text>
			<xsl:value-of select="concat($wb, '/tb/', $tb_id)"/>
			<xsl:text disable-output-escaping="yes">&quot; /&gt;
          &lt;/rdf:RDF&gt;
          </xsl:text>
		</xsl:comment>

	</xsl:if>
</xsl:template>

<xsl:template name="displayGenres">
	<xsl:param name="genre" select="self::node()"/>
	<xsl:param name="genre_">
		<xsl:choose>
			<xsl:when test="contains($genre, ' ')"><xsl:value-of select="substring-before($genre, ' ')"/></xsl:when>
			<xsl:otherwise><xsl:value-of select="$genre"/></xsl:otherwise>
		</xsl:choose>
	</xsl:param>

	<xsl:choose>
		<xsl:when test="document($genreList)/descendant::ng:genre[@name = $genre_]">
			<a title="{document($genreList)/descendant::ng:genre[@name = $genre_]}" href="../{$genreDirName}/{$genre_}.html">
				<xsl:value-of select="$genre_"/>
			</a>
		</xsl:when>
		<xsl:otherwise>
				<xsl:value-of select="$genre_"/>
		</xsl:otherwise>
	</xsl:choose>

	<xsl:if test="contains($genre, ' ')">
		<xsl:text> | </xsl:text>
		<xsl:call-template name="displayGenres">
			<xsl:with-param name="genre" select="substring-after($genre, ' ')"/>
		</xsl:call-template>
	</xsl:if>
</xsl:template>

<!-- ====== s:genrelist 要素 (ジャンル別見出し一覧ページへのリンクを生成) ======  -->

<xsl:template match="s:genrelist">
	<xsl:for-each select="document($genreList)/ng:genres/ng:category">
		<div id="{@name}" class="section">
			<h3><xsl:value-of select="@label"/></h3>
			<ul>
				<xsl:for-each select="ng:genre">
					<li id="{@name}"><a href="{$genreDirName}/{@name}.html"><xsl:value-of select="."/></a></li>
				</xsl:for-each>
			</ul>
		</div>
	</xsl:for-each>
</xsl:template>

</xsl:stylesheet>

