git.iko.soy/typeable/pandoc
on master help sign in
Org reader: support for inline LaTeX

Inline LaTeX is now accepted and parsed by the org-mode reader. Both, math symbols (like \tau) and LaTeX commands (like \cite{Coffee}), can be used without any further escaping.

commit3238a2f9191b83864abd682261634a603ec89056 authorAlbert Krewinkel <tarleb@moltkeplatz.de> dateTue 20 May 2014, 22:29 (12 years ago) parent0915967
3 files changed+59−1
src/Text/Pandoc/Readers/LaTeX.hs+1 src/Text/Pandoc/Readers/Org.hs+31−1 tests/Tests/Readers/Org.hs+27
src/Text/Pandoc/Readers/LaTeX.hs+1
@@ -31,6 +31,7 @@ Conversion of LaTeX to 'Pandoc' document.
3131module Text.Pandoc.Readers.LaTeX ( readLaTeX,
3232 rawLaTeXInline,
3333 rawLaTeXBlock,
34+ inlineCommand,
3435 handleIncludes
3536 ) where
3637
src/Text/Pandoc/Readers/Org.hs+31−1
@@ -39,12 +39,15 @@ import Text.Pandoc.Parsing hiding ( F, unF, askF, asksF, runF
3939 , newline, orderedListMarker
4040 , parseFromString
4141 , updateLastStrPos )
42+import Text.Pandoc.Readers.LaTeX (inlineCommand, rawLaTeXInline)
4243import Text.Pandoc.Shared (compactify', compactify'DL)
44+import Text.Parsec.Pos (updatePosString)
45+import Text.TeXMath (texMathToPandoc, DisplayType(..))
4346
4447import Control.Applicative ( Applicative, pure
4548 , (<$>), (<$), (<*>), (<*), (*>), (<**>) )
4649import Control.Arrow (first)
47−import Control.Monad (foldM, guard, liftM, liftM2, mzero, when)
50+import Control.Monad (foldM, guard, liftM, liftM2, mplus, mzero, when)
4851import Control.Monad.Reader (Reader, runReader, ask, asks)
4952import Data.Char (isAlphaNum, toLower)
5053import Data.Default
@@ -886,6 +889,7 @@ inline =
886889 , verbatim
887890 , subscript
888891 , superscript
892+ , inlineLaTeX
889893 , symbol
890894 ] <* (guard =<< newlinesCountWithinLimits)
891895 <?> "inline"
@@ -1351,3 +1355,29 @@ simpleSubOrSuperString = try $
13511355 , mappend <$> option [] ((:[]) <$> oneOf "+-")
13521356 <*> many1 alphaNum
13531357 ]
1358+
1359+inlineLaTeX :: OrgParser (F Inlines)
1360+inlineLaTeX = try $ do
1361+ cmd <- inlineLaTeXCommand
1362+ maybe mzero returnF $ parseAsMath cmd `mplus` parseAsInlineLaTeX cmd
1363+ where
1364+ parseAsMath :: String -> Maybe Inlines
1365+ parseAsMath cs = maybeRight $ B.fromList <$> texMathToPandoc DisplayInline cs
1366+
1367+ parseAsInlineLaTeX :: String -> Maybe Inlines
1368+ parseAsInlineLaTeX cs = maybeRight $ runParser inlineCommand state "" cs
1369+
1370+ state :: ParserState
1371+ state = def{ stateOptions = def{ readerParseRaw = True }}
1372+
1373+maybeRight :: Either a b -> Maybe b
1374+maybeRight = either (const Nothing) Just
1375+
1376+inlineLaTeXCommand :: OrgParser String
1377+inlineLaTeXCommand = try $ do
1378+ rest <- getInput
1379+ pos <- getPosition
1380+ case runParser rawLaTeXInline def "source" rest of
1381+ Right (RawInline _ cs) -> cs <$ (setInput $ drop (length cs) rest)
1382+ <* (setPosition $ updatePosString pos cs)
1383+ _ -> mzero
tests/Tests/Readers/Org.hs+27
@@ -247,6 +247,33 @@ tests =
247247 , citationNoteNum = 0
248248 , citationHash = 0}
249249 in (para $ cite [citation] "[see @item1 p. 34-35]")
250+
251+ , "Inline LaTeX symbol" =:
252+ "\\dots" =?>
253+ para "…"
254+
255+ , "Inline LaTeX command" =:
256+ "\\textit{Emphasised}" =?>
257+ para (emph "Emphasised")
258+
259+ , "Inline LaTeX math symbol" =:
260+ "\\tau" =?>
261+ para (emph "τ")
262+
263+ , "Unknown inline LaTeX command" =:
264+ "\\notacommand{foo}" =?>
265+ para (rawInline "latex" "\\notacommand{foo}")
266+
267+ , "LaTeX citation" =:
268+ "\\cite{Coffee}" =?>
269+ let citation = Citation
270+ { citationId = "Coffee"
271+ , citationPrefix = []
272+ , citationSuffix = []
273+ , citationMode = AuthorInText
274+ , citationNoteNum = 0
275+ , citationHash = 0}
276+ in (para . cite [citation] $ rawInline "latex" "\\cite{Coffee}")
250277 ]
251278
252279 , testGroup "Meta Information" $