git.iko.soy/typeable/pandoc
on master help sign in
Class: Add getModificationTime

This is to enable macros in T2T, but can be used for other stuff in the future, I imagine. This requires building up the info in our fake file trees. Note the version in IO is safe.

commitfe0b71a2f1505e265202fd9e07458ff1e9554651 authorJesse Rosenthal <jrosenthal@jhu.edu> dateWed 30 Nov 2016, 09:21 (10 years ago) parentb53ebcd
1 file changed+37−9
src/Text/Pandoc/Class.hs+37−9
src/Text/Pandoc/Class.hs+37−9
@@ -39,6 +39,7 @@ module Text.Pandoc.Class ( PandocMonad(..)
3939 , PandocIO(..)
4040 , PandocPure(..)
4141 , PandocExecutionError(..)
42+ , FileInfo(..)
4243 , runIO
4344 , runIOorExplode
4445 , runPure
@@ -73,6 +74,7 @@ import qualified System.Environment as IO (lookupEnv)
7374import System.FilePath.Glob (match, compile)
7475import System.FilePath ((</>))
7576import qualified System.FilePath.Glob as IO (glob)
77+import qualified System.Directory as IO (getModificationTime)
7678import Control.Monad.State hiding (fail)
7779import Control.Monad.Reader hiding (fail)
7880import Control.Monad.Except hiding (fail)
@@ -80,6 +82,8 @@ import Data.Word (Word8)
8082import Data.Typeable
8183import Data.Default
8284import System.IO.Error
85+import Data.Map (Map)
86+import qualified Data.Map as M
8387
8488class (Functor m, Applicative m, Monad m, MonadError PandocExecutionError m) => PandocMonad m where
8589 lookupEnv :: String -> m (Maybe String)
@@ -105,6 +109,8 @@ class (Functor m, Applicative m, Monad m, MonadError PandocExecutionError m) =>
105109 glob :: String -> m [FilePath]
106110 setMediaBag :: MediaBag -> m ()
107111 insertMedia :: FilePath -> Maybe MimeType -> BL.ByteString -> m ()
112+ getModificationTime :: FilePath -> m UTCTime
113+
108114
109115--Some functions derived from Primitives:
110116
@@ -190,6 +196,11 @@ instance PandocMonad PandocIO where
190196 modify $ \st -> st{ioStMediaBag = mb}
191197 insertMedia fp mime bs =
192198 modify $ \st -> st{ioStMediaBag = MB.insertMedia fp mime bs (ioStMediaBag st) }
199+ getModificationTime fp = do
200+ eitherMtime <- liftIO (tryIOError $ IO.getModificationTime fp)
201+ case eitherMtime of
202+ Right mtime -> return mtime
203+ Left _ -> throwError $ PandocFileReadError fp
193204
194205data PureState = PureState { stStdGen :: StdGen
195206 , stWord8Store :: [Word8] -- should be
@@ -210,15 +221,26 @@ instance Default PureState where
210221 , stWarnings = []
211222 , stUniqStore = [1..]
212223 , stMediaBag = mempty
224+
225+
213226 }
227+data FileInfo = FileInfo { infoFileMTime :: UTCTime
228+ , infoFileContents :: B.ByteString
229+ }
230+
231+newtype FileTree = FileTree {unFileTree :: M.Map FilePath FileInfo}
232+ deriving (Monoid)
233+
234+getFileInfo :: FilePath -> FileTree -> Maybe FileInfo
235+getFileInfo fp tree = M.lookup fp $ unFileTree tree
214236
215237data PureEnv = PureEnv { envEnv :: [(String, String)]
216238 , envTime :: UTCTime
217239 , envReferenceDocx :: Archive
218240 , envReferenceODT :: Archive
219− , envFiles :: [(FilePath, B.ByteString)]
220− , envUserDataDir :: [(FilePath, B.ByteString)]
221− , envCabalDataDir :: [(FilePath, B.ByteString)]
241+ , envFiles :: FileTree
242+ , envUserDataDir :: FileTree
243+ , envCabalDataDir :: FileTree
222244 , envFontFiles :: [FilePath]
223245 }
224246
@@ -229,9 +251,9 @@ instance Default PureEnv where
229251 , envTime = posixSecondsToUTCTime 0
230252 , envReferenceDocx = emptyArchive
231253 , envReferenceODT = emptyArchive
232− , envFiles = []
233− , envUserDataDir = []
234− , envCabalDataDir = []
254+ , envFiles = mempty
255+ , envUserDataDir = mempty
256+ , envCabalDataDir = mempty
235257 , envFontFiles = []
236258 }
237259
@@ -277,7 +299,7 @@ instance PandocMonad PandocPure where
277299 _ -> M.fail "uniq store ran out of elements"
278300 readFileLazy fp = do
279301 fps <- asks envFiles
280− case lookup fp fps of
302+ case infoFileContents <$> getFileInfo fp fps of
281303 Just bs -> return (BL.fromStrict bs)
282304 Nothing -> throwError $ PandocFileReadError fp
283305 readDataFile Nothing "reference.docx" = do
@@ -289,13 +311,13 @@ instance PandocMonad PandocPure where
289311 BL.toStrict <$> (readFileLazy fname')
290312 readDataFile (Just userDir) fname = do
291313 userDirFiles <- asks envUserDataDir
292− case lookup (userDir </> fname) userDirFiles of
314+ case infoFileContents <$> (getFileInfo (userDir </> fname) userDirFiles) of
293315 Just bs -> return bs
294316 Nothing -> readDataFile Nothing fname
295317 fail = M.fail
296318 fetchItem _ fp = do
297319 fps <- asks envFiles
298− case lookup fp fps of
320+ case infoFileContents <$> (getFileInfo fp fps) of
299321 Just bs -> return (Right (bs, getMimeType fp))
300322 Nothing -> return (Left $ E.toException $ PandocFileReadError fp)
301323
@@ -317,3 +339,9 @@ instance PandocMonad PandocPure where
317339
318340 insertMedia fp mime bs =
319341 modify $ \st -> st{stMediaBag = MB.insertMedia fp mime bs (stMediaBag st) }
342+
343+ getModificationTime fp = do
344+ fps <- asks envFiles
345+ case infoFileMTime <$> (getFileInfo fp fps) of
346+ Just tm -> return tm
347+ Nothing -> throwError $ PandocFileReadError fp