<?xml version="1.0" encoding="UTF-8"?>

<!-- 文章系ページ用-->

<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:fl="http://www.remus.dti.ne.jp/~a-satomi/filelist"
	xmlns:foaf="http://xmlns.com/foaf/0.1/"
	xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" 
	xmlns:xalan="http://xml.apache.org/xalan"
	exclude-result-prefixes="h s js fl foaf rdf xalan">

<xsl:import href="common.xsl"/>
<xsl:import href="foaf2html-person.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="oldestPagePath">
	<xsl:for-each select="document($fileList)/fl:dir/fl:dir[@name = $curDirName]/fl:file[2]">
		<xsl:call-template name="getData">
			<xsl:with-param name="data">path</xsl:with-param>
		</xsl:call-template>
	</xsl:for-each>
</xsl:param>

<!-- 子ページのうちいちばん新しい物の仮想的フルパス -->
<xsl:param name="newestPagePath">
	<xsl:for-each select="document($fileList)/fl:dir/fl:dir[@name = $curDirName]/fl:file[last()]">
		<xsl:call-template name="getData">
			<xsl:with-param name="data">path</xsl:with-param>
		</xsl:call-template>
	</xsl:for-each>
</xsl:param>

<!-- 一連の子ページ中で最新の更新日付 -->
<xsl:param name="latestModifyDay">
	<xsl:call-template name="getLatestModifyDay">
		<xsl:with-param name="path" select="$oldestPagePath"/>
	</xsl:call-template>
</xsl:param>

<xsl:param name="cssFile">textpages.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>

<!-- ================ 一覧ページの ul ================  -->

<xsl:template match="/h:html/h:ul">
	<xsl:choose>
		<!-- 処理中ページが一覧ページだったら -->
		<xsl:when test="$curFilePath = concat('/', $curDirName, '/index.xml')">
			<dl>
				<xsl:for-each select="h:li">
					<xsl:variable name="targetFileFPath"
						select="concat($baseDir, substring-before($curFilePath, '/index.xml'), '/', descendant::s:a/@href)"/>
					<xsl:variable name="targetFileLastMod" select="document($targetFileFPath)/h:html/h:head/s:file/@lastmod"/>
					<xsl:variable name="targetFileLastModText" select="concat('(', js:n2DTF(string($targetFileLastMod)), ')')"/>
					<dt>
						<xsl:apply-templates/>
						<span class="datetime">
							<xsl:choose>
								<xsl:when test="$targetFileLastMod = $latestModifyDay">
									<strong><xsl:value-of select="$targetFileLastModText"/></strong>
								</xsl:when>
								<xsl:otherwise>
									<xsl:value-of select="$targetFileLastModText"/>
								</xsl:otherwise>
							</xsl:choose>
						</span>
					</dt>
					<dd>
						<xsl:apply-templates select="document($targetFileFPath)/h:html/h:head/s:summary/node()"/>
					</dd>
				</xsl:for-each>
			</dl>
		</xsl:when>
		<!-- 処理中ページが一覧ページでは無いなら -->
		<xsl:otherwise>
			<xsl:apply-imports/>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<!-- ================ foaf ページ用 ================  -->

<xsl:template match="s:foaf">
	<xsl:param name="foafFile" select="concat($baseDir, '/', $curDirName, '/foaf/foaf.rdf')"/>
	<xsl:choose>
		<xsl:when test="@mode = 'myself'">
			<xsl:apply-templates select="document($foafFile)/rdf:RDF/foaf:Person" mode="myself"/>
		</xsl:when>
		<xsl:when test="@mode = 'friends'">
			<xsl:apply-templates select="document($foafFile)/rdf:RDF/foaf:Person/foaf:knows[1]"/>
		</xsl:when>
		<xsl:otherwise/>
	</xsl:choose>
</xsl:template>

<!-- ================ 100 質問ページの dt ================  -->

<xsl:template match="/h:html/h:dl/h:dt">
	<xsl:choose>
		<!-- 処理中ページが 100 質問ページ (class="q100") だったら -->
		<xsl:when test="contains(/h:html/h:h1/@class, 'q100')">
			<xsl:variable name="question-num">
				<xsl:number level="any" count="/h:html/h:dl/h:dt"/>
			</xsl:variable>
			<dt id="q{$question-num}">
				<xsl:value-of select="concat('Q', $question-num, '. ')"/>
				<xsl:apply-templates select="node()"/>
			</dt>
		</xsl:when>
		<!-- 処理中ページが 100 質問ページではないなら -->
		<xsl:otherwise>
			<xsl:apply-imports/>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<!-- ================ 作成日・更新日時表示テンプレを変更 ================  -->

<xsl:template match="s:file">
	<xsl:choose>
		<!-- 一覧ページ用処理 -->
		<xsl:when test="$curFilePath = concat('/', $curDirName, '/index.xml')">
			<span class="datetime">
				<xsl:text>Created: </xsl:text><xsl:value-of select="js:n2DTF(string(@created))"/>
				<xsl:text>, </xsl:text>
				<xsl:text>Last-modified: </xsl:text><xsl:value-of select="js:n2DTF(string($latestModifyDay))"/>
			</span>
		</xsl:when>
		<!-- 各ページ用処理 -->
		<xsl:otherwise>
			<xsl:apply-imports/>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

</xsl:stylesheet>
