hakyll/src/Hakyll/Core/ResourceProvider/Internal.hs

87 lines
3.2 KiB
Haskell
Raw Normal View History

2012-11-08 11:45:26 +00:00
--------------------------------------------------------------------------------
2012-11-09 15:34:45 +00:00
module Hakyll.Core.ResourceProvider.Internal
2012-11-08 11:45:26 +00:00
( ResourceProvider (..)
, newResourceProvider
, resourceList
, resourceExists
, resourceMetadataResource
, resourceString
, resourceLBS
) where
--------------------------------------------------------------------------------
import Control.Applicative ((<$>))
import qualified Data.ByteString.Lazy as BL
import Data.IORef
import Data.Map (Map)
import qualified Data.Map as M
import Data.Set (Set)
import qualified Data.Set as S
import System.FilePath (addExtension)
--------------------------------------------------------------------------------
import Hakyll.Core.Store
import Hakyll.Core.Util.File
2012-11-09 15:34:45 +00:00
import Hakyll.Core.Identifier
2012-11-08 11:45:26 +00:00
--------------------------------------------------------------------------------
-- | Responsible for retrieving and listing resources
data ResourceProvider = ResourceProvider
{ -- | A list of all files found
2012-11-09 15:34:45 +00:00
resourceSet :: Set (Identifier ())
2012-11-08 11:45:26 +00:00
, -- | Cache keeping track of modified files
2012-11-09 15:34:45 +00:00
resourceModifiedCache :: IORef (Map (Identifier ()) Bool)
2012-11-08 11:45:26 +00:00
, -- | Underlying persistent store for caching
resourceStore :: Store
}
--------------------------------------------------------------------------------
-- | Create a resource provider
newResourceProvider :: Store -- ^ Store to use
-> (FilePath -> Bool) -- ^ Should we ignore this file?
-> FilePath -- ^ Search directory
-> IO ResourceProvider -- ^ Resulting provider
newResourceProvider store ignore directory = do
2012-11-09 15:34:45 +00:00
list <- map parseIdentifier . filter (not . ignore) <$>
2012-11-08 11:45:26 +00:00
getRecursiveContents False directory
cache <- newIORef M.empty
return $ ResourceProvider (S.fromList list) cache store
--------------------------------------------------------------------------------
2012-11-09 15:34:45 +00:00
resourceList :: ResourceProvider -> [Identifier ()]
2012-11-08 11:45:26 +00:00
resourceList = S.toList . resourceSet
--------------------------------------------------------------------------------
-- | Check if a given resiyrce exists
2012-11-09 15:34:45 +00:00
resourceExists :: ResourceProvider -> Identifier a -> Bool
resourceExists provider =
(`S.member` resourceSet provider) . setGroup Nothing . castIdentifier
2012-11-08 11:45:26 +00:00
--------------------------------------------------------------------------------
-- | Each resource may have an associated metadata resource (with a @.metadata@
-- filename)
2012-11-09 15:34:45 +00:00
resourceMetadataResource :: Identifier a -> Identifier ()
resourceMetadataResource =
parseIdentifier . flip addExtension "metadata" . toFilePath
2012-11-08 11:45:26 +00:00
--------------------------------------------------------------------------------
-- | Get the raw body of a resource as string
2012-11-09 15:34:45 +00:00
resourceString :: Identifier a -> IO String
resourceString = readFile . toFilePath
2012-11-08 11:45:26 +00:00
--------------------------------------------------------------------------------
-- | Get the raw body of a resource of a lazy bytestring
2012-11-09 15:34:45 +00:00
resourceLBS :: Identifier a -> IO BL.ByteString
resourceLBS = BL.readFile . toFilePath