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

    {-
    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 (\case
                       [Exp
    x] -> Exp
    x
                       [Exp]
    xs -> [Exp] -> Exp
    EGrouped [Exp]
    xs) [[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
    $ [Element] -> [Exp]
    elemsToExps ([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 -> String -> String
    [OMathRunElem] -> String -> String
    OMathRunElem -> String
    (Int -> OMathRunElem -> String -> String)
    -> (OMathRunElem -> String)
    -> ([OMathRunElem] -> String -> String)
    -> Show OMathRunElem
    forall a.
    (Int -> a -> String -> String)
    -> (a -> String) -> ([a] -> String -> String) -> Show a
    $cshowsPrec :: Int -> OMathRunElem -> String -> String
    showsPrec :: Int -> OMathRunElem -> String -> String
    $cshow :: OMathRunElem -> String
    show :: OMathRunElem -> String
    $cshowList :: [OMathRunElem] -> String -> String
    showList :: [OMathRunElem] -> String -> String
    Show

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

    data OMathTextScript = ORoman
                         | OScript
                         | OFraktur
                         | ODoubleStruck
                         | OSansSerif
                         | OMonospace
                         deriving (Int -> OMathTextScript -> String -> String
    [OMathTextScript] -> String -> String
    OMathTextScript -> String
    (Int -> OMathTextScript -> String -> String)
    -> (OMathTextScript -> String)
    -> ([OMathTextScript] -> String -> String)
    -> Show OMathTextScript
    forall a.
    (Int -> a -> String -> String)
    -> (a -> String) -> ([a] -> String -> String) -> Show a
    $cshowsPrec :: Int -> OMathTextScript -> String -> String
    showsPrec :: Int -> OMathTextScript -> String -> String
    $cshow :: OMathTextScript -> String
    show :: OMathTextScript -> String
    $cshowList :: [OMathTextScript] -> String -> String
    showList :: [OMathTextScript] -> String -> String
    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 -> String -> String
    [OMathTextStyle] -> String -> String
    OMathTextStyle -> String
    (Int -> OMathTextStyle -> String -> String)
    -> (OMathTextStyle -> String)
    -> ([OMathTextStyle] -> String -> String)
    -> Show OMathTextStyle
    forall a.
    (Int -> a -> String -> String)
    -> (a -> String) -> ([a] -> String -> String) -> Show a
    $cshowsPrec :: Int -> OMathTextStyle -> String -> String
    showsPrec :: Int -> OMathTextStyle -> String -> String
    $cshow :: OMathTextStyle -> String
    show :: OMathTextStyle -> String
    $cshowList :: [OMathTextStyle] -> String -> String
    showList :: [OMathTextStyle] -> String -> String
    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)

    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

    -- merge adjacent Exps with same style; see jgm/pandoc#10560:
    mergeExps :: [Exp] -> [Exp]
    mergeExps :: [Exp] -> [Exp]
    mergeExps [] = []
    mergeExps (EStyled TextType
    tt [Exp]
    xs : EStyled TextType
    tt' [Exp]
    xs' : [Exp]
    rest)
      | TextType
    tt TextType -> TextType -> Bool
    forall a. Eq a => a -> a -> Bool
    == TextType
    tt' = [Exp] -> [Exp]
    mergeExps (TextType -> [Exp] -> Exp
    EStyled TextType
    tt ([Exp]
    xs [Exp] -> [Exp] -> [Exp]
    forall a. Semigroup a => a -> a -> a
    <> [Exp]
    xs') Exp -> [Exp] -> [Exp]
    forall a. a -> [a] -> [a]
    : [Exp]
    rest)
    mergeExps (EText TextType
    tt Text
    x : EText TextType
    tt' Text
    x' : [Exp]
    rest)
      | TextType
    tt TextType -> TextType -> Bool
    forall a. Eq a => a -> a -> Bool
    == TextType
    tt' = [Exp] -> [Exp]
    mergeExps (TextType -> Text -> Exp
    EText TextType
    tt (Text
    x Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    x') Exp -> [Exp] -> [Exp]
    forall a. a -> [a] -> [a]
    : [Exp]
    rest)
    mergeExps (Exp
    x:[Exp]
    xs) = Exp
    x Exp -> [Exp] -> [Exp]
    forall a. a -> [a] -> [a]
    : [Exp] -> [Exp]
    mergeExps [Exp]
    xs

    elemsToExps :: [Element] -> [Exp]
    elemsToExps :: [Element] -> [Exp]
    elemsToExps = [Exp] -> [Exp]
    mergeExps ([Exp] -> [Exp]) -> ([Element] -> [Exp]) -> [Element] -> [Exp]
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . [[Exp]] -> [Exp]
    forall (t :: * -> *) a. Foldable t => t [a] -> [a]
    concat ([[Exp]] -> [Exp]) -> ([Element] -> [[Exp]]) -> [Element] -> [Exp]
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . (Element -> Maybe [Exp]) -> [Element] -> [[Exp]]
    forall a b. (a -> Maybe b) -> [a] -> [b]
    mapMaybe Element -> Maybe [Exp]
    elemToExps

    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

    elemToBase :: Element -> Maybe Exp
    elemToBase :: Element -> Maybe Exp
    elemToBase Element
    element | String -> String -> Element -> Bool
    isElem String
    "m" String
    "e" Element
    element = do
      bs <- Element -> Maybe [Exp]
    elemToBases Element
    element
      return $ case 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

    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 String
    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")
          chr' :: Text
    chr' = Text -> (String -> Text) -> Maybe String -> Text
    forall b a. b -> (a -> b) -> Maybe a -> b
    maybe Text
    "\x302" String -> Text
    T.pack Maybe String
    chr  -- default to wide hat
      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
      return $ [EOver False baseExp (ESymbol Accent 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")
      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 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
      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
      return [baseExp]
    elemToExps' Element
    element | String -> String -> Element -> Bool
    isElem String
    "m" String
    "borderBox" Element
    element = do
      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
      return [EBoxed 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
          headOrSpace :: String -> Maybe Char
    headOrSpace [] = Char -> Maybe Char
    forall a. a -> Maybe a
    Just Char
    ' '
          headOrSpace (Char
    c:String
    _) = Char -> Maybe Char
    forall a. a -> Maybe a
    Just Char
    c
          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 -> Maybe Char
    headOrSpace
          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 -> Maybe Char
    headOrSpace
          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 -> Maybe Char
    headOrSpace
          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
      num <- (QName -> Bool) -> Element -> Maybe Element
    filterChildName (String -> String -> QName -> Bool
    hasElemName String
    "m" String
    "num") Element
    element
      den <- filterChildName (hasElemName "m" "den") element
      let 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] -> Exp
    EGrouped ([Exp] -> Exp) -> [Exp] -> Exp
    forall a b. (a -> b) -> a -> b
    $ [Element] -> [Exp]
    elemsToExps (Element -> [Element]
    elChildren Element
    num)
          denExp = [Exp] -> Exp
    EGrouped ([Exp] -> Exp) -> [Exp] -> Exp
    forall a b. (a -> b) -> a -> b
    $ [Element] -> [Exp]
    elemsToExps (Element -> [Element]
    elChildren Element
    den)
      case 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
      fName <- (QName -> Bool) -> Element -> Maybe Element
    filterChildName (String -> String -> QName -> Bool
    hasElemName String
    "m" String
    "fName") Element
    element
      baseExp <- filterChildName (hasElemName "m" "e") element >>=
              elemToBase
      let 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
      return [fnameExp, 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")
      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 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
      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
      limExp <- filterChildName (hasElemName "m" "lim") element
                >>= (\Element
    e -> [Exp] -> Maybe [Exp]
    forall a. a -> Maybe a
    Just ([Exp] -> Maybe [Exp]) -> [Exp] -> Maybe [Exp]
    forall a b. (a -> b) -> a -> b
    $ [Element] -> [Exp]
    elemsToExps (Element -> [Element]
    elChildren Element
    e))
                >>= (return . EGrouped)
      return [EUnder True baseExp limExp]
    elemToExps' Element
    element | String -> String -> Element -> Bool
    isElem String
    "m" String
    "limUpp" Element
    element = do
      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
      limExp <- filterChildName (hasElemName "m" "lim") element
                >>= (\Element
    e -> [Exp] -> Maybe [Exp]
    forall a. a -> Maybe a
    Just ([Exp] -> Maybe [Exp]) -> [Exp] -> Maybe [Exp]
    forall a b. (a -> b) -> a -> b
    $ [Element] -> [Exp]
    elemsToExps (Element -> [Element]
    elChildren Element
    e))
                >>= (return . EGrouped)
      return [EOver True baseExp 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")
      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
    >>=
             [Exp] -> Maybe [Exp]
    forall a. a -> Maybe a
    forall (m :: * -> *) a. Monad m => a -> m a
    return ([Exp] -> Maybe [Exp])
    -> (Element -> [Exp]) -> Element -> Maybe [Exp]
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . [Element] -> [Exp]
    elemsToExps ([Element] -> [Exp]) -> (Element -> [Element]) -> Element -> [Exp]
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Element -> [Element]
    elChildren
      supExps <- filterChildName (hasElemName "m" "sup") element >>=
             return . elemsToExps . elChildren
      let 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 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
      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
      return [EPhantom baseExp]
    elemToExps' Element
    element | String -> String -> Element -> Bool
    isElem String
    "m" String
    "rad" Element
    element = do
      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
    >>=
                  [Exp] -> Maybe [Exp]
    forall a. a -> Maybe a
    forall (m :: * -> *) a. Monad m => a -> m a
    return ([Exp] -> Maybe [Exp])
    -> (Element -> [Exp]) -> Element -> Maybe [Exp]
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . [Element] -> [Exp]
    elemsToExps ([Element] -> [Exp]) -> (Element -> [Element]) -> Element -> [Exp]
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Element -> [Element]
    elChildren
      baseExp <- filterChildName (hasElemName "m" "e") element >>=
                 elemToBase
      return $ case 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
      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
    >>=
                [Exp] -> Maybe [Exp]
    forall a. a -> Maybe a
    forall (m :: * -> *) a. Monad m => a -> m a
    return ([Exp] -> Maybe [Exp])
    -> (Element -> [Exp]) -> Element -> Maybe [Exp]
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . [Element] -> [Exp]
    elemsToExps ([Element] -> [Exp]) -> (Element -> [Element]) -> Element -> [Exp]
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Element -> [Element]
    elChildren
      supExps <- filterChildName (hasElemName "m" "sup") element >>=
                return . elemsToExps . elChildren
      baseExp <- filterChildName (hasElemName "m" "e") element >>=
                 elemToBase
      return [ESubsup
              (EIdentifier "")
              (EGrouped subExps)
              (EGrouped supExps)
             , baseExp]
    elemToExps' Element
    element | String -> String -> Element -> Bool
    isElem String
    "m" String
    "sSub" Element
    element = do
      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
      subExps <- filterChildName (hasElemName "m" "sub") element >>=
                return . elemsToExps . elChildren
      return [ESub baseExp (EGrouped subExps)]
    elemToExps' Element
    element | String -> String -> Element -> Bool
    isElem String
    "m" String
    "sSubSup" Element
    element = do
      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
      subExps <- filterChildName (hasElemName "m" "sub") element >>=
                 return . elemsToExps . elChildren
      supExps <- filterChildName (hasElemName "m" "sup") element >>=
                 return . elemsToExps . elChildren
      return [ESubsup baseExp (EGrouped subExps) (EGrouped supExps)]
    elemToExps' Element
    element | String -> String -> Element -> Bool
    isElem String
    "m" String
    "sSup" Element
    element = do
      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
      supExps <- filterChildName (hasElemName "m" "sup") element >>=
                return . elemsToExps . elChildren
      return [ESuper baseExp (EGrouped 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
      mrElems <- Element -> Maybe [OMathRunElem]
    elemToOMathRunElems Element
    element
      return $
        if null lit && null nor
           then case 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 [EText (fromMaybe TextNormal txtSty) $ oMathRunElemsToText 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 <- String -> String
    lowerFromPrivate (String -> String) -> 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 -> String -> String
    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 :: String -> String
    lowerFromPrivate (Char
    'F':String
    xs) = Char
    '0'Char -> String -> String
    forall a. a -> [a] -> [a]
    :String
    xs
        lowerFromPrivate String
    xs = String
    xs
    getSymChar Element
    _ = Text
    ""