git.iko.soy/typeable/pandoc
on master help sign in
Text.Pandoc.Class: add insertInFileTree (API change).

This gives a pure way to insert an ersatz file into a FileTree. In addition, we normalize paths both on insertion and on lookup, so that "foo" and "./foo" will be judged equivalent.

commitddd6a892470181b321ac2f80ffb76809f15419c8 authorJohn MacFarlane <jgm@berkeley.edu> dateThu 28 Dec 2017, 10:01 (9 years ago) parent999dd2a
2 files changed+15−14
src/Text/Pandoc/Class.hs+13−7 test/Tests/Readers/Org/Directive.hs+2−7
src/Text/Pandoc/Class.hs+13−7
@@ -81,6 +81,7 @@ module Text.Pandoc.Class ( PandocMonad(..)
8181 , FileTree(..)
8282 , FileInfo(..)
8383 , addToFileTree
84+ , insertInFileTree
8485 , runIO
8586 , runIOorExplode
8687 , runPure
@@ -141,7 +142,7 @@ import qualified System.Environment as IO (lookupEnv)
141142import System.FilePath.Glob (match, compile)
142143import System.Directory (createDirectoryIfMissing, getDirectoryContents,
143144 doesDirectoryExist)
144−import System.FilePath ((</>), (<.>), takeDirectory,
145+import System.FilePath ((</>), (<.>), takeDirectory, makeRelative,
145146 takeExtension, dropExtension, isRelative, normalise)
146147import qualified System.FilePath.Glob as IO (glob)
147148import qualified System.Directory as IO (getModificationTime)
@@ -912,12 +913,13 @@ newtype FileTree = FileTree {unFileTree :: M.Map FilePath FileInfo}
912913 deriving (Monoid)
913914
914915getFileInfo :: FilePath -> FileTree -> Maybe FileInfo
915−getFileInfo fp tree = M.lookup fp $ unFileTree tree
916+getFileInfo fp tree =
917+ M.lookup (makeRelative "." fp) (unFileTree tree)
916918
917919-- | Add the specified file to the FileTree. If file
918920-- is a directory, add its contents recursively.
919921addToFileTree :: FileTree -> FilePath -> IO FileTree
920−addToFileTree (FileTree treemap) fp = do
922+addToFileTree tree fp = do
921923 isdir <- doesDirectoryExist fp
922924 if isdir
923925 then do -- recursively add contents of directories
@@ -925,13 +927,17 @@ addToFileTree (FileTree treemap) fp = do
925927 isSpecial "." = True
926928 isSpecial _ = False
927929 fs <- (map (fp </>) . filter (not . isSpecial)) <$> getDirectoryContents fp
928− foldM addToFileTree (FileTree treemap) fs
930+ foldM addToFileTree tree fs
929931 else do
930932 contents <- B.readFile fp
931933 mtime <- IO.getModificationTime fp
932− return $ FileTree $
933− M.insert fp FileInfo{ infoFileMTime = mtime
934− , infoFileContents = contents } treemap
934+ return $ insertInFileTree fp FileInfo{ infoFileMTime = mtime
935+ , infoFileContents = contents } tree
936+
937+-- | Insert an ersatz file into the 'FileTree'.
938+insertInFileTree :: FilePath -> FileInfo -> FileTree -> FileTree
939+insertInFileTree fp info (FileTree treemap) =
940+ FileTree $ M.insert (makeRelative "." fp) info treemap
935941
936942newtype PandocPure a = PandocPure {
937943 unPandocPure :: ExceptT PandocError
test/Tests/Readers/Org/Directive.hs+2−7
@@ -1,7 +1,6 @@
11{-# LANGUAGE OverloadedStrings #-}
22module Tests.Readers.Org.Directive (tests) where
33
4−import Control.Arrow (second)
54import Data.Time (UTCTime (UTCTime), secondsToDiffTime)
65import Data.Time.Calendar (Day (ModifiedJulianDay))
76import Test.Tasty (TestTree, testGroup)
@@ -10,7 +9,6 @@ import Tests.Readers.Org.Shared ((=:), tagSpan)
109import Text.Pandoc
1110import Text.Pandoc.Builder
1211import qualified Data.ByteString as BS
13−import qualified Data.Map as Map
1412import qualified Data.Text as T
1513
1614testWithFiles :: (ToString c)
@@ -31,11 +29,8 @@ orgWithFiles fileDefs input =
3129files :: [(FilePath, BS.ByteString)] -> FileTree
3230files fileDefs =
3331 let dummyTime = UTCTime (ModifiedJulianDay 125) (secondsToDiffTime 0)
34− fileInfo content = FileInfo
35− { infoFileMTime = dummyTime
36− , infoFileContents = content
37− }
38− in FileTree (Map.fromList (map (second fileInfo) fileDefs))
32+ in foldr (\(fp, bs) -> insertInFileTree fp (FileInfo dummyTime bs))
33+ mempty fileDefs
3934
4035tests :: [TestTree]
4136tests =