From bfcff3eb993d66446d5599efbf6db07880f9d41a Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Sun, 11 May 2025 15:51:04 -0700 Subject: Org reader: change handling of inline TeX. Previously inline TeX was handled in a way that was different from org's own export, and that could lead to information loss. This was particularly noticeable for inline math environments such as `equation`. Previously, an `equation` environment starting at the beginning of a line would create a raw block, splitting up the paragraph containing it (see #10836). On the other hand, an `equation` environment not at the beginning of a line would be turned into regular inline elements representing the math. (This would cause the equation number to go missing and in some cases degrade the math formatting.) Now, we parse all of these as raw "latex" inlines, which will be omitted when converting to formats other than LaTeX (and other formats like pandoc's Markdown that allow raw LaTex). Closes #10836. --- src/Text/Pandoc/Readers/Org/BlockStarts.hs | 14 +++++++++----- src/Text/Pandoc/Readers/Org/Blocks.hs | 4 +++- src/Text/Pandoc/Readers/Org/Inlines.hs | 14 +++++++++----- 3 files changed, 21 insertions(+), 11 deletions(-) (limited to 'src/Text') diff --git a/src/Text/Pandoc/Readers/Org/BlockStarts.hs b/src/Text/Pandoc/Readers/Org/BlockStarts.hs index c91c7ad46..52fbec889 100644 --- a/src/Text/Pandoc/Readers/Org/BlockStarts.hs +++ b/src/Text/Pandoc/Readers/Org/BlockStarts.hs @@ -23,13 +23,14 @@ module Text.Pandoc.Readers.Org.BlockStarts , endOfBlock ) where -import Control.Monad (void) +import Control.Monad (void, guard) import Data.Text (Text) import Text.Pandoc.Readers.Org.Parsing import Text.Pandoc.Definition as Pandoc import Text.Pandoc.Shared (safeRead) import Text.Pandoc.Parsing (lowerAlpha, upperAlpha) import Text.Pandoc.Extensions +import Text.Pandoc.Readers.LaTeX.Math (inlineEnvironmentNames) import Data.Functor (($>)) -- | Horizontal Line (five -- dashes or more) @@ -55,10 +56,13 @@ gridTableStart = try $ skipSpaces <* char '+' <* char '-' latexEnvStart :: Monad m => OrgParser m Text -latexEnvStart = try $ - skipSpaces *> string "\\begin{" - *> latexEnvName - <* string "}" +latexEnvStart = try $ do + skipSpaces + string "\\begin{" + name <- latexEnvName + char '}' + guard $ name `notElem` inlineEnvironmentNames + pure name 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 82cc6d49a..8c2b406b9 100644 --- a/src/Text/Pandoc/Readers/Org/Blocks.hs +++ b/src/Text/Pandoc/Readers/Org/Blocks.hs @@ -1,3 +1,4 @@ +{-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE LambdaCase #-} {-# LANGUAGE OverloadedStrings #-} @@ -25,7 +26,7 @@ import Text.Pandoc.Readers.Org.ParserState import Text.Pandoc.Readers.Org.Parsing import Text.Pandoc.Readers.Org.Shared (cleanLinkText, isImageFilename, originalLang, translateLang, exportsCode) - +import Text.Pandoc.Readers.LaTeX.Math (inlineEnvironmentNames) import Text.Pandoc.Builder (Blocks, Inlines, Many(..)) import Text.Pandoc.Class.PandocMonad (PandocMonad) import Text.Pandoc.Definition @@ -796,6 +797,7 @@ rowToContent tbl row = latexFragment :: PandocMonad m => OrgParser m (F Blocks) latexFragment = try $ do envName <- latexEnvStart + guard $ envName `notElem` inlineEnvironmentNames texOpt <- getExportSetting exportWithLatex let envStart = "\\begin{" <> envName <> "}" let envEnd = "\\end{" <> envName <> "}" diff --git a/src/Text/Pandoc/Readers/Org/Inlines.hs b/src/Text/Pandoc/Readers/Org/Inlines.hs index b4d732a74..0f9a36d0c 100644 --- a/src/Text/Pandoc/Readers/Org/Inlines.hs +++ b/src/Text/Pandoc/Readers/Org/Inlines.hs @@ -808,16 +808,20 @@ inlineLaTeX = try $ do allowEntities <- getExportSetting exportWithEntities ils <- parseAsInlineLaTeX cmd texOpt maybe mzero returnF $ - parseAsMathMLSym allowEntities cmd `mplus` - parseAsMath cmd texOpt `mplus` - ils + if "\\begin{" `T.isPrefixOf` cmd + then ils + else parseAsMathMLSym allowEntities cmd `mplus` + parseAsMath cmd texOpt `mplus` + ils where parseAsInlineLaTeX :: PandocMonad m => Text -> TeXExport -> OrgParser m (Maybe Inlines) parseAsInlineLaTeX cs = \case - TeXExport -> maybeRight <$> runParserT inlineCommand state "" (toSources cs) + TeXExport -> maybeRight <$> runParserT + (B.rawInline "latex" . snd <$> withRaw inlineCommand) + state "" (toSources cs) TeXIgnore -> return (Just mempty) - TeXVerbatim -> return (Just $ B.str cs) + TeXVerbatim -> return (Just $ B.text cs) parseAsMathMLSym :: Bool -> Text -> Maybe Inlines parseAsMathMLSym allowEntities cs = do -- cgit v1.2.3