From ae22b1e977cfb1357bb21fabc227e76a6adb0599 Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Fri, 9 Jul 2021 12:27:41 -0700 Subject: RST reader: fix regression with code includes. With the recent changes to include infrastructure, included code blocks were getting an extra newline. Closes #7436. Added regression test. --- pandoc.cabal | 1 + src/Text/Pandoc/Readers/RST.hs | 6 +++++- test/command/7436.md | 14 ++++++++++++++ test/command/three.txt | 3 +++ 4 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 test/command/7436.md create mode 100644 test/command/three.txt diff --git a/pandoc.cabal b/pandoc.cabal index 84a04c6b6..e3cd7e54f 100644 --- a/pandoc.cabal +++ b/pandoc.cabal @@ -213,6 +213,7 @@ extra-source-files: test/command/B.txt test/command/C.txt test/command/D.txt + test/command/three.txt test/command/01.csv test/command/chap1/spider.png test/command/chap2/spider.png diff --git a/src/Text/Pandoc/Readers/RST.hs b/src/Text/Pandoc/Readers/RST.hs index 35292d949..3990f0cb5 100644 --- a/src/Text/Pandoc/Readers/RST.hs +++ b/src/Text/Pandoc/Readers/RST.hs @@ -474,6 +474,7 @@ includeDirective top fields body = do case lookup "literal" fields of Just _ -> B.rawBlock "rst" . sourcesToText <$> getInput Nothing -> parseBlocks + let isLiteral = isJust (lookup "code" fields `mplus` lookup "literal" fields) let selectLines = (case trim <$> lookup "end-before" fields of Just patt -> takeWhile (not . (patt `T.isInfixOf`)) @@ -482,8 +483,11 @@ includeDirective top fields body = do Just patt -> drop 1 . dropWhile (not . (patt `T.isInfixOf`)) Nothing -> id) + let toStream t = - toSources [(f, T.unlines . selectLines . T.lines $ t)] + Sources [(initialPos f, + (T.unlines . selectLines . T.lines $ t) <> + if isLiteral then mempty else "\n")] -- see #7436 currentDir <- takeDirectory . sourceName <$> getPosition insertIncludedFile parser toStream [currentDir] f startLine endLine diff --git a/test/command/7436.md b/test/command/7436.md new file mode 100644 index 000000000..ad4cb8c2f --- /dev/null +++ b/test/command/7436.md @@ -0,0 +1,14 @@ +``` +% pandoc -f rst -t native +.. include:: command/three.txt + :code: + +.. include:: command/three.txt + :literal: + +.. include:: command/three.txt +^D +[CodeBlock ("",[""],[("code","")]) "1st line.\n2nd line.\n3rd line.\n" +,RawBlock (Format "rst") "1st line.\n2nd line.\n3rd line.\n" +,Para [Str "1st",Space,Str "line.",SoftBreak,Str "2nd",Space,Str "line.",SoftBreak,Str "3rd",Space,Str "line."]] +``` diff --git a/test/command/three.txt b/test/command/three.txt new file mode 100644 index 000000000..3ca3fdd4e --- /dev/null +++ b/test/command/three.txt @@ -0,0 +1,3 @@ +1st line. +2nd line. +3rd line. -- cgit v1.2.3