<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
	xmlns="http://www.w3.org/1999/xhtml">
	<xsl:output method="html" encoding="UTF-8" doctype-public="-//W3C//DTD HTML 4.01//EN"
		doctype-system="http://www.w3.org/TR/html4/strict.dtd" omit-xml-declaration="yes"/>
	<xsl:key name="bibl" match="bibl" use="@id"/>

	<xsl:template match="/">
		<html>
			<head>
				<title>
					<xsl:value-of select="TEI.2/teiHeader/fileDesc/titleStmt/title"/>
				</title>
				<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
				<script type="javascript" xml:space="preserve">
					<![CDATA[
					
					]]>
				</script>
				<style type="text/css" xml:space="preserve">
                    body {
                    font-family: Georgia, Times, "Times New Roman", serif;
                    margin-top: 1em;
                    margin-bottom: 1em;
                    margin-left: 1em;
                    margin-right: 1em;
                    }
                    h1 {
                    font-family: Verdana, Arial, Helvetica, sans-serif;
                    font-size:16pt;
                    text-align: center;
                    }
                    h2 {
                    font-family: Verdana, Arial, Helvetica, sans-serif;
                    font-size:14pt;
                    }
                    ul {
                    margin-left: 1em;
                    }
                    li {
                    margin-bottom: 1em;
                    }
                </style>
			</head>
			<body>
				<xsl:apply-templates select="TEI.2/text/body"/>
			</body>
		</html>
	</xsl:template>

	<xsl:template match="body">
		<xsl:apply-templates/>
	</xsl:template>

	<xsl:template match="div">
		<div>
			<xsl:if test="@id"><xsl:attribute name="id"><xsl:value-of select="@id"/></xsl:attribute></xsl:if>
			<xsl:apply-templates/>
		</div>
	</xsl:template>

	<xsl:template match="head">
		<xsl:variable name="level">
			<xsl:choose>
				<xsl:when
					test="count(ancestor::*[local-name(.) = 'div' or local-name(.) = 'list']) &gt; 7">7</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="count(ancestor::*[local-name(.) = 'div' or local-name(.) = 'list'])"
					/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:element name="h{$level}">
			<xsl:apply-templates/>
		</xsl:element>
	</xsl:template>

	<!-- HTML Does not allow lists inside paragraphs -->
	<xsl:template match="list[not(ancestor::p)]">
		<xsl:choose>
			<xsl:when test="@type = 'ordered'">
				<ol>
					<xsl:apply-templates/>
				</ol>
			</xsl:when>
			<xsl:otherwise>
				<ul>
					<xsl:apply-templates/>
				</ul>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template match="item[not(ancestor::p)]">
		<li>
			<xsl:apply-templates/>
		</li>
	</xsl:template>
	
	<xsl:template match="list[ancestor::p]">
		<div style="margin-left:1em; margin-top:1em; margin-bottom:1em">
			<xsl:apply-templates/>
		</div>
	</xsl:template>
	
	<xsl:template match="item[ancestor::p]">
		<xsl:choose>
			<xsl:when test="../@type='ordered'"><xsl:value-of select="count(preceding-sibling::item) + 1"/>. </xsl:when>
			<xsl:otherwise>- </xsl:otherwise>
		</xsl:choose>
		<xsl:apply-templates/><br/>
	</xsl:template>

	<xsl:template match="p">
		<p>
			<xsl:apply-templates/>
		</p>
	</xsl:template>

	<xsl:template match="lb">
		<br/>
	</xsl:template>

	<xsl:template match="xref">
		<a href="{@uri}">
			<xsl:apply-templates/>
		</a>
	</xsl:template>

	<xsl:template match="xptr">
		<a href="{@uri}">
			<xsl:value-of select="@uri"/>
		</a>
	</xsl:template>

	<xsl:template match="bibl[@copyOf]">
		<xsl:apply-templates select="key('bibl', @copyOf)"/>
	</xsl:template>

	<xsl:template match="bibl/author">
		<xsl:apply-templates/>, </xsl:template>
	
	<xsl:template match="bibl/editor">
		<xsl:apply-templates/>, </xsl:template>

	<xsl:template match="bibl/title[@level='a']"> "<xsl:apply-templates/>" </xsl:template>

	<xsl:template match="bibl/title[@level='m' or @level='j']">
		<i><b><xsl:apply-templates/></b></i>
	</xsl:template>

	<xsl:template match="bibl/title[not(@level)]">
		<i><b><xsl:apply-templates/></b></i>			
	</xsl:template>

	<xsl:template match="biblScope[@type='vol']"> vol. <xsl:apply-templates/>
	</xsl:template>

	<xsl:template match="biblScope[@type='num']"> no. <xsl:apply-templates/>
	</xsl:template>

	<xsl:template match="biblScope[@type='pages']"> pp. <xsl:apply-templates/>
	</xsl:template>

	<xsl:template match="biblScope[@type='chapter']"> ch. <xsl:apply-templates/>
	</xsl:template>
	
	<xsl:template match="title">
		<span style="border:thin solid gray; padding:2px"><xsl:apply-templates/></span>
	</xsl:template>

	<xsl:template match="note">
		<i><xsl:apply-templates/></i>
	</xsl:template>

</xsl:stylesheet>
