﻿<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:maiden="http://xml-maiden.com" version="1.0">
<xsl:output method="text" encoding="utf-8"/>
<xsl:strip-space elements="maiden:row maiden:vector maiden:case maiden:fenced maiden:apply  maiden:opgroup"/>
<xsl:include href="xhtml.xsl"/>

<xsl:template match="maiden:formula">
<xsl:text>$$</xsl:text><xsl:apply-templates/><xsl:text>$$</xsl:text>
</xsl:template>

<xsl:template match="maiden:math">
<xsl:text>$</xsl:text><xsl:apply-templates/><xsl:text>$</xsl:text>
</xsl:template>

<xsl:template match="maiden:sup | maiden:marker">
<xsl:choose>
<xsl:when test="not(maiden:*) and string-length(.)=1 and translate(.,'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789','')=''">
<xsl:text>^</xsl:text><xsl:apply-templates/>
</xsl:when>
<xsl:otherwise>
<xsl:text>^{</xsl:text><xsl:apply-templates/><xsl:text>}</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template match="maiden:sub | maiden:submarker">
<xsl:choose>
<xsl:when test="not(maiden:*) and string-length(.)=1 and translate(.,'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789','')=''">
<xsl:text>_</xsl:text><xsl:apply-templates/>
</xsl:when>
<xsl:otherwise>
<xsl:text>_{</xsl:text><xsl:apply-templates/><xsl:text>}</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template match="maiden:sur|maiden:inf|maiden:underline|maiden:overline|maiden:strike|maiden:matrix|maiden:det|maiden:cases">
<xsl:value-of select="concat('\',name(),'{')"/><xsl:apply-templates/><xsl:text>}</xsl:text>
</xsl:template>

<xsl:template match="maiden:float">
<xsl:choose>
<xsl:when test="maiden:sur">
<xsl:text>\pre{</xsl:text><xsl:apply-templates select="maiden:sur/node()"/><xsl:text>}{</xsl:text><xsl:apply-templates select="maiden:inf/node()"/><xsl:text>}</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template match="maiden:fraction">
<xsl:text>\frac{</xsl:text><xsl:apply-templates select="maiden:num"/><xsl:text>}{</xsl:text><xsl:apply-templates select="maiden:den"/><xsl:text>}</xsl:text>
</xsl:template>

<xsl:template match="maiden:over">
<xsl:choose>
<xsl:when test="child::maiden:overbrace and child::maiden:sat">
<xsl:text>\overbrace{</xsl:text><xsl:apply-templates select="maiden:ker|maiden:ope"/><xsl:text>}^{</xsl:text><xsl:apply-templates select="maiden:sat"/><xsl:text>}</xsl:text>
</xsl:when>
<xsl:when test="child::maiden:overbrace and not(child::maiden:sat)">
<xsl:text>\overbrace{</xsl:text><xsl:apply-templates select="maiden:ker|maiden:ope"/><xsl:text>}</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>\stackrel{</xsl:text><xsl:apply-templates select="maiden:sat"/><xsl:text>}{</xsl:text><xsl:apply-templates select="maiden:ker|maiden:ope"/><xsl:text>}</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template match="maiden:under">
<xsl:choose>
<xsl:when test="child::maiden:underbrace and child::maiden:sat">
<xsl:text>\underbrace{</xsl:text><xsl:apply-templates select="maiden:ker|maiden:ope"/><xsl:text>}_{</xsl:text><xsl:apply-templates select="maiden:sat"/><xsl:text>}</xsl:text>
</xsl:when>
<xsl:when test="child::maiden:underbrace and not(child::maiden:sat)">
<xsl:text>\underbrace{</xsl:text><xsl:apply-templates select="maiden:ker|maiden:ope|maiden:under"/><xsl:text>}</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>\stackrev{</xsl:text><xsl:apply-templates select="maiden:ker|maiden:ope|maiden:over"/><xsl:text>}{</xsl:text><xsl:apply-templates select="maiden:sat"/><xsl:text>}</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template match="maiden:radical">
<xsl:choose>
<xsl:when test="child::maiden:radix/* or child::maiden:radix/text()[string-length(.)>0]">
<xsl:text>\rad{</xsl:text><xsl:apply-templates select="maiden:radix"/><xsl:text>}{</xsl:text><xsl:apply-templates select="maiden:radicand"/><xsl:text>}</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>\sqrt{</xsl:text><xsl:apply-templates select="maiden:radicand"/><xsl:text>}</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template match="maiden:ope">
<xsl:choose>
<xsl:when test="text()[translate(.,'∏∐∑∫∬∭∮∯∰∱∲∳⋀⋁⋂⋃⨀⨁⨂⨃⨄⨅⨆⨊⨋','∑∑∑∑∑∑∑∑∑∑∑∑∑∑∑∑∑∑∑∑∑∑∑∑∑')='∑' ]">
<xsl:apply-templates/>
</xsl:when>
<xsl:otherwise>
<xsl:text>\ope{</xsl:text><xsl:apply-templates/><xsl:text>}</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template match="maiden:wrap|maiden:vector|maiden:row|maiden:case">
<xsl:value-of select="concat('\',name())"/><xsl:apply-templates/>
</xsl:template>

<xsl:template match="maiden:cell|maiden:value|maiden:scope|maiden:entry">
<xsl:text>{</xsl:text><xsl:apply-templates/><xsl:text>}</xsl:text>
</xsl:template>

<xsl:template match="maiden:formula/maiden:line">
<xsl:text>\\</xsl:text>
</xsl:template>

<xsl:template match="maiden:subformula">
<xsl:apply-templates/><xsl:text>\\</xsl:text>
</xsl:template>

<xsl:template match="maiden:group|maiden:formulae|maiden:apply|maiden:opgroup|maiden:fenced|maiden:markers|maiden:ker|maiden:sat|maiden:radicand|maiden:radix">
<xsl:apply-templates/>
</xsl:template>

<xsl:template match="maiden:bold">
<xsl:text>\mathbf{</xsl:text><xsl:apply-templates/><xsl:text>}</xsl:text>
</xsl:template>

<xsl:template match="maiden:italic">
<xsl:text>\mathsl{</xsl:text><xsl:apply-templates/><xsl:text>}</xsl:text>
</xsl:template>

<xsl:template match="maiden:fence">
<xsl:if test="@left='none'">
<xsl:text>\left.</xsl:text>
</xsl:if>
<xsl:if test="not(@left) or @left='square'">
<xsl:text>\left[</xsl:text>
</xsl:if>
<xsl:if test="@left='round'">
<xsl:text>\left(</xsl:text>
</xsl:if>
<xsl:if test="@left='curly'">
<xsl:text>\left{</xsl:text>
</xsl:if>
<xsl:if test="@left='solid' or @left='dashed'">
<xsl:text>\left|</xsl:text>
</xsl:if>
<xsl:apply-templates/>
<xsl:if test="not(@right) or @right='square'">
<xsl:text>\right]</xsl:text>
</xsl:if>
<xsl:if test="@right='round'">
<xsl:text>\right)</xsl:text>
</xsl:if>
<xsl:if test="@right='curly'">
<xsl:text>\right}</xsl:text>
</xsl:if>
<xsl:if test="@right='solid' or @right='dashed'">
<xsl:text>\right|</xsl:text>
</xsl:if>
</xsl:template>


<!--


<xsl:template match="maiden:core[text() = '&#x220F;' or text() = '&#x2210;' or text() = '&#x2211;' or text() = '&#x222B;' or text() = '&#x222C;' or text() = '&#x222D;' or text() = ' &#x222E;' or text() = '&#x222F;' or text() = '&#x2230;' or text() = '&#x2227;' or text() = '&#x2228;' or text() = '&#x2229;' or text() = '&#x222A;' or text() = '&#x228E;' or text() = '&#x2295;' or text() = '&#x2297;' or text() = '&#x2299;' or text() = '&#x2294;' or text() = '&#x22C0;' or text() = '&#x22C1;' or text() = '&#x22C2;' or text() = '&#x22C3;' or text() = '&#x2A00;' or text() = '&#x2A01;' or text() = '&#x2A02;' or text() = '&#x2A04;' or text() = '&#x2A06;']">
<xsl:choose>
<xsl:when test="text() = '&#x220F;'">
<xsl:text>\prod </xsl:text>
</xsl:when>
<xsl:when test="text() = '&#x2210;'">
<xsl:text>\coprod </xsl:text>
</xsl:when>
<xsl:when test="text() = '&#x2211;'">
<xsl:text>\sum </xsl:text>
</xsl:when>
<xsl:when test="text() = '&#x222B;'">
<xsl:text>\int </xsl:text>
</xsl:when>
<xsl:when test="text() = '&#x222C;'">
<xsl:text>\int \int </xsl:text>
</xsl:when>
<xsl:when test="text() = '&#x222D;'">
<xsl:text>\int \int \int </xsl:text>
</xsl:when>
<xsl:when test="text() = '&#x222E;'">
<xsl:text>\oint </xsl:text>
</xsl:when>
<xsl:when test="text() = '&#x222F;'">
<xsl:text>\oint \oint </xsl:text>
</xsl:when>
<xsl:when test="text() = '&#x2230;'">
<xsl:text>\oint \oint \oint </xsl:text>
</xsl:when>
<xsl:when test="text() = '&#x2227;' or text() = '&#x22C0;'">
<xsl:text>\bigwedge </xsl:text>
</xsl:when>
<xsl:when test="text() = '&#x2228;' or text() = '&#x22C1;'">
<xsl:text>\bigvee </xsl:text>
</xsl:when>
<xsl:when test="text() = '&#x228E;' or text() = '&#x2A04;'">
<xsl:text>\biguplus </xsl:text>
</xsl:when>
<xsl:when test="text() = '&#x2294;' or text() = '&#x2A06;'">
<xsl:text>\bigsqcup </xsl:text>
</xsl:when>
<xsl:when test="text() = '&#x2229;' or text() = '&#x22C2;'">
<xsl:text>\bigcap </xsl:text>
</xsl:when>
<xsl:when test="text() = '&#x222A;' or text() = '&#x22C3;'">
<xsl:text>\bigcup </xsl:text>
</xsl:when>
<xsl:when test="text() = '&#x2295;' or text() = '&#x2A01;'">
<xsl:text>\bigoplus </xsl:text>
</xsl:when>
<xsl:when test="text() = '&#x2297;' or text() = '&#x2A02;'">
<xsl:text>\bigotimes </xsl:text>
</xsl:when>
<xsl:when test="text() = '&#x2299;' or text() = '&#x2A00;'">
<xsl:text>\bigodot </xsl:text>
</xsl:when>
</xsl:choose>
</xsl:template>

<xsl:template match="text()">
<xsl:choose>
<xsl:when test="ancestor-or-self::maiden:math or ancestor-or-self::maiden:formula">
<xsl:call-template name="math">
<xsl:with-param name="txt" select="translate(.,'&#x200B;&#x200C;&#x200D;&#x2061;&#x2062;&#x2063;','')"/>
<xsl:with-param name="utf" select="substring(translate(.,'&#x200B;&#x200C;&#x200D;&#x2061;&#x2062;&#x2063;',''),1,1)"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="text">
<xsl:with-param name="txt" select="translate(.,'&#x200B;&#x200C;&#x200D;&#x2060;','')"/>
<xsl:with-param name="utf" select="substring(translate(.,'&#x200B;&#x200C;&#x200D;&#x2060;',''),1,1)"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
-->

<xsl:template match="*|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/> 
</xsl:copy>
</xsl:template>


</xsl:stylesheet>