@@ -0,0 +1,218 @@
1+{-
2+Copyright (C) 2007 John MacFarlane <jgm@berkeley.edu>
3+
4+This program is free software; you can redistribute it and/or modify
5+it under the terms of the GNU General Public License as published by
6+the Free Software Foundation; either version 2 of the License, or
7+(at your option) any later version.
8+
9+This program is distributed in the hope that it will be useful,
10+but WITHOUT ANY WARRANTY; without even the implied warranty of
11+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+GNU General Public License for more details.
13+
14+You should have received a copy of the GNU General Public License
15+along with this program; if not, write to the Free Software
16+Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17+-}
18+
19+{- |
20+ Module : Text.Pandoc.Readers.TeXMath
21+ Copyright : Copyright (C) 2007 John MacFarlane
22+ License : GNU GPL, version 2 or above
23+
24+ Maintainer : John MacFarlane <jgm@berkeley.edu>
25+ Stability : alpha
26+ Portability : portable
27+
28+Conversion of TeX math to a list of 'Pandoc' inline elements.
29+-}
30+module Text.Pandoc.Readers.TeXMath (
31+ readTeXMath
32+ ) where
33+
34+import Text.ParserCombinators.Parsec
35+import Text.Pandoc.UTF8
36+import Text.Pandoc.Definition
37+import Text.Pandoc.Shared
38+
39+-- | Converts a string of raw TeX math to a list of 'Pandoc' inlines.
40+readTeXMath :: String -> [Inline]
41+readTeXMath inp = case parse teXMath "input" inp of
42+ Left err -> error $ "\nError:\n" ++ show err
43+ Right res -> res
44+
45+teXMath = manyTill mathPart eof >>= return . concat
46+
47+mathPart = whitespace <|> superscript <|> subscript <|> symbol <|>
48+ argument <|> plain <|> misc
49+
50+whitespace = many1 space >> return []
51+
52+symbol = try $ do
53+ char '\\'
54+ res <- many1 letter
55+ case lookup res teXsymbols of
56+ Just m -> return [Str m]
57+ Nothing -> return [Str $ "\\" ++ res]
58+
59+argument = try $ do
60+ char '{'
61+ res <- many mathPart
62+ char '}'
63+ return $ if null res
64+ then [Str " "]
65+ else [Str "{"] ++ concat res ++ [Str "}"]
66+
67+plain = do
68+ res <- many1 alphaNum
69+ return $ [Emph [Str res]]
70+
71+misc = do
72+ res <- noneOf "{}\\"
73+ return [Str [res]]
74+
75+scriptArg = try $ do
76+ (try (do{char '{'; r <- many mathPart; char '}'; return $ concat r}))
77+ <|> symbol
78+ <|> (do{c <- (letter <|> digit); return [Str [c]]})
79+
80+superscript = try $ do
81+ char '^'
82+ arg <- scriptArg
83+ return [Superscript arg]
84+
85+subscript = try $ do
86+ char '_'
87+ arg <- scriptArg
88+ return [Subscript arg]
89+
90+withThinSpace str = "\x2009" ++ str ++ "\x2009"
91+
92+teXsymbols =
93+ [("alpha","\x3B1")
94+ ,("beta", "\x3B2")
95+ ,("chi", "\x3C7")
96+ ,("delta", "\x3B4")
97+ ,("Delta", "\x394")
98+ ,("epsilon", "\x3B5")
99+ ,("varepsilon", "\x25B")
100+ ,("eta", "\x3B7")
101+ ,("gamma", "\x3B3")
102+ ,("Gamma", "\x393")
103+ ,("iota", "\x3B9")
104+ ,("kappa", "\x3BA")
105+ ,("lambda", "\x3BB")
106+ ,("Lambda", "\x39B")
107+ ,("mu", "\x3BC")
108+ ,("nu", "\x3BD")
109+ ,("omega", "\x3C9")
110+ ,("Omega", "\x3A9")
111+ ,("phi", "\x3C6")
112+ ,("varphi", "\x3D5")
113+ ,("Phi", "\x3A6")
114+ ,("pi", "\x3C0")
115+ ,("Pi", "\x3A0")
116+ ,("psi", "\x3C8")
117+ ,("Psi", "\x3A8")
118+ ,("rho", "\x3C1")
119+ ,("sigma", "\x3C3")
120+ ,("Sigma", "\x3A3")
121+ ,("tau", "\x3C4")
122+ ,("theta", "\x3B8")
123+ ,("vartheta", "\x3D1")
124+ ,("Theta", "\x398")
125+ ,("upsilon", "\x3C5")
126+ ,("xi", "\x3BE")
127+ ,("Xi", "\x39E")
128+ ,("zeta", "\x3B6")
129+ ,("ne", "\x2260")
130+ ,("lt", withThinSpace "<")
131+ ,("le", withThinSpace "\x2264")
132+ ,("leq", withThinSpace "\x2264")
133+ ,("ge", withThinSpace "\x2265")
134+ ,("geq", withThinSpace "\x2265")
135+ ,("prec", withThinSpace "\x227A")
136+ ,("succ", withThinSpace "\x227B")
137+ ,("preceq", withThinSpace "\x2AAF")
138+ ,("succeq", withThinSpace "\x2AB0")
139+ ,("in", withThinSpace "\x2208")
140+ ,("notin", withThinSpace "\x2209")
141+ ,("subset", withThinSpace "\x2282")
142+ ,("supset", withThinSpace "\x2283")
143+ ,("subseteq", withThinSpace "\x2286")
144+ ,("supseteq", withThinSpace "\x2287")
145+ ,("equiv", withThinSpace "\x2261")
146+ ,("cong", withThinSpace "\x2245")
147+ ,("approx", withThinSpace "\x2248")
148+ ,("propto", withThinSpace "\x221D")
149+ ,("cdot", withThinSpace "\x22C5")
150+ ,("star", withThinSpace "\x22C6")
151+ ,("backslash", "\\")
152+ ,("times", withThinSpace "\x00D7")
153+ ,("divide", withThinSpace "\x00F7")
154+ ,("circ", withThinSpace "\x2218")
155+ ,("oplus", withThinSpace "\x2295")
156+ ,("otimes", withThinSpace "\x2297")
157+ ,("odot", withThinSpace "\x2299")
158+ ,("sum", "\x2211")
159+ ,("prod", "\x220F")
160+ ,("wedge", withThinSpace "\x2227")
161+ ,("bigwedge", withThinSpace "\x22C0")
162+ ,("vee", withThinSpace "\x2228")
163+ ,("bigvee", withThinSpace "\x22C1")
164+ ,("cap", withThinSpace "\x2229")
165+ ,("bigcap", withThinSpace "\x22C2")
166+ ,("cup", withThinSpace "\x222A")
167+ ,("bigcup", withThinSpace "\x22C3")
168+ ,("neg", "\x00AC")
169+ ,("implies", withThinSpace "\x21D2")
170+ ,("iff", withThinSpace "\x21D4")
171+ ,("forall", "\x2200")
172+ ,("exists", "\x2203")
173+ ,("bot", "\x22A5")
174+ ,("top", "\x22A4")
175+ ,("vdash", "\x22A2")
176+ ,("models", withThinSpace "\x22A8")
177+ ,("uparrow", "\x2191")
178+ ,("downarrow", "\x2193")
179+ ,("rightarrow", withThinSpace "\x2192")
180+ ,("to", withThinSpace "\x2192")
181+ ,("rightarrowtail", "\x21A3")
182+ ,("twoheadrightarrow", withThinSpace "\x21A0")
183+ ,("twoheadrightarrowtail", withThinSpace "\x2916")
184+ ,("mapsto", withThinSpace "\x21A6")
185+ ,("leftarrow", withThinSpace "\x2190")
186+ ,("leftrightarrow", withThinSpace "\x2194")
187+ ,("Rightarrow", withThinSpace "\x21D2")
188+ ,("Leftarrow", withThinSpace "\x21D0")
189+ ,("Leftrightarrow", withThinSpace "\x21D4")
190+ ,("partial", "\x2202")
191+ ,("nabla", "\x2207")
192+ ,("pm", "\x00B1")
193+ ,("emptyset", "\x2205")
194+ ,("infty", "\x221E")
195+ ,("aleph", "\x2135")
196+ ,("ldots", "...")
197+ ,("therefore", "\x2234")
198+ ,("angle", "\x2220")
199+ ,("quad", "\x00A0\x00A0")
200+ ,("cdots", "\x22EF")
201+ ,("vdots", "\x22EE")
202+ ,("ddots", "\x22F1")
203+ ,("diamond", "\x22C4")
204+ ,("Box", "\x25A1")
205+ ,("lfloor", "\x230A")
206+ ,("rfloor", "\x230B")
207+ ,("lceiling", "\x2308")
208+ ,("rceiling", "\x2309")
209+ ,("langle", "\x2329")
210+ ,("rangle", "\x232A")
211+ ,("{", "{")
212+ ,("}", "}")
213+ ,("[", "[")
214+ ,("]", "]")
215+ ,("|", "|")
216+ ,("||", "||")
217+ ]
218+