summaryrefslogtreecommitdiff
path: root/src/Hakyll/Core/ResourceProvider/Internal.hs
blob: 628d1b55f9620e936309f658adfbc6eb937d9c24 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
--------------------------------------------------------------------------------
module Hakyll.Core.ResourceProvider.Internal
    ( 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
import           Hakyll.Core.Identifier


--------------------------------------------------------------------------------
-- | Responsible for retrieving and listing resources
data ResourceProvider = ResourceProvider
    { -- | A list of all files found
      resourceSet           :: Set Identifier
    , -- | Cache keeping track of modified files
      resourceModifiedCache :: IORef (Map Identifier Bool)
    , -- | 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
    list  <- map fromFilePath . filter (not . ignore) <$>
        getRecursiveContents False directory
    cache <- newIORef M.empty
    return $ ResourceProvider (S.fromList list) cache store


--------------------------------------------------------------------------------
resourceList :: ResourceProvider -> [Identifier]
resourceList = S.toList . resourceSet


--------------------------------------------------------------------------------
-- | Check if a given resource exists
resourceExists :: ResourceProvider -> Identifier -> Bool
resourceExists provider =
    (`S.member` resourceSet provider) . setVersion Nothing


--------------------------------------------------------------------------------
-- | Each resource may have an associated metadata resource (with a @.metadata@
-- filename)
resourceMetadataResource :: Identifier -> Identifier
resourceMetadataResource =
    fromFilePath . flip addExtension "metadata" . toFilePath


--------------------------------------------------------------------------------
-- | Get the raw body of a resource as string
resourceString :: Identifier -> IO String
resourceString = readFile . toFilePath


--------------------------------------------------------------------------------
-- | Get the raw body of a resource of a lazy bytestring
resourceLBS :: Identifier -> IO BL.ByteString
resourceLBS = BL.readFile . toFilePath