<?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:xalan="http://xml.apache.org/xalan"
	xmlns:redirect="http://xml.apache.org/xalan/redirect"
	extension-element-prefixes="redirect"
	exclude-result-prefixes="h s js fl xalan">

<xsl:import href="common.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[@name = 'index.xml']/following-sibling::fl:file[1]">
		<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>

<!-- 適用 CSS -->
<xsl:param name="cssFile" select="concat($curDirName, '.css')"/>

<!-- だーくフラグ -->
<xsl:param name="darkMode" select="false()"/>


<!-- ================ メインテンプレ ================  -->

<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>

<!-- ================ h1 要素変更 ================  -->

<xsl:template match="h:h1">
	<xsl:choose>
		<!-- 各展示ページ用処理 -->
		<xsl:when test="not(@class)">
			<div class="section">
				<h1><xsl:apply-templates/></h1>
				<xsl:apply-templates select="/h:html/h:head/s:img"/>
				<xsl:apply-templates select="/h:html/h:head/s:imgs"/>
				<xsl:apply-templates select="/h:html/h:head/s:bookdata"/>
				<xsl:apply-templates select="/h:html/h:h2"/>
				<xsl:apply-templates select="/h:html/h:head/s:add-img"/>
			</div>
		</xsl:when>
		<!-- 一覧ページ/前置きページ用処理 -->
		<xsl:otherwise>
			<xsl:apply-imports/>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<!-- ================ h2 要素変更 ================  -->

<xsl:template match="h:h2[1]">
	<xsl:call-template name="section"/>
</xsl:template>

<xsl:template match="h:h2[position() != 1]"/>

<!-- ================ 一覧ページのコンテンツリスト ================  -->

<xsl:template match="s:toc">
	<xsl:apply-templates select="
			document($fileList)/fl:dir/fl:dir[@name = $curDirName]/fl:file[@name = 'index.xml']/following-sibling::fl:file">
				<xsl:sort select="position()" order="descending" data-type="number"/>
	</xsl:apply-templates>
</xsl:template>

<!-- ================ 一覧ページ本文生成 ================  -->

<xsl:template match="fl:file">
	<xsl:param name="path"><!-- 現在の処理対象ファイルの仮想的フルパス -->
		<xsl:call-template name="getData">
			<xsl:with-param name="data">path</xsl:with-param>
		</xsl:call-template>
	</xsl:param>

	<!-- 現在の対象ファイルへカレントノード移動 -->
	<xsl:for-each select="document(concat($baseDir, $path))/h:html">

		<!-- リンクアンカーの title 要素 -->
		<xsl:variable name="anchor-title">
			<xsl:value-of select="concat('「', h:h1, '」')"/>
			<xsl:choose>
				<xsl:when test="h:head/s:bookdata">の詳細</xsl:when>
				<xsl:otherwise>をみる</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>

		<!-- img 要素の width 属性値 -->
		<xsl:variable name="img-width">
			<xsl:choose>
				<xsl:when test="h:head/s:bookdata">70</xsl:when>
				<xsl:otherwise>100</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>

		<!-- div.section 生成 -->
		<div class="section">

			<!-- 見出しとリンクアンカー生成 -->
			<h2>
				<a title="{$anchor-title}" href="{js:chsuffix(js:a2r(string($curFilePath), string($path)), 'html')}">
					<img alt="" height="100" width="{$img-width}" src="thumbnails/{h:head/s:thumbnail/@src}" />
					<xsl:apply-templates select="h:h1/node()"/>
				</a>
			</h2>

			<!-- 説明を生成 -->
			<xsl:choose>
				<xsl:when test="h:head/s:bookdata">
					<ul>
						<li><xsl:apply-templates select="h:head/s:summary/node()"/></li>
						<li><xsl:call-template name="book-releaseDay"/></li>
					</ul>
				</xsl:when>
				<xsl:otherwise>
					<p><xsl:apply-templates select="h:head/s:summary/node()"/></p>
				</xsl:otherwise>
			</xsl:choose>

			<!-- 補足データ表示を生成 -->
			<xsl:apply-templates select="h:head/s:memo"/>

			<!-- 更新日表示を生成 -->
			<xsl:variable name="targetFileLastMod" select="concat('(', js:n2DTF(string(h:head/s:file/@lastmod)), ')')"/>
			<p class="datetime">
				<xsl:choose>
					<xsl:when test="h:head/s:file/@lastmod = $latestModifyDay">
						<strong><xsl:value-of select="$targetFileLastMod"/></strong>
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="$targetFileLastMod"/>
					</xsl:otherwise>
				</xsl:choose>
			</p>
		</div>
	</xsl:for-each>
</xsl:template>

<!-- ================ s:img 要素 （展示イラスト/同人誌表紙) ================  -->

<xsl:template match="s:img[1]">
	<div class="picture">
		<xsl:apply-templates select="self::node() | following-sibling::s:img" mode="inner"/>
	</div>
</xsl:template>

<xsl:template match="s:img" mode="inner">
	<xsl:choose>
		<!-- 処理対象が同人誌展示ページ -->
		<xsl:when test="/h:html/h:head/s:bookdata">
			<xsl:variable name="img-title">
				<xsl:choose>
					<xsl:when test="position() = 1"><xsl:value-of select="concat('「', /h:html/h:h1 ,'」表紙')"/></xsl:when>
					<xsl:when test="position() = 2"><xsl:value-of select="concat('「', /h:html/h:h1 ,'」裏表紙')"/></xsl:when>
					<xsl:otherwise/>
				</xsl:choose>
			</xsl:variable>
			<img title="{$img-title}" alt="" height="{@height}" width="{@width}" src="bookcovers/{@src}"/>
		</xsl:when>
		<!-- 処理対象がイラスト展示系ページ -->
		<xsl:otherwise>
			<img title="{/h:html/h:h1}" alt="" height="{@height}" width="{@width}" src="pictures/{@src}"/>
			<xsl:call-template name="makePictureData"/>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<!-- ========= s:add-img 要素 (おまけファイルセクション) =========  -->

<xsl:template match="s:add-img[1]">
	<div class="section">
		<h2>おまけ</h2>
		<dl class="refLink">
			<xsl:apply-templates select="self::node() | following-sibling::s:add-img" mode="inner"/>
		</dl>
	</div>
</xsl:template>

<xsl:template match="s:add-img" mode="inner">
	<dt>
		<a title="{@src}" href="pictures/{@src}">
			<xsl:value-of select="@label"/>
		</a>
	</dt>
	<dd>
		<xsl:call-template name="makePictureData"/>
	</dd>
</xsl:template>

<!-- ================ s:bookdata 要素（同人誌の諸元データ） ================  -->

<xsl:template match="s:bookdata">
	<ul class="bookdata">
		<li><xsl:apply-templates select="/h:html/h:head/s:summary/node()"/></li>
		<li>
			<xsl:value-of select="@size"/>
			<xsl:text> / </xsl:text>
			<xsl:value-of select="@pages"/>
			<xsl:text> pages / </xsl:text>
			<xsl:choose>
				<xsl:when test="@method = 'offset'">オフセット</xsl:when>
				<xsl:when test="@method = 'copy'">コピー</xsl:when>
				<xsl:otherwise/>
			</xsl:choose>
		</li>
		<li><xsl:call-template name="book-releaseDay"/></li>
	</ul>
	<!-- 補足データ表示を生成 -->
	<xsl:apply-templates select="/h:html/h:head/s:memo"/>
</xsl:template>

<!-- ================ イラスト画像ファイルの諸元データ ================  -->

<xsl:template name="makePictureData">
	<xsl:value-of select="@src"/>
	<xsl:text> (</xsl:text><xsl:value-of select="@width"/>
	<xsl:text> x </xsl:text>
	<xsl:value-of select="@height"/>
	<xsl:text> pixels , </xsl:text>
	<xsl:value-of select="@fsize"/>
	<xsl:text> KBytes) </xsl:text>
	<xsl:value-of select="js:n2DTF(string(@created))"/>
	<xsl:text> 作成</xsl:text>
	
	<xsl:if test="@lastmod">
		<xsl:text>、 </xsl:text>
		<xsl:value-of select="js:n2DTF(string(@lastmod))"/>
		<xsl:text> 手直し</xsl:text>
	</xsl:if>
</xsl:template>

<!-- ================ 同人誌発行日表示 ================  -->

<xsl:template name="book-releaseDay">
	<xsl:value-of select="js:n2DTF(string(/h:html/h:head/s:bookdata/@release))"/>
	<xsl:choose>
		<xsl:when test="/h:html/h:head/s:bookdata/@limited = 'yes'"> 限定本</xsl:when>
		<xsl:otherwise> 初版発行</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<!-- ================ s:file 要素（作成日・更新日時表示）を変更 ================  -->

<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>

<!-- ================ /h:html/h:head/s:memo（補足データ） ================  -->

<xsl:template match="/h:html/h:head/s:memo[1]">
	<ul class="memo">
		<xsl:apply-templates select="self::node() | following-sibling::s:memo" mode="inner"/>
	</ul>
</xsl:template>

<xsl:template match="/h:html/h:head/s:memo[position() != 1]"/>

<xsl:template match="/h:html/h:head/s:memo" mode="inner">
	<li><xsl:apply-templates/></li>
</xsl:template>


<!-- ====== s:imgs 要素（連番各画像ページの HTML 生成とそこへのリンクを生成 ======  -->

<xsl:template match="s:imgs">
	<ul class="picture">
		<xsl:call-template name="s-imgs-roop"/>
	</ul>
	<p class="pictureinfo"><xsl:call-template name="makePictureData-s-imgs"/></p>
</xsl:template>

<!-- ループ -->
<xsl:template name="s-imgs-roop">
	<xsl:param name="num" select="1"/>
	<xsl:param name="fnum" select="format-number($num, '00')"/>
	<xsl:param name="fname" select="js:replace(string($curFilePath), '/.+\/(.+).xml/', '$1')"/>

	<li>
		<a title="{$curFileTitle} / Page {$fnum}" href="{$fname}/{$fnum}.html">
			<img alt="" height="{s:thumbnail/@height}" width="{s:thumbnail/@width}" src="{$fname}/thumbnails/{$fnum}.{s:thumbnail/@suffix}"/>
			<xsl:value-of select="concat('Page ', $fnum)"/>
		</a>
	</li>

	<xsl:call-template name="s-imgs-each-page">
		<xsl:with-param name="fnum" select="$fnum"/>
		<xsl:with-param name="fname" select="$fname"/>
	</xsl:call-template>

	<xsl:if test="$num &lt; @length">
		<xsl:call-template name="s-imgs-roop">
			<xsl:with-param name="num" select="$num + 1"/>
		</xsl:call-template>
	</xsl:if>
</xsl:template>

<!-- 各 HTML 出力 -->
<xsl:template name="s-imgs-each-page">
	<xsl:param name="fnum"/>
	<xsl:param name="fname"/>

	<redirect:write select="concat($baseDir, '/', $curDirName, '/', $fname, '/', $fnum , '.html')">

		<!-- head 要素内。再利用される。href, src 属性値の相対パス ../ と ./ はそれぞれ ../../ と ../ に変換される -->
		<xsl:variable name="head">
			<head>
			<xsl:call-template name="head-meta"/>
			<title><xsl:value-of select="concat($curFileTitle, ' / Page ', $fnum)"/></title>
			<link href="{$rootURI}" title="{$rootTitle}" rel="home"/>
			<link href="./{$fname}.html" title="{$curFileTitle}" rel="contents"/>
			<xsl:if test="number($fnum) &gt; 1">
				<xsl:variable name="fnum_" select="format-number(number($fnum) - 1, '00')"/>
				<link href="{$fnum_}.html" title="{$curFileTitle} - Page {$fnum_}" rel="prev"/>
			</xsl:if>
			<xsl:if test="number($fnum) &lt; @length">
				<xsl:variable name="fnum_" select="format-number(number($fnum) + 1, '00')"/>
				<link href="{$fnum_}.html" title="{$curFileTitle} - Page {$fnum_}" rel="next"/>
			</xsl:if>
			<xsl:call-template name="head-metaLink"/>
			<xsl:call-template name="head-miscLink"/>
			<xsl:call-template name="head-css"/>
			<xsl:call-template name="head-script"/>
			</head>
		</xsl:variable>

		<!-- 注意事項セクション。再利用される。 -->
		<xsl:variable name="notice">
			<s:notice/>
		</xsl:variable>

		<!-- フッタ。再利用される。href, src 属性値の相対パス ../ と ./ はそれぞれ ../../ と ../ に変換される -->
		<xsl:variable name="footer">
			<xsl:choose>
				<xsl:when test="not(boolean($darkMode))"><xsl:call-template name="footer"/></xsl:when>
				<xsl:otherwise><xsl:call-template name="footer-dark"/></xsl:otherwise>
			</xsl:choose>
		</xsl:variable>

		<html xml:lang="ja">
			<xsl:apply-templates select="xalan:nodeset($head)/node()" mode="s-imgs-each-page"/>
			<body>
				<xsl:call-template name="s-imgs-each-page-navi">
					<xsl:with-param name="fnum" select="$fnum"/>
					<xsl:with-param name="fname" select="$fname"/>
				</xsl:call-template>
				<div class="section">
					<h1><xsl:value-of select="concat($curFileTitle, ' / Page ', $fnum)"/></h1>
					<div class="picture" id="p">
						<img title="{$curFileTitle} / Page {$fnum}" alt="" height="{@height}" width="{@width}" src="pictures/{$fnum}.{@suffix}"/>
						<xsl:call-template name="makePictureData-s-imgs">
							<xsl:with-param name="fnum" select="$fnum"/>
						</xsl:call-template>
					</div>
					<xsl:apply-templates select="s:img[format-number(@num, '00') = $fnum]" mode="s-imgs-each-page"/>
					<xsl:apply-templates select="xalan:nodeset($notice)"/>
				</div>
				<xsl:call-template name="s-imgs-each-page-navi">
					<xsl:with-param name="fnum" select="$fnum"/>
					<xsl:with-param name="fname" select="$fname"/>
					<xsl:with-param name="switchDarkMode" select="true()"/>
				</xsl:call-template>
				<xsl:apply-templates select="xalan:nodeset($footer)/node()" mode="s-imgs-each-page"/>
			</body>
		</html>
	</redirect:write>
</xsl:template>

<!-- ================ 各連番ページ用 / xalan:nodeset() 内ノード処理 ================  -->

<xsl:template match="h:*" mode="s-imgs-each-page" priority="-5.0">
	<xsl:element name="{local-name()}">
		<xsl:apply-templates select="node() | @*" mode="s-imgs-each-page"/>
	</xsl:element>
</xsl:template>

<!-- href, src 属性値の相対パスを一階層深くする -->
<xsl:template match="@*" mode="s-imgs-each-page" priority="-5.0">
	<xsl:choose>
		<xsl:when test="js:match(string(name()), '/^(href|src)$/') and js:match(string(self::node()), '/^\.\.?\//')">
			<xsl:attribute name="{name()}">
				<xsl:choose>
					<xsl:when test="starts-with(self::node(), '../')">
						<xsl:value-of select="concat('../', self::node())"/>
					</xsl:when>
					<xsl:when test="starts-with(self::node(), './')">
						<xsl:value-of select="concat('.', self::node())"/>
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="self::node()"/>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:attribute>
		</xsl:when>
		<xsl:otherwise>
			<xsl:copy/>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<!-- テキストノードとして存在している script 要素の src 属性値の相対パスを一階層深くする -->
<xsl:template match="text()[js:match(string(self::node()), '/(&lt;\/?script|^&quot;.+&quot;$)/')]" mode="s-imgs-each-page">
	<xsl:value-of select="js:replace(string(self::node()), '/\.\.\//g', '../../')" disable-output-escaping="yes"/>
</xsl:template>

<!-- ================ 各連番ページのナビリンク生成 ================  -->

<xsl:template name="s-imgs-each-page-navi">
	<xsl:param name="fnum"/>
	<xsl:param name="fname"/>
	<xsl:param name="iconDir">
		<xsl:choose>
			<xsl:when test="not(boolean($darkMode))"><xsl:value-of select="concat('../', $commonDir, '/img/icon')"/></xsl:when>
			<xsl:otherwise><xsl:value-of select="concat('../', $commonDir, '/img/dark/icon')"/></xsl:otherwise>
		</xsl:choose>
	</xsl:param>

	<ul class="navigation">
		<li class="home">
			<a href="../{$rootURI}" title="{$rootTitle} [T]" accesskey="T" rel="home">
				<img src="{$iconDir}/LeftArrow.png" width="15" height="15" alt="←" />
				<xsl:value-of select="$rootLabel"/>
			</a>
		</li>
		<li class="contents">
			<a href="../{$fname}.html" title="{$curFileTitle} [I]" accesskey="I" rel="contents">
				<img src="{$iconDir}/LeftArrow.png" width="15" height="15" alt="←" />
				<xsl:value-of select="$curFileTitle"/>
			</a>
		</li>
		<xsl:if test="number($fnum) &gt; 1">
			<xsl:variable name="fnum_" select="format-number(number($fnum) - 1, '00')"/>
			<li class="prev">
				<a href="{$fnum_}.html#p" title="{$curFileTitle} / Page {$fnum_} [P]" accesskey="P" rel="prev">
					<img src="{$iconDir}/LeftArrow.png" width="15" height="15" alt="←" />
					<xsl:value-of select="concat('Page ', $fnum_)"/>
				</a>
			</li>
		</xsl:if>
		<xsl:if test="number($fnum) &lt; @length">
			<xsl:variable name="fnum_" select="format-number(number($fnum) + 1, '00')"/>
			<li class="next">
				<a href="{$fnum_}.html#p" title="{$curFileTitle} / Page {$fnum_} [N]" accesskey="N" rel="next">
					<img src="{$iconDir}/RightArrow.png" width="15" height="15" alt="←" />
					<xsl:value-of select="concat('Page ', $fnum_)"/>
				</a>
			</li>
		</xsl:if>
	</ul>
</xsl:template>

<!-- ============ s:img 要素 （s:imgs 要素の子要素としての） ============  -->
<!--
　<s:imgs>
　　<s:img num="01">fugahoge</s:img>
　　　：
　</s:img>
-->

<xsl:template match="s:img" mode="s-imgs-each-page">
	<xsl:if test="child::node()">
		<div class="section">
			<h2>こめんと</h2>
			<p><xsl:apply-templates/></p>
		</div>
	</xsl:if>
</xsl:template>

<!-- ================ 連番イラスト画像群の諸元データ ================  -->

<xsl:template name="makePictureData-s-imgs">
	<xsl:param name="fnum"/>
	<xsl:value-of select="js:replace(string($curFilePath), '/.+\/(.+).xml/', '$1')"/>
	<xsl:choose>
		<xsl:when test="string($fnum)">
			<xsl:value-of select="concat('/', $fnum, '.', @suffix)"/>
		</xsl:when>
		<xsl:otherwise>
			<xsl:value-of select="concat(' [01-', format-number(@length, '00'), ']')"/>
		</xsl:otherwise>
	</xsl:choose>
	<xsl:value-of select="concat(' (', @width, ' x ', @height, ' pixels ,') "/>
	<xsl:if test="not(string($fnum))">それぞれ</xsl:if>
	<xsl:value-of select="concat('約 ', @fsize, ' KBytes) ', js:n2DTF(string(@created)), ' 作成')"/>
	<xsl:if test="@lastmod">
		<xsl:value-of select="concat('、 ', js:n2DTF(string(@lastmod)), ' 手直し')"/>
	</xsl:if>
</xsl:template>


<!-- ================ エラー回避用ダミー ================  -->

<xsl:template name="footer-dark"/>

</xsl:stylesheet>
