{-
Copyright (C) 2012 John MacFarlane <jgm@berkeley.edu>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-}{- | Functions for writing a parsed formula as OMML.
-}moduleText.TeXMath.OMML(toOMML,showExp)whereimportText.XML.LightimportText.TeXMath.TypesimportData.Generics(everywhere,mkT)toOMML::DisplayType->[Exp]->ElementtoOMMLdt=container.concatMapshowExp.everywhere(mkT$handleDownupdt)wherecontainer=casedtofDisplayBlock->mnode"oMathPara".mnode"oMath"DisplayInline->mnode"oMath"mnode::Nodet=>String->t->Elementmnodes=node(QNamesNothing(Just"m"))mnodeA::Nodet=>String->String->t->ElementmnodeAsv=add_attr(Attr(QName"val"Nothing(Just"m"))v).mnodesstr::[Element]->String->Elementstrpropss=mnode"r"[mnode"rPr"props,mnode"t"s]showBinary::String->Exp->Exp->ElementshowBinarycxy=casecof"\\frac"->mnode"f"[mnode"fPr"$mnodeA"type""bar"(),mnode"num"x',mnode"den"y']"\\dfrac"->showBinary"\\frac"xy"\\tfrac"->mnode"f"[mnode"fPr"$mnodeA"type""lin"(),mnode"num"x',mnode"den"y']"\\sqrt"->mnode"rad"[mnode"radPr"$mnodeA"degHide""on"(),mnode"deg"y',mnode"e"x']"\\stackrel"->mnode"limUpp"[mnode"e"x',mnode"lim"y']"\\overset"->mnode"limUpp"[mnode"e"x',mnode"lim"y']"\\underset"->mnode"limLow"[mnode"e"x',mnode"lim"y']"\\binom"->mnode"d"[mnode"dPr"$mnodeA"sepChr"","(),mnode"e"$mnode"f"[mnode"fPr"$mnodeA"type""noBar"(),mnode"num"x',mnode"den"y']]_->error$"Unknown binary operator "++cwherex'=showExpxy'=showExpymakeArray::[Alignment]->[ArrayLine]->ElementmakeArrayasrs=mnode"m"$mProps:maptoMrrswheremProps=mnode"mPr"[mnodeA"baseJc""center"(),mnodeA"plcHide""on"(),mnode"mcs"$maptoMcas']as'=take(lengthrs)$as++cycle[AlignDefault]toMrr=mnode"mr"$map(mnode"e".concatMapshowExp)rtoMca=mnode"mc"$mnode"mcPr"$mnodeA"mcJc"(toAligna)()toAlignAlignLeft="left"toAlignAlignRight="right"toAlignAlignCenter="center"toAlignAlignDefault="left"makeText::TextType->String->ElementmakeTextas=strattrsswhereattrs=caseaofTextNormal->[sty"p"]TextBold->[sty"b"]TextItalic->[sty"i"]TextMonospace->[sty"p",scr"monospace"]TextSansSerif->[sty"p",scr"sans-serif"]TextDoubleStruck->[sty"p",scr"double-struck"]TextScript->[sty"p",scr"script"]TextFraktur->[sty"p",scr"fraktur"]styx=mnodeA"sty"x()scrx=mnodeA"scr"x()handleDownup::DisplayType->[Exp]->[Exp]handleDownupdt(exp':xs)=caseexp'ofEDownxy|isNaryx->EGrouped[constructorxyemptyGroup,next]:rest|otherwise->casedtofDisplayBlock->EUnderxy:xsDisplayInline->ESubxy:xsEUpxy|isNaryx->EGrouped[constructorxemptyGroupy,next]:rest|otherwise->casedtofDisplayBlock->EOverxy:xsDisplayInline->ESuperxy:xsEDownupxyz|isNaryx->EGrouped[constructorxyz,next]:rest|otherwise->casedtofDisplayBlock->EUnderoverxyz:xsDisplayInline->ESubsupxyz:xsESubxy|isNaryx->EGrouped[ESubsupxyemptyGroup,next]:restESuperxy|isNaryx->EGrouped[ESubsupxemptyGroupy,next]:restESubsupxyz|isNaryx->EGrouped[ESubsupxyz,next]:restEOverxy|isNaryx->EGrouped[EUnderoverxyemptyGroup,next]:restEUnderxy|isNaryx->EGrouped[EUnderoverxemptyGroupy,next]:restEUnderoverxyz|isNaryx->EGrouped[EUnderoverxyz,next]:rest_->exp':next:restwhere(next,rest)=casexsof(t:ts)->(t,ts)[]->(emptyGroup,[])emptyGroup=EGrouped[]constructor=casedtofDisplayBlock->EUnderoverDisplayInline->ESubsuphandleDownup_[]=[]showExp::Exp->[Element]showExpe=caseeofENumberx->[str[]x]EGrouped[EUnderover(ESymbolOps)yz,w]->[makeNary"undOvr"syzw]EGrouped[ESubsup(ESymbolOps)yz,w]->[makeNary"subSup"syzw]EGroupedxs->concatMapshowExpxsEDelimitedstartendxs->[mnode"d"[mnode"dPr"[mnodeA"begChr"start(),mnodeA"endChr"end(),mnode"grow"()],mnode"e"$concatMapshowExpxs]]EIdentifierx->[str[]x]EMathOperatorx->[str[]x]EStretchyx->showExpx-- no support for stretchy in OMMLESymbol_x->[str[]x]ESpace"0.167em"->[str[]"\x2009"]ESpace"0.222em"->[str[]"\x2005"]ESpace"0.278em"->[str[]"\x2004"]ESpace"0.333em"->[str[]"\x2004"]ESpace"1em"->[str[]"\x2001"]ESpace"2em"->[str[]"\x2001\x2001"]ESpace_->[]-- this is how the xslt sheet handles all spacesEBinarycxy->[showBinarycxy]EUnderx(ESymbolAccent[c])|isBarCharc->[mnode"bar"[mnode"barPr"$mnodeA"pos""bot"(),mnode"e"$showExpx]]EOverx(ESymbolAccent[c])|isBarCharc->[mnode"bar"[mnode"barPr"$mnodeA"pos""top"(),mnode"e"$showExpx]]EOverx(ESymbolAccenty)->[mnode"acc"[mnode"accPr"$mnodeA"chr"y(),mnode"e"$showExpx]]ESubxy->[mnode"sSub"[mnode"e"$showExpx,mnode"sub"$showExpy]]ESuperxy->[mnode"sSup"[mnode"e"$showExpx,mnode"sup"$showExpy]]ESubsupxyz->[mnode"sSubSup"[mnode"e"$showExpx,mnode"sub"$showExpy,mnode"sup"$showExpz]]EUnderxy->[mnode"limLow"[mnode"e"$showExpx,mnode"lim"$showExpy]]EOverxy->[mnode"limUpp"[mnode"e"$showExpx,mnode"lim"$showExpy]]EUnderoverxyz->showExp(EUnderx(EOveryz))EUnary"\\sqrt"x->[mnode"rad"[mnode"radPr"$mnodeA"degHide""on"(),mnode"deg"(),mnode"e"$showExpx]]EUnary"\\surd"x->showExp$EUnary"\\sqrt"xEScaled_x->showExpx-- no support for scaler?EArrayasls->[makeArrayasls]ETextas->[makeTextas]x->error$"showExp encountered "++showx-- note: EUp, EDown, EDownup should be removed by handleDownupisBarChar::Char->BoolisBarCharc=c=='\x203E'||c=='\x00AF'isNary::Exp->BoolisNary(ESymbolOp_)=TrueisNary_=FalsemakeNary::String->String->Exp->Exp->Exp->ElementmakeNarytsyzw=mnode"nary"[mnode"naryPr"[mnodeA"chr"s(),mnodeA"limLoc"t(),mnode"grow"(),mnodeA"supHide"(ify==EGrouped[]then"on"else"off")(),mnodeA"supHide"(ify==EGrouped[]then"on"else"off")()],mnode"e"$showExpw,mnode"sub"$showExpy,mnode"sup"$showExpz]