git.iko.soy/typeable/pandoc
on master help sign in
T.P.Parsing: improve include file functions.

Remove old `insertIncludedFileF`. [API change] Give `insertIncludedFile` a more general type, allowing it to be used where `insertIncludedFileF` was.

commit05ea507bd75e0bb4bbb8f25cad5fa2f02e4f6796 authorJohn MacFarlane <jgm@berkeley.edu> dateSun 9 May 2021, 15:26 (5 years ago) parent6e45607
3 files changed+34−33
src/Text/Pandoc/Parsing.hs+31−30 src/Text/Pandoc/Readers/Org/Blocks.hs+2−1 src/Text/Pandoc/Readers/Org/Parsing.hs+1−2
src/Text/Pandoc/Parsing.hs+31−30
@@ -121,7 +121,6 @@ module Text.Pandoc.Parsing ( module Text.Pandoc.Sources,
121121 (<+?>),
122122 extractIdClass,
123123 insertIncludedFile,
124− insertIncludedFileF,
125124 -- * Re-exports from Text.Parsec
126125 Stream,
127126 runParser,
@@ -1638,12 +1637,15 @@ extractIdClass (ident, cls, kvs) = (ident', cls', kvs')
16381637 cls' = maybe cls T.words $ lookup "class" kvs
16391638 kvs' = filter (\(k,_) -> k /= "id" || k /= "class") kvs
16401639
1641−insertIncludedFile' :: (PandocMonad m, HasIncludeFiles st)
1642− => ParserT a st m (mf Blocks)
1643− -> (Text -> a)
1644− -> [FilePath] -> FilePath
1645− -> ParserT a st m (mf Blocks)
1646−insertIncludedFile' blocks totoks dirs f = do
1640+insertIncludedFile :: (PandocMonad m, HasIncludeFiles st)
1641+ => ParserT a st m b -- ^ parser to apply
1642+ -> (Text -> a) -- ^ convert Text to stream type
1643+ -> [FilePath] -- ^ search path (directories)
1644+ -> FilePath -- ^ path of file to include
1645+ -> Maybe Int -- ^ start line (negative counts from end)
1646+ -> Maybe Int -- ^ end line (negative counts from end)
1647+ -> ParserT a st m b
1648+insertIncludedFile parser toStream dirs f mbstartline mbendline = do
16471649 oldPos <- getPosition
16481650 oldInput <- getInput
16491651 containers <- getIncludeFiles <$> getState
@@ -1652,33 +1654,32 @@ insertIncludedFile' blocks totoks dirs f = do
16521654 updateState $ addIncludeFile $ T.pack f
16531655 mbcontents <- readFileFromDirs dirs f
16541656 contents <- case mbcontents of
1655− Just s -> return s
1657+ Just s -> return $ exciseLines mbstartline mbendline s
16561658 Nothing -> do
16571659 report $ CouldNotLoadIncludeFile (T.pack f) oldPos
16581660 return ""
1659− setPosition $ newPos f 1 1
1660− setInput $ totoks contents
1661− bs <- blocks
1661+ setInput $ toStream contents
1662+ setPosition $ newPos f (fromMaybe 1 mbstartline) 1
1663+ result <- parser
16621664 setInput oldInput
16631665 setPosition oldPos
16641666 updateState dropLatestIncludeFile
1665− return bs
1667+ return result
1668+
1669+exciseLines :: Maybe Int -> Maybe Int -> Text -> Text
1670+exciseLines Nothing Nothing t = t
1671+exciseLines mbstartline mbendline t =
1672+ T.unlines $ take (endline' - (startline' - 1))
1673+ $ drop (startline' - 1) contentLines
1674+ where
1675+ contentLines = T.lines t
1676+ numLines = length contentLines
1677+ startline' = case mbstartline of
1678+ Nothing -> 1
1679+ Just x | x >= 0 -> x
1680+ | otherwise -> numLines + x -- negative from end
1681+ endline' = case mbendline of
1682+ Nothing -> numLines
1683+ Just x | x >= 0 -> x
1684+ | otherwise -> numLines + x -- negative from end
16661685
1667−-- | Parse content of include file as blocks. Circular includes result in an
1668−-- @PandocParseError@.
1669−insertIncludedFile :: (PandocMonad m, HasIncludeFiles st)
1670− => ParserT [a] st m Blocks
1671− -> (Text -> [a])
1672− -> [FilePath] -> FilePath
1673− -> ParserT [a] st m Blocks
1674−insertIncludedFile blocks totoks dirs f =
1675− runIdentity <$> insertIncludedFile' (Identity <$> blocks) totoks dirs f
1676−
1677−-- TODO: replace this with something using addToSources.
1678−-- | Parse content of include file as future blocks. Circular includes result in
1679−-- an @PandocParseError@.
1680−insertIncludedFileF :: (PandocMonad m, HasIncludeFiles st)
1681− => ParserT Sources st m (Future st Blocks)
1682− -> [FilePath] -> FilePath
1683− -> ParserT Sources st m (Future st Blocks)
1684−insertIncludedFileF p = insertIncludedFile' p (\t -> Sources [(initialPos "",t)])
src/Text/Pandoc/Readers/Org/Blocks.hs+2−1
@@ -44,6 +44,7 @@ import Data.List.NonEmpty (nonEmpty)
4444import qualified Data.Text as T
4545import qualified Text.Pandoc.Builder as B
4646import qualified Text.Pandoc.Walk as Walk
47+import Text.Pandoc.Sources (ToSources(..))
4748
4849--
4950-- parsing blocks
@@ -527,7 +528,7 @@ include = try $ do
527528 _ -> nullAttr
528529 return $ pure . B.codeBlockWith attr <$> parseRaw
529530 _ -> return $ return . B.fromList . blockFilter params <$> blockList
530− insertIncludedFileF blocksParser ["."] filename
531+ insertIncludedFile blocksParser toSources ["."] filename Nothing Nothing
531532 where
532533 includeTarget :: PandocMonad m => OrgParser m FilePath
533534 includeTarget = do
src/Text/Pandoc/Readers/Org/Parsing.hs+1−2
@@ -63,8 +63,7 @@ module Text.Pandoc.Readers.Org.Parsing
6363 , ellipses
6464 , citeKey
6565 , gridTableWith
66− , insertIncludedFileF
67− -- * Re-exports from Text.Pandoc.Parsec
66+ , insertIncludedFile
6867 , runParser
6968 , runParserT
7069 , getInput