From 591cdca38b8af5a50a4e121b34d87f3c9bca5844 Mon Sep 17 00:00:00 2001 From: Simon Schuster Date: Fri, 20 Aug 2021 16:41:50 +0200 Subject: LaTeX-parser: restrict \endinput to current file --- pandoc.cabal | 1 + src/Text/Pandoc/Readers/LaTeX.hs | 6 +++++- src/Text/Pandoc/Readers/LaTeX/Parsing.hs | 4 ++++ test/command/bar-endinput.tex | 3 +++ test/command/input-with-endinput.md | 14 ++++++++++++++ 5 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 test/command/bar-endinput.tex create mode 100644 test/command/input-with-endinput.md diff --git a/pandoc.cabal b/pandoc.cabal index d924169e6..b90a61942 100644 --- a/pandoc.cabal +++ b/pandoc.cabal @@ -242,6 +242,7 @@ extra-source-files: test/command/sub-file-chapter-1.tex test/command/sub-file-chapter-2.tex test/command/bar.tex + test/command/bar-endinput.tex test/command/yaml-metadata.yaml test/command/3510-subdoc.org test/command/3510-export.latex diff --git a/src/Text/Pandoc/Readers/LaTeX.hs b/src/Text/Pandoc/Readers/LaTeX.hs index 31c8d9095..5d3ff5f95 100644 --- a/src/Text/Pandoc/Readers/LaTeX.hs +++ b/src/Text/Pandoc/Readers/LaTeX.hs @@ -890,7 +890,7 @@ blockCommands = M.fromList addMeta "bibliography" . splitBibs . untokenize)) , ("addbibresource", mempty <$ (skipopts *> braced >>= addMeta "bibliography" . splitBibs . untokenize)) - , ("endinput", mempty <$ skipMany anyTok) + , ("endinput", mempty <$ skipSameFileToks) -- includes , ("lstinputlisting", inputListing) , ("inputminted", inputMinted) @@ -921,6 +921,10 @@ blockCommands = M.fromList , ("epigraph", epigraph) ] +skipSameFileToks :: PandocMonad m => LP m () +skipSameFileToks = do + pos <- getPosition + skipMany $ infile (sourceName pos) environments :: PandocMonad m => M.Map Text (LP m Blocks) environments = M.union (tableEnvironments blocks inline) $ diff --git a/src/Text/Pandoc/Readers/LaTeX/Parsing.hs b/src/Text/Pandoc/Readers/LaTeX/Parsing.hs index 075e1f656..9b84cdd21 100644 --- a/src/Text/Pandoc/Readers/LaTeX/Parsing.hs +++ b/src/Text/Pandoc/Readers/LaTeX/Parsing.hs @@ -45,6 +45,7 @@ module Text.Pandoc.Readers.LaTeX.Parsing , isNewlineTok , isWordTok , isArgTok + , infile , spaces , spaces1 , tokTypeIn @@ -646,6 +647,9 @@ isArgTok :: Tok -> Bool isArgTok (Tok _ (Arg _) _) = True isArgTok _ = False +infile :: PandocMonad m => SourceName -> LP m Tok +infile reference = satisfyTok (\(Tok source _ _) -> (sourceName source) == reference) + spaces :: PandocMonad m => LP m () spaces = skipMany (satisfyTok (tokTypeIn [Comment, Spaces, Newline])) diff --git a/test/command/bar-endinput.tex b/test/command/bar-endinput.tex new file mode 100644 index 000000000..2f3f9bd6c --- /dev/null +++ b/test/command/bar-endinput.tex @@ -0,0 +1,3 @@ +\emph{hi there} +\endinput +\emph{invisible} diff --git a/test/command/input-with-endinput.md b/test/command/input-with-endinput.md new file mode 100644 index 000000000..df0475a15 --- /dev/null +++ b/test/command/input-with-endinput.md @@ -0,0 +1,14 @@ +``` +% pandoc --from=latex -t native +\begin{document} +Visible + +\include{command/bar-endinput} + +Visible +\end{document} +^D +[Para [Str "Visible"] +,Para [Emph [Str "hi",Space,Str "there"]] +,Para [Str "Visible"]] +``` -- cgit v1.2.3