git.iko.soy/typeable/pandoc
on master help sign in
Org reader: support for inline LaTeX
3238a2fAlbert Krewinkel
src/Text/Pandoc/Readers/LaTeX.hs+1
@@ -31,6 +31,7 @@ Conversion of LaTeX to 'Pandoc' document.
31module Text.Pandoc.Readers.LaTeX ( readLaTeX,
31module Text.Pandoc.Readers.LaTeX ( readLaTeX,
32 rawLaTeXInline,
32 rawLaTeXInline,
33 rawLaTeXBlock,
33 rawLaTeXBlock,
34+ inlineCommand,
34 handleIncludes
35 handleIncludes
35 ) where
36 ) where
36
37
src/Text/Pandoc/Readers/Org.hs+31−1
@@ -39,12 +39,15 @@ import Text.Pandoc.Parsing hiding ( F, unF, askF, asksF, runF
39 , newline, orderedListMarker
39 , newline, orderedListMarker
40 , parseFromString
40 , parseFromString
41 , updateLastStrPos )
41 , updateLastStrPos )
42+import Text.Pandoc.Readers.LaTeX (inlineCommand, rawLaTeXInline)
42import Text.Pandoc.Shared (compactify', compactify'DL)
43import Text.Pandoc.Shared (compactify', compactify'DL)
44+import Text.Parsec.Pos (updatePosString)
45+import Text.TeXMath (texMathToPandoc, DisplayType(..))
43
46
44import Control.Applicative ( Applicative, pure
47import Control.Applicative ( Applicative, pure
45 , (<$>), (<$), (<*>), (<*), (*>), (<**>) )
48 , (<$>), (<$), (<*>), (<*), (*>), (<**>) )
46import Control.Arrow (first)
49import Control.Arrow (first)
47−import Control.Monad (foldM, guard, liftM, liftM2, mzero, when)
50+import Control.Monad (foldM, guard, liftM, liftM2, mplus, mzero, when)
48import Control.Monad.Reader (Reader, runReader, ask, asks)
51import Control.Monad.Reader (Reader, runReader, ask, asks)
49import Data.Char (isAlphaNum, toLower)
52import Data.Char (isAlphaNum, toLower)
50import Data.Default
53import Data.Default
@@ -886,6 +889,7 @@ inline =
886 , verbatim
889 , verbatim
887 , subscript
890 , subscript
888 , superscript
891 , superscript
892+ , inlineLaTeX
889 , symbol
893 , symbol
890 ] <* (guard =<< newlinesCountWithinLimits)
894 ] <* (guard =<< newlinesCountWithinLimits)
891 <?> "inline"
895 <?> "inline"
@@ -1351,3 +1355,29 @@ simpleSubOrSuperString = try $
1351 , mappend <$> option [] ((:[]) <$> oneOf "+-")
1355 , mappend <$> option [] ((:[]) <$> oneOf "+-")
1352 <*> many1 alphaNum
1356 <*> many1 alphaNum
1353 ]
1357 ]
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 =
247 , citationNoteNum = 0
247 , citationNoteNum = 0
248 , citationHash = 0}
248 , citationHash = 0}
249 in (para $ cite [citation] "[see @item1 p. 34-35]")
249 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}")
250 ]
277 ]
251
278
252 , testGroup "Meta Information" $
279 , testGroup "Meta Information" $