<?xml version="1.0" encoding="UTF-8"?>

<!--
　にっき最新 15 項目の RSS を「とりあえず」生成。
　これで生成した rss_base.rdf を元に、description の内容を要約化した公開用の rss.rdf を
　AppleScript (XSLT Tools OSAX + nikki_rss_summarize.scpt + nikki_rss_summarize.xsl) で生成。
-->

<xsl:stylesheet version="1.0" 
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns="http://purl.org/rss/1.0/"
	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: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 fl xalan">

<xsl:import href="common.xsl"/>

<!-- ================ 出力設定 ================  -->

<xsl:output method="xml"
	encoding="UTF-8"
	omit-xml-declaration="no"
	indent="yes"
	xalan:indent-amount="2"
	media-type="application/xml"/>

<!-- ================ 各種設定 ================  -->

<!-- このRSSのファイルパスとURI -->
<xsl:param name="curFilePath">/nikki/rss.rdf</xsl:param>
<xsl:param name="rssURI" select="concat($baseURI, $curFilePath)"/>

<!-- 最新のにっきページURI -->
<xsl:param name="nikkiLatestURI" select="concat($baseURI, '/', $curDirName, '/latest.html')"/>

<!-- バナーのURI -->
<xsl:param name="bannerURI" select="concat($baseURI, '/common/img/banner/bouhoumen_banner.png')"/>

<!-- 処理をスタートするファイル (= 最新のにっきページ) のパス -->
<xsl:param name="newestPagePath">
	<xsl:for-each select="document($fileList)/fl:dir/fl:dir[@name = $curDirName]/fl:dir[last()]/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>

<!-- アイテムの最大個数 (RSS0.9 との互換性を考慮する場合の推奨：15個以内) -->
<xsl:param name="items-max" select="15"/>

<!-- ================ メインテンプレ ================  -->

<xsl:template match="/">
	<xsl:processing-instruction name="xml-stylesheet">
		<xsl:text>href=&quot;../common/xsl/rss2html.xsl&quot; type=&quot;text/xsl&quot;</xsl:text>
	</xsl:processing-instruction>

	<rdf:RDF xml:lang="ja">
		<channel rdf:about="{$rssURI}">

			<!-- RSS0.9 との互換性を考慮する場合の推奨：40バイト以内 -->
			<title>ねこめしにっきヘッドライン</title>

			<link><xsl:value-of select="$nikkiLatestURI"/></link>

			<!-- RSS0.9 との互換性を考慮する場合の推奨：500バイト以内 -->
			<description>不定期にっき。 Web 技術系、 Mac 系、アニヲタ同人系などを主なネタとしている「ねこめしにっき」の、最新 15 記事分のヘッドラインと要約。要約文は AppleScript によって自動生成されているので時々妙な文章になるけども、気にしたら負けですよ？</description>

			<image rdf:resource="{$bannerURI}" />

			<dc:date><xsl:value-of select="js:getTime()"/></dc:date>
			<dc:creator>ありみかさとみ</dc:creator>

			<items>
				<rdf:Seq>
					<xsl:call-template name="makeItems">
						<xsl:with-param name="filePath" select="$newestPagePath"/>
						<xsl:with-param name="mode">items</xsl:with-param>
					</xsl:call-template>
				</rdf:Seq>
			</items>

		</channel>
		
		<image rdf:about="{$bannerURI}">
			<title>ねこめしにっき</title>
			<link><xsl:value-of select="$nikkiLatestURI"/></link>
			<url><xsl:value-of select="$bannerURI"/></url>
		</image>
		
		<xsl:call-template name="makeItems">
			<xsl:with-param name="filePath" select="$newestPagePath"/>
			<xsl:with-param name="mode">item</xsl:with-param>
		</xsl:call-template>
	</rdf:RDF>

</xsl:template>

<!-- ================ items 要素, item 要素生成テンプレ ================  -->

<xsl:template name="makeItems">
	<xsl:param name="filePath"/>
	<xsl:param name="mode"/>
	<xsl:param name="count" select="0"/>

	<xsl:for-each select="document(concat($baseDir, $filePath))/h:html/h:h3[position() &lt;= $items-max - $count]">
		<xsl:choose>
			<xsl:when test="$mode = 'items'">
				<xsl:apply-templates mode="items" select="self::node()">
					<xsl:with-param name="URI" select="js:chsuffix(concat($baseURI, $filePath, '#', @id), 'html')"/>
				</xsl:apply-templates>
			</xsl:when>
			<xsl:when test="$mode = 'item'">
				<xsl:apply-templates mode="item" select="self::node()">
					<xsl:with-param name="URI" select="js:chsuffix(concat($baseURI, $filePath, '#', @id), 'html')"/>
				</xsl:apply-templates>
			</xsl:when>
			<xsl:otherwise/>
		</xsl:choose>
	</xsl:for-each>
	
	<xsl:if test="$count &lt; $items-max">
		<xsl:call-template name="makeItems">
			<xsl:with-param name="filePath">
				<xsl:call-template name="searchFile">
					<xsl:with-param name="path" select="$filePath"/>
					<xsl:with-param name="rel">prev</xsl:with-param>
					<xsl:with-param name="data">path</xsl:with-param>
				</xsl:call-template>
			</xsl:with-param>
			<xsl:with-param name="count" select="$count + count(document(concat($baseDir, $filePath))/h:html/h:h3)"/>
			<xsl:with-param name="mode" select="$mode"/>
		</xsl:call-template>
	</xsl:if>
</xsl:template>

<xsl:template match="h:h3" mode="items">
	<xsl:param name="URI"/>
	<xsl:if test="string(self::node())">
		<rdf:li rdf:resource="{$URI}"/>
	</xsl:if>
</xsl:template>

<xsl:template match="h:h3" mode="item">
	<xsl:param name="URI"/>
	<xsl:param name="WB" select="string('http://alimika.alib.jp/nikki/writeback')"/>

	<!-- description の内容になる部分 -->
	<xsl:param name="innerText">
		<xsl:call-template name="description-content"/>
	</xsl:param>
	
	<xsl:if test="string(self::node())">
		<item rdf:about="{$URI}">
			<title>
				<xsl:value-of select="self::node()"/>
			</title>
			<link><xsl:value-of select="$URI"/></link>
			<description>
				<xsl:value-of select="normalize-space($innerText)"/>
			</description>
			<dc:date>
				<xsl:value-of select="@s:datetime"/>
			</dc:date>
			 <trackback:ping rdf:resource="{$WB}/tb/{js:replace($URI, '/.+\/(\d+)\/(\w+)\.html#d(\d+)n(\d+)$/', '$1_$2_$3_$4')}"/>
		</item>
	</xsl:if>
</xsl:template>

<xsl:template name="description-content">
	<xsl:for-each select="following-sibling::*[1]">
		<xsl:if test="local-name() != 'h3'">
			<xsl:apply-templates select="self::node()"/>
			<xsl:call-template name="description-content"/>
		</xsl:if>
	</xsl:for-each>
</xsl:template>

<!-- ============== RSS の description に出力する要素 ==============  -->

<xsl:template match="h:* | s:memo | s:reflink | s:datetime">
	<xsl:apply-templates/>
</xsl:template>

<xsl:template match="h:ul | h:ol | h:dl | s:reflink">
	<xsl:apply-templates/>
	<xsl:text>　</xsl:text>
</xsl:template>

<xsl:template match="h:li | h:dt">
	<xsl:text>「</xsl:text>
	<xsl:apply-templates/>
	<xsl:text>」</xsl:text>
</xsl:template>

<xsl:template match="h:dd | s:data">
	<xsl:text>…</xsl:text>
	<xsl:apply-templates/>
</xsl:template>

<xsl:template match="h:img">
	<xsl:value-of select="@alt"/>
</xsl:template>

<xsl:template match="h:q">
	<xsl:param name="kakkoSrt">
		<xsl:choose>
			<xsl:when test="starts-with(@xml:lang, 'ja') or not(string(@xml:lang))">「</xsl:when>
			<xsl:otherwise>&quot;</xsl:otherwise>
		</xsl:choose>
	</xsl:param>

	<xsl:param name="kakkoEnd">
		<xsl:choose>
			<xsl:when test="starts-with(@xml:lang, 'ja') or not(string(@xml:lang))">」</xsl:when>
			<xsl:otherwise>&quot;</xsl:otherwise>
		</xsl:choose>
	</xsl:param>

	<xsl:value-of select="$kakkoSrt"/>
	<xsl:apply-templates/>
	<xsl:value-of select="$kakkoEnd"/>
</xsl:template>

<xsl:template match="s:a">
	<xsl:param name="curFilePath" select="ancestor::h:html/h:head/s:file/@path"/>
	<xsl:param name="targetPath">
		<xsl:choose>
			<xsl:when test="starts-with(@href, '#')">
				<xsl:value-of select="concat($curFilePath, @href)"/>
			</xsl:when>
			<xsl:when test="starts-with(@href, '/')">
				<xsl:value-of select="@href"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="js:r2a(string($curFilePath), string(@href))"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:param>

	<xsl:choose>
		<xsl:when test="string(self::node())">
			<xsl:value-of select="self::node()"/>
		</xsl:when>
		<xsl:otherwise>
			<xsl:call-template name="makeAnchorText">
				<xsl:with-param name="path" select="$targetPath"/>
			</xsl:call-template>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<!-- ============== RSS の description に出力しない要素 ==============  -->

<xsl:template match="h:del | h:blockquote | h:h4 | h:h5 | h:h6 | h:pre"/>
<xsl:template match="s:*" priority="-5.0"/>

</xsl:stylesheet>

