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

This adds support for LaTeX fragments like the following: ``` \begin{equation} \int fg \mathrm{d}x \end{equation} ```

commit09441b65a83f372410394a88af7808f494c3aa57 authorAlbert Krewinkel <tarleb@moltkeplatz.de> dateFri 18 Apr 2014, 10:15 (13 years ago) parentf19d723
2 files changed+59−2
src/Text/Pandoc/Readers/Org.hs+40−1 tests/Tests/Readers/Org.hs+19−1
src/Text/Pandoc/Readers/Org.hs+40−1
@@ -218,6 +218,7 @@ block = choice [ mempty <$ blanklines
218218 , return <$> hline
219219 , list
220220 , table
221+ , latexFragment
221222 , noteBlock
222223 , paraOrPlain
223224 ] <?> "block"
@@ -544,6 +545,41 @@ setAligns :: [Alignment]
544545 -> F OrgTable
545546setAligns aligns t = return $ t{ orgTableAlignments = aligns }
546547
548+
549+--
550+-- LaTeX fragments
551+--
552+latexFragment :: OrgParser (F Blocks)
553+latexFragment = try $ do
554+ envName <- latexEnvStart
555+ content <- mconcat <$> manyTill anyLineNewline (latexEnd envName)
556+ return . return $ B.rawBlock "latex" (content `inLatexEnv` envName)
557+ where
558+ c `inLatexEnv` e = mconcat [ "\\begin{", e, "}\n"
559+ , c
560+ , "\\end{", e, "}\n"
561+ ]
562+
563+latexEnvStart :: OrgParser String
564+latexEnvStart = try $ do
565+ skipSpaces *> string "\\begin{"
566+ *> latexEnvName
567+ <* string "}"
568+ <* blankline
569+
570+latexEnd :: String -> OrgParser ()
571+latexEnd envName = try $
572+ () <$ skipSpaces
573+ <* string ("\\end{" ++ envName ++ "}")
574+ <* blankline
575+
576+-- | Parses a LaTeX environment name.
577+latexEnvName :: OrgParser String
578+latexEnvName = try $ do
579+ mappend <$> many1 alphaNum
580+ <*> option "" (string "*")
581+
582+
547583--
548584-- Footnote defintions
549585--
@@ -683,7 +719,9 @@ str :: OrgParser (F Inlines)
683719str = return . B.str <$> many1 (noneOf $ specialChars ++ "\n\r ")
684720 <* updateLastStrPos
685721
686−-- an endline character that can be treated as a space, not a structural break
722+-- | An endline character that can be treated as a space, not a structural
723+-- break. This should reflect the values of the Emacs variable
724+-- @org-element-pagaraph-separate@.
687725endline :: OrgParser (F Inlines)
688726endline = try $ do
689727 newline
@@ -695,6 +733,7 @@ endline = try $ do
695733 notFollowedBy' drawerStart
696734 notFollowedBy' headerStart
697735 notFollowedBy' metaLineStart
736+ notFollowedBy' latexEnvStart
698737 notFollowedBy' commentLineStart
699738 notFollowedBy' bulletListStart
700739 notFollowedBy' orderedListStart
tests/Tests/Readers/Org.hs+19−1
@@ -650,7 +650,7 @@ tests =
650650 ]
651651 ]
652652
653− , testGroup "Blocks"
653+ , testGroup "Blocks and fragments"
654654 [ "Source block" =:
655655 unlines [ " #+BEGIN_SRC haskell"
656656 , " main = putStrLn greeting"
@@ -722,5 +722,23 @@ tests =
722722 ]
723723 ]
724724
725+ , "LaTeX fragment" =:
726+ unlines [ "\\begin{equation}"
727+ , "X_i = \\begin{cases}"
728+ , " G_{\\alpha(i)} & \\text{if }\\alpha(i-1) = \\alpha(i)\\\\"
729+ , " C_{\\alpha(i)} & \\text{otherwise}"
730+ , " \\end{cases}"
731+ , "\\end{equation}"
732+ ] =?>
733+ rawBlock "latex"
734+ (unlines [ "\\begin{equation}"
735+ , "X_i = \\begin{cases}"
736+ , " G_{\\alpha(i)} & \\text{if }\\alpha(i-1) =" ++
737+ " \\alpha(i)\\\\"
738+ , " C_{\\alpha(i)} & \\text{otherwise}"
739+ , " \\end{cases}"
740+ , "\\end{equation}"
741+ ])
742+
725743 ]
726744 ]