@@ -28,208 +28,64 @@ 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.
30module Text.Pandoc.Readers.TeXMath (
30module Text.Pandoc.Readers.TeXMath (
31− readTeXMath
31+ readTeXMath
34import Text.ParserCombinators.Parsec
34import Text.ParserCombinators.Parsec
35import Text.Pandoc.Definition
35import Text.Pandoc.Definition
36+import Text.TeXMath.Parser
37−-- | Converts a string of raw TeX math to a list of 'Pandoc' inlines.
38+-- | Converts a raw TeX math formula to a list of 'Pandoc' inlines.
39+-- Defaults to raw formula between @$@ characters if entire formula
40+-- can't be converted.
38readTeXMath :: String -- ^ String to parse (assumes @'\n'@ line endings)
41readTeXMath :: String -- ^ String to parse (assumes @'\n'@ line endings)
39 -> [Inline]
42 -> [Inline]
40−readTeXMath inp = case parse teXMath ("formula: " ++ inp) inp of
43+readTeXMath inp = case readTeXMath' inp of
41− Left _ -> [Str inp] -- if unparseable, just include original
44+ Nothing -> [Str ("$" ++ inp ++ "$")]
42− Right res -> res
45+ Just res -> res
44−teXMath :: GenParser Char st [Inline]
47+-- | Like 'readTeXMath', but without the default.
45−teXMath = manyTill mathPart eof >>= return . concat
48+readTeXMath' :: String -- ^ String to parse (assumes @'\n'@ line endings)
47−mathPart :: GenParser Char st [Inline]
50+readTeXMath' inp = case parse formula "formula" inp of
48−mathPart = whitespace <|> superscript <|> subscript <|> symbol <|>
51+ Left _ -> Just [Str inp]
49− argument <|> digits <|> letters <|> misc
52+ Right exps -> expsToInlines exps
51−whitespace :: GenParser Char st [Inline]
54+expsToInlines :: [Exp] -> Maybe [Inline]
52−whitespace = many1 space >> return []
55+expsToInlines xs = do
53−
56+ res <- mapM expToInlines xs
54−symbol :: GenParser Char st [Inline]
57+ return (concat res)
56− char '\\'
59+expToInlines :: Exp -> Maybe [Inline]
57− res <- many1 letter
60+expToInlines (ENumber s) = Just [Str s]
58− case lookup res teXsymbols of
61+expToInlines (EIdentifier s) = Just [Emph [Str s]]
59− Just m -> return [Str m]
62+expToInlines (EMathOperator s) = Just [Str s]
60− Nothing -> return [Str $ "\\" ++ res]
63+expToInlines (ESymbol t s) = Just $ addSpace t (Str s)
61−
64+ where addSpace Op x = [x, thinspace]
62−argument :: GenParser Char st [Inline]
65+ addSpace Bin x = [medspace, x, medspace]
63−argument = try $ do
66+ addSpace Rel x = [widespace, x, widespace]
64− char '{'
67+ addSpace Pun x = [x, thinspace]
65− res <- many mathPart
68+ addSpace _ x = [x]
66− char '}'
69+ thinspace = Str "\x2006"
67− return $ if null res
70+ medspace = Str "\x2005"
68− then [Str " "]
71+ widespace = Str "\x2004"
69− else [Str "{"] ++ concat res ++ [Str "}"]
72+expToInlines (EStretchy x) = expToInlines x
70−
73+expToInlines (EGrouped xs) = expsToInlines xs
71−digits :: GenParser Char st [Inline]
74+expToInlines (ESpace _) = Just [Str " "] -- variable widths not supported
72−digits = do
75+expToInlines (EBinary _ _ _) = Nothing
73− res <- many1 digit
76+expToInlines (ESub x y) = do
74− return [Str res]
77+ x' <- expToInlines x
75−
78+ y' <- expToInlines y
76−letters :: GenParser Char st [Inline]
79+ return $ x' ++ [Subscript y']
77−letters = do
80+expToInlines (ESuper x y) = do
78− res <- many1 letter
81+ x' <- expToInlines x
79− return [Emph [Str res]]
82+ y' <- expToInlines y
80−
83+ return $ x' ++ [Superscript y']
81−misc :: GenParser Char st [Inline]
84+expToInlines (ESubsup x y z) = do
82−misc = do
85+ x' <- expToInlines x
83− res <- noneOf "}"
86+ y' <- expToInlines y
84− return [Str [res]]
87+ z' <- expToInlines z
85−
88+ return $ x' ++ [Subscript y'] ++ [Superscript z']
86−scriptArg :: GenParser Char st [Inline]
89+expToInlines (EText _ x) = Just [Emph [Str x]]
87−scriptArg = try $ do
90+expToInlines _ = Nothing
88− (try (do{char '{'; r <- many mathPart; char '}'; return $ concat r}))
90− <|> (do{c <- (letter <|> digit); return [Str [c]]})
92−superscript :: GenParser Char st [Inline]
93−superscript = try $ do
96− return [Superscript arg]
98−subscript :: GenParser Char st [Inline]
102− return [Subscript arg]
104−withThinSpace :: String -> String
105−withThinSpace str = "\x2009" ++ str ++ "\x2009"
107−teXsymbols :: [(String, String)]
114− ,("epsilon", "\x3B5")
115− ,("varepsilon", "\x25B")
121− ,("lambda", "\x3BB")
122− ,("Lambda", "\x39B")
128− ,("varphi", "\x3D5")
139− ,("vartheta", "\x3D1")
141− ,("upsilon", "\x3C5")
146− ,("lt", withThinSpace "<")
147− ,("le", withThinSpace "\x2264")
148− ,("leq", withThinSpace "\x2264")
149− ,("ge", withThinSpace "\x2265")
150− ,("geq", withThinSpace "\x2265")
151− ,("prec", withThinSpace "\x227A")
152− ,("succ", withThinSpace "\x227B")
153− ,("preceq", withThinSpace "\x2AAF")
154− ,("succeq", withThinSpace "\x2AB0")
155− ,("in", withThinSpace "\x2208")
156− ,("notin", withThinSpace "\x2209")
157− ,("subset", withThinSpace "\x2282")
158− ,("supset", withThinSpace "\x2283")
159− ,("subseteq", withThinSpace "\x2286")
160− ,("supseteq", withThinSpace "\x2287")
161− ,("equiv", withThinSpace "\x2261")
162− ,("cong", withThinSpace "\x2245")
163− ,("approx", withThinSpace "\x2248")
164− ,("propto", withThinSpace "\x221D")
165− ,("cdot", withThinSpace "\x22C5")
166− ,("star", withThinSpace "\x22C6")
167− ,("backslash", "\\")
168− ,("times", withThinSpace "\x00D7")
169− ,("divide", withThinSpace "\x00F7")
170− ,("circ", withThinSpace "\x2218")
171− ,("oplus", withThinSpace "\x2295")
172− ,("otimes", withThinSpace "\x2297")
173− ,("odot", withThinSpace "\x2299")
176− ,("wedge", withThinSpace "\x2227")
177− ,("bigwedge", withThinSpace "\x22C0")
178− ,("vee", withThinSpace "\x2228")
179− ,("bigvee", withThinSpace "\x22C1")
180− ,("cap", withThinSpace "\x2229")
181− ,("bigcap", withThinSpace "\x22C2")
182− ,("cup", withThinSpace "\x222A")
183− ,("bigcup", withThinSpace "\x22C3")
185− ,("implies", withThinSpace "\x21D2")
186− ,("iff", withThinSpace "\x21D4")
187− ,("forall", "\x2200")
188− ,("exists", "\x2203")
191− ,("vdash", "\x22A2")
192− ,("models", withThinSpace "\x22A8")
193− ,("uparrow", "\x2191")
194− ,("downarrow", "\x2193")
195− ,("rightarrow", withThinSpace "\x2192")
196− ,("to", withThinSpace "\x2192")
197− ,("rightarrowtail", "\x21A3")
198− ,("twoheadrightarrow", withThinSpace "\x21A0")
199− ,("twoheadrightarrowtail", withThinSpace "\x2916")
200− ,("mapsto", withThinSpace "\x21A6")
201− ,("leftarrow", withThinSpace "\x2190")
202− ,("leftrightarrow", withThinSpace "\x2194")
203− ,("Rightarrow", withThinSpace "\x21D2")
204− ,("Leftarrow", withThinSpace "\x21D0")
205− ,("Leftrightarrow", withThinSpace "\x21D4")
206− ,("partial", "\x2202")
207− ,("nabla", "\x2207")
209− ,("emptyset", "\x2205")
210− ,("infty", "\x221E")
211− ,("aleph", "\x2135")
213− ,("therefore", "\x2234")
214− ,("angle", "\x2220")
215− ,("quad", "\x00A0\x00A0")
216− ,("cdots", "\x22EF")
217− ,("vdots", "\x22EE")
218− ,("ddots", "\x22F1")
219− ,("diamond", "\x22C4")
221− ,("lfloor", "\x230A")
222− ,("rfloor", "\x230B")
223− ,("lceiling", "\x2308")
224− ,("rceiling", "\x2309")
225− ,("langle", "\x2329")
226− ,("rangle", "\x232A")