git.iko.soy/typeable/pandoc
on master help sign in
T.P.Parsing: improve include file functions.
05ea507John MacFarlane
src/Text/Pandoc/Parsing.hs+31−30
@@ -121,7 +121,6 @@ module Text.Pandoc.Parsing ( module Text.Pandoc.Sources,
121 (<+?>),
121 (<+?>),
122 extractIdClass,
122 extractIdClass,
123 insertIncludedFile,
123 insertIncludedFile,
124− insertIncludedFileF,
125 -- * Re-exports from Text.Parsec
124 -- * Re-exports from Text.Parsec
126 Stream,
125 Stream,
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
1640
1639
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
1661− bs <- blocks
1662 setInput oldInput
1664 setInput oldInput
1663 setPosition oldPos
1665 setPosition oldPos
1664 updateState dropLatestIncludeFile
1666 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
1666
1685
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)
44import qualified Data.Text as T
44import qualified Data.Text as T
45import qualified Text.Pandoc.Builder as B
45import qualified Text.Pandoc.Builder as B
46import qualified Text.Pandoc.Walk as Walk
46import qualified Text.Pandoc.Walk as Walk
47+import Text.Pandoc.Sources (ToSources(..))
47
48
48--
49--
49-- parsing blocks
50-- parsing blocks
@@ -527,7 +528,7 @@ include = try $ do
527 _ -> nullAttr
528 _ -> nullAttr
528 return $ pure . B.codeBlockWith attr <$> parseRaw
529 return $ pure . B.codeBlockWith attr <$> parseRaw
529 _ -> return $ return . B.fromList . blockFilter params <$> blockList
530 _ -> return $ return . B.fromList . blockFilter params <$> blockList
530− insertIncludedFileF blocksParser ["."] filename
531+ insertIncludedFile blocksParser toSources ["."] filename Nothing Nothing
531 where
532 where
532 includeTarget :: PandocMonad m => OrgParser m FilePath
533 includeTarget :: PandocMonad m => OrgParser m FilePath
533 includeTarget = do
534 includeTarget = do
src/Text/Pandoc/Readers/Org/Parsing.hs+1−2
@@ -63,8 +63,7 @@ module Text.Pandoc.Readers.Org.Parsing
63 , ellipses
63 , ellipses
64 , citeKey
64 , citeKey
65 , gridTableWith
65 , gridTableWith
66− , insertIncludedFileF
66+ , insertIncludedFile
67− -- * Re-exports from Text.Pandoc.Parsec
68 , runParser
67 , runParser
69 , runParserT
68 , runParserT
70 , getInput
69 , getInput