diff options
| author | Lucas V. R <[email protected]> | 2022-02-21 09:54:37 -0300 |
|---|---|---|
| committer | Albert Krewinkel <[email protected]> | 2022-02-21 16:04:14 +0100 |
| commit | 3bc3e968372e567cbaf3a7a512e15b93b90d2520 (patch) | |
| tree | e1be96d9616f2961a514a1b926058ec1fde8f9b9 | |
| parent | 6fe8014a2cc1d732dd11c0fc386d7c8449048c90 (diff) | |
Org reader: More flexible LaTeX environments
Looking at the definition of `org-element-latex-environment-parser`, one
sees that Org allows arbitrary arguments to LaTeX environments. In fact,
it parses every char just after `\begin{xxx}` until `\end{xxx}` as
content for the environment, so all the following examples are valid
environments:
```org
\begin{equation} e = mc^2 \end{equations}
```
```org
\begin{tikzcd}[ampersand replacement=\&]
A \& B \\
C \& D
\arrow[from=1-1, to=1-2]
\arrow["f", from=2-1, to=2-2]
\end{tikzcd}
```
| -rw-r--r-- | src/Text/Pandoc/Readers/Org/BlockStarts.hs | 1 | ||||
| -rw-r--r-- | src/Text/Pandoc/Readers/Org/Blocks.hs | 16 | ||||
| -rw-r--r-- | test/Tests/Readers/Org/Block.hs | 21 | ||||
| -rw-r--r-- | test/Tests/Readers/Org/Directive.hs | 2 |
4 files changed, 29 insertions, 11 deletions
diff --git a/src/Text/Pandoc/Readers/Org/BlockStarts.hs b/src/Text/Pandoc/Readers/Org/BlockStarts.hs index e5b2f7041..087819421 100644 --- a/src/Text/Pandoc/Readers/Org/BlockStarts.hs +++ b/src/Text/Pandoc/Readers/Org/BlockStarts.hs @@ -58,7 +58,6 @@ latexEnvStart = try $ skipSpaces *> string "\\begin{" *> latexEnvName <* string "}" - <* blankline where latexEnvName :: Monad m => OrgParser m Text latexEnvName = try $ mappend <$> many1Char alphaNum <*> option "" (textStr "*") diff --git a/src/Text/Pandoc/Readers/Org/Blocks.hs b/src/Text/Pandoc/Readers/Org/Blocks.hs index 6b759901a..5fcafc703 100644 --- a/src/Text/Pandoc/Readers/Org/Blocks.hs +++ b/src/Text/Pandoc/Readers/Org/Blocks.hs @@ -36,7 +36,7 @@ import Control.Monad (foldM, guard, mplus, mzero, void) import Data.Char (isSpace) import Data.Default (Default) import Data.Functor (($>)) -import Data.List (foldl', intersperse) +import Data.List (foldl') import Data.Maybe (fromMaybe, isJust, isNothing) import Data.Text (Text) import Data.List.NonEmpty (nonEmpty) @@ -760,19 +760,17 @@ latexFragment = try $ do texOpt <- getExportSetting exportWithLatex let envStart = "\\begin{" <> envName <> "}" let envEnd = "\\end{" <> envName <> "}" - envLines <- do - content <- manyTill anyLine (latexEnd envName) - return $ envStart : content ++ [envEnd] + envContent <- do + content <- manyTillChar anyChar (latexEnd envName) + return $ envStart <> content <> envEnd returnF $ case texOpt of - TeXExport -> B.rawBlock "latex" . T.unlines $ envLines + TeXExport -> B.rawBlock "latex" (envContent <> "\n") TeXIgnore -> mempty - TeXVerbatim -> B.para . mconcat . intersperse B.softbreak $ - map B.str envLines + TeXVerbatim -> B.para . B.text $ envContent where latexEnd :: Monad m => Text -> OrgParser m () latexEnd envName = try . void - $ skipSpaces - <* textStr ("\\end{" <> envName <> "}") + $ textStr ("\\end{" <> envName <> "}") <* blankline diff --git a/test/Tests/Readers/Org/Block.hs b/test/Tests/Readers/Org/Block.hs index 4f3038f93..fd11b9ad7 100644 --- a/test/Tests/Readers/Org/Block.hs +++ b/test/Tests/Readers/Org/Block.hs @@ -186,6 +186,27 @@ tests = , "\\end{equation}" ]) + , "One-line LaTeX fragment" =: + "\\begin{equation} 2 + 3 \\end{equation}" =?> + rawBlock "latex" "\\begin{equation} 2 + 3 \\end{equation}\n" + + , "LaTeX fragment with more arguments" =: + T.unlines [ "\\begin{tikzcd}[ampersand replacement=\\&]" + , " A \\& B \\\\" + , " C \\& D" + , " \\arrow[from=1-1, to=1-2]" + , " \\arrow[\"f\", from=2-1, to=2-2]" + , "\\end{tikzcd}" + ] =?> + rawBlock "latex" + (T.unlines [ "\\begin{tikzcd}[ampersand replacement=\\&]" + , " A \\& B \\\\" + , " C \\& D" + , " \\arrow[from=1-1, to=1-2]" + , " \\arrow[\"f\", from=2-1, to=2-2]" + , "\\end{tikzcd}" + ]) + , "Convert blank lines in blocks to single newlines" =: T.unlines [ "#+begin_html" , "" diff --git a/test/Tests/Readers/Org/Directive.hs b/test/Tests/Readers/Org/Directive.hs index 7839cf193..cceed8108 100644 --- a/test/Tests/Readers/Org/Directive.hs +++ b/test/Tests/Readers/Org/Directive.hs @@ -250,7 +250,7 @@ tests = , "\\end{equation}" ] =?> para (str "\\begin{equation}" <> softbreak <> - str "f(x) = x^2" <> softbreak <> + text "f(x) = x^2" <> softbreak <> str "\\end{equation}") ] ] |
