@@ -27,12 +27,10 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28Conversion of TeX math to a list of 'Pandoc' inline elements.
28Conversion of TeX math to a list of 'Pandoc' inline elements.
30−module Text.Pandoc.Readers.TeXMath (
30+module Text.Pandoc.Readers.TeXMath ( readTeXMath ) where
34−import Text.ParserCombinators.Parsec
35import Text.Pandoc.Definition
32import Text.Pandoc.Definition
33+import Text.TeXMath.Types
36import Text.TeXMath.Parser
34import Text.TeXMath.Parser
38-- | Converts a raw TeX math formula to a list of 'Pandoc' inlines.
36-- | Converts a raw TeX math formula to a list of 'Pandoc' inlines.
@@ -40,16 +38,17 @@ import Text.TeXMath.Parser
40-- can't be converted.
38-- can't be converted.
41readTeXMath :: String -- ^ String to parse (assumes @'\n'@ line endings)
39readTeXMath :: String -- ^ String to parse (assumes @'\n'@ line endings)
42 -> [Inline]
40 -> [Inline]
43−readTeXMath inp = case readTeXMath' inp of
41+readTeXMath inp = case texMathToPandoc inp of
44− Nothing -> [Str ("$" ++ inp ++ "$")]
42+ Left _ -> [Str ("$" ++ inp ++ "$")]
45− Just res -> res
43+ Right res -> res
47−-- | Like 'readTeXMath', but without the default.
45+texMathToPandoc :: String -> Either String [Inline]
48−readTeXMath' :: String -- ^ String to parse (assumes @'\n'@ line endings)
46+texMathToPandoc inp = inp `seq`
49− -> Maybe [Inline]
47+ case parseFormula inp of
50−readTeXMath' inp = case parse formula "formula" inp of
48+ Left err -> Left err
51− Left _ -> Just [Str inp]
49+ Right exps -> case expsToInlines exps of
52− Right exps -> expsToInlines exps
50+ Nothing -> Left "Formula too complex for [Inline]"
54expsToInlines :: [Exp] -> Maybe [Inline]
53expsToInlines :: [Exp] -> Maybe [Inline]
55expsToInlines xs = do
54expsToInlines xs = do
@@ -111,3 +110,4 @@ expToInlines (EOver (EGrouped [EIdentifier [c]]) (ESymbol Accent [accent])) =
111 _ -> Nothing
110 _ -> Nothing
112expToInlines _ = Nothing
111expToInlines _ = Nothing