<?xml version="1.0" encoding="UTF-8"?>
<!-- 
	Stylesheet to convert a RecipeML recipe into the iEat XML format.
-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" 
	xmlns:eat="http://msqr.us/xsd/ieat"
	xmlns:rml="http://www.formatdata.com/recipeml/recipeml.dtd"
	exclude-result-prefixes="rml">
	
	<xsl:output method="xml" omit-xml-declaration="no" indent="yes" />
	
	<xsl:template match="recipeml[@version='0.5']">
		<eat:ui>
			<xsl:apply-templates select="recipe"/>
		</eat:ui>
	</xsl:template>
	
	<xsl:template match="recipe">
		<eat:recipe>
			<xsl:attribute name="servingSize">
				<xsl:choose>
					<xsl:when test="string-length(head/yield) &gt; 0">
						<xsl:value-of select="head/yield"/>
					</xsl:when>
					<xsl:otherwise>
						<xsl:text>1</xsl:text>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:attribute>
			
			<!-- recipe name -->
			<eat:name>
				<xsl:value-of select="head/title"/>
			</eat:name>
			
			<eat:excerpt>
				<xsl:choose>
					<xsl:when test="string-length(description) &gt; 0">
						<xsl:value-of select="description"/>
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="head/title"/>
					</xsl:otherwise>
				</xsl:choose>
			</eat:excerpt>
			
			<eat:source>
				<xsl:value-of select="head/source"/>
			</eat:source>
			
			<eat:directions/>
			
			<eat:base>
				<xsl:if test="not(string(number(substring-after(head/categories/cat[@type='base']/@id,'-'))) = 'NaN')">
					<xsl:attribute name="baseId">
						<xsl:value-of select="substring-after(head/categories/cat[@type='base']/@id,'-')"/>
					</xsl:attribute>
				</xsl:if>
				<xsl:value-of select="head/categories/cat[@type='base']"/>
			</eat:base>
			
			<eat:course>
				<xsl:if test="not(string(number(substring-after(head/categories/cat[@type='course']/@id,'-'))) = 'NaN')">
					<xsl:attribute name="courseId">
						<xsl:value-of select="substring-after(head/categories/cat[@type='course']/@id,'-')"/>
					</xsl:attribute>
				</xsl:if>
				<xsl:value-of select="head/categories/cat[@type='course']"/>
			</eat:course>
			
			<eat:ethnicity>
				<xsl:if test="not(string(number(substring-after(head/categories/cat[@type='ethnicity']/@id,'-'))) = 'NaN')">
					<xsl:attribute name="ethnicityId">
						<xsl:value-of select="substring-after(head/categories/cat[@type='ethnicity']/@id,'-')"/>
					</xsl:attribute>
				</xsl:if>
				<xsl:value-of select="head/categories/cat[@type='ethnicity']"/>
			</eat:ethnicity>
			
			<eat:difficulty>
				<xsl:if test="not(string(number(substring-after(head/categories/cat[@type='difficulty']/@id,'-'))) = 'NaN')">
					<xsl:attribute name="difficultyId">
						<xsl:value-of select="substring-after(head/categories/cat[@type='difficulty']/@id,'-')"/>
					</xsl:attribute>
				</xsl:if>
				<xsl:value-of select="head/categories/cat[@type='difficulty']"/>
			</eat:difficulty>
			
			<eat:system>
				<xsl:choose>
					<xsl:when test="@system">
						<xsl:value-of select="@system"/>
					</xsl:when>
					<xsl:otherwise>
						<xsl:text>Unknown.</xsl:text>
					</xsl:otherwise>
				</xsl:choose>
			</eat:system>
			
			<!-- ingredients -->
			<xsl:apply-templates select="ingredients//ing"/>
			
			<!-- steps -->
			<xsl:apply-templates select="directions/step"/>
			
		</eat:recipe>
	</xsl:template>
	
	<xsl:template match="ing">
		<xsl:variable name="quantity" select="amt/qty"/>
		<eat:ingredient>
			<xsl:attribute name="quantity">
				<xsl:choose>
					<xsl:when test="contains($quantity,'/')">
						<!-- treat as franction, and try to do division -->
						<xsl:choose>
							<xsl:when test="contains($quantity,' ')">
								<!-- treat as X Y/Z -->
								<xsl:variable name="whole" select="number(substring-before($quantity,' '))"/>
								<xsl:variable name="num" select="number(substring-before(
									substring-after($quantity,' '), '/'))"/>
								<xsl:variable name="denom" select="number(substring-after($quantity,'/'))"/>
								<xsl:value-of select="$whole + ($num div $denom)"/>
							</xsl:when>
							<xsl:otherwise>
								<!-- treat as Y/Z -->
								<xsl:variable name="num" select="number(substring-before($quantity,'/'))"/>
								<xsl:variable name="denom" select="number(substring-after($quantity,'/'))"/>
								<xsl:value-of select="$num div $denom"/>
							</xsl:otherwise>
						</xsl:choose>
					</xsl:when>
					<xsl:when test="string(number($quantity)) != 'NaN'">
						<xsl:value-of select="$quantity"/>
					</xsl:when>
					<xsl:otherwise>
						<!-- If can't convert, default to 1 -->
						<xsl:text>1</xsl:text>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:attribute>
			<xsl:if test="modifier">
				<xsl:attribute name="qualifier">
					<xsl:value-of select="modifier"/>
				</xsl:attribute>
			</xsl:if>
			<eat:unit unitId="-1" systemId="-1">
				<xsl:attribute name="abbreviation">
					<xsl:choose>
						<xsl:when test="string(amt/unit)">
							<xsl:value-of select="amt/unit"/>
						</xsl:when>
						<xsl:otherwise>
							<xsl:text>?</xsl:text>
						</xsl:otherwise>
					</xsl:choose>			
				</xsl:attribute>
				<xsl:value-of select="amt/unit"/>
			</eat:unit>
			<eat:ingredient>
				<eat:name>
					<xsl:value-of select="item"/>
				</eat:name>
			</eat:ingredient>
		</eat:ingredient>
	</xsl:template>
	
	<xsl:template match="step">
		<eat:step>
			<xsl:value-of select="."/>
		</eat:step>
	</xsl:template>
	
</xsl:stylesheet>