summaryrefslogtreecommitdiff
path: root/src/Hakyll/Core/Provider/Internal.hs
blob: 54332a9f848f8fb0bce8a8e300b4bfa4bfd97cce (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.Provider.Internal
    ( Provider (..)
    , newProvider

    , 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 Provider = Provider
    { -- | A list of all files found
      providerSet           :: Set Identifier
    , -- | Cache keeping track of modified files
      providerModifiedCache :: IORef (Map Identifier Bool)
    , -- | Underlying persistent store for caching
      providerStore         :: Store
    }


--------------------------------------------------------------------------------
-- | Create a resource provider
newProvider :: Store               -- ^ Store to use
            -> (FilePath -> Bool)  -- ^ Should we ignore this file?
            -> FilePath            -- ^ Search directory
            -> IO Provider         -- ^ Resulting provider
newProvider store ignore directory = do
    list  <- map fromFilePath . filter (not . ignore) <$>
        getRecursiveContents False directory
    cache <- newIORef M.empty
    return $ Provider (S.fromList list) cache store


--------------------------------------------------------------------------------
resourceList :: Provider -> [Identifier]
resourceList = S.toList . providerSet


--------------------------------------------------------------------------------
-- | Check if a given resource exists
resourceExists :: Provider -> Identifier -> Bool
resourceExists provider =
    (`S.member` providerSet 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