{-# LANGUAGE PatternGuards #-}
{-# LANGUAGE OverloadedStrings #-}

{-
Copyright (C) 2014 Jesse Rosenthal <jrosenthal@jhu.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
-}

{- |
 Module : Text.TeXMath.Readers.OMML
 Copyright : Copyright (C) 2014 Jesse Rosenthal
 License : GNU GPL, version 2 or above

 Maintainer : Jesse Rosenthal <jrosenthal@jhu.edu>
 Stability : alpha
 Portability : portable

Types and functions for conversion of OMML into TeXMath 'Exp's.
-}

module Text.TeXMath.Readers.OMML (readOMML) where

import Text.XML.Light
import Data.Maybe (isJust, mapMaybe, fromMaybe, maybeToList)
import Data.List (intercalate)
import Data.Char (isDigit, readLitChar)
import qualified Data.Text as T
import Text.TeXMath.Types
import Text.TeXMath.Shared (fixTree, getSpaceWidth, getOperator)
import Text.TeXMath.Unicode.ToTeX (getSymbolType)
import Text.TeXMath.Unicode.Fonts (getUnicode, textToFont)
import Data.List.Split (splitWhen)

readOMML :: T.Text -> Either T.Text [Exp]
readOMML :: Text -> Either Text [Exp]
readOMML Text
s | Just Element
e <- Text -> Maybe Element
forall s. XmlSource s => s -> Maybe Element
parseXMLDoc Text
s =
  case Element -> Maybe [Exp]
elemToOMML Element
e of
    Just [Exp]
exs -> [Exp] -> Either Text [Exp]
forall a b. b -> Either a b
Right ([Exp] -> Either Text [Exp]) -> [Exp] -> Either Text [Exp]
forall a b. (a -> b) -> a -> b
$ (Exp -> Exp) -> [Exp] -> [Exp]
forall a b. (a -> b) -> [a] -> [b]
map Exp -> Exp
fixTree ([Exp] -> [Exp]) -> [Exp] -> [Exp]
forall a b. (a -> b) -> a -> b
$ [Exp] -> [Exp]
unGroup [Exp]
exs
    Maybe [Exp]
Nothing   -> Text -> Either Text [Exp]
forall a b. a -> Either a b
Left Text
"xml file was not an <m:oMathPara> or <m:oMath> element."
readOMML Text
_ = Text -> Either Text [Exp]
forall a b. a -> Either a b
Left Text
"Couldn't parse OMML file"

unGroup :: [Exp] -> [Exp]
unGroup :: [Exp] -> [Exp]
unGroup [EGrouped [Exp]
exps] = [Exp]
exps
unGroup [Exp]
exps = [Exp]
exps

elemToOMML :: Element -> Maybe [Exp]
elemToOMML :: Element -> Maybe [Exp]
elemToOMML Element
element  | String -> String -> Element -> Bool
isElem String
"m" String
"oMathPara" Element
element = do
  let expList :: [[Exp]]
expList = (Element -> Maybe [Exp]) -> [Element] -> [[Exp]]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe Element -> Maybe [Exp]
elemToOMML (Element -> [Element]
elChildren Element
element)
  [Exp] -> Maybe [Exp]
forall a. a -> Maybe a
forall (m :: * -> *) a. Monad m => a -> m a
return ([Exp] -> Maybe [Exp]) -> [Exp] -> Maybe [Exp]
forall a b. (a -> b) -> a -> b
$ ([Exp] -> Exp) -> [[Exp]] -> [Exp]
forall a b. (a -> b) -> [a] -> [b]
map (\[Exp]
l -> if [Exp] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Exp]
l Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
1 then ([Exp] -> Exp
forall a. HasCallStack => [a] -> a
head [Exp]
l) else [Exp] -> Exp
EGrouped [Exp]
l) [[Exp]]
expList
elemToOMML Element
element  | String -> String -> Element -> Bool
isElem String
"m" String
"oMath" Element
element =
  [Exp] -> Maybe [Exp]
forall a. a -> Maybe a
Just ([Exp] -> Maybe [Exp]) -> [Exp] -> Maybe [Exp]
forall a b. (a -> b) -> a -> b
$ [[Exp]] -> [Exp]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[Exp]] -> [Exp]) -> [[Exp]] -> [Exp]
forall a b. (a -> b) -> a -> b
$ (Element -> Maybe [Exp]) -> [Element] -> [[Exp]]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe Element -> Maybe [Exp]
elemToExps ([Element] -> [[Exp]]) -> [Element] -> [[Exp]]
forall a b. (a -> b) -> a -> b
$ [Element] -> [Element]
unwrapWTags ([Element] -> [Element]) -> [Element] -> [Element]
forall a b. (a -> b) -> a -> b
$ Element -> [Element]
elChildren Element
element
elemToOMML Element
_ = Maybe [Exp]
forall a. Maybe a
Nothing

-- oMath can contain w:hyperlink, w:sdt, etc. I can't find a complete
-- documentation of this, so we just unwrap any w:tag immediately
-- beneath oMath. Note that this shouldn't affect the "w" tags in
-- elemToOMathRunElem(s) because, those are underneath an "m:r" tag.
unwrapWTags :: [Element] -> [Element]
unwrapWTags :: [Element] -> [Element]
unwrapWTags [Element]
elements = (Element -> [Element]) -> [Element] -> [Element]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Element -> [Element]
unwrapChild [Element]
elements
  where unwrapChild :: Element -> [Element]
unwrapChild Element
element = case QName -> Maybe String
qPrefix (QName -> Maybe String) -> QName -> Maybe String
forall a b. (a -> b) -> a -> b
$ Element -> QName
elName Element
element of
                                Just String
"w" -> Element -> [Element]
elChildren Element
element
                                Maybe String
_        -> [Element
element]

-- Kept as String because of Text.XML.Light
isElem :: String -> String -> Element -> Bool
isElem :: String -> String -> Element -> Bool
isElem String
prefix String
name Element
element =
  let qp :: String
qp = String -> Maybe String -> String
forall a. a -> Maybe a -> a
fromMaybe String
"" (QName -> Maybe String
qPrefix (Element -> QName
elName Element
element))
  in
   QName -> String
qName (Element -> QName
elName Element
element) String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
name Bool -> Bool -> Bool
&&
   String
qp String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
prefix

-- Kept as String because of Text.XML.Light
hasElemName :: String -> String -> QName -> Bool
hasElemName :: String -> String -> QName -> Bool
hasElemName String
prefix String
name QName
qn =
  let qp :: String
qp = String -> Maybe String -> String
forall a. a -> Maybe a -> a
fromMaybe String
"" (QName -> Maybe String
qPrefix QName
qn)
  in
   QName -> String
qName QName
qn String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
name Bool -> Bool -> Bool
&&
   String
qp       String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
prefix

data OMathRunElem = TextRun T.Text
                  | LnBrk
                  | Tab
                    deriving Int -> OMathRunElem -> ShowS
[OMathRunElem] -> ShowS
OMathRunElem -> String
(Int -> OMathRunElem -> ShowS)
-> (OMathRunElem -> String)
-> ([OMathRunElem] -> ShowS)
-> Show OMathRunElem
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> OMathRunElem -> ShowS
showsPrec :: Int -> OMathRunElem -> ShowS
$cshow :: OMathRunElem -> String
show :: OMathRunElem -> String
$cshowList :: [OMathRunElem] -> ShowS
showList :: [OMathRunElem] -> ShowS
Show

data OMathRunTextStyle = NoStyle
                       | Normal
                       | Styled { OMathRunTextStyle -> Maybe OMathTextScript
oMathScript :: Maybe OMathTextScript
                                , OMathRunTextStyle -> Maybe OMathTextStyle
oMathStyle  :: Maybe OMathTextStyle }
                       deriving Int -> OMathRunTextStyle -> ShowS
[OMathRunTextStyle] -> ShowS
OMathRunTextStyle -> String
(Int -> OMathRunTextStyle -> ShowS)
-> (OMathRunTextStyle -> String)
-> ([OMathRunTextStyle] -> ShowS)
-> Show OMathRunTextStyle
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> OMathRunTextStyle -> ShowS
showsPrec :: Int -> OMathRunTextStyle -> ShowS
$cshow :: OMathRunTextStyle -> String
show :: OMathRunTextStyle -> String
$cshowList :: [OMathRunTextStyle] -> ShowS
showList :: [OMathRunTextStyle] -> ShowS
Show

data OMathTextScript = ORoman
                     | OScript
                     | OFraktur
                     | ODoubleStruck
                     | OSansSerif
                     | OMonospace
                     deriving (Int -> OMathTextScript -> ShowS
[OMathTextScript] -> ShowS
OMathTextScript -> String
(Int -> OMathTextScript -> ShowS)
-> (OMathTextScript -> String)
-> ([OMathTextScript] -> ShowS)
-> Show OMathTextScript
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> OMathTextScript -> ShowS
showsPrec :: Int -> OMathTextScript -> ShowS
$cshow :: OMathTextScript -> String
show :: OMathTextScript -> String
$cshowList :: [OMathTextScript] -> ShowS
showList :: [OMathTextScript] -> ShowS
Show, OMathTextScript -> OMathTextScript -> Bool
(OMathTextScript -> OMathTextScript -> Bool)
-> (OMathTextScript -> OMathTextScript -> Bool)
-> Eq OMathTextScript
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: OMathTextScript -> OMathTextScript -> Bool
== :: OMathTextScript -> OMathTextScript -> Bool
$c/= :: OMathTextScript -> OMathTextScript -> Bool
/= :: OMathTextScript -> OMathTextScript -> Bool
Eq)

data OMathTextStyle = OPlain
                    | OBold
                    | OItalic
                    | OBoldItalic
                    deriving (Int -> OMathTextStyle -> ShowS
[OMathTextStyle] -> ShowS
OMathTextStyle -> String
(Int -> OMathTextStyle -> ShowS)
-> (OMathTextStyle -> String)
-> ([OMathTextStyle] -> ShowS)
-> Show OMathTextStyle
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> OMathTextStyle -> ShowS
showsPrec :: Int -> OMathTextStyle -> ShowS
$cshow :: OMathTextStyle -> String
show :: OMathTextStyle -> String
$cshowList :: [OMathTextStyle] -> ShowS
showList :: [OMathTextStyle] -> ShowS
Show, OMathTextStyle -> OMathTextStyle -> Bool
(OMathTextStyle -> OMathTextStyle -> Bool)
-> (OMathTextStyle -> OMathTextStyle -> Bool) -> Eq OMathTextStyle
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: OMathTextStyle -> OMathTextStyle -> Bool
== :: OMathTextStyle -> OMathTextStyle -> Bool
$c/= :: OMathTextStyle -> OMathTextStyle -> Bool
/= :: OMathTextStyle -> OMathTextStyle -> Bool
Eq)

elemToBase :: Element -> Maybe Exp
elemToBase :: Element -> Maybe Exp
elemToBase Element
element | String -> String -> Element -> Bool
isElem String
"m" String
"e" Element
element = do
  [Exp]
bs <- Element -> Maybe [Exp]
elemToBases Element
element
  Exp -> Maybe Exp
forall a. a -> Maybe a
forall (m :: * -> *) a. Monad m => a -> m a
return (Exp -> Maybe Exp) -> Exp -> Maybe Exp
forall a b. (a -> b) -> a -> b
$ case [Exp]
bs of
    (Exp
e : []) -> Exp
e
    [Exp]
exps     -> [Exp] -> Exp
EGrouped [Exp]
exps
elemToBase Element
_ = Maybe Exp
forall a. Maybe a
Nothing

elemToBases :: Element -> Maybe [Exp]
elemToBases :: Element -> Maybe [Exp]
elemToBases Element
element | String -> String -> Element -> Bool
isElem String
"m" String
"e" Element
element =
  [Exp] -> Maybe [Exp]
forall a. a -> Maybe a
forall (m :: * -> *) a. Monad m => a -> m a
return ([Exp] -> Maybe [Exp]) -> [Exp] -> Maybe [Exp]
forall a b. (a -> b) -> a -> b
$ [[Exp]] -> [Exp]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[Exp]] -> [Exp]) -> [[Exp]] -> [Exp]
forall a b. (a -> b) -> a -> b
$ (Element -> Maybe [Exp]) -> [Element] -> [[Exp]]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe Element -> Maybe [Exp]
elemToExps' (Element -> [Element]
elChildren Element
element)
elemToBases Element
_ = Maybe [Exp]
forall a. Maybe a
Nothing


breakOnAmpersand :: [Exp] -> [[Exp]]
breakOnAmpersand :: [Exp] -> [[Exp]]
breakOnAmpersand = (Exp -> Bool) -> [Exp] -> [[Exp]]
forall a. (a -> Bool) -> [a] -> [[a]]
splitWhen Exp -> Bool
isAmpersand
 where
  isAmpersand :: Exp -> Bool
isAmpersand (ESymbol TeXSymbolType
_ Text
"&") = Bool
True
  isAmpersand Exp
_ = Bool
False

elemToOMathRunTextStyle :: Element -> OMathRunTextStyle
elemToOMathRunTextStyle :: Element -> OMathRunTextStyle
elemToOMathRunTextStyle Element
element
  | Just Element
mrPr <- (QName -> Bool) -> Element -> Maybe Element
filterChildName (String -> String -> QName -> Bool
hasElemNameString
"m" String
"rPr") Element
element
  , Just Element
_    <- (QName -> Bool) -> Element -> Maybe Element
filterChildName (String -> String -> QName -> Bool
hasElemNameString
"m" String
"nor") Element
mrPr =
    OMathRunTextStyle
Normal
  | Just Element
mrPr <- (QName -> Bool) -> Element -> Maybe Element
filterChildName (String -> String -> QName -> Bool
hasElemNameString
"m" String
"rPr") Element
element =
    let scr :: Maybe OMathTextScript
scr =
          case
            (QName -> Bool) -> Element -> Maybe Element
filterChildName (String -> String -> QName -> Bool
hasElemNameString
"m" String
"scr") Element
mrPr Maybe Element -> (Element -> Maybe String) -> Maybe String
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
            (QName -> Bool) -> Element -> Maybe String
findAttrBy (String -> String -> QName -> Bool
hasElemNameString
"m" String
"val")
          of
            Just String
"roman"         -> OMathTextScript -> Maybe OMathTextScript
forall a. a -> Maybe a
Just OMathTextScript
ORoman
            Just String
"script"        -> OMathTextScript -> Maybe OMathTextScript
forall a. a -> Maybe a
Just OMathTextScript
OScript
            Just String
"fraktur"       -> OMathTextScript -> Maybe OMathTextScript
forall a. a -> Maybe a
Just OMathTextScript
OFraktur
            Just String
"double-struck" -> OMathTextScript -> Maybe OMathTextScript
forall a. a -> Maybe a
Just OMathTextScript
ODoubleStruck
            Just String
"sans-serif"    -> OMathTextScript -> Maybe OMathTextScript
forall a. a -> Maybe a
Just OMathTextScript
OSansSerif
            Just String
"monospace"     -> OMathTextScript -> Maybe OMathTextScript
forall a. a -> Maybe a
Just OMathTextScript
OMonospace
            Maybe String
_                    -> Maybe OMathTextScript
forall a. Maybe a
Nothing

        sty :: Maybe OMathTextStyle
sty =
          case
            (QName -> Bool) -> Element -> Maybe Element
filterChildName (String -> String -> QName -> Bool
hasElemNameString
"m" String
"sty") Element
mrPr Maybe Element -> (Element -> Maybe String) -> Maybe String
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
            (QName -> Bool) -> Element -> Maybe String
findAttrBy (String -> String -> QName -> Bool
hasElemNameString
"m" String
"val")
          of
            Just String
"p"             -> OMathTextStyle -> Maybe OMathTextStyle
forall a. a -> Maybe a
Just OMathTextStyle
OPlain
            Just String
"b"             -> OMathTextStyle -> Maybe OMathTextStyle
forall a. a -> Maybe a
Just OMathTextStyle
OBold
            Just String
"i"             -> OMathTextStyle -> Maybe OMathTextStyle
forall a. a -> Maybe a
Just OMathTextStyle
OItalic
            Just String
"bi"            -> OMathTextStyle -> Maybe OMathTextStyle
forall a. a -> Maybe a
Just OMathTextStyle
OBoldItalic
            Maybe String
_                    -> Maybe OMathTextStyle
forall a. Maybe a
Nothing
    in
     Styled { oMathScript :: Maybe OMathTextScript
oMathScript = Maybe OMathTextScript
scr, oMathStyle :: Maybe OMathTextStyle
oMathStyle = Maybe OMathTextStyle
sty }
  | Bool
otherwise = OMathRunTextStyle
NoStyle

elemToOMathRunElem :: Element -> Maybe OMathRunElem
elemToOMathRunElem :: Element -> Maybe OMathRunElem
elemToOMathRunElem Element
element
  | String -> String -> Element -> Bool
isElem String
"w" String
"t" Element
element
    Bool -> Bool -> Bool
|| String -> String -> Element -> Bool
isElem String
"m" String
"t" Element
element
    Bool -> Bool -> Bool
|| String -> String -> Element -> Bool
isElem String
"w" String
"delText" Element
element = OMathRunElem -> Maybe OMathRunElem
forall a. a -> Maybe a
Just (OMathRunElem -> Maybe OMathRunElem)
-> OMathRunElem -> Maybe OMathRunElem
forall a b. (a -> b) -> a -> b
$ Text -> OMathRunElem
TextRun (Text -> OMathRunElem) -> Text -> OMathRunElem
forall a b. (a -> b) -> a -> b
$ String -> Text
T.pack (String -> Text) -> String -> Text
forall a b. (a -> b) -> a -> b
$ Element -> String
strContent Element
element
  | String -> String -> Element -> Bool
isElem String
"w" String
"br" Element
element = OMathRunElem -> Maybe OMathRunElem
forall a. a -> Maybe a
Just OMathRunElem
LnBrk
  | String -> String -> Element -> Bool
isElem String
"w" String
"tab" Element
element = OMathRunElem -> Maybe OMathRunElem
forall a. a -> Maybe a
Just OMathRunElem
Tab
  | String -> String -> Element -> Bool
isElem String
"w" String
"sym" Element
element = OMathRunElem -> Maybe OMathRunElem
forall a. a -> Maybe a
Just (OMathRunElem -> Maybe OMathRunElem)
-> OMathRunElem -> Maybe OMathRunElem
forall a b. (a -> b) -> a -> b
$ Text -> OMathRunElem
TextRun (Text -> OMathRunElem) -> Text -> OMathRunElem
forall a b. (a -> b) -> a -> b
$ Element -> Text
getSymChar Element
element
  | Bool
otherwise = Maybe OMathRunElem
forall a. Maybe a
Nothing

elemToOMathRunElems :: Element -> Maybe [OMathRunElem]
elemToOMathRunElems :: Element -> Maybe [OMathRunElem]
elemToOMathRunElems Element
element
  | String -> String -> Element -> Bool
isElem String
"w" String
"r" Element
element
    Bool -> Bool -> Bool
|| String -> String -> Element -> Bool
isElem String
"m" String
"r" Element
element =
      [OMathRunElem] -> Maybe [OMathRunElem]
forall a. a -> Maybe a
Just ([OMathRunElem] -> Maybe [OMathRunElem])
-> [OMathRunElem] -> Maybe [OMathRunElem]
forall a b. (a -> b) -> a -> b
$ (Element -> Maybe OMathRunElem) -> [Element] -> [OMathRunElem]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (Element -> Maybe OMathRunElem
elemToOMathRunElem) (Element -> [Element]
elChildren Element
element)
elemToOMathRunElems Element
_ = Maybe [OMathRunElem]
forall a. Maybe a
Nothing

----- And now the TeXMath Creation

oMathRunElemToText :: OMathRunElem -> T.Text
oMathRunElemToText :: OMathRunElem -> Text
oMathRunElemToText (TextRun Text
s) = Text
s
oMathRunElemToText (OMathRunElem
LnBrk) = Text
"\n"
oMathRunElemToText (OMathRunElem
Tab) = Text
"\t"

oMathRunElemsToText :: [OMathRunElem] -> T.Text
oMathRunElemsToText :: [OMathRunElem] -> Text
oMathRunElemsToText = [Text] -> Text
T.concat ([Text] -> Text)
-> ([OMathRunElem] -> [Text]) -> [OMathRunElem] -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (OMathRunElem -> Text) -> [OMathRunElem] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map OMathRunElem -> Text
oMathRunElemToText

oMathRunTextStyleToTextType :: OMathRunTextStyle -> Maybe TextType
oMathRunTextStyleToTextType :: OMathRunTextStyle -> Maybe TextType
oMathRunTextStyleToTextType (OMathRunTextStyle
Normal) = TextType -> Maybe TextType
forall a. a -> Maybe a
Just (TextType -> Maybe TextType) -> TextType -> Maybe TextType
forall a b. (a -> b) -> a -> b
$ TextType
TextNormal
oMathRunTextStyleToTextType (OMathRunTextStyle
NoStyle) = Maybe TextType
forall a. Maybe a
Nothing
oMathRunTextStyleToTextType (Styled Maybe OMathTextScript
scr Maybe OMathTextStyle
sty)
  | Just OMathTextStyle
OBold <- Maybe OMathTextStyle
sty
  , Just OMathTextScript
OSansSerif <- Maybe OMathTextScript
scr =
    TextType -> Maybe TextType
forall a. a -> Maybe a
Just (TextType -> Maybe TextType) -> TextType -> Maybe TextType
forall a b. (a -> b) -> a -> b
$ TextType
TextSansSerifBold
  | Just OMathTextStyle
OBoldItalic <- Maybe OMathTextStyle
sty
  , Just OMathTextScript
OSansSerif <- Maybe OMathTextScript
scr =
    TextType -> Maybe TextType
forall a. a -> Maybe a
Just (TextType -> Maybe TextType) -> TextType -> Maybe TextType
forall a b. (a -> b) -> a -> b
$ TextType
TextSansSerifBoldItalic
  | Just OMathTextStyle
OBold <- Maybe OMathTextStyle
sty
  , Just OMathTextScript
OScript <- Maybe OMathTextScript
scr =
    TextType -> Maybe TextType
forall a. a -> Maybe a
Just (TextType -> Maybe TextType) -> TextType -> Maybe TextType
forall a b. (a -> b) -> a -> b
$ TextType
TextBoldScript
  | Just OMathTextStyle
OBold <- Maybe OMathTextStyle
sty
  , Just OMathTextScript
OFraktur <- Maybe OMathTextScript
scr =
    TextType -> Maybe TextType
forall a. a -> Maybe a
Just (TextType -> Maybe TextType) -> TextType -> Maybe TextType
forall a b. (a -> b) -> a -> b
$ TextType
TextBoldFraktur
  | Just OMathTextStyle
OItalic <- Maybe OMathTextStyle
sty
  , Just OMathTextScript
OSansSerif <- Maybe OMathTextScript
scr =
    TextType -> Maybe TextType
forall a. a -> Maybe a
Just (TextType -> Maybe TextType) -> TextType -> Maybe TextType
forall a b. (a -> b) -> a -> b
$ TextType
TextSansSerifItalic
  | Just OMathTextStyle
OBold <- Maybe OMathTextStyle
sty =
    TextType -> Maybe TextType
forall a. a -> Maybe a
Just (TextType -> Maybe TextType) -> TextType -> Maybe TextType
forall a b. (a -> b) -> a -> b
$ TextType
TextBold
  | Just OMathTextStyle
OItalic <- Maybe OMathTextStyle
sty =
    TextType -> Maybe TextType
forall a. a -> Maybe a
Just (TextType -> Maybe TextType) -> TextType -> Maybe TextType
forall a b. (a -> b) -> a -> b
$ TextType
TextItalic
  | Just OMathTextScript
OMonospace <- Maybe OMathTextScript
scr =
    TextType -> Maybe TextType
forall a. a -> Maybe a
Just (TextType -> Maybe TextType) -> TextType -> Maybe TextType
forall a b. (a -> b) -> a -> b
$ TextType
TextMonospace
  | Just OMathTextScript
OSansSerif <- Maybe OMathTextScript
scr =
    TextType -> Maybe TextType
forall a. a -> Maybe a
Just (TextType -> Maybe TextType) -> TextType -> Maybe TextType
forall a b. (a -> b) -> a -> b
$ TextType
TextSansSerif
  | Just OMathTextScript
ODoubleStruck <- Maybe OMathTextScript
scr =
    TextType -> Maybe TextType
forall a. a -> Maybe a
Just (TextType -> Maybe TextType) -> TextType -> Maybe TextType
forall a b. (a -> b) -> a -> b
$ TextType
TextDoubleStruck
  | Just OMathTextScript
OScript <- Maybe OMathTextScript
scr =
    TextType -> Maybe TextType
forall a. a -> Maybe a
Just (TextType -> Maybe TextType) -> TextType -> Maybe TextType
forall a b. (a -> b) -> a -> b
$ TextType
TextScript
  | Just OMathTextScript
OFraktur <- Maybe OMathTextScript
scr =
    TextType -> Maybe TextType
forall a. a -> Maybe a
Just (TextType -> Maybe TextType) -> TextType -> Maybe TextType
forall a b. (a -> b) -> a -> b
$ TextType
TextFraktur
  | Just OMathTextStyle
OBoldItalic <- Maybe OMathTextStyle
sty =
    TextType -> Maybe TextType
forall a. a -> Maybe a
Just (TextType -> Maybe TextType) -> TextType -> Maybe TextType
forall a b. (a -> b) -> a -> b
$ TextType
TextBoldItalic
  | Bool
otherwise = Maybe TextType
forall a. Maybe a
Nothing

elemToExps :: Element -> Maybe [Exp]
elemToExps :: Element -> Maybe [Exp]
elemToExps Element
element = [Exp] -> [Exp]
unGroup ([Exp] -> [Exp]) -> Maybe [Exp] -> Maybe [Exp]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Element -> Maybe [Exp]
elemToExps' Element
element

elemToExps' :: Element -> Maybe [Exp]
elemToExps' :: Element -> Maybe [Exp]
elemToExps' Element
element | String -> String -> Element -> Bool
isElem String
"m" String
"acc" Element
element = do
  let chr :: Maybe Char
chr = (QName -> Bool) -> Element -> Maybe Element
filterChildName (String -> String -> QName -> Bool
hasElemName String
"m" String
"accPr") Element
element Maybe Element -> (Element -> Maybe Element) -> Maybe Element
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
            (QName -> Bool) -> Element -> Maybe Element
filterChildName (String -> String -> QName -> Bool
hasElemName String
"m" String
"chr") Maybe Element -> (Element -> Maybe String) -> Maybe String
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
            (QName -> Bool) -> Element -> Maybe String
findAttrBy (String -> String -> QName -> Bool
hasElemName String
"m" String
"val") Maybe String -> (String -> Maybe Char) -> Maybe Char
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
            Char -> Maybe Char
forall a. a -> Maybe a
Just (Char -> Maybe Char) -> (String -> Char) -> String -> Maybe Char
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Char
forall a. HasCallStack => [a] -> a
head
      chr' :: Text
chr' = case Maybe Char
chr of
        Just Char
c -> Char -> Text
T.singleton Char
c
        Maybe Char
Nothing -> Text
"\x302"       -- default to wide hat.
  Exp
baseExp <- (QName -> Bool) -> Element -> Maybe Element
filterChildName (String -> String -> QName -> Bool
hasElemName String
"m" String
"e") Element
element Maybe Element -> (Element -> Maybe Exp) -> Maybe Exp
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
             Element -> Maybe Exp
elemToBase
  [Exp] -> Maybe [Exp]
forall a. a -> Maybe a
forall (m :: * -> *) a. Monad m => a -> m a
return ([Exp] -> Maybe [Exp]) -> [Exp] -> Maybe [Exp]
forall a b. (a -> b) -> a -> b
$ [Bool -> Exp -> Exp -> Exp
EOver Bool
False Exp
baseExp (TeXSymbolType -> Text -> Exp
ESymbol TeXSymbolType
Accent Text
chr')]
elemToExps' Element
element | String -> String -> Element -> Bool
isElem String
"m" String
"bar" Element
element = do
  let pos :: Maybe String
pos = (QName -> Bool) -> Element -> Maybe Element
filterChildName (String -> String -> QName -> Bool
hasElemName String
"m" String
"barPr") Element
element Maybe Element -> (Element -> Maybe Element) -> Maybe Element
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
            (QName -> Bool) -> Element -> Maybe Element
filterChildName (String -> String -> QName -> Bool
hasElemName String
"m" String
"pos") Maybe Element -> (Element -> Maybe String) -> Maybe String
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
            (QName -> Bool) -> Element -> Maybe String
findAttrBy (String -> String -> QName -> Bool
hasElemName String
"m" String
"val")
  Exp
baseExp <- (QName -> Bool) -> Element -> Maybe Element
filterChildName (String -> String -> QName -> Bool
hasElemName String
"m" String
"e") Element
element Maybe Element -> (Element -> Maybe Exp) -> Maybe Exp
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
             Element -> Maybe Exp
elemToBase
  -- According to OMML Specification, the default value of pos (whether it exists or not) is "bot"
  -- see https://github.com/jgm/texmath/issues/187
  case Maybe String
pos of
    Just String
"top" -> [Exp] -> Maybe [Exp]
forall a. a -> Maybe a
Just [Bool -> Exp -> Exp -> Exp
EOver Bool
False Exp
baseExp (TeXSymbolType -> Text -> Exp
ESymbol TeXSymbolType
TOver Text
"\773")]
    Maybe String
_          -> [Exp] -> Maybe [Exp]
forall a. a -> Maybe a
Just [Bool -> Exp -> Exp -> Exp
EUnder Bool
False Exp
baseExp (TeXSymbolType -> Text -> Exp
ESymbol TeXSymbolType
TUnder Text
"\818")]
elemToExps' Element
element | String -> String -> Element -> Bool
isElem String
"m" String
"box" Element
element = do
  Exp
baseExp <- (QName -> Bool) -> Element -> Maybe Element
filterChildName (String -> String -> QName -> Bool
hasElemName String
"m" String
"e") Element
element Maybe Element -> (Element -> Maybe Exp) -> Maybe Exp
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
             Element -> Maybe Exp
elemToBase
  [Exp] -> Maybe [Exp]
forall a. a -> Maybe a
forall (m :: * -> *) a. Monad m => a -> m a
return [Exp
baseExp]
elemToExps' Element
element | String -> String -> Element -> Bool
isElem String
"m" String
"borderBox" Element
element = do
  Exp
baseExp <- (QName -> Bool) -> Element -> Maybe Element
filterChildName (String -> String -> QName -> Bool
hasElemName String
"m" String
"e") Element
element Maybe Element -> (Element -> Maybe Exp) -> Maybe Exp
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
             Element -> Maybe Exp
elemToBase
  [Exp] -> Maybe [Exp]
forall a. a -> Maybe a
forall (m :: * -> *) a. Monad m => a -> m a
return [Exp -> Exp
EBoxed Exp
baseExp]
elemToExps' Element
element | String -> String -> Element -> Bool
isElem String
"m" String
"d" Element
element =
  let baseExps :: [[Exp]]
baseExps  = (Element -> Maybe [Exp]) -> [Element] -> [[Exp]]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe
                  Element -> Maybe [Exp]
elemToBases
                  (Element -> [Element]
elChildren Element
element)
      inDelimExps :: [[Either a Exp]]
inDelimExps = ([Exp] -> [Either a Exp]) -> [[Exp]] -> [[Either a Exp]]
forall a b. (a -> b) -> [a] -> [b]
map ((Exp -> Either a Exp) -> [Exp] -> [Either a Exp]
forall a b. (a -> b) -> [a] -> [b]
map Exp -> Either a Exp
forall a b. b -> Either a b
Right) [[Exp]]
baseExps
      dPr :: Maybe Element
dPr = (QName -> Bool) -> Element -> Maybe Element
filterChildName (String -> String -> QName -> Bool
hasElemName String
"m" String
"dPr") Element
element
      begChr :: Maybe Char
begChr = Maybe Element
dPr Maybe Element -> (Element -> Maybe Element) -> Maybe Element
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
               (QName -> Bool) -> Element -> Maybe Element
filterChildName (String -> String -> QName -> Bool
hasElemName String
"m" String
"begChr") Maybe Element -> (Element -> Maybe String) -> Maybe String
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
               (QName -> Bool) -> Element -> Maybe String
findAttrBy (String -> String -> QName -> Bool
hasElemName String
"m" String
"val") Maybe String -> (String -> Maybe Char) -> Maybe Char
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
               (\String
c -> if String -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null String
c then (Char -> Maybe Char
forall a. a -> Maybe a
Just Char
' ') else (Char -> Maybe Char
forall a. a -> Maybe a
Just (Char -> Maybe Char) -> Char -> Maybe Char
forall a b. (a -> b) -> a -> b
$ String -> Char
forall a. HasCallStack => [a] -> a
head String
c))
      sepChr :: Maybe Char
sepChr = Maybe Element
dPr Maybe Element -> (Element -> Maybe Element) -> Maybe Element
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
               (QName -> Bool) -> Element -> Maybe Element
filterChildName (String -> String -> QName -> Bool
hasElemName String
"m" String
"sepChr") Maybe Element -> (Element -> Maybe String) -> Maybe String
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
               (QName -> Bool) -> Element -> Maybe String
findAttrBy (String -> String -> QName -> Bool
hasElemName String
"m" String
"val") Maybe String -> (String -> Maybe Char) -> Maybe Char
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
               (\String
c -> if String -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null String
c then (Char -> Maybe Char
forall a. a -> Maybe a
Just Char
' ') else (Char -> Maybe Char
forall a. a -> Maybe a
Just (Char -> Maybe Char) -> Char -> Maybe Char
forall a b. (a -> b) -> a -> b
$ String -> Char
forall a. HasCallStack => [a] -> a
head String
c))
      endChr :: Maybe Char
endChr = Maybe Element
dPr Maybe Element -> (Element -> Maybe Element) -> Maybe Element
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
               (QName -> Bool) -> Element -> Maybe Element
filterChildName (String -> String -> QName -> Bool
hasElemName String
"m" String
"endChr") Maybe Element -> (Element -> Maybe String) -> Maybe String
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
               (QName -> Bool) -> Element -> Maybe String
findAttrBy (String -> String -> QName -> Bool
hasElemName String
"m" String
"val") Maybe String -> (String -> Maybe Char) -> Maybe Char
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
               (\String
c -> if String -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null String
c then (Char -> Maybe Char
forall a. a -> Maybe a
Just Char
' ') else (Char -> Maybe Char
forall a. a -> Maybe a
Just (Char -> Maybe Char) -> Char -> Maybe Char
forall a b. (a -> b) -> a -> b
$ String -> Char
forall a. HasCallStack => [a] -> a
head String
c))
      beg :: Text
beg = Text -> (Char -> Text) -> Maybe Char -> Text
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Text
"(" Char -> Text
T.singleton Maybe Char
begChr
      end :: Text
end = Text -> (Char -> Text) -> Maybe Char -> Text
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Text
")" Char -> Text
T.singleton Maybe Char
endChr
      sep :: Text
sep = Text -> (Char -> Text) -> Maybe Char -> Text
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Text
"|" Char -> Text
T.singleton Maybe Char
sepChr
      exps :: [Either Text Exp]
exps = [Either Text Exp] -> [[Either Text Exp]] -> [Either Text Exp]
forall a. [a] -> [[a]] -> [a]
intercalate [Text -> Either Text Exp
forall a b. a -> Either a b
Left Text
sep] [[Either Text Exp]]
forall {a}. [[Either a Exp]]
inDelimExps
  in
   [Exp] -> Maybe [Exp]
forall a. a -> Maybe a
Just [Text -> Text -> [Either Text Exp] -> Exp
EDelimited Text
beg Text
end [Either Text Exp]
exps]
elemToExps' Element
element | String -> String -> Element -> Bool
isElem String
"m" String
"eqArr" Element
element =
  let expLst :: [[Exp]]
expLst = (Element -> Maybe [Exp]) -> [Element] -> [[Exp]]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe Element -> Maybe [Exp]
elemToBases (Element -> [Element]
elChildren Element
element)
      expLst' :: [[[Exp]]]
expLst' = ([Exp] -> [[Exp]]) -> [[Exp]] -> [[[Exp]]]
forall a b. (a -> b) -> [a] -> [b]
map [Exp] -> [[Exp]]
breakOnAmpersand [[Exp]]
expLst
      cols :: Int
cols = [Int] -> Int
forall a. Ord a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
maximum (([[Exp]] -> Int) -> [[[Exp]]] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map [[Exp]] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [[[Exp]]]
expLst')
      colspecs :: [Alignment]
colspecs = Int -> [Alignment] -> [Alignment]
forall a. Int -> [a] -> [a]
take Int
cols ([Alignment] -> [Alignment]) -> [Alignment] -> [Alignment]
forall a b. (a -> b) -> a -> b
$ [Alignment] -> [Alignment]
forall a. HasCallStack => [a] -> [a]
cycle [Alignment
AlignRight , Alignment
AlignLeft]
  in
   [Exp] -> Maybe [Exp]
forall a. a -> Maybe a
forall (m :: * -> *) a. Monad m => a -> m a
return [[Alignment] -> [[[Exp]]] -> Exp
EArray [Alignment]
colspecs [[[Exp]]]
expLst']
elemToExps' Element
element | String -> String -> Element -> Bool
isElem String
"m" String
"f" Element
element = do
  Element
num <- (QName -> Bool) -> Element -> Maybe Element
filterChildName (String -> String -> QName -> Bool
hasElemName String
"m" String
"num") Element
element
  Element
den <- (QName -> Bool) -> Element -> Maybe Element
filterChildName (String -> String -> QName -> Bool
hasElemName String
"m" String
"den") Element
element
  let barType :: Maybe String
barType = (QName -> Bool) -> Element -> Maybe Element
filterChildName (String -> String -> QName -> Bool
hasElemName String
"m" String
"fPr") Element
element Maybe Element -> (Element -> Maybe Element) -> Maybe Element
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
            (QName -> Bool) -> Element -> Maybe Element
filterChildName (String -> String -> QName -> Bool
hasElemName String
"m" String
"type") Maybe Element -> (Element -> Maybe String) -> Maybe String
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
            (QName -> Bool) -> Element -> Maybe String
findAttrBy (String -> String -> QName -> Bool
hasElemName String
"m" String
"val")
  let numExp :: Exp
numExp = [Exp] -> Exp
EGrouped ([Exp] -> Exp) -> [Exp] -> Exp
forall a b. (a -> b) -> a -> b
$ [[Exp]] -> [Exp]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[Exp]] -> [Exp]) -> [[Exp]] -> [Exp]
forall a b. (a -> b) -> a -> b
$ (Element -> Maybe [Exp]) -> [Element] -> [[Exp]]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (Element -> Maybe [Exp]
elemToExps) (Element -> [Element]
elChildren Element
num)
      denExp :: Exp
denExp = [Exp] -> Exp
EGrouped ([Exp] -> Exp) -> [Exp] -> Exp
forall a b. (a -> b) -> a -> b
$ [[Exp]] -> [Exp]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[Exp]] -> [Exp]) -> [[Exp]] -> [Exp]
forall a b. (a -> b) -> a -> b
$ (Element -> Maybe [Exp]) -> [Element] -> [[Exp]]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (Element -> Maybe [Exp]
elemToExps) (Element -> [Element]
elChildren Element
den)
  case Maybe String
barType of
    Just String
"noBar" -> [Exp] -> Maybe [Exp]
forall a. a -> Maybe a
Just [FractionType -> Exp -> Exp -> Exp
EFraction FractionType
NoLineFrac Exp
numExp Exp
denExp]
    Maybe String
_            -> [Exp] -> Maybe [Exp]
forall a. a -> Maybe a
Just [FractionType -> Exp -> Exp -> Exp
EFraction FractionType
NormalFrac Exp
numExp Exp
denExp]
elemToExps' Element
element | String -> String -> Element -> Bool
isElem String
"m" String
"func" Element
element = do
  Element
fName <- (QName -> Bool) -> Element -> Maybe Element
filterChildName (String -> String -> QName -> Bool
hasElemName String
"m" String
"fName") Element
element
  Exp
baseExp <- (QName -> Bool) -> Element -> Maybe Element
filterChildName (String -> String -> QName -> Bool
hasElemName String
"m" String
"e") Element
element Maybe Element -> (Element -> Maybe Exp) -> Maybe Exp
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
          Element -> Maybe Exp
elemToBase
  let fnameExp :: Exp
fnameExp = case [[Exp]] -> [Exp]
forall a. Monoid a => [a] -> a
mconcat ([[Exp]] -> [Exp]) -> [[Exp]] -> [Exp]
forall a b. (a -> b) -> a -> b
$ (Element -> Maybe [Exp]) -> [Element] -> [[Exp]]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (Element -> Maybe [Exp]
elemToExps') (Element -> [Element]
elChildren Element
fName) of
                   [Exp
x] -> Exp
x
                   [Exp]
xs  -> [Exp] -> Exp
EGrouped [Exp]
xs
  [Exp] -> Maybe [Exp]
forall a. a -> Maybe a
forall (m :: * -> *) a. Monad m => a -> m a
return [Exp
fnameExp, Exp
baseExp]
elemToExps' Element
element | String -> String -> Element -> Bool
isElem String
"m" String
"groupChr" Element
element = do
  let gPr :: Maybe Element
gPr = (QName -> Bool) -> Element -> Maybe Element
filterChildName (String -> String -> QName -> Bool
hasElemName String
"m" String
"groupChrPr") Element
element
      chr :: Maybe String
chr = Maybe Element
gPr Maybe Element -> (Element -> Maybe Element) -> Maybe Element
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
            (QName -> Bool) -> Element -> Maybe Element
filterChildName (String -> String -> QName -> Bool
hasElemName String
"m" String
"chr") Maybe Element -> (Element -> Maybe String) -> Maybe String
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
            (QName -> Bool) -> Element -> Maybe String
findAttrBy (String -> String -> QName -> Bool
hasElemName String
"m" String
"val")
      pos :: Maybe String
pos = Maybe Element
gPr Maybe Element -> (Element -> Maybe Element) -> Maybe Element
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
            (QName -> Bool) -> Element -> Maybe Element
filterChildName (String -> String -> QName -> Bool
hasElemName String
"m" String
"pos") Maybe Element -> (Element -> Maybe String) -> Maybe String
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
            (QName -> Bool) -> Element -> Maybe String
findAttrBy (String -> String -> QName -> Bool
hasElemName String
"m" String
"val")
      justif :: Maybe String
justif = Maybe Element
gPr Maybe Element -> (Element -> Maybe Element) -> Maybe Element
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
               (QName -> Bool) -> Element -> Maybe Element
filterChildName (String -> String -> QName -> Bool
hasElemName String
"m" String
"vertJC") Maybe Element -> (Element -> Maybe String) -> Maybe String
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
               (QName -> Bool) -> Element -> Maybe String
findAttrBy (String -> String -> QName -> Bool
hasElemName String
"m" String
"val")
  Exp
baseExp <- (QName -> Bool) -> Element -> Maybe Element
filterChildName (String -> String -> QName -> Bool
hasElemName String
"m" String
"e") Element
element Maybe Element -> (Element -> Maybe Exp) -> Maybe Exp
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
             Element -> Maybe Exp
elemToBase
  case Maybe String
pos of
    Just String
"top" ->
      let chr' :: Text
chr' = case Maybe String
chr of
            Just (Char
c:String
_) -> Char -> Text
T.singleton Char
c
            Maybe String
_           -> Text
"\65079"   -- default to overbrace
      in
       [Exp] -> Maybe [Exp]
forall a. a -> Maybe a
forall (m :: * -> *) a. Monad m => a -> m a
return ([Exp] -> Maybe [Exp]) -> [Exp] -> Maybe [Exp]
forall a b. (a -> b) -> a -> b
$
         case Maybe String
justif of
           Just String
"top" -> [Bool -> Exp -> Exp -> Exp
EUnder Bool
False (TeXSymbolType -> Text -> Exp
ESymbol TeXSymbolType
TOver Text
chr') Exp
baseExp]
           Maybe String
_ -> [Bool -> Exp -> Exp -> Exp
EOver Bool
False Exp
baseExp (TeXSymbolType -> Text -> Exp
ESymbol TeXSymbolType
TOver Text
chr')]
    Maybe String
_ -> -- bot is default
      let chr' :: Text
chr' = case Maybe String
chr of
            Just (Char
c:String
_) -> Char -> Text
T.singleton Char
c
            Maybe String
_           -> Text
"\65080"   -- default to underbrace
      in
       [Exp] -> Maybe [Exp]
forall a. a -> Maybe a
forall (m :: * -> *) a. Monad m => a -> m a
return ([Exp] -> Maybe [Exp]) -> [Exp] -> Maybe [Exp]
forall a b. (a -> b) -> a -> b
$
         case Maybe String
justif of
           Just String
"top" -> [Bool -> Exp -> Exp -> Exp
EUnder Bool
False Exp
baseExp (TeXSymbolType -> Text -> Exp
ESymbol TeXSymbolType
TUnder Text
chr')]
           Maybe String
_ -> [Bool -> Exp -> Exp -> Exp
EOver Bool
False (TeXSymbolType -> Text -> Exp
ESymbol TeXSymbolType
TUnder Text
chr') Exp
baseExp]
elemToExps' Element
element | String -> String -> Element -> Bool
isElem String
"m" String
"limLow" Element
element = do
  Exp
baseExp <- (QName -> Bool) -> Element -> Maybe Element
filterChildName (String -> String -> QName -> Bool
hasElemName String
"m" String
"e") Element
element
          Maybe Element -> (Element -> Maybe Exp) -> Maybe Exp
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Element -> Maybe Exp
elemToBase
  Exp
limExp <- (QName -> Bool) -> Element -> Maybe Element
filterChildName (String -> String -> QName -> Bool
hasElemName String
"m" String
"lim") Element
element
            Maybe Element -> (Element -> Maybe [Exp]) -> Maybe [Exp]
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (\Element
e -> [Exp] -> Maybe [Exp]
forall a. a -> Maybe a
Just ([Exp] -> Maybe [Exp]) -> [Exp] -> Maybe [Exp]
forall a b. (a -> b) -> a -> b
$ [[Exp]] -> [Exp]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[Exp]] -> [Exp]) -> [[Exp]] -> [Exp]
forall a b. (a -> b) -> a -> b
$ (Element -> Maybe [Exp]) -> [Element] -> [[Exp]]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (Element -> Maybe [Exp]
elemToExps) (Element -> [Element]
elChildren Element
e))
            Maybe [Exp] -> ([Exp] -> Maybe Exp) -> Maybe Exp
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Exp -> Maybe Exp
forall a. a -> Maybe a
forall (m :: * -> *) a. Monad m => a -> m a
return (Exp -> Maybe Exp) -> ([Exp] -> Exp) -> [Exp] -> Maybe Exp
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Exp] -> Exp
EGrouped)
  [Exp] -> Maybe [Exp]
forall a. a -> Maybe a
forall (m :: * -> *) a. Monad m => a -> m a
return [Bool -> Exp -> Exp -> Exp
EUnder Bool
True Exp
baseExp Exp
limExp]
elemToExps' Element
element | String -> String -> Element -> Bool
isElem String
"m" String
"limUpp" Element
element = do
  Exp
baseExp <- (QName -> Bool) -> Element -> Maybe Element
filterChildName (String -> String -> QName -> Bool
hasElemName String
"m" String
"e") Element
element
          Maybe Element -> (Element -> Maybe Exp) -> Maybe Exp
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Element -> Maybe Exp
elemToBase
  Exp
limExp <- (QName -> Bool) -> Element -> Maybe Element
filterChildName (String -> String -> QName -> Bool
hasElemName String
"m" String
"lim") Element
element
            Maybe Element -> (Element -> Maybe [Exp]) -> Maybe [Exp]
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (\Element
e -> [Exp] -> Maybe [Exp]
forall a. a -> Maybe a
Just ([Exp] -> Maybe [Exp]) -> [Exp] -> Maybe [Exp]
forall a b. (a -> b) -> a -> b
$ [[Exp]] -> [Exp]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[Exp]] -> [Exp]) -> [[Exp]] -> [Exp]
forall a b. (a -> b) -> a -> b
$ (Element -> Maybe [Exp]) -> [Element] -> [[Exp]]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (Element -> Maybe [Exp]
elemToExps) (Element -> [Element]
elChildren Element
e))
            Maybe [Exp] -> ([Exp] -> Maybe Exp) -> Maybe Exp
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Exp -> Maybe Exp
forall a. a -> Maybe a
forall (m :: * -> *) a. Monad m => a -> m a
return (Exp -> Maybe Exp) -> ([Exp] -> Exp) -> [Exp] -> Maybe Exp
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Exp] -> Exp
EGrouped)
  [Exp] -> Maybe [Exp]
forall a. a -> Maybe a
forall (m :: * -> *) a. Monad m => a -> m a
return [Bool -> Exp -> Exp -> Exp
EOver Bool
True Exp
baseExp Exp
limExp]
elemToExps' Element
element | String -> String -> Element -> Bool
isElem String
"m" String
"m" Element
element =
  let rows :: [Element]
rows = (QName -> Bool) -> Element -> [Element]
filterChildrenName (String -> String -> QName -> Bool
hasElemName String
"m" String
"mr") Element
element
      rowExps :: [[[Exp]]]
rowExps = (Element -> [[Exp]]) -> [Element] -> [[[Exp]]]
forall a b. (a -> b) -> [a] -> [b]
map
                (\Element
mr -> (Element -> Maybe [Exp]) -> [Element] -> [[Exp]]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe
                        Element -> Maybe [Exp]
elemToBases
                        (Element -> [Element]
elChildren Element
mr))
                [Element]
rows
  in
   [Exp] -> Maybe [Exp]
forall a. a -> Maybe a
forall (m :: * -> *) a. Monad m => a -> m a
return [[Alignment] -> [[[Exp]]] -> Exp
EArray [Alignment
AlignCenter] [[[Exp]]]
rowExps]
elemToExps' Element
element | String -> String -> Element -> Bool
isElem String
"m" String
"nary" Element
element = do
  let naryPr :: Maybe Element
naryPr = (QName -> Bool) -> Element -> Maybe Element
filterChildName (String -> String -> QName -> Bool
hasElemName String
"m" String
"naryPr") Element
element
      naryChr :: Maybe String
naryChr = Maybe Element
naryPr Maybe Element -> (Element -> Maybe Element) -> Maybe Element
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
                (QName -> Bool) -> Element -> Maybe Element
filterChildName (String -> String -> QName -> Bool
hasElemName String
"m" String
"chr") Maybe Element -> (Element -> Maybe String) -> Maybe String
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
                (QName -> Bool) -> Element -> Maybe String
findAttrBy (String -> String -> QName -> Bool
hasElemName String
"m" String
"val")
      opChr :: Text
opChr = case Maybe String
naryChr of
        Just (Char
c:String
_) -> Char -> Text
T.singleton Char
c
        Maybe String
_          -> Text
"\8747"   -- default to integral
      limLoc :: Maybe String
limLoc = Maybe Element
naryPr Maybe Element -> (Element -> Maybe Element) -> Maybe Element
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
               (QName -> Bool) -> Element -> Maybe Element
filterChildName (String -> String -> QName -> Bool
hasElemName String
"m" String
"limLoc") Maybe Element -> (Element -> Maybe String) -> Maybe String
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
               (QName -> Bool) -> Element -> Maybe String
findAttrBy (String -> String -> QName -> Bool
hasElemName String
"m" String
"val")
  [Exp]
subExps <- (QName -> Bool) -> Element -> Maybe Element
filterChildName (String -> String -> QName -> Bool
hasElemName String
"m" String
"sub") Element
element Maybe Element -> (Element -> Maybe [Exp]) -> Maybe [Exp]
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
         (\Element
e -> [Exp] -> Maybe [Exp]
forall a. a -> Maybe a
forall (m :: * -> *) a. Monad m => a -> m a
return ([Exp] -> Maybe [Exp]) -> [Exp] -> Maybe [Exp]
forall a b. (a -> b) -> a -> b
$ [[Exp]] -> [Exp]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[Exp]] -> [Exp]) -> [[Exp]] -> [Exp]
forall a b. (a -> b) -> a -> b
$ (Element -> Maybe [Exp]) -> [Element] -> [[Exp]]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (Element -> Maybe [Exp]
elemToExps) (Element -> [Element]
elChildren Element
e))
  [Exp]
supExps <- (QName -> Bool) -> Element -> Maybe Element
filterChildName (String -> String -> QName -> Bool
hasElemName String
"m" String
"sup") Element
element Maybe Element -> (Element -> Maybe [Exp]) -> Maybe [Exp]
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
         (\Element
e -> [Exp] -> Maybe [Exp]
forall a. a -> Maybe a
forall (m :: * -> *) a. Monad m => a -> m a
return ([Exp] -> Maybe [Exp]) -> [Exp] -> Maybe [Exp]
forall a b. (a -> b) -> a -> b
$ [[Exp]] -> [Exp]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[Exp]] -> [Exp]) -> [[Exp]] -> [Exp]
forall a b. (a -> b) -> a -> b
$ (Element -> Maybe [Exp]) -> [Element] -> [[Exp]]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (Element -> Maybe [Exp]
elemToExps) (Element -> [Element]
elChildren Element
e))
  let baseExp :: [Exp]
baseExp = Maybe Exp -> [Exp]
forall a. Maybe a -> [a]
maybeToList (Maybe Exp -> [Exp]) -> Maybe Exp -> [Exp]
forall a b. (a -> b) -> a -> b
$
        (QName -> Bool) -> Element -> Maybe Element
filterChildName (String -> String -> QName -> Bool
hasElemName String
"m" String
"e") Element
element Maybe Element -> (Element -> Maybe Exp) -> Maybe Exp
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Element -> Maybe Exp
elemToBase
  case Maybe String
limLoc of
    Just String
"undOvr" -> [Exp] -> Maybe [Exp]
forall a. a -> Maybe a
forall (m :: * -> *) a. Monad m => a -> m a
return ([Exp] -> Maybe [Exp]) -> [Exp] -> Maybe [Exp]
forall a b. (a -> b) -> a -> b
$ Bool -> Exp -> Exp -> Exp -> Exp
EUnderover Bool
True
                              (TeXSymbolType -> Text -> Exp
ESymbol TeXSymbolType
Op Text
opChr)
                              ([Exp] -> Exp
EGrouped [Exp]
subExps)
                              ([Exp] -> Exp
EGrouped [Exp]
supExps)
                            Exp -> [Exp] -> [Exp]
forall a. a -> [a] -> [a]
: [Exp]
baseExp
    Maybe String
_             -> [Exp] -> Maybe [Exp]
forall a. a -> Maybe a
forall (m :: * -> *) a. Monad m => a -> m a
return ([Exp] -> Maybe [Exp]) -> [Exp] -> Maybe [Exp]
forall a b. (a -> b) -> a -> b
$ Exp -> Exp -> Exp -> Exp
ESubsup
                              (TeXSymbolType -> Text -> Exp
ESymbol TeXSymbolType
Op Text
opChr)
                              ([Exp] -> Exp
EGrouped [Exp]
subExps)
                              ([Exp] -> Exp
EGrouped [Exp]
supExps)
                            Exp -> [Exp] -> [Exp]
forall a. a -> [a] -> [a]
: [Exp]
baseExp

elemToExps' Element
element | String -> String -> Element -> Bool
isElem String
"m" String
"phant" Element
element = do
  Exp
baseExp <- (QName -> Bool) -> Element -> Maybe Element
filterChildName (String -> String -> QName -> Bool
hasElemName String
"m" String
"e") Element
element Maybe Element -> (Element -> Maybe Exp) -> Maybe Exp
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
             Element -> Maybe Exp
elemToBase
  [Exp] -> Maybe [Exp]
forall a. a -> Maybe a
forall (m :: * -> *) a. Monad m => a -> m a
return [Exp -> Exp
EPhantom Exp
baseExp]
elemToExps' Element
element | String -> String -> Element -> Bool
isElem String
"m" String
"rad" Element
element = do
  [Exp]
degExps <- (QName -> Bool) -> Element -> Maybe Element
filterChildName (String -> String -> QName -> Bool
hasElemName String
"m" String
"deg") Element
element Maybe Element -> (Element -> Maybe [Exp]) -> Maybe [Exp]
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
              (\Element
e -> [Exp] -> Maybe [Exp]
forall a. a -> Maybe a
forall (m :: * -> *) a. Monad m => a -> m a
return ([Exp] -> Maybe [Exp]) -> [Exp] -> Maybe [Exp]
forall a b. (a -> b) -> a -> b
$ [[Exp]] -> [Exp]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[Exp]] -> [Exp]) -> [[Exp]] -> [Exp]
forall a b. (a -> b) -> a -> b
$ (Element -> Maybe [Exp]) -> [Element] -> [[Exp]]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (Element -> Maybe [Exp]
elemToExps) (Element -> [Element]
elChildren Element
e))
  Exp
baseExp <- (QName -> Bool) -> Element -> Maybe Element
filterChildName (String -> String -> QName -> Bool
hasElemName String
"m" String
"e") Element
element Maybe Element -> (Element -> Maybe Exp) -> Maybe Exp
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
             Element -> Maybe Exp
elemToBase
  [Exp] -> Maybe [Exp]
forall a. a -> Maybe a
forall (m :: * -> *) a. Monad m => a -> m a
return ([Exp] -> Maybe [Exp]) -> [Exp] -> Maybe [Exp]
forall a b. (a -> b) -> a -> b
$ case [Exp]
degExps of
    [] -> [Exp -> Exp
ESqrt Exp
baseExp]
    [Exp]
ds -> [Exp -> Exp -> Exp
ERoot ([Exp] -> Exp
EGrouped [Exp]
ds) Exp
baseExp]
elemToExps' Element
element | String -> String -> Element -> Bool
isElem String
"m" String
"sPre" Element
element = do
  [Exp]
subExps <- (QName -> Bool) -> Element -> Maybe Element
filterChildName (String -> String -> QName -> Bool
hasElemName String
"m" String
"sub") Element
element Maybe Element -> (Element -> Maybe [Exp]) -> Maybe [Exp]
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
            (\Element
e -> [Exp] -> Maybe [Exp]
forall a. a -> Maybe a
forall (m :: * -> *) a. Monad m => a -> m a
return ([Exp] -> Maybe [Exp]) -> [Exp] -> Maybe [Exp]
forall a b. (a -> b) -> a -> b
$ [[Exp]] -> [Exp]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[Exp]] -> [Exp]) -> [[Exp]] -> [Exp]
forall a b. (a -> b) -> a -> b
$ (Element -> Maybe [Exp]) -> [Element] -> [[Exp]]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (Element -> Maybe [Exp]
elemToExps) (Element -> [Element]
elChildren Element
e))
  [Exp]
supExps <- (QName -> Bool) -> Element -> Maybe Element
filterChildName (String -> String -> QName -> Bool
hasElemName String
"m" String
"sup") Element
element Maybe Element -> (Element -> Maybe [Exp]) -> Maybe [Exp]
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
            (\Element
e -> [Exp] -> Maybe [Exp]
forall a. a -> Maybe a
forall (m :: * -> *) a. Monad m => a -> m a
return ([Exp] -> Maybe [Exp]) -> [Exp] -> Maybe [Exp]
forall a b. (a -> b) -> a -> b
$ [[Exp]] -> [Exp]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[Exp]] -> [Exp]) -> [[Exp]] -> [Exp]
forall a b. (a -> b) -> a -> b
$ (Element -> Maybe [Exp]) -> [Element] -> [[Exp]]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (Element -> Maybe [Exp]
elemToExps) (Element -> [Element]
elChildren Element
e))
  Exp
baseExp <- (QName -> Bool) -> Element -> Maybe Element
filterChildName (String -> String -> QName -> Bool
hasElemName String
"m" String
"e") Element
element Maybe Element -> (Element -> Maybe Exp) -> Maybe Exp
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
             Element -> Maybe Exp
elemToBase
  [Exp] -> Maybe [Exp]
forall a. a -> Maybe a
forall (m :: * -> *) a. Monad m => a -> m a
return [Exp -> Exp -> Exp -> Exp
ESubsup
          (Text -> Exp
EIdentifier Text
"")
          ([Exp] -> Exp
EGrouped [Exp]
subExps)
          ([Exp] -> Exp
EGrouped [Exp]
supExps)
         , Exp
baseExp]
elemToExps' Element
element | String -> String -> Element -> Bool
isElem String
"m" String
"sSub" Element
element = do
  Exp
baseExp <- (QName -> Bool) -> Element -> Maybe Element
filterChildName (String -> String -> QName -> Bool
hasElemName String
"m" String
"e") Element
element Maybe Element -> (Element -> Maybe Exp) -> Maybe Exp
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
             Element -> Maybe Exp
elemToBase
  [Exp]
subExps <- (QName -> Bool) -> Element -> Maybe Element
filterChildName (String -> String -> QName -> Bool
hasElemName String
"m" String
"sub") Element
element Maybe Element -> (Element -> Maybe [Exp]) -> Maybe [Exp]
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
            (\Element
e -> [Exp] -> Maybe [Exp]
forall a. a -> Maybe a
forall (m :: * -> *) a. Monad m => a -> m a
return ([Exp] -> Maybe [Exp]) -> [Exp] -> Maybe [Exp]
forall a b. (a -> b) -> a -> b
$ [[Exp]] -> [Exp]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[Exp]] -> [Exp]) -> [[Exp]] -> [Exp]
forall a b. (a -> b) -> a -> b
$ (Element -> Maybe [Exp]) -> [Element] -> [[Exp]]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (Element -> Maybe [Exp]
elemToExps) (Element -> [Element]
elChildren Element
e))
  [Exp] -> Maybe [Exp]
forall a. a -> Maybe a
forall (m :: * -> *) a. Monad m => a -> m a
return [Exp -> Exp -> Exp
ESub Exp
baseExp ([Exp] -> Exp
EGrouped [Exp]
subExps)]
elemToExps' Element
element | String -> String -> Element -> Bool
isElem String
"m" String
"sSubSup" Element
element = do
  Exp
baseExp <- (QName -> Bool) -> Element -> Maybe Element
filterChildName (String -> String -> QName -> Bool
hasElemName String
"m" String
"e") Element
element Maybe Element -> (Element -> Maybe Exp) -> Maybe Exp
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
             Element -> Maybe Exp
elemToBase
  [Exp]
subExps <- (QName -> Bool) -> Element -> Maybe Element
filterChildName (String -> String -> QName -> Bool
hasElemName String
"m" String
"sub") Element
element Maybe Element -> (Element -> Maybe [Exp]) -> Maybe [Exp]
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
             (\Element
e -> [Exp] -> Maybe [Exp]
forall a. a -> Maybe a
forall (m :: * -> *) a. Monad m => a -> m a
return ([Exp] -> Maybe [Exp]) -> [Exp] -> Maybe [Exp]
forall a b. (a -> b) -> a -> b
$ [[Exp]] -> [Exp]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[Exp]] -> [Exp]) -> [[Exp]] -> [Exp]
forall a b. (a -> b) -> a -> b
$ (Element -> Maybe [Exp]) -> [Element] -> [[Exp]]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (Element -> Maybe [Exp]
elemToExps) (Element -> [Element]
elChildren Element
e))
  [Exp]
supExps <- (QName -> Bool) -> Element -> Maybe Element
filterChildName (String -> String -> QName -> Bool
hasElemName String
"m" String
"sup") Element
element Maybe Element -> (Element -> Maybe [Exp]) -> Maybe [Exp]
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
             (\Element
e -> [Exp] -> Maybe [Exp]
forall a. a -> Maybe a
forall (m :: * -> *) a. Monad m => a -> m a
return ([Exp] -> Maybe [Exp]) -> [Exp] -> Maybe [Exp]
forall a b. (a -> b) -> a -> b
$ [[Exp]] -> [Exp]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[Exp]] -> [Exp]) -> [[Exp]] -> [Exp]
forall a b. (a -> b) -> a -> b
$ (Element -> Maybe [Exp]) -> [Element] -> [[Exp]]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (Element -> Maybe [Exp]
elemToExps) (Element -> [Element]
elChildren Element
e))
  [Exp] -> Maybe [Exp]
forall a. a -> Maybe a
forall (m :: * -> *) a. Monad m => a -> m a
return [Exp -> Exp -> Exp -> Exp
ESubsup Exp
baseExp ([Exp] -> Exp
EGrouped [Exp]
subExps) ([Exp] -> Exp
EGrouped [Exp]
supExps)]
elemToExps' Element
element | String -> String -> Element -> Bool
isElem String
"m" String
"sSup" Element
element = do
  Exp
baseExp <- (QName -> Bool) -> Element -> Maybe Element
filterChildName (String -> String -> QName -> Bool
hasElemName String
"m" String
"e") Element
element Maybe Element -> (Element -> Maybe Exp) -> Maybe Exp
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
             Element -> Maybe Exp
elemToBase
  [Exp]
supExps <- (QName -> Bool) -> Element -> Maybe Element
filterChildName (String -> String -> QName -> Bool
hasElemName String
"m" String
"sup") Element
element Maybe Element -> (Element -> Maybe [Exp]) -> Maybe [Exp]
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
            (\Element
e -> [Exp] -> Maybe [Exp]
forall a. a -> Maybe a
forall (m :: * -> *) a. Monad m => a -> m a
return ([Exp] -> Maybe [Exp]) -> [Exp] -> Maybe [Exp]
forall a b. (a -> b) -> a -> b
$ [[Exp]] -> [Exp]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[Exp]] -> [Exp]) -> [[Exp]] -> [Exp]
forall a b. (a -> b) -> a -> b
$ (Element -> Maybe [Exp]) -> [Element] -> [[Exp]]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (Element -> Maybe [Exp]
elemToExps) (Element -> [Element]
elChildren Element
e))
  [Exp] -> Maybe [Exp]
forall a. a -> Maybe a
forall (m :: * -> *) a. Monad m => a -> m a
return [Exp -> Exp -> Exp
ESuper Exp
baseExp ([Exp] -> Exp
EGrouped [Exp]
supExps)]
elemToExps' Element
element | String -> String -> Element -> Bool
isElem String
"m" String
"r" Element
element = do
  let mrPr :: Maybe Element
mrPr = (QName -> Bool) -> Element -> Maybe Element
filterChildName (String -> String -> QName -> Bool
hasElemName String
"m" String
"rPr") Element
element
      lit :: Maybe Element
lit = Maybe Element
mrPr Maybe Element -> (Element -> Maybe Element) -> Maybe Element
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (QName -> Bool) -> Element -> Maybe Element
filterChildName (String -> String -> QName -> Bool
hasElemName String
"m" String
"lit")
      nor :: Maybe Element
nor = Maybe Element
mrPr Maybe Element -> (Element -> Maybe Element) -> Maybe Element
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (QName -> Bool) -> Element -> Maybe Element
filterChildName (String -> String -> QName -> Bool
hasElemName String
"m" String
"nor")
      txtSty :: Maybe TextType
txtSty = OMathRunTextStyle -> Maybe TextType
oMathRunTextStyleToTextType (OMathRunTextStyle -> Maybe TextType)
-> OMathRunTextStyle -> Maybe TextType
forall a b. (a -> b) -> a -> b
$ Element -> OMathRunTextStyle
elemToOMathRunTextStyle Element
element
  [OMathRunElem]
mrElems <- Element -> Maybe [OMathRunElem]
elemToOMathRunElems Element
element
  [Exp] -> Maybe [Exp]
forall a. a -> Maybe a
forall (m :: * -> *) a. Monad m => a -> m a
return ([Exp] -> Maybe [Exp]) -> [Exp] -> Maybe [Exp]
forall a b. (a -> b) -> a -> b
$
    if Maybe Element -> Bool
forall a. Maybe a -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null Maybe Element
lit Bool -> Bool -> Bool
&& Maybe Element -> Bool
forall a. Maybe a -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null Maybe Element
nor
       then case Maybe TextType
txtSty of
              Maybe TextType
Nothing ->
                Text -> [Exp]
interpretText (Text -> [Exp]) -> Text -> [Exp]
forall a b. (a -> b) -> a -> b
$ [OMathRunElem] -> Text
oMathRunElemsToText [OMathRunElem]
mrElems
              Just TextType
textSty ->
                [TextType -> [Exp] -> Exp
EStyled TextType
textSty ([Exp] -> Exp) -> [Exp] -> Exp
forall a b. (a -> b) -> a -> b
$ Text -> [Exp]
interpretText (Text -> [Exp]) -> Text -> [Exp]
forall a b. (a -> b) -> a -> b
$ [OMathRunElem] -> Text
oMathRunElemsToText [OMathRunElem]
mrElems]
       else [TextType -> Text -> Exp
EText (TextType -> Maybe TextType -> TextType
forall a. a -> Maybe a -> a
fromMaybe TextType
TextNormal Maybe TextType
txtSty) (Text -> Exp) -> Text -> Exp
forall a b. (a -> b) -> a -> b
$ [OMathRunElem] -> Text
oMathRunElemsToText [OMathRunElem]
mrElems]
elemToExps' Element
_ = Maybe [Exp]
forall a. Maybe a
Nothing

interpretChar :: Char -> Exp
interpretChar :: Char -> Exp
interpretChar Char
c | Char -> Bool
isDigit Char
c = Text -> Exp
ENumber (Text -> Exp) -> Text -> Exp
forall a b. (a -> b) -> a -> b
$ Char -> Text
T.singleton Char
c
interpretChar Char
c = case Char -> TeXSymbolType
getSymbolType Char
c of
  TeXSymbolType
Alpha           -> Text -> Exp
EIdentifier Text
c'
  TeXSymbolType
Ord | Char -> Bool
isDigit Char
c -> Text -> Exp
ENumber Text
c'
      | Bool
otherwise -> case Char -> Maybe Rational
getSpaceWidth Char
c of
                           Just Rational
x  -> Rational -> Exp
ESpace Rational
x
                           Maybe Rational
Nothing -> TeXSymbolType -> Text -> Exp
ESymbol TeXSymbolType
Ord Text
c'
  TeXSymbolType
symType         -> TeXSymbolType -> Text -> Exp
ESymbol TeXSymbolType
symType Text
c'
  where
    c' :: Text
c' = Char -> Text
T.singleton Char
c

interpretText :: T.Text -> [Exp]
interpretText :: Text -> [Exp]
interpretText Text
s
  | Just (Char
c, Text
xs) <- Text -> Maybe (Char, Text)
T.uncons Text
s
  , Text -> Bool
T.null Text
xs = [Char -> Exp
interpretChar Char
c]
  | (Char -> Bool) -> Text -> Bool
T.all Char -> Bool
isDigit Text
s         = [Text -> Exp
ENumber Text
s]
  | Maybe TeX -> Bool
forall a. Maybe a -> Bool
isJust (Exp -> Maybe TeX
getOperator (Text -> Exp
EMathOperator Text
s))
                          = [Text -> Exp
EMathOperator Text
s]
  | Bool
otherwise             = (Char -> Exp) -> String -> [Exp]
forall a b. (a -> b) -> [a] -> [b]
map Char -> Exp
interpretChar (Text -> String
T.unpack Text
s)

-- The char attribute is a hex string
getSymChar :: Element -> T.Text
getSymChar :: Element -> Text
getSymChar Element
element
  | Just String
s <- ShowS
lowerFromPrivate ShowS -> Maybe String -> Maybe String
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe String
getCodepoint
  , Just Font
font <- Maybe Font
getFont =
  case ReadS Char
readLitChar (String
"\\x" String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
s) of
     [(Char
char, String
_)] -> Text -> (Char -> Text) -> Maybe Char -> Text
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Text
"" Char -> Text
T.singleton (Maybe Char -> Text) -> Maybe Char -> Text
forall a b. (a -> b) -> a -> b
$ Font -> Char -> Maybe Char
getUnicode Font
font Char
char
     [(Char, String)]
_ -> Text
""
  where
    getCodepoint :: Maybe String
getCodepoint = (QName -> Bool) -> Element -> Maybe String
findAttrBy (String -> String -> QName -> Bool
hasElemName String
"w" String
"char") Element
element
    getFont :: Maybe Font
getFont = (Text -> Maybe Font
textToFont (Text -> Maybe Font) -> (String -> Text) -> String -> Maybe Font
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack) (String -> Maybe Font) -> Maybe String -> Maybe Font
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< (QName -> Bool) -> Element -> Maybe String
findAttrBy (String -> String -> QName -> Bool
hasElemName String
"w" String
"font") Element
element
    lowerFromPrivate :: ShowS
lowerFromPrivate (Char
'F':String
xs) = Char
'0'Char -> ShowS
forall a. a -> [a] -> [a]
:String
xs
    lowerFromPrivate String
xs = String
xs
getSymChar Element
_ = Text
""