diff options
author | Jasper Van der Jeugt <jaspervdj@gmail.com> | 2010-12-24 08:42:05 +0100 |
---|---|---|
committer | Jasper Van der Jeugt <jaspervdj@gmail.com> | 2010-12-24 08:42:05 +0100 |
commit | 4b7c42d644a1fb2242ad79a2193edad4ba6b2b7e (patch) | |
tree | 1d5a45afd55b7eba093018eef81d9fde10ef3417 /src/Hakyll | |
parent | 4bdd93b331eec642873262a524b6b3a6132eb1c9 (diff) | |
download | hakyll-4b7c42d644a1fb2242ad79a2193edad4ba6b2b7e.tar.gz |
Add resource provider modules
Diffstat (limited to 'src/Hakyll')
-rw-r--r-- | src/Hakyll/Core/ResourceProvider.hs | 18 | ||||
-rw-r--r-- | src/Hakyll/Core/ResourceProvider/FileResourceProvider.hs | 44 |
2 files changed, 62 insertions, 0 deletions
diff --git a/src/Hakyll/Core/ResourceProvider.hs b/src/Hakyll/Core/ResourceProvider.hs new file mode 100644 index 0000000..7b4f94a --- /dev/null +++ b/src/Hakyll/Core/ResourceProvider.hs @@ -0,0 +1,18 @@ +-- | This module provides an API for resource providers. Resource providers +-- allow Hakyll to get content from resources; the type of resource depends on +-- the concrete instance. +-- +module Hakyll.Core.ResourceProvider + ( ResourceProvider (..) + ) where + +import Hakyll.Core.Identifier + +-- | A value responsible for retrieving and listing resources +-- +data ResourceProvider = ResourceProvider + { -- | A list of all resources this provider is able to provide + resourceList :: [Identifier] + , -- | Retrieve a certain resource as string + resourceString :: Identifier -> IO String + } diff --git a/src/Hakyll/Core/ResourceProvider/FileResourceProvider.hs b/src/Hakyll/Core/ResourceProvider/FileResourceProvider.hs new file mode 100644 index 0000000..b682634 --- /dev/null +++ b/src/Hakyll/Core/ResourceProvider/FileResourceProvider.hs @@ -0,0 +1,44 @@ +-- | A concrete 'ResourceProvider' that gets it's resources from the filesystem +-- +module Hakyll.Core.ResourceProvider.FileResourceProvider + ( fileResourceProvider + ) where + +import Control.Applicative ((<$>)) +import Control.Monad (forM) + +import System.Directory (doesDirectoryExist, getDirectoryContents) +import System.FilePath ((</>), normalise) + +import Hakyll.Core.ResourceProvider +import Hakyll.Core.Identifier + +-- | Create a filesystem-based 'ResourceProvider' +-- +fileResourceProvider :: IO ResourceProvider +fileResourceProvider = do + list <- map parseIdentifier <$> getRecursiveContents "." + return $ ResourceProvider + { resourceList = list + , resourceString = readFile . toFilePath + } + +-- | Get all contents of a directory. Note that files starting with a dot (.) +-- will be ignored. +-- +getRecursiveContents :: FilePath -> IO [FilePath] +getRecursiveContents topdir = do + topdirExists <- doesDirectoryExist topdir + if topdirExists + then do names <- getDirectoryContents topdir + let properNames = filter isProper names + paths <- forM properNames $ \name -> do + let path = topdir </> name + isDirectory <- doesDirectoryExist path + if isDirectory + then getRecursiveContents path + else return [normalise path] + return (concat paths) + else return [] + where + isProper = not . (== '.') . head |