@@ -121,7 +121,6 @@ module Text.Pandoc.Parsing ( module Text.Pandoc.Sources,
122 extractIdClass,
122 extractIdClass,
123 insertIncludedFile,
123 insertIncludedFile,
124− insertIncludedFileF,
125 -- * Re-exports from Text.Parsec
124 -- * Re-exports from Text.Parsec
127 runParser,
126 runParser,
@@ -1638,12 +1637,15 @@ extractIdClass (ident, cls, kvs) = (ident', cls', kvs')
1638 cls' = maybe cls T.words $ lookup "class" kvs
1637 cls' = maybe cls T.words $ lookup "class" kvs
1639 kvs' = filter (\(k,_) -> k /= "id" || k /= "class") kvs
1638 kvs' = filter (\(k,_) -> k /= "id" || k /= "class") kvs
1641−insertIncludedFile' :: (PandocMonad m, HasIncludeFiles st)
1640+insertIncludedFile :: (PandocMonad m, HasIncludeFiles st)
1642− => ParserT a st m (mf Blocks)
1641+ => ParserT a st m b -- ^ parser to apply
1643− -> (Text -> a)
1642+ -> (Text -> a) -- ^ convert Text to stream type
1644− -> [FilePath] -> FilePath
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)
1645− -> ParserT a st m (mf Blocks)
1647+ -> ParserT a st m b
1646−insertIncludedFile' blocks totoks dirs f = do
1648+insertIncludedFile parser toStream dirs f mbstartline mbendline = do
1647 oldPos <- getPosition
1649 oldPos <- getPosition
1648 oldInput <- getInput
1650 oldInput <- getInput
1649 containers <- getIncludeFiles <$> getState
1651 containers <- getIncludeFiles <$> getState
@@ -1652,33 +1654,32 @@ insertIncludedFile' blocks totoks dirs f = do
1652 updateState $ addIncludeFile $ T.pack f
1654 updateState $ addIncludeFile $ T.pack f
1653 mbcontents <- readFileFromDirs dirs f
1655 mbcontents <- readFileFromDirs dirs f
1654 contents <- case mbcontents of
1656 contents <- case mbcontents of
1655− Just s -> return s
1657+ Just s -> return $ exciseLines mbstartline mbendline s
1656 Nothing -> do
1658 Nothing -> do
1657 report $ CouldNotLoadIncludeFile (T.pack f) oldPos
1659 report $ CouldNotLoadIncludeFile (T.pack f) oldPos
1658 return ""
1660 return ""
1661+ setInput $ toStream contents
1659− setPosition $ newPos f 1 1
1662+ setPosition $ newPos f (fromMaybe 1 mbstartline) 1
1660− setInput $ totoks contents
1663+ result <- parser
1662 setInput oldInput
1664 setInput oldInput
1663 setPosition oldPos
1665 setPosition oldPos
1664 updateState dropLatestIncludeFile
1666 updateState dropLatestIncludeFile
1665− return bs
1667+ return result
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
1675+ contentLines = T.lines t
1676+ numLines = length contentLines
1677+ startline' = case mbstartline of
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
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
1672− -> [FilePath] -> FilePath
1673− -> ParserT [a] st m Blocks
1674−insertIncludedFile blocks totoks dirs f =
1675− runIdentity <$> insertIncludedFile' (Identity <$> blocks) totoks dirs f
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)])