{-# LANGUAGE ViewPatterns, ScopedTypeVariables, OverloadedStrings,
TupleSections #-}
module Text.TeXMath.Writers.MathML (writeMathML)
where
import Text.XML.Light
import Text.TeXMath.Types
import Data.Generics (everywhere, mkT)
import Text.TeXMath.Unicode.ToUnicode (toUnicode)
import Text.TeXMath.Shared (getMMLType, handleDownup,
isUppercaseGreek, isRLSequence)
import Text.TeXMath.Readers.MathML.MMLDict (getMathMLOperator)
import qualified Data.Text as T
import Text.Printf
writeMathML :: DisplayType -> [Exp] -> Element
writeMathML :: DisplayType -> [Exp] -> Element
writeMathML DisplayType
dt [Exp]
exprs =
Attr -> Element -> Element
add_attr Attr
dtattr (Element -> Element) -> Element -> Element
forall a b. (a -> b) -> a -> b
$ Element -> Element
math (Element -> Element) -> Element -> Element
forall a b. (a -> b) -> a -> b
$ Maybe TextType -> Exp -> Element
showExp Maybe TextType
forall a. Maybe a
Nothing (Exp -> Element) -> Exp -> Element
forall a b. (a -> b) -> a -> b
$ [Exp] -> Exp
EGrouped
([Exp] -> Exp) -> [Exp] -> Exp
forall a b. (a -> b) -> a -> b
$ (forall a. Data a => a -> a) -> forall a. Data a => a -> a
everywhere ((Exp -> Exp) -> a -> a
forall a b. (Typeable a, Typeable b) => (b -> b) -> a -> a
mkT ((Exp -> Exp) -> a -> a) -> (Exp -> Exp) -> a -> a
forall a b. (a -> b) -> a -> b
$ DisplayType -> Exp -> Exp
handleDownup DisplayType
dt) [Exp]
exprs
where dtattr :: Attr
dtattr = QName -> [Char] -> Attr
Attr ([Char] -> QName
unqual [Char]
"display") [Char]
dt'
dt' :: [Char]
dt' = case DisplayType
dt of
DisplayType
DisplayBlock -> [Char]
"block"
DisplayType
DisplayInline -> [Char]
"inline"
math :: Element -> Element
math :: Element -> Element
math = Attr -> Element -> Element
add_attr (QName -> [Char] -> Attr
Attr ([Char] -> QName
unqual [Char]
"xmlns") [Char]
"http://www.w3.org/1998/Math/MathML") (Element -> Element) -> (Element -> Element) -> Element -> Element
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> Element -> Element
forall t. Node t => [Char] -> t -> Element
unode [Char]
"math"
mrow :: [Element] -> Element
mrow :: [Element] -> Element
mrow = [Char] -> [Element] -> Element
forall t. Node t => [Char] -> t -> Element
unode [Char]
"mrow"
showFraction :: Maybe TextType -> FractionType -> Exp -> Exp -> Element
showFraction :: Maybe TextType -> FractionType -> Exp -> Exp -> Element
showFraction Maybe TextType
tt FractionType
ft Exp
x Exp
y =
case FractionType
ft of
FractionType
NormalFrac -> [Char] -> [Element] -> Element
forall t. Node t => [Char] -> t -> Element
unode [Char]
"mfrac" [Element
x', Element
y']
FractionType
InlineFrac -> [Char] -> Text -> Element -> Element
withAttribute [Char]
"displaystyle" Text
"false" (Element -> Element)
-> ([Element] -> Element) -> [Element] -> Element
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
[Char] -> Element -> Element
forall t. Node t => [Char] -> t -> Element
unode [Char]
"mstyle" (Element -> Element)
-> ([Element] -> Element) -> [Element] -> Element
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
[Char] -> [Element] -> Element
forall t. Node t => [Char] -> t -> Element
unode [Char]
"mfrac" ([Element] -> Element) -> [Element] -> Element
forall a b. (a -> b) -> a -> b
$ [Element
x', Element
y']
FractionType
DisplayFrac -> [Char] -> Text -> Element -> Element
withAttribute [Char]
"displaystyle" Text
"true" (Element -> Element)
-> ([Element] -> Element) -> [Element] -> Element
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
[Char] -> Element -> Element
forall t. Node t => [Char] -> t -> Element
unode [Char]
"mstyle" (Element -> Element)
-> ([Element] -> Element) -> [Element] -> Element
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
[Char] -> [Element] -> Element
forall t. Node t => [Char] -> t -> Element
unode [Char]
"mfrac" ([Element] -> Element) -> [Element] -> Element
forall a b. (a -> b) -> a -> b
$ [Element
x', Element
y']
FractionType
NoLineFrac -> [Char] -> Text -> Element -> Element
withAttribute [Char]
"linethickness" Text
"0" (Element -> Element)
-> ([Element] -> Element) -> [Element] -> Element
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
[Char] -> [Element] -> Element
forall t. Node t => [Char] -> t -> Element
unode [Char]
"mfrac" ([Element] -> Element) -> [Element] -> Element
forall a b. (a -> b) -> a -> b
$ [Element
x', Element
y']
where x' :: Element
x' = Maybe TextType -> Exp -> Element
showExp Maybe TextType
tt Exp
x
y' :: Element
y' = Maybe TextType -> Exp -> Element
showExp Maybe TextType
tt Exp
y
spaceWidth :: Rational -> Element
spaceWidth :: Rational -> Element
spaceWidth Rational
w =
[Char] -> Text -> Element -> Element
withAttribute [Char]
"width" (Text -> Text
dropTrailing0s
([Char] -> Text
T.pack ([Char] -> Text) -> [Char] -> Text
forall a b. (a -> b) -> a -> b
$ [Char] -> Double -> [Char]
forall r. PrintfType r => [Char] -> r
printf [Char]
"%.3f" (Rational -> Double
forall a. Fractional a => Rational -> a
fromRational Rational
w :: Double)) Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"em") (Element -> Element) -> Element -> Element
forall a b. (a -> b) -> a -> b
$ [Char] -> () -> Element
forall t. Node t => [Char] -> t -> Element
unode [Char]
"mspace" ()
makeStretchy :: FormType -> Element -> Element
makeStretchy :: FormType -> Element -> Element
makeStretchy (FormType -> Text
fromForm -> Text
t) = [Char] -> Text -> Element -> Element
withAttribute [Char]
"stretchy" Text
"true"
(Element -> Element) -> (Element -> Element) -> Element -> Element
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> Text -> Element -> Element
withAttribute [Char]
"form" Text
t
fromForm :: FormType -> T.Text
fromForm :: FormType -> Text
fromForm FormType
FInfix = Text
"infix"
fromForm FormType
FPostfix = Text
"postfix"
fromForm FormType
FPrefix = Text
"prefix"
makeScaled :: Rational -> Element -> Element
makeScaled :: Rational -> Element -> Element
makeScaled Rational
x = [Char] -> Text -> Element -> Element
withAttribute [Char]
"minsize" Text
p (Element -> Element) -> (Element -> Element) -> Element -> Element
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> Text -> Element -> Element
withAttribute [Char]
"maxsize" Text
p
(Element -> Element) -> (Element -> Element) -> Element -> Element
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> Text -> Element -> Element
setAttribute [Char]
"stretchy" Text
"true"
where p :: Text
p = [Char] -> Text
T.pack ([Char] -> Text) -> [Char] -> Text
forall a b. (a -> b) -> a -> b
$ [Char] -> Int -> [Char]
forall r. PrintfType r => [Char] -> r
printf [Char]
"%d%%" (Rational -> Int
forall b. Integral b => Rational -> b
forall a b. (RealFrac a, Integral b) => a -> b
round (Rational
100Rational -> Rational -> Rational
forall a. Num a => a -> a -> a
*Rational
x) :: Int)
dropTrailing0s :: T.Text -> T.Text
dropTrailing0s :: Text -> Text
dropTrailing0s Text
t = case Text -> Maybe (Text, Char)
T.unsnoc Text
t of
Just (Text
ts, Char
'0') -> Text -> Text
addZero (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ (Char -> Bool) -> Text -> Text
T.dropWhileEnd (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'0') Text
ts
Maybe (Text, Char)
_ -> Text
t
where
addZero :: Text -> Text
addZero Text
x = case Text -> Maybe (Text, Char)
T.unsnoc Text
x of
Just (Text
_, Char
'.') -> Text -> Char -> Text
T.snoc Text
x Char
'0'
Maybe (Text, Char)
_ -> Text
x
makeText :: TextType -> T.Text -> Element
makeText :: TextType -> Text -> Element
makeText TextType
a Text
s = case (Bool
leadingSp, Bool
trailingSp) of
(Bool
False, Bool
False) -> Element
s'
(Bool
True, Bool
False) -> [Element] -> Element
mrow [Element
sp, Element
s']
(Bool
False, Bool
True) -> [Element] -> Element
mrow [Element
s', Element
sp]
(Bool
True, Bool
True) -> [Element] -> Element
mrow [Element
sp, Element
s', Element
sp]
where sp :: Element
sp = Rational -> Element
spaceWidth (Rational
1Rational -> Rational -> Rational
forall a. Fractional a => a -> a -> a
/Rational
3)
s' :: Element
s' = [Char] -> Text -> Element -> Element
withAttribute [Char]
"mathvariant" (TextType -> Text
getMMLType TextType
a) (Element -> Element) -> Element -> Element
forall a b. (a -> b) -> a -> b
$
[Char] -> Text -> Element
tunode [Char]
"mtext" (Text -> Element) -> Text -> Element
forall a b. (a -> b) -> a -> b
$ TextType -> Text -> Text
toUnicode TextType
a Text
s
trailingSp :: Bool
trailingSp = case Text -> Maybe (Text, Char)
T.unsnoc Text
s of
Just (Text
_, Char
c) -> (Char -> Bool) -> Text -> Bool
T.any (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
c) Text
" \t"
Maybe (Text, Char)
_ -> Bool
False
leadingSp :: Bool
leadingSp = case Text -> Maybe (Char, Text)
T.uncons Text
s of
Just (Char
c, Text
_) -> (Char -> Bool) -> Text -> Bool
T.any (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
c) Text
" \t"
Maybe (Char, Text)
_ -> Bool
False
makeArray :: Maybe TextType -> [Alignment] -> [ArrayLine] -> Element
makeArray :: Maybe TextType -> [Alignment] -> [ArrayLine] -> Element
makeArray Maybe TextType
tt [Alignment]
as [ArrayLine]
ls = [Char] -> [Element] -> Element
forall t. Node t => [Char] -> t -> Element
unode [Char]
"mtable" ([Element] -> Element) -> [Element] -> Element
forall a b. (a -> b) -> a -> b
$
(ArrayLine -> Element) -> [ArrayLine] -> [Element]
forall a b. (a -> b) -> [a] -> [b]
map ([Char] -> [Element] -> Element
forall t. Node t => [Char] -> t -> Element
unode [Char]
"mtr" ([Element] -> Element)
-> (ArrayLine -> [Element]) -> ArrayLine -> Element
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
(Alignment -> [Exp] -> Element)
-> [Alignment] -> ArrayLine -> [Element]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (\Alignment
a -> Alignment -> Element -> Element
setAlignment Alignment
a (Element -> Element) -> ([Exp] -> Element) -> [Exp] -> Element
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> [Element] -> Element
forall t. Node t => [Char] -> t -> Element
unode [Char]
"mtd"([Element] -> Element) -> ([Exp] -> [Element]) -> [Exp] -> Element
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe TextType -> [Exp] -> [Element]
showExps Maybe TextType
tt) [Alignment]
as') [ArrayLine]
ls
where setAlignment :: Alignment -> Element -> Element
setAlignment Alignment
AlignLeft =
[Char] -> Text -> Element -> Element
withAttribute [Char]
"columnalign" Text
"left" (Element -> Element) -> (Element -> Element) -> Element -> Element
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
[Char] -> Text -> Element -> Element
withAttribute [Char]
"style"
(if [Alignment] -> Bool
isRLSequence [Alignment]
as
then Text
"text-align: left; padding-left: 0"
else Text
"text-align: left")
setAlignment Alignment
AlignRight =
[Char] -> Text -> Element -> Element
withAttribute [Char]
"columnalign" Text
"right" (Element -> Element) -> (Element -> Element) -> Element -> Element
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
[Char] -> Text -> Element -> Element
withAttribute [Char]
"style"
(if [Alignment] -> Bool
isRLSequence [Alignment]
as
then Text
"text-align: right; padding-right: 0"
else Text
"text-align: right")
setAlignment Alignment
AlignCenter =
[Char] -> Text -> Element -> Element
withAttribute [Char]
"columnalign" Text
"center" (Element -> Element) -> (Element -> Element) -> Element -> Element
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
[Char] -> Text -> Element -> Element
withAttribute [Char]
"style" Text
"text-align: center"
as' :: [Alignment]
as' = [Alignment]
as [Alignment] -> [Alignment] -> [Alignment]
forall a. [a] -> [a] -> [a]
++ Alignment -> [Alignment]
forall a. a -> [a]
repeat Alignment
AlignCenter
withAttribute :: String -> T.Text -> Element -> Element
withAttribute :: [Char] -> Text -> Element -> Element
withAttribute [Char]
a = Attr -> Element -> Element
add_attr (Attr -> Element -> Element)
-> (Text -> Attr) -> Text -> Element -> Element
forall b c a. (b -> c) -> (a -> b) -> a -> c
. QName -> [Char] -> Attr
Attr ([Char] -> QName
unqual [Char]
a) ([Char] -> Attr) -> (Text -> [Char]) -> Text -> Attr
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> [Char]
T.unpack
setAttribute :: String -> T.Text -> Element -> Element
setAttribute :: [Char] -> Text -> Element -> Element
setAttribute [Char]
a Text
v Element
e = Element
e { elAttribs = update (elAttribs e) }
where
newAttr :: Attr
newAttr = QName -> [Char] -> Attr
Attr ([Char] -> QName
unqual [Char]
a) (Text -> [Char]
T.unpack Text
v)
update :: [Attr] -> [Attr]
update [] = [Attr
newAttr]
update (Attr
x:[Attr]
xs)
| QName -> [Char]
qName (Attr -> QName
attrKey Attr
x) [Char] -> [Char] -> Bool
forall a. Eq a => a -> a -> Bool
== [Char]
a =
Attr
newAttr Attr -> [Attr] -> [Attr]
forall a. a -> [a] -> [a]
: [Attr]
xs
| Bool
otherwise =
Attr
x Attr -> [Attr] -> [Attr]
forall a. a -> [a] -> [a]
: [Attr] -> [Attr]
update [Attr]
xs
accent :: T.Text -> Element
accent :: Text -> Element
accent = Attr -> Element -> Element
add_attr (QName -> [Char] -> Attr
Attr ([Char] -> QName
unqual [Char]
"accent") [Char]
"true") (Element -> Element) -> (Text -> Element) -> Text -> Element
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
[Char] -> Text -> Element
tunode [Char]
"mo"
makeFence :: FormType -> Element -> Element
makeFence :: FormType -> Element -> Element
makeFence (FormType -> Text
fromForm -> Text
t) = [Char] -> Text -> Element -> Element
withAttribute [Char]
"stretchy" Text
"false" (Element -> Element) -> (Element -> Element) -> Element -> Element
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> Text -> Element -> Element
withAttribute [Char]
"form" Text
t
showExp' :: Maybe TextType -> Exp -> Element
showExp' :: Maybe TextType -> Exp -> Element
showExp' Maybe TextType
tt Exp
e =
case Exp
e of
ESymbol TeXSymbolType
Accent Text
x -> Text -> Element
accent Text
x
ESymbol TeXSymbolType
_ Text
x ->
let isaccent :: Text
isaccent = case (Text -> [Text] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
elem Text
"accent") ([Text] -> Bool) -> (Operator -> [Text]) -> Operator -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Operator -> [Text]
properties (Operator -> Bool) -> Maybe Operator -> Maybe Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
Text -> FormType -> Maybe Operator
getMathMLOperator Text
x FormType
FPostfix of
Just Bool
True -> Text
"true"
Maybe Bool
_ -> Text
"false"
in [Char] -> Text -> Element -> Element
withAttribute [Char]
"accent" Text
isaccent (Element -> Element) -> Element -> Element
forall a b. (a -> b) -> a -> b
$ [Char] -> Text -> Element
tunode [Char]
"mo" Text
x
Exp
_ -> Maybe TextType -> Exp -> Element
showExp Maybe TextType
tt Exp
e
showExps :: Maybe TextType -> [Exp] -> [Element]
showExps :: Maybe TextType -> [Exp] -> [Element]
showExps Maybe TextType
tt = (Exp -> Element) -> [Exp] -> [Element]
forall a b. (a -> b) -> [a] -> [b]
map (Maybe TextType -> Exp -> Element
showExp Maybe TextType
tt) ([Exp] -> [Element]) -> ([Exp] -> [Exp]) -> [Exp] -> [Element]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Exp] -> [Exp]
insertFunctionApps
insertFunctionApps :: [Exp] -> [Exp]
insertFunctionApps :: [Exp] -> [Exp]
insertFunctionApps [e :: Exp
e@EMathOperator{}, ESymbol TeXSymbolType
_ Text
"\x2061"] =
[Exp
e, TeXSymbolType -> Text -> Exp
ESymbol TeXSymbolType
Pun Text
"\x2061"]
insertFunctionApps [Exp]
es' = [Exp] -> [Exp]
go [Exp]
es'
where
go :: [Exp] -> [Exp]
go [] = []
go (e :: Exp
e@EMathOperator{} : ESymbol TeXSymbolType
_ Text
"\x2061" : [Exp]
es) =
[Exp] -> Exp
EGrouped [Exp
e , TeXSymbolType -> Text -> Exp
ESymbol TeXSymbolType
Pun Text
"\x2061"] Exp -> [Exp] -> [Exp]
forall a. a -> [a] -> [a]
: [Exp] -> [Exp]
go [Exp]
es
go (e :: Exp
e@EMathOperator{} : es :: [Exp]
es@(Exp
_:[Exp]
_)) =
[Exp] -> Exp
EGrouped [Exp
e, TeXSymbolType -> Text -> Exp
ESymbol TeXSymbolType
Pun Text
"\x2061"] Exp -> [Exp] -> [Exp]
forall a. a -> [a] -> [a]
: [Exp] -> [Exp]
go [Exp]
es
go (Exp
e:[Exp]
es) = Exp
e Exp -> [Exp] -> [Exp]
forall a. a -> [a] -> [a]
: [Exp] -> [Exp]
go [Exp]
es
showExp :: Maybe TextType -> Exp -> Element
showExp :: Maybe TextType -> Exp -> Element
showExp Maybe TextType
tt Exp
e =
let
vnode :: String -> T.Text -> Element
vnode :: [Char] -> Text -> Element
vnode [Char]
elname Text
t
= case Maybe TextType
tt of
Maybe TextType
Nothing ->
if Text -> Bool
isUppercaseGreek Text
t
then [Char] -> Text -> Element -> Element
withAttribute [Char]
"mathvariant" Text
"normal" (Element -> Element) -> Element -> Element
forall a b. (a -> b) -> a -> b
$ [Char] -> Text -> Element
tunode [Char]
elname Text
t
else [Char] -> Text -> Element
tunode [Char]
elname Text
t
Just TextType
TextNormal -> [Char] -> Text -> Element -> Element
withAttribute [Char]
"mathvariant" Text
"normal" (Element -> Element) -> Element -> Element
forall a b. (a -> b) -> a -> b
$
[Char] -> Text -> Element
tunode [Char]
elname Text
t
Just TextType
TextBold
| [Char]
elname [Char] -> [Char] -> Bool
forall a. Eq a => a -> a -> Bool
== [Char]
"mi" Bool -> Bool -> Bool
&& Bool -> Bool
not (Text -> Bool
isUppercaseGreek Text
t)
-> [Char] -> Text -> Element -> Element
withAttribute [Char]
"mathvariant" Text
"bold-italic" (Element -> Element) -> Element -> Element
forall a b. (a -> b) -> a -> b
$
[Char] -> Text -> Element
tunode [Char]
elname (TextType -> Text -> Text
toUnicode TextType
TextBoldItalic Text
t)
| Bool
otherwise
-> [Char] -> Text -> Element -> Element
withAttribute [Char]
"mathvariant" Text
"bold" (Element -> Element) -> Element -> Element
forall a b. (a -> b) -> a -> b
$
[Char] -> Text -> Element
tunode [Char]
elname (TextType -> Text -> Text
toUnicode TextType
TextBold Text
t)
Just TextType
textStyle -> [Char] -> Text -> Element -> Element
withAttribute [Char]
"mathvariant" (TextType -> Text
getMMLType TextType
textStyle) (Element -> Element) -> Element -> Element
forall a b. (a -> b) -> a -> b
$
[Char] -> Text -> Element
tunode [Char]
elname (TextType -> Text -> Text
toUnicode TextType
textStyle Text
t)
in case Exp
e of
ENumber Text
x -> [Char] -> Text -> Element
vnode [Char]
"mn" Text
x
EGrouped [Exp
x] -> Maybe TextType -> Exp -> Element
showExp Maybe TextType
tt Exp
x
EGrouped [Exp]
xs -> [Element] -> Element
mrow ([Element] -> Element) -> [Element] -> Element
forall a b. (a -> b) -> a -> b
$ Maybe TextType -> [Exp] -> [Element]
showExps Maybe TextType
tt [Exp]
xs
EDelimited Text
start Text
end [InEDelimited]
xs -> [Element] -> Element
mrow ([Element] -> Element) -> [Element] -> Element
forall a b. (a -> b) -> a -> b
$
[ FormType -> Element -> Element
makeStretchy FormType
FPrefix ([Char] -> Text -> Element
vnode [Char]
"mo" Text
start) | Bool -> Bool
not (Text -> Bool
T.null Text
start) ] [Element] -> [Element] -> [Element]
forall a. [a] -> [a] -> [a]
++
(InEDelimited -> Element) -> [InEDelimited] -> [Element]
forall a b. (a -> b) -> [a] -> [b]
map ((Text -> Element) -> (Exp -> Element) -> InEDelimited -> Element
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (FormType -> Element -> Element
makeStretchy FormType
FInfix (Element -> Element) -> (Text -> Element) -> Text -> Element
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> Text -> Element
vnode [Char]
"mo") (Maybe TextType -> Exp -> Element
showExp Maybe TextType
tt)) [InEDelimited]
xs [Element] -> [Element] -> [Element]
forall a. [a] -> [a] -> [a]
++
[ FormType -> Element -> Element
makeStretchy FormType
FPostfix ([Char] -> Text -> Element
vnode [Char]
"mo" Text
end)
| Bool -> Bool
not (Text -> Bool
T.null Text
end) ]
EIdentifier Text
x -> [Char] -> Text -> Element
vnode [Char]
"mi" Text
x
EMathOperator Text
x ->
case Maybe TextType
tt of
Maybe TextType
Nothing -> [Char] -> Text -> Element -> Element
withAttribute [Char]
"mathvariant" Text
"normal" (Element -> Element) -> Element -> Element
forall a b. (a -> b) -> a -> b
$ [Char] -> Text -> Element
tunode [Char]
"mi" Text
x
Maybe TextType
_ -> [Char] -> Text -> Element
vnode [Char]
"mi" Text
x
ESymbol TeXSymbolType
Open Text
x -> FormType -> Element -> Element
makeFence FormType
FPrefix (Element -> Element) -> Element -> Element
forall a b. (a -> b) -> a -> b
$ [Char] -> Text -> Element
vnode [Char]
"mo" Text
x
ESymbol TeXSymbolType
Close Text
x -> FormType -> Element -> Element
makeFence FormType
FPostfix (Element -> Element) -> Element -> Element
forall a b. (a -> b) -> a -> b
$ [Char] -> Text -> Element
vnode [Char]
"mo" Text
x
ESymbol TeXSymbolType
Ord Text
x
| Text
x Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"\x2061" -> [Char] -> Text -> Element
vnode [Char]
"mo" Text
x
| Bool
otherwise -> [Char] -> Text -> Element
vnode [Char]
"mi" Text
x
ESymbol TeXSymbolType
_ Text
x -> [Char] -> Text -> Element
vnode [Char]
"mo" Text
x
ESpace Rational
x -> Rational -> Element
spaceWidth Rational
x
EFraction FractionType
ft Exp
x Exp
y -> Maybe TextType -> FractionType -> Exp -> Exp -> Element
showFraction Maybe TextType
tt FractionType
ft Exp
x Exp
y
ESub Exp
x Exp
y -> [Char] -> [Element] -> Element
forall t. Node t => [Char] -> t -> Element
unode [Char]
"msub" ([Element] -> Element) -> [Element] -> Element
forall a b. (a -> b) -> a -> b
$ Maybe TextType -> [Exp] -> [Element]
showExps Maybe TextType
tt [Exp
x, Exp
y]
ESuper Exp
x Exp
y -> [Char] -> [Element] -> Element
forall t. Node t => [Char] -> t -> Element
unode [Char]
"msup" ([Element] -> Element) -> [Element] -> Element
forall a b. (a -> b) -> a -> b
$ Maybe TextType -> [Exp] -> [Element]
showExps Maybe TextType
tt [Exp
x, Exp
y]
ESubsup Exp
x Exp
y Exp
z -> [Char] -> [Element] -> Element
forall t. Node t => [Char] -> t -> Element
unode [Char]
"msubsup" ([Element] -> Element) -> [Element] -> Element
forall a b. (a -> b) -> a -> b
$ Maybe TextType -> [Exp] -> [Element]
showExps Maybe TextType
tt [Exp
x, Exp
y, Exp
z]
EUnder Bool
_ Exp
x Exp
y -> [Char] -> [Element] -> Element
forall t. Node t => [Char] -> t -> Element
unode [Char]
"munder" ([Element] -> Element) -> [Element] -> Element
forall a b. (a -> b) -> a -> b
$ Maybe TextType -> [Exp] -> [Element]
showExps Maybe TextType
tt [Exp
x] [Element] -> [Element] -> [Element]
forall a. [a] -> [a] -> [a]
++ [Maybe TextType -> Exp -> Element
showExp' Maybe TextType
tt Exp
y]
EOver Bool
_ Exp
x (ESymbol TeXSymbolType
Accent Text
"\8407")
-> [Char] -> [Element] -> Element
forall t. Node t => [Char] -> t -> Element
unode [Char]
"mover" ([Element] -> Element) -> [Element] -> Element
forall a b. (a -> b) -> a -> b
$ Maybe TextType -> [Exp] -> [Element]
showExps Maybe TextType
tt [Exp
x] [Element] -> [Element] -> [Element]
forall a. [a] -> [a] -> [a]
++ [Maybe TextType -> Exp -> Element
showExp' Maybe TextType
tt (TeXSymbolType -> Text -> Exp
ESymbol TeXSymbolType
Accent Text
"\8594")]
EOver Bool
_ Exp
x Exp
y -> [Char] -> [Element] -> Element
forall t. Node t => [Char] -> t -> Element
unode [Char]
"mover" ([Element] -> Element) -> [Element] -> Element
forall a b. (a -> b) -> a -> b
$ Maybe TextType -> [Exp] -> [Element]
showExps Maybe TextType
tt [Exp
x] [Element] -> [Element] -> [Element]
forall a. [a] -> [a] -> [a]
++ [Maybe TextType -> Exp -> Element
showExp' Maybe TextType
tt Exp
y]
EUnderover Bool
_ Exp
x Exp
y Exp
z -> [Char] -> [Element] -> Element
forall t. Node t => [Char] -> t -> Element
unode [Char]
"munderover" ([Element] -> Element) -> [Element] -> Element
forall a b. (a -> b) -> a -> b
$
Maybe TextType -> [Exp] -> [Element]
showExps Maybe TextType
tt [Exp
x] [Element] -> [Element] -> [Element]
forall a. [a] -> [a] -> [a]
++ [Maybe TextType -> Exp -> Element
showExp' Maybe TextType
tt Exp
y, Maybe TextType -> Exp -> Element
showExp' Maybe TextType
tt Exp
z]
EPhantom Exp
x -> [Char] -> [Element] -> Element
forall t. Node t => [Char] -> t -> Element
unode [Char]
"mphantom" ([Element] -> Element) -> [Element] -> Element
forall a b. (a -> b) -> a -> b
$ Maybe TextType -> [Exp] -> [Element]
showExps Maybe TextType
tt [Exp
x]
EBoxed Exp
x -> [Char] -> Text -> Element -> Element
withAttribute [Char]
"notation" Text
"box" (Element -> Element) -> (Element -> Element) -> Element -> Element
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
[Char] -> Element -> Element
forall t. Node t => [Char] -> t -> Element
unode [Char]
"menclose" (Element -> Element) -> Element -> Element
forall a b. (a -> b) -> a -> b
$ Maybe TextType -> Exp -> Element
showExp Maybe TextType
tt Exp
x
ECancel StrokeType
ForwardSlash Exp
x -> [Char] -> Text -> Element -> Element
withAttribute [Char]
"notation" Text
"updiagonalstrike" (Element -> Element) -> (Element -> Element) -> Element -> Element
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
[Char] -> Element -> Element
forall t. Node t => [Char] -> t -> Element
unode [Char]
"menclose" (Element -> Element) -> Element -> Element
forall a b. (a -> b) -> a -> b
$ Maybe TextType -> Exp -> Element
showExp Maybe TextType
tt Exp
x
ECancel StrokeType
BackSlash Exp
x -> [Char] -> Text -> Element -> Element
withAttribute [Char]
"notation" Text
"downdiagonalstrike" (Element -> Element) -> (Element -> Element) -> Element -> Element
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
[Char] -> Element -> Element
forall t. Node t => [Char] -> t -> Element
unode [Char]
"menclose" (Element -> Element) -> Element -> Element
forall a b. (a -> b) -> a -> b
$ Maybe TextType -> Exp -> Element
showExp Maybe TextType
tt Exp
x
ECancel StrokeType
XSlash Exp
x -> [Char] -> Text -> Element -> Element
withAttribute [Char]
"notation" Text
"updiagonalstrike downdiagonalstrike" (Element -> Element) -> (Element -> Element) -> Element -> Element
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
[Char] -> Element -> Element
forall t. Node t => [Char] -> t -> Element
unode [Char]
"menclose" (Element -> Element) -> Element -> Element
forall a b. (a -> b) -> a -> b
$ Maybe TextType -> Exp -> Element
showExp Maybe TextType
tt Exp
x
ESqrt Exp
x -> [Char] -> Element -> Element
forall t. Node t => [Char] -> t -> Element
unode [Char]
"msqrt" (Element -> Element) -> Element -> Element
forall a b. (a -> b) -> a -> b
$ Maybe TextType -> Exp -> Element
showExp Maybe TextType
tt Exp
x
ERoot Exp
i Exp
x -> [Char] -> [Element] -> Element
forall t. Node t => [Char] -> t -> Element
unode [Char]
"mroot" [Maybe TextType -> Exp -> Element
showExp Maybe TextType
tt Exp
x, Maybe TextType -> Exp -> Element
showExp Maybe TextType
tt Exp
i]
EScaled Rational
s Exp
x -> Rational -> Element -> Element
makeScaled Rational
s (Element -> Element) -> Element -> Element
forall a b. (a -> b) -> a -> b
$ Maybe TextType -> Exp -> Element
showExp Maybe TextType
tt Exp
x
EArray [Alignment]
as [ArrayLine]
ls -> Maybe TextType -> [Alignment] -> [ArrayLine] -> Element
makeArray Maybe TextType
tt [Alignment]
as [ArrayLine]
ls
EText TextType
a Text
s -> case (Maybe TextType
tt, TextType
a) of
(Just TextType
ty, TextType
TextNormal) -> TextType -> Text -> Element
makeText TextType
ty Text
s
(Maybe TextType, TextType)
_ -> TextType -> Text -> Element
makeText TextType
a Text
s
EStyled TextType
a [Exp]
es -> Maybe TextType -> Exp -> Element
showExp (TextType -> Maybe TextType
forall a. a -> Maybe a
Just TextType
a) ([Exp] -> Exp
EGrouped [Exp]
es)
tunode :: String -> T.Text -> Element
tunode :: [Char] -> Text -> Element
tunode [Char]
s = [Char] -> [Char] -> Element
forall t. Node t => [Char] -> t -> Element
unode [Char]
s ([Char] -> Element) -> (Text -> [Char]) -> Text -> Element
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> [Char]
T.unpack