2012-11-08 11:45:26 +00:00
|
|
|
--------------------------------------------------------------------------------
|
2012-11-18 20:56:52 +00:00
|
|
|
module Hakyll.Core.Provider.Internal
|
|
|
|
( Provider (..)
|
|
|
|
, newProvider
|
2012-11-08 11:45:26 +00:00
|
|
|
|
|
|
|
, resourceList
|
|
|
|
, resourceExists
|
|
|
|
, resourceMetadataResource
|
|
|
|
|
2012-11-19 13:59:55 +00:00
|
|
|
, resourceFilePath
|
2012-11-08 11:45:26 +00:00
|
|
|
, resourceString
|
|
|
|
, resourceLBS
|
|
|
|
) where
|
|
|
|
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
2012-11-19 13:59:55 +00:00
|
|
|
import Control.Applicative ((<$>))
|
|
|
|
import qualified Data.ByteString.Lazy as BL
|
2012-11-08 11:45:26 +00:00
|
|
|
import Data.IORef
|
2012-11-19 13:59:55 +00:00
|
|
|
import Data.Map (Map)
|
|
|
|
import qualified Data.Map as M
|
|
|
|
import Data.Set (Set)
|
|
|
|
import qualified Data.Set as S
|
|
|
|
import System.FilePath (addExtension, (</>))
|
2012-11-08 11:45:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
2012-11-19 13:59:55 +00:00
|
|
|
import Hakyll.Core.Identifier
|
2012-11-08 11:45:26 +00:00
|
|
|
import Hakyll.Core.Store
|
|
|
|
import Hakyll.Core.Util.File
|
|
|
|
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- | Responsible for retrieving and listing resources
|
2012-11-18 20:56:52 +00:00
|
|
|
data Provider = Provider
|
2012-11-19 13:59:55 +00:00
|
|
|
{ -- Top of the provided directory
|
|
|
|
providerDirectory :: FilePath
|
|
|
|
, -- | A list of all files found
|
2012-11-18 20:56:52 +00:00
|
|
|
providerSet :: Set Identifier
|
2012-11-08 11:45:26 +00:00
|
|
|
, -- | Cache keeping track of modified files
|
2012-11-18 20:56:52 +00:00
|
|
|
providerModifiedCache :: IORef (Map Identifier Bool)
|
2012-11-08 11:45:26 +00:00
|
|
|
, -- | Underlying persistent store for caching
|
2012-11-18 20:56:52 +00:00
|
|
|
providerStore :: Store
|
2012-11-08 11:45:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- | Create a resource provider
|
2012-11-18 20:56:52 +00:00
|
|
|
newProvider :: Store -- ^ Store to use
|
|
|
|
-> (FilePath -> Bool) -- ^ Should we ignore this file?
|
|
|
|
-> FilePath -- ^ Search directory
|
|
|
|
-> IO Provider -- ^ Resulting provider
|
|
|
|
newProvider store ignore directory = do
|
2013-02-06 21:40:18 +00:00
|
|
|
list <- map fromFilePath <$> getRecursiveContents ignore directory
|
2012-11-08 11:45:26 +00:00
|
|
|
cache <- newIORef M.empty
|
2012-11-19 13:59:55 +00:00
|
|
|
return $ Provider directory (S.fromList list) cache store
|
2012-11-08 11:45:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
2012-11-18 20:56:52 +00:00
|
|
|
resourceList :: Provider -> [Identifier]
|
|
|
|
resourceList = S.toList . providerSet
|
2012-11-08 11:45:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
2012-11-12 15:10:06 +00:00
|
|
|
-- | Check if a given resource exists
|
2012-11-18 20:56:52 +00:00
|
|
|
resourceExists :: Provider -> Identifier -> Bool
|
2012-11-09 15:34:45 +00:00
|
|
|
resourceExists provider =
|
2012-11-18 20:56:52 +00:00
|
|
|
(`S.member` providerSet provider) . setVersion Nothing
|
2012-11-08 11:45:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- | Each resource may have an associated metadata resource (with a @.metadata@
|
|
|
|
-- filename)
|
2012-11-13 12:13:17 +00:00
|
|
|
resourceMetadataResource :: Identifier -> Identifier
|
2012-11-09 15:34:45 +00:00
|
|
|
resourceMetadataResource =
|
2012-11-12 15:10:06 +00:00
|
|
|
fromFilePath . flip addExtension "metadata" . toFilePath
|
2012-11-08 11:45:26 +00:00
|
|
|
|
|
|
|
|
2012-11-19 13:59:55 +00:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
resourceFilePath :: Provider -> Identifier -> FilePath
|
|
|
|
resourceFilePath p i = providerDirectory p </> toFilePath i
|
|
|
|
|
|
|
|
|
2012-11-08 11:45:26 +00:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- | Get the raw body of a resource as string
|
2012-11-19 13:59:55 +00:00
|
|
|
resourceString :: Provider -> Identifier -> IO String
|
|
|
|
resourceString p i = readFile $ resourceFilePath p i
|
2012-11-08 11:45:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- | Get the raw body of a resource of a lazy bytestring
|
2012-11-19 13:59:55 +00:00
|
|
|
resourceLBS :: Provider -> Identifier -> IO BL.ByteString
|
|
|
|
resourceLBS p i = BL.readFile $ resourceFilePath p i
|