summaryrefslogtreecommitdiff
path: root/src/Hakyll/Core/Provider/Internal.hs
blob: 1360ef5d26f9690f000a90a607f75ed1a706d6ae (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
87
88
89
90
91
92
93
94
--------------------------------------------------------------------------------
module Hakyll.Core.Provider.Internal
    ( Provider (..)
    , newProvider

    , resourceList
    , resourceExists
    , resourceMetadataResource

    , resourceFilePath
    , 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.Identifier
import           Hakyll.Core.Store
import           Hakyll.Core.Util.File


--------------------------------------------------------------------------------
-- | Responsible for retrieving and listing resources
data Provider = Provider
    { -- Top of the provided directory
      providerDirectory     :: FilePath
    , -- | 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 directory
    cache <- newIORef M.empty
    return $ Provider directory (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


--------------------------------------------------------------------------------
resourceFilePath :: Provider -> Identifier -> FilePath
resourceFilePath p i = providerDirectory p </> toFilePath i


--------------------------------------------------------------------------------
-- | Get the raw body of a resource as string
resourceString :: Provider -> Identifier -> IO String
resourceString p i = readFile $ resourceFilePath p i


--------------------------------------------------------------------------------
-- | Get the raw body of a resource of a lazy bytestring
resourceLBS :: Provider -> Identifier -> IO BL.ByteString
resourceLBS p i = BL.readFile $ resourceFilePath p i