<?xml version="1.0" encoding="UTF-8"?>

<!--
　XHTML2 風 div.section/h[1-6] 構造 XHTML → 「フラットでリニア (R)」 構造変換
　これまで手書きしてきた既存の XHTML1.1 文書を、 XSLT 変換を前提とした独自仕様 XML へ変換する用途
-->

<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:xalan="http://xml.apache.org/xalan"
	exclude-result-prefixes="h xalan">

<xsl:output
	method="xml"
	encoding="Shift_JIS"
	omit-xml-declaration="no"
	indent="yes"
	xalan:indent-amount="2"
	media-type="application/xhtml+xml" />

<!-- ================ 各種設定 ================  -->

<xsl:param name="curFileName"/><!-- AppleScript ドロップレットより付与 -->
<xsl:param name="parentFolder"/><!-- AppleScript ドロップレットより付与 -->

<xsl:strip-space elements="*"/>
<xsl:preserve-space elements="pre"/>

<!-- ================ メイン ================  -->

<xsl:template match="/">
	<xsl:processing-instruction name="xml-stylesheet">
		<xsl:value-of select="concat('href=&quot;../common/xsl/', $parentFolder)"/>
		<xsl:text>.xsl&quot; type=&quot;text/xsl&quot;</xsl:text>
	</xsl:processing-instruction>
	<xsl:apply-templates select="h:html/h:body"/>
</xsl:template>

<xsl:template match="h:body">
	<html xml:lang="ja">
		<head>

			<s:file path="/{$parentFolder}/{$curFileName}">
				<xsl:if test="h:address/h:span[@class = 'datetime']">
					<xsl:attribute name="created">
						<xsl:value-of select="substring(substring-after(h:address/h:span[@class = 'datetime'], 'Created: '), 1, 10)"/>
					</xsl:attribute>
					<xsl:attribute name="lastmod">
						<xsl:value-of select="substring(substring-after(h:address/h:span[@class = 'datetime'], 'Last-modified: '), 1, 10)"/>
					</xsl:attribute>
				</xsl:if>
			</s:file>

			<xsl:apply-templates select="/h:html/h:head/h:style"/>
			<xsl:apply-templates select="/h:html/h:head/h:script[not(contains(@src, 'common.js'))]"/>

		</head>
		<h1>
			<xsl:apply-templates select="@*"/>
			<xsl:apply-templates select="h:h1/node()"/>
		</h1>
		<xsl:apply-templates select="h:*[contains(@class, 'preface')]"/>
		<xsl:apply-templates select="h:div[@class = 'section'][name(..) = 'body']"/>
	</html>
</xsl:template>

<xsl:template match="h:style">
	<s:style><xsl:value-of select="." disable-output-escaping="yes"/></s:style>
</xsl:template>

<xsl:template match="h:script">
	<s:script>
		<xsl:apply-templates select="@*"/>
		<xsl:value-of select="." disable-output-escaping="yes"/>
	</s:script>
</xsl:template>

<xsl:template match="h:div[@class = 'section'][name(..) = 'body']">
	<h2>
		<xsl:if test="@id != ''">
			<xsl:attribute name="id"><xsl:value-of select="@id"/></xsl:attribute>
		</xsl:if>
		<xsl:value-of select="h:h2"/>
	</h2>
	<xsl:apply-templates select="comment() | *[name() != 'h2']"/>
</xsl:template>

<xsl:template match="h:div[@class = 'section'][name(../..) = 'body']">
	<h3>
		<xsl:if test="@id">
			<xsl:attribute name="id"><xsl:value-of select="@id"/></xsl:attribute>
		</xsl:if>
		<xsl:if test="h:h3/h:span[@class = 'datetime']">
			<xsl:attribute name="s:datetime">
				<xsl:value-of select="translate(substring-before(substring-after(h:h3/h:span, '@'), ' - '), '/', '-')"/>
				<xsl:value-of select="concat('T', substring-after(h:h3/h:span, ' - '), ':00+09:00')"/>
			</xsl:attribute>
			<xsl:attribute name="s:genre"/>
		</xsl:if>
		<xsl:choose>
			<xsl:when test="h:h3/h:a">
				<xsl:apply-templates select="h:h3/h:a/node()"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:apply-templates select="h:h3/node()"/>
			</xsl:otherwise>
		</xsl:choose>
	</h3>
	<xsl:apply-templates select="comment() | *[name() != 'h3']"/>
</xsl:template>

<xsl:template match="h:div[@class = 'section'][name(../../..) = 'body']">
	<h4>
		<xsl:if test="@id != ''">
			<xsl:attribute name="id"><xsl:value-of select="@id"/></xsl:attribute>
		</xsl:if>
		<xsl:copy-of select="h:h4/node()"/>
	</h4>
	<xsl:apply-templates select="comment() | *[name() != 'h4']"/>
</xsl:template>

<xsl:template match="h:div[contains(@class, 'picture')] | h:p[contains(@class, 'picture')]">
	<s:picture>
		<xsl:if test="name() = 'p'">
			<xsl:attribute name="mode">p</xsl:attribute>
		</xsl:if>
		<xsl:apply-templates/>
	</s:picture>
</xsl:template>

<xsl:template match="h:ul[@class = 'refLink']">
	<s:reflink>
		<xsl:for-each select="h:li">
			<li><xsl:apply-templates select="node()[name() != 'cite']"/></li>
			<xsl:if test="h:cite">
				<s:from><xsl:apply-templates select="h:cite/node()"/></s:from>
			</xsl:if>
		</xsl:for-each>
	</s:reflink>
</xsl:template>

<xsl:template match="h:dl[@class = 'refLink']">
	<s:reflink>
		<xsl:for-each select="h:dt">
			<li><xsl:apply-templates select="node()[name() != 'cite']"/></li>
			<xsl:if test="h:cite">
				<s:from><xsl:apply-templates select="h:cite/node()"/></s:from>
			</xsl:if>
			<s:data><xsl:apply-templates select="following-sibling::h:dd[1]/node()"/></s:data>
		</xsl:for-each>
	</s:reflink>
</xsl:template>

<xsl:template match="h:p[@class = 'source']">
	<s:from><xsl:apply-templates select="h:cite/node()"/></s:from>
</xsl:template>

<xsl:template match="h:span[@class = 'datetime']">
	<xsl:element name="s:datetime">
		<xsl:apply-templates/>
	</xsl:element>
</xsl:template>

<xsl:template match="h:a[@title]">
	<xsl:variable name="innertext">
		<xsl:value-of select="."/>
	</xsl:variable>
	<xsl:choose>
		<xsl:when test="normalize-space(@title) = normalize-space($innertext) or @title =''">
			<xsl:element name="a">
				<xsl:apply-templates select="@*[name() != 'title'] | node()"/>
			</xsl:element>
		</xsl:when>
		<xsl:otherwise>
			<xsl:element name="a">
				<xsl:apply-templates select="@* | node()"/>
			</xsl:element>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<xsl:template match="h:dfn[contains(@class, 'cfTitle')]">
	<xsl:element name="dfn">
		<xsl:variable name="class1">
			<xsl:value-of select="substring-before(@class, ' cfTitle')"/>
		</xsl:variable>
		<xsl:variable name="class2">
			<xsl:value-of select="substring-after(@class, 'cfTitle ')"/>
		</xsl:variable>
		<xsl:if test="$class1 != '' or $class2 != ''">
			<xsl:attribute name="class">
				<xsl:value-of select="$class1"/>
				<xsl:if test="$class1 and $class2"><xsl:text> </xsl:text></xsl:if>
				<xsl:value-of select="$class2"/>
			</xsl:attribute>
		</xsl:if>
		<xsl:apply-templates select="@*[name() != 'class'] | node()"/>
	</xsl:element>
</xsl:template>

<xsl:template match="h:span[contains(@class, 'tuiki')] | h:p[contains(@class, 'tuiki')]">
	<xsl:element name="s:memo">
		<xsl:if test="name() = 'p'">
			<xsl:attribute name="mode">p</xsl:attribute>
		</xsl:if>
		<xsl:if test="substring-before(@class, 'tuiki')">
			<xsl:attribute name="class"><xsl:value-of select="substring-before(@class, 'tuiki')"/></xsl:attribute>
		</xsl:if>
		<xsl:if test="substring-after(@class, 'tuiki')">
			<xsl:attribute name="class"><xsl:value-of select="substring-after(@class, 'tuiki')"/></xsl:attribute>
		</xsl:if>
		<xsl:apply-templates select="node() | @*[name() != 'class']"/>
	</xsl:element>
</xsl:template>

<xsl:template match="h:pre">
	<xsl:element name="pre">
		<xsl:apply-templates select="@*"/>
		<xsl:choose>
			<xsl:when test="name() = 'pre' and child::node()[1] = *">
				<xsl:text disable-output-escaping="yes">&lt;</xsl:text>
				<xsl:value-of select="name(child::*[1])"/>
				<xsl:text disable-output-escaping="yes">&gt;</xsl:text>
					<xsl:apply-templates select="child::*[1]/child::node()"/>
				<xsl:text disable-output-escaping="yes">&lt;/</xsl:text>
				<xsl:value-of select="name(child::*[1])"/>
				<xsl:text disable-output-escaping="yes">&gt;</xsl:text>
				<xsl:apply-templates select="child::node()[position() &gt; 1]"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:apply-templates/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:element>
</xsl:template>

<xsl:template match="h:hr"/>

<!-- ======= built-in template を、要素も出力するように変更 ====== -->

<xsl:template match="@*|node()" priority="-5.0">
	<xsl:copy>
		<xsl:apply-templates select="@*|node()"/>
	</xsl:copy>
</xsl:template>

<!-- ============= コメントをそのまんま通す =============  -->

<xsl:template match="comment()">
	<xsl:copy-of select="."/>
</xsl:template>

</xsl:stylesheet>
