<?xml version="1.0" encoding="UTF-8"?>

<!-- section 構造なしの見出し/本文「フラットでリニア(R)」構造 → XHTML2 的 div.section/h[1-6] 構造変換 -->

<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/"
	exclude-result-prefixes="h s">

<!-- ================ section 構造生成 ================  -->
<!--
　section 構造化を開始する最上位の見出し要素のうち、出現順一番目の物が
　カレントノードとなってる状態でこのテンプレを呼び出す事。
　以下を参考にしました。多謝。
　・http://members.jcom.home.ne.jp/jintrick/Personal/d20026l.html#d29_8
　・http://tr.vis.ne.jp/diary/d200303b.html#d19a
-->

<xsl:template name="section">
	<!-- 現在の見出しレベル。無指定時は要素名から取得 -->
	<xsl:param name="level" select="substring(local-name(), 2, 1)"/>

	<xsl:if test="local-name() = concat('h', $level)">

		<div>
			<xsl:attribute name="class">
				<xsl:text>section</xsl:text>
				<xsl:if test="string(@class) and $level != 1"><!-- h1 の class は body の class とする方針なので -->
					<xsl:value-of select="concat(' ', @class)"/>
				</xsl:if>
			</xsl:attribute>
			<xsl:if test="string(@id) and $level != 1"><!-- h1 の id は body の id とする方針なので -->

				<xsl:apply-templates select="@id"/>
			</xsl:if>
			<xsl:apply-templates select="@*[name() != 'class' and name() != 'id' and not(starts-with(name(), 's:'))]"/>

			<xsl:element name="h{$level}">
				<xsl:choose>
					<!-- 現在の見出しに id 属性値があり、かつ -->
					<!-- 現在処理中ファイルの仮想的フルパスが '/nikki/4桁数字' で始まる (=日記の各ページである) 場合-->
					<xsl:when test="string(@id) and starts-with($curFilePath, '/nikki/') and substring($curFilePath, 8, 4) &gt; 0">

						<!-- 見出しアンカーを作成 -->
						<a title="#{@id}" href="#{@id}">
							<xsl:if test="not(string(self::node()))">*untitled*</xsl:if><!-- 内容がカラになるのを防止 -->
							<xsl:apply-templates/>
						</a>
					</xsl:when>

					<!-- それ以外 -->
					<xsl:otherwise>

						<!-- 単に内容をコピー -->
						<xsl:if test="not(string(self::node())) and not(h:img)">*untitled*</xsl:if><!-- 内容がカラになるのを防止 -->
						<xsl:apply-templates/>
					</xsl:otherwise>
				</xsl:choose>

				<!-- 日時表示を生成 -->
				<xsl:apply-templates select="@s:datetime"/>
			</xsl:element>

			<!-- ジャンル表示・ WriteBack リンク・ TrackBack Auto Discovery 用の隠し RDF 生成（日記ページ） -->
			<xsl:apply-templates select="@s:genre"/>

			<!-- 暗黙的に従属する本文部分を生成 -->
			<xsl:call-template name="section-content">
				<xsl:with-param name="level" select="$level"/>
			</xsl:call-template>
		</div>
	</xsl:if>

	<xsl:for-each select="following-sibling::node()[1]">
		<xsl:choose>
			<!-- カレントノードが、見出しの暗黙的従属部分の終端を表す s:h[1-6]end 要素だったら -->
			<xsl:when test="name() = concat('s:h', $level, 'end')">
				<xsl:call-template name="section-content">
					<xsl:with-param name="level" select="$level - 1"/>
				</xsl:call-template>
			</xsl:when>

			<!-- カレントノードが、現在の見出しレベルより上位の見出し要素でなければ -->
			<xsl:when test="not(
					starts-with(local-name(), 'h') and
					string-length(local-name()) = 2 and
					substring(local-name(), 2, 1) &lt; $level)">
				<xsl:call-template name="section">
					<xsl:with-param name="level" select="$level"/>
				</xsl:call-template>
			</xsl:when>
			<xsl:otherwise/>
		</xsl:choose>

	</xsl:for-each>
</xsl:template>

<xsl:template name="section-content">
	<xsl:param name="level"/><!-- 現在の見出しレベル -->
	<xsl:param name="afterHnEnd" select="false()"/><!-- s:h[1-6]end 要素を通過したか否か -->

	<xsl:for-each select="following-sibling::node()[1]">
		<xsl:choose>
			<!-- カレントノードが、見出し要素だったら -->
			<xsl:when test="
					starts-with(local-name(), 'h') and
					string-length(local-name()) = 2 and
					substring(local-name(), 2, 1) &gt; 0">

				<!-- 見出し要素のレベルが、現在のレベルよりひとつ下だったら -->
				<xsl:if test="substring(local-name(), 2, 1) = $level + 1">
					<xsl:call-template name="section">
						<xsl:with-param name="level" select="$level + 1"/>
					</xsl:call-template>
				</xsl:if>
			</xsl:when>

			<!-- s:h[1-6]end 要素をまだ通過してなければ -->

			<xsl:when test="not($afterHnEnd)">
				<xsl:apply-templates select="self::node()"/>
				<xsl:call-template name="section-content">
					<xsl:with-param name="level" select="$level"/>
					<xsl:with-param name="afterHnEnd" select="name() = concat('s:h', $level, 'end')"/>
				</xsl:call-template>
			</xsl:when>
			<xsl:otherwise/>
		</xsl:choose>

	</xsl:for-each>
</xsl:template>

</xsl:stylesheet>
