<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:dret="namespace:madness" exclude-result-prefixes="dret xs">
	<xsl:variable name="stdin" select="."/>
	<xsl:template match="/">
		<xsl:result-document href="dretblog/index.html">
			<html>
				<head><xsl:sequence select="dret:head('dretblog', '')"/></head>
				<body>
					<h1>dretblog</h1>
					<xsl:for-each select="//post">
						<p><a href="{translate(@date, '-', '/')}/{dret:postname(title)}"><xsl:value-of select="concat(format-date(@date, '[F] [MNn] [D], [Y]'), ': ', title)"/></a></p>
					</xsl:for-each>
				</body>
			</html>
		</xsl:result-document>
		<xsl:for-each select="//post">
			<xsl:result-document href="dretblog/{translate(@date, '-', '/')}/{dret:postname(title)}.html">
				<html>
					<head>
						<xsl:sequence select="dret:head(title, '../../../')"/>
						<xsl:variable name="prev" select="preceding-sibling::post[1]"/>
						<xsl:variable name="next" select="following-sibling::post[1]"/>
						<xsl:if test="exists($prev)"><link rel="prev" href="../../../{translate($prev/@date, '-', '/')}/{dret:postname($prev/title)}" title="{$prev/title}"/></xsl:if>
						<xsl:if test="exists($next)"><link rel="next" href="../../../{translate($next/@date, '-', '/')}/{dret:postname($next/title)}" title="{$next/title}"/></xsl:if>
					</head>
					<body>
						<h1><xsl:value-of select="title"/></h1>
						<h2>
							<a href="./"><xsl:value-of select="format-date(@date, '[F]')"/></a>
							<xsl:text> </xsl:text>
							<a href=".."><xsl:value-of select="format-date(@date, '[MNn]')"/></a>
							<xsl:text> </xsl:text>
							<a href="./"><xsl:value-of select="format-date(@date, '[D]')"/></a>
							<xsl:text>, </xsl:text>
							<a href="../.."><xsl:value-of select="format-date(@date, '[Y]')"/></a>
						</h2>
						<a href="../../../../../img/{image/@src}.jpg" title="{image}"><img src="../../../../../img/{image/@src}-small.jpg"/></a>
						<p><xsl:value-of select="text"/></p>
					</body>
				</html>
			</xsl:result-document>
		</xsl:for-each>
		<xsl:for-each select="distinct-values( for $i in //post/@date return substring($i, 1, 4) )">
			<xsl:result-document href="dretblog/{current()}/index.html">
				<html>
					<head><xsl:sequence select="dret:head(format-date(xs:date(concat(current(), '-01-01')), '[Y]'), '../')"/></head>
					<body>
						<h1><a href="..">dretblog</a></h1>
						<h2><xsl:value-of select="format-date(xs:date(concat(current(), '-01-01')), '[Y]')"/></h2>
						<xsl:for-each select="$stdin//post[starts-with(@date, current())]">
							<p><a href="../{translate(@date, '-', '/')}/{dret:postname(title)}"><xsl:value-of select="concat(format-date(@date, '[F] [MNn] [D], [Y]'), ': ', title)"/></a></p>
						</xsl:for-each>
					</body>
				</html>
			</xsl:result-document>
		</xsl:for-each>
		<xsl:for-each select="distinct-values( for $i in //post/@date return substring($i, 1, 7) )">
			<xsl:result-document href="dretblog/{translate(current(), '-', '/')}/index.html">
				<html>
					<head><xsl:sequence select="dret:head(format-date(xs:date(concat(current(), '-01')), '[MNn] [Y]'), '../../')"/></head>
					<body>
						<h1><a href="../..">dretblog</a></h1>
						<h2>
							<xsl:value-of select="format-date(xs:date(concat(current(), '-01')), '[MNn] ')"/>
							<a href="../.."><xsl:value-of select="format-date(xs:date(concat(current(), '-01')), '[Y]')"/></a>
						</h2>
						<xsl:for-each select="$stdin//post[starts-with(@date, current())]">
							<p><a href="../../{translate(@date, '-', '/')}/{dret:postname(title)}"><xsl:value-of select="concat(format-date(@date, '[F] [MNn] [D], [Y]'), ': ', title)"/></a></p>
						</xsl:for-each>
					</body>
				</html>
			</xsl:result-document>
		</xsl:for-each>
		<xsl:for-each select="distinct-values(//post/@date)">
			<xsl:result-document href="dretblog/{translate(current(), '-', '/')}/index.html">
				<html>
					<head><xsl:sequence select="dret:head(format-date(xs:date(current()), '[F] [MNn] [D], [Y]'), '../../../')"/></head>
					<body>
						<h1><a href="../../..">dretblog</a></h1>
						<h2>
							<xsl:value-of select="format-date(xs:date(current()), '[F] ')"/>
							<a href=".."><xsl:value-of select="format-date(xs:date(current()), '[MNn]')"/></a>
							<xsl:value-of select="format-date(xs:date(current()), ' [D], ')"/>
							<a href="../.."><xsl:value-of select="format-date(xs:date(current()), '[Y]')"/></a>
						</h2>
						<xsl:for-each select="$stdin//post[starts-with(@date, current())]">
							<p><a href="../../../{translate(@date, '-', '/')}/{dret:postname(title)}"><xsl:value-of select="concat(format-date(@date, '[F] [MNn] [D], [Y]'), ': ', title)"/></a></p>
						</xsl:for-each>
					</body>
				</html>
			</xsl:result-document>
		</xsl:for-each>
	</xsl:template>
	<xsl:function name="dret:head">
		<xsl:param name="title"/>
		<xsl:param name="prefix"/>
		<title><xsl:value-of select="$title"/></title>
		<link rel="home" href="{$prefix}" title="dretblog"/>
		<link rel="stylesheet" type="text/css" href="{$prefix}../dretblog.css"/>
	</xsl:function>
	<xsl:function name="dret:postname">
		<xsl:param name="title"/>
		<xsl:value-of select="lower-case(translate($title, ' /-+,;?&amp;', '_'))"/>
	</xsl:function>
</xsl:stylesheet>
