{-# 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)
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 <- forall s. XmlSource s => s -> Maybe Element
parseXMLDoc Text
s =
  case Element -> Maybe [Exp]
elemToOMML Element
e of
    Just [Exp]
exs -> forall a b. b -> Either a b
Right forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map Exp -> Exp
fixTree forall a b. (a -> b) -> a -> b
$ [Exp] -> [Exp]
unGroup [Exp]
exs
    Maybe [Exp]
Nothing   -> forall a b. a -> Either a b
Left Text
"xml file was not an <m:oMathPara> or <m:oMath> element."
readOMML Text
_ = 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 = forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe Element -> Maybe [Exp]
elemToOMML (Element -> [Element]
elChildren Element
element)
  forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map (\[Exp]
l -> if forall (t :: * -> *) a. Foldable t => t a -> Int
length [Exp]
l forall a. Eq a => a -> a -> Bool
== Int
1 then (forall a. [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 =
  forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat forall a b. (a -> b) -> a -> b
$ forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe Element -> Maybe [Exp]
elemToExps forall a b. (a -> b) -> a -> b
$ [Element] -> [Element]
unwrapWTags forall a b. (a -> b) -> a -> b
$ Element -> [Element]
elChildren Element
element
elemToOMML Element
_ = 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 = 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 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 = 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) forall a. Eq a => a -> a -> Bool
== String
name Bool -> Bool -> Bool
&&
   String
qp 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 = forall a. a -> Maybe a -> a
fromMaybe String
"" (QName -> Maybe String
qPrefix QName
qn)
  in
   QName -> String
qName QName
qn forall a. Eq a => a -> a -> Bool
== String
name Bool -> Bool -> Bool
&&
   String
qp       forall a. Eq a => a -> a -> Bool
== String
prefix

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

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

data OMathTextStyle = OPlain
                    | OBold
                    | OItalic
                    | OBoldItalic
                    deriving (Int -> OMathTextStyle -> ShowS
[OMathTextStyle] -> ShowS
OMathTextStyle -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [OMathTextStyle] -> ShowS
$cshowList :: [OMathTextStyle] -> ShowS
show :: OMathTextStyle -> String
$cshow :: OMathTextStyle -> String
showsPrec :: Int -> OMathTextStyle -> ShowS
$cshowsPrec :: Int -> OMathTextStyle -> ShowS
Show, OMathTextStyle -> OMathTextStyle -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: OMathTextStyle -> OMathTextStyle -> Bool
$c/= :: OMathTextStyle -> OMathTextStyle -> Bool
== :: OMathTextStyle -> OMathTextStyle -> Bool
$c== :: 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
  forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ case [Exp]
bs of
    (Exp
e : []) -> Exp
e
    [Exp]
exps     -> [Exp] -> Exp
EGrouped [Exp]
exps
elemToBase Element
_ = 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 =
  forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat forall a b. (a -> b) -> a -> b
$ forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe Element -> Maybe [Exp]
elemToExps' (Element -> [Element]
elChildren Element
element)
elemToBases Element
_ = forall a. Maybe a
Nothing


breakOnAmpersand :: [Exp] -> [[Exp]]
breakOnAmpersand :: [Exp] -> [[Exp]]
breakOnAmpersand = 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 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"         -> forall a. a -> Maybe a
Just OMathTextScript
ORoman
            Just String
"script"        -> forall a. a -> Maybe a
Just OMathTextScript
OScript
            Just String
"fraktur"       -> forall a. a -> Maybe a
Just OMathTextScript
OFraktur
            Just String
"double-struck" -> forall a. a -> Maybe a
Just OMathTextScript
ODoubleStruck
            Just String
"sans-serif"    -> forall a. a -> Maybe a
Just OMathTextScript
OSansSerif
            Just String
"monospace"     -> forall a. a -> Maybe a
Just OMathTextScript
OMonospace
            Maybe String
_                    -> 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 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"             -> forall a. a -> Maybe a
Just OMathTextStyle
OPlain
            Just String
"b"             -> forall a. a -> Maybe a
Just OMathTextStyle
OBold
            Just String
"i"             -> forall a. a -> Maybe a
Just OMathTextStyle
OItalic
            Just String
"bi"            -> forall a. a -> Maybe a
Just OMathTextStyle
OBoldItalic
            Maybe String
_                    -> 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 = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Text -> OMathRunElem
TextRun forall a b. (a -> b) -> a -> b
$ String -> Text
T.pack forall a b. (a -> b) -> a -> b
$ Element -> String
strContent Element
element
  | String -> String -> Element -> Bool
isElem String
"w" String
"br" Element
element = forall a. a -> Maybe a
Just OMathRunElem
LnBrk
  | String -> String -> Element -> Bool
isElem String
"w" String
"tab" Element
element = forall a. a -> Maybe a
Just OMathRunElem
Tab
  | String -> String -> Element -> Bool
isElem String
"w" String
"sym" Element
element = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Text -> OMathRunElem
TextRun forall a b. (a -> b) -> a -> b
$ Element -> Text
getSymChar Element
element
  | Bool
otherwise = 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 =
      forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (Element -> Maybe OMathRunElem
elemToOMathRunElem) (Element -> [Element]
elChildren Element
element)
elemToOMathRunElems Element
_ = 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 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map OMathRunElem -> Text
oMathRunElemToText

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

elemToExps :: Element -> Maybe [Exp]
elemToExps :: Element -> Maybe [Exp]
elemToExps Element
element = [Exp] -> [Exp]
unGroup 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 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") 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") forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
            forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. [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 forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
             Element -> Maybe Exp
elemToBase
  forall (m :: * -> *) a. Monad m => a -> m a
return 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 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") 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 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" -> 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
_          -> 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 forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
             Element -> Maybe Exp
elemToBase
  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 forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
             Element -> Maybe Exp
elemToBase
  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  = forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe
                  Element -> Maybe [Exp]
elemToBases
                  (Element -> [Element]
elChildren Element
element)
      inDelimExps :: [[Either a Exp]]
inDelimExps = forall a b. (a -> b) -> [a] -> [b]
map (forall a b. (a -> b) -> [a] -> [b]
map 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 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") 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") forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
               (\String
c -> if forall (t :: * -> *) a. Foldable t => t a -> Bool
null String
c then (forall a. a -> Maybe a
Just Char
' ') else (forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall a. [a] -> a
head String
c))
      sepChr :: Maybe Char
sepChr = Maybe Element
dPr 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") 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") forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
               (\String
c -> if forall (t :: * -> *) a. Foldable t => t a -> Bool
null String
c then (forall a. a -> Maybe a
Just Char
' ') else (forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall a. [a] -> a
head String
c))
      endChr :: Maybe Char
endChr = Maybe Element
dPr 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") 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") forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
               (\String
c -> if forall (t :: * -> *) a. Foldable t => t a -> Bool
null String
c then (forall a. a -> Maybe a
Just Char
' ') else (forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall a. [a] -> a
head String
c))
      beg :: Text
beg = forall b a. b -> (a -> b) -> Maybe a -> b
maybe Text
"(" Char -> Text
T.singleton Maybe Char
begChr
      end :: Text
end = forall b a. b -> (a -> b) -> Maybe a -> b
maybe Text
")" Char -> Text
T.singleton Maybe Char
endChr
      sep :: Text
sep = forall b a. b -> (a -> b) -> Maybe a -> b
maybe Text
"|" Char -> Text
T.singleton Maybe Char
sepChr
      exps :: [Either Text Exp]
exps = forall a. [a] -> [[a]] -> [a]
intercalate [forall a b. a -> Either a b
Left Text
sep] forall {a}. [[Either a Exp]]
inDelimExps
  in
   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 = forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe Element -> Maybe [Exp]
elemToBases (Element -> [Element]
elChildren Element
element)
      expLst' :: [[[Exp]]]
expLst' = forall a b. (a -> b) -> [a] -> [b]
map [Exp] -> [[Exp]]
breakOnAmpersand [[Exp]]
expLst
      cols :: Int
cols = forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
maximum (forall a b. (a -> b) -> [a] -> [b]
map forall (t :: * -> *) a. Foldable t => t a -> Int
length [[[Exp]]]
expLst')
      colspecs :: [Alignment]
colspecs = forall a. Int -> [a] -> [a]
take Int
cols forall a b. (a -> b) -> a -> b
$ forall a. [a] -> [a]
cycle [Alignment
AlignRight , Alignment
AlignLeft]
  in
   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 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") 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 forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat forall a b. (a -> b) -> a -> b
$ forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (Element -> Maybe [Exp]
elemToExps) (Element -> [Element]
elChildren Element
num)
      denExp :: Exp
denExp = [Exp] -> Exp
EGrouped forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat forall a b. (a -> b) -> a -> b
$ 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" -> forall a. a -> Maybe a
Just [FractionType -> Exp -> Exp -> Exp
EFraction FractionType
NoLineFrac Exp
numExp Exp
denExp]
    Maybe String
_            -> 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 forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
          Element -> Maybe Exp
elemToBase
  let fnameExp :: Exp
fnameExp = case forall a. Monoid a => [a] -> a
mconcat forall a b. (a -> b) -> a -> b
$ 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
  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 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") 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 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") 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 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") 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 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
       forall (m :: * -> *) a. Monad m => a -> m a
return 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
       forall (m :: * -> *) a. Monad m => a -> m a
return 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
          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
            forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (\Element
e -> forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat forall a b. (a -> b) -> a -> b
$ forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (Element -> Maybe [Exp]
elemToExps) (Element -> [Element]
elChildren Element
e))
            forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Exp] -> Exp
EGrouped)
  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
          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
            forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (\Element
e -> forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat forall a b. (a -> b) -> a -> b
$ forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (Element -> Maybe [Exp]
elemToExps) (Element -> [Element]
elChildren Element
e))
            forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Exp] -> Exp
EGrouped)
  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 = forall a b. (a -> b) -> [a] -> [b]
map
                (\Element
mr -> forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe
                        Element -> Maybe [Exp]
elemToBases
                        (Element -> [Element]
elChildren Element
mr))
                [Element]
rows
  in
   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 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") 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 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") 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 forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
         (\Element
e -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat forall a b. (a -> b) -> a -> b
$ 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 forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
         (\Element
e -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat forall a b. (a -> b) -> a -> b
$ 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 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" -> forall (m :: * -> *) a. Monad m => a -> m a
return [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
baseExp]
    Maybe String
_             -> forall (m :: * -> *) a. Monad m => a -> m a
return [Exp -> Exp -> Exp -> Exp
ESubsup
                              (TeXSymbolType -> Text -> Exp
ESymbol TeXSymbolType
Op Text
opChr)
                              ([Exp] -> Exp
EGrouped [Exp]
subExps)
                              ([Exp] -> Exp
EGrouped [Exp]
supExps)
                            , 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 forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
             Element -> Maybe Exp
elemToBase
  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 forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
              (\Element
e -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat forall a b. (a -> b) -> a -> b
$ 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 forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
             Element -> Maybe Exp
elemToBase
  forall (m :: * -> *) a. Monad m => a -> m a
return 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 forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
            (\Element
e -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat forall a b. (a -> b) -> a -> b
$ 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 forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
            (\Element
e -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat forall a b. (a -> b) -> a -> b
$ 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 forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
             Element -> Maybe Exp
elemToBase
  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 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 forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
            (\Element
e -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat forall a b. (a -> b) -> a -> b
$ forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (Element -> Maybe [Exp]
elemToExps) (Element -> [Element]
elChildren Element
e))
  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 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 forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
             (\Element
e -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat forall a b. (a -> b) -> a -> b
$ 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 forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
             (\Element
e -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat forall a b. (a -> b) -> a -> b
$ forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (Element -> Maybe [Exp]
elemToExps) (Element -> [Element]
elChildren Element
e))
  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 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 forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
            (\Element
e -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat forall a b. (a -> b) -> a -> b
$ forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (Element -> Maybe [Exp]
elemToExps) (Element -> [Element]
elChildren Element
e))
  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 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 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 forall a b. (a -> b) -> a -> b
$ Element -> OMathRunTextStyle
elemToOMathRunTextStyle Element
element
  [OMathRunElem]
mrElems <- Element -> Maybe [OMathRunElem]
elemToOMathRunElems Element
element
  forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$
    if forall (t :: * -> *) a. Foldable t => t a -> Bool
null Maybe Element
lit Bool -> Bool -> 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 forall a b. (a -> b) -> a -> b
$ [OMathRunElem] -> Text
oMathRunElemsToText [OMathRunElem]
mrElems
              Just TextType
textSty ->
                [TextType -> [Exp] -> Exp
EStyled TextType
textSty forall a b. (a -> b) -> a -> b
$ Text -> [Exp]
interpretText forall a b. (a -> b) -> a -> b
$ [OMathRunElem] -> Text
oMathRunElemsToText [OMathRunElem]
mrElems]
       else [TextType -> Text -> Exp
EText (forall a. a -> Maybe a -> a
fromMaybe TextType
TextNormal Maybe TextType
txtSty) forall a b. (a -> b) -> a -> b
$ [OMathRunElem] -> Text
oMathRunElemsToText [OMathRunElem]
mrElems]
elemToExps' Element
_ = forall a. Maybe a
Nothing

interpretChar :: Char -> Exp
interpretChar :: Char -> Exp
interpretChar Char
c | Char -> Bool
isDigit Char
c = Text -> Exp
ENumber 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]
  | forall a. Maybe a -> Bool
isJust (Exp -> Maybe TeX
getOperator (Text -> Exp
EMathOperator Text
s))
                          = [Text -> Exp
EMathOperator Text
s]
  | Bool
otherwise             = 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 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" forall a. [a] -> [a] -> [a]
++ String
s) of
     [(Char
char, String
_)] -> forall b a. b -> (a -> b) -> Maybe a -> b
maybe Text
"" Char -> Text
T.singleton 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 forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack) 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'forall a. a -> [a] -> [a]
:String
xs
    lowerFromPrivate String
xs = String
xs
getSymChar Element
_ = Text
""