Org reader: support line selection in INCLUDE directives. (#8060)

This commit is contained in:
Brian Leung 2022-05-20 00:46:16 -07:00 committed by GitHub
parent ed054b4c74
commit ae8ed408b0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 55 additions and 1 deletions

View file

@ -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

8
test/command/6466-beg.hs Normal file
View file

@ -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

4
test/command/6466-end.hs Normal file
View file

@ -0,0 +1,4 @@
-- DO NOT WANT
want :: Int
want = 42

10
test/command/6466-mid.hs Normal file
View file

@ -0,0 +1,10 @@
-- DO NOT WANT
want :: Int
want = 42
-- AVOID EXCLUSIVE RIGHT BOUND
-- DO NOT WANT
doNotWant :: Int
doNotWant = 999

View file

@ -0,0 +1,2 @@
want :: Int
want = 42

23
test/command/6466.md Normal file
View file

@ -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" ]
]
```