From ae8ed408b03688f4ddf6a13d0e55fba9138c6eb9 Mon Sep 17 00:00:00 2001
From: Brian Leung <29217594+leungbk@users.noreply.github.com>
Date: Fri, 20 May 2022 00:46:16 -0700
Subject: [PATCH] Org reader: support line selection in INCLUDE directives.
 (#8060)

---
 src/Text/Pandoc/Readers/Org/Blocks.hs |  9 ++++++++-
 test/command/6466-beg.hs              |  8 ++++++++
 test/command/6466-end.hs              |  4 ++++
 test/command/6466-mid.hs              | 10 ++++++++++
 test/command/6466-whole.hs            |  2 ++
 test/command/6466.md                  | 23 +++++++++++++++++++++++
 6 files changed, 55 insertions(+), 1 deletion(-)
 create mode 100644 test/command/6466-beg.hs
 create mode 100644 test/command/6466-end.hs
 create mode 100644 test/command/6466-mid.hs
 create mode 100644 test/command/6466-whole.hs
 create mode 100644 test/command/6466.md

diff --git a/src/Text/Pandoc/Readers/Org/Blocks.hs b/src/Text/Pandoc/Readers/Org/Blocks.hs
index fbc5e6182..03cfdfaed 100644
--- a/src/Text/Pandoc/Readers/Org/Blocks.hs
+++ b/src/Text/Pandoc/Readers/Org/Blocks.hs
@@ -535,8 +535,15 @@ include = try $ do
         return $ pure . B.codeBlockWith attr <$> parseRaw
       _ -> return $ return . B.fromList . blockFilter params <$> blockList
   currentDir <- takeDirectory . sourceName <$> getPosition
+  let (startLine, endLine) =
+        case lookup "lines" params of
+          Nothing -> (Nothing, Nothing)
+          Just bounds -> let boundStr = T.drop 1 (T.dropEnd 1 bounds)
+                             begStr = T.takeWhile (/= '-') boundStr
+                             endStr = T.takeWhileEnd (/= '-') boundStr
+                         in (safeRead begStr, pred <$> safeRead endStr)
   insertIncludedFile blocksParser toSources
-                     [currentDir] filename Nothing Nothing
+                     [currentDir] filename startLine endLine
  where
   includeTarget :: PandocMonad m => OrgParser m FilePath
   includeTarget = do
diff --git a/test/command/6466-beg.hs b/test/command/6466-beg.hs
new file mode 100644
index 000000000..33767968c
--- /dev/null
+++ b/test/command/6466-beg.hs
@@ -0,0 +1,8 @@
+want :: Int
+want = 42
+
+-- DO NOT WANT this; right bound is exclusive
+
+-- DO NOT WANT the below
+doNotWant :: Int
+doNotWant = 999
diff --git a/test/command/6466-end.hs b/test/command/6466-end.hs
new file mode 100644
index 000000000..46dd94772
--- /dev/null
+++ b/test/command/6466-end.hs
@@ -0,0 +1,4 @@
+-- DO NOT WANT
+
+want :: Int
+want = 42
diff --git a/test/command/6466-mid.hs b/test/command/6466-mid.hs
new file mode 100644
index 000000000..fe13ecfa1
--- /dev/null
+++ b/test/command/6466-mid.hs
@@ -0,0 +1,10 @@
+-- DO NOT WANT
+
+want :: Int
+want = 42
+
+-- AVOID EXCLUSIVE RIGHT BOUND
+
+-- DO NOT WANT
+doNotWant :: Int
+doNotWant = 999
diff --git a/test/command/6466-whole.hs b/test/command/6466-whole.hs
new file mode 100644
index 000000000..cd2efb02f
--- /dev/null
+++ b/test/command/6466-whole.hs
@@ -0,0 +1,2 @@
+want :: Int
+want = 42
diff --git a/test/command/6466.md b/test/command/6466.md
new file mode 100644
index 000000000..61a9ccd59
--- /dev/null
+++ b/test/command/6466.md
@@ -0,0 +1,23 @@
+```
+% pandoc -f org -t native
+Stuff
+
+#+INCLUDE: "command/6466-beg.hs" src haskell :lines "-4"
+#+INCLUDE: "command/6466-mid.hs" src haskell :lines "3-6"
+#+INCLUDE: "command/6466-end.hs" src haskell :lines "3-"
+#+INCLUDE: "command/6466-whole.hs" src haskell :lines "-"
+
+More stuff
+^D
+[ Para [ Str "Stuff" ]
+, CodeBlock
+    ( "" , [ "haskell" ] , [] ) "want :: Int\nwant = 42\n\n"
+, CodeBlock
+    ( "" , [ "haskell" ] , [] ) "want :: Int\nwant = 42\n\n"
+, CodeBlock
+    ( "" , [ "haskell" ] , [] ) "want :: Int\nwant = 42\n"
+, CodeBlock
+    ( "" , [ "haskell" ] , [] ) "want :: Int\nwant = 42\n"
+, Para [ Str "More" , Space , Str "stuff" ]
+]
+```