hakyll/src/Hakyll/Core/Metadata.hs

64 lines
2.3 KiB
Haskell
Raw Normal View History

2012-11-09 15:34:45 +00:00
--------------------------------------------------------------------------------
module Hakyll.Core.Metadata
( Metadata
2012-11-22 12:38:28 +00:00
, MonadMetadata (..)
2013-03-07 10:18:34 +00:00
, getMetadataField
, getMetadataField'
2013-03-05 11:51:27 +00:00
, makePatternDependency
2012-11-09 15:34:45 +00:00
) where
--------------------------------------------------------------------------------
2012-11-22 12:38:28 +00:00
import Control.Monad (forM)
2012-11-13 22:59:49 +00:00
import Data.Map (Map)
2013-03-07 10:18:34 +00:00
import qualified Data.Map as M
2012-11-09 15:34:45 +00:00
2012-11-22 12:38:28 +00:00
--------------------------------------------------------------------------------
2013-03-05 11:51:27 +00:00
import Hakyll.Core.Dependencies
2012-11-22 12:38:28 +00:00
import Hakyll.Core.Identifier
import Hakyll.Core.Identifier.Pattern
2012-11-09 15:34:45 +00:00
--------------------------------------------------------------------------------
type Metadata = Map String String
2012-11-22 12:38:28 +00:00
--------------------------------------------------------------------------------
class Monad m => MonadMetadata m where
getMetadata :: Identifier -> m Metadata
getMatches :: Pattern -> m [Identifier]
getAllMetadata :: Pattern -> m [(Identifier, Metadata)]
getAllMetadata pattern = do
matches' <- getMatches pattern
forM matches' $ \id' -> do
metadata <- getMetadata id'
return (id', metadata)
2013-03-05 11:51:27 +00:00
2013-03-07 10:18:34 +00:00
--------------------------------------------------------------------------------
getMetadataField :: MonadMetadata m => Identifier -> String -> m (Maybe String)
getMetadataField identifier key = do
metadata <- getMetadata identifier
return $ M.lookup key metadata
--------------------------------------------------------------------------------
-- | Version of 'getMetadataField' which throws an error if the field does not
-- exist.
getMetadataField' :: MonadMetadata m => Identifier -> String -> m String
getMetadataField' identifier key = do
field <- getMetadataField identifier key
case field of
Just v -> return v
Nothing -> fail $ "Hakyll.Core.Metadata.getMetadataField': " ++
"Item " ++ show identifier ++ " has no metadata field " ++ show key
2013-03-05 11:51:27 +00:00
--------------------------------------------------------------------------------
makePatternDependency :: MonadMetadata m => Pattern -> m Dependency
makePatternDependency pattern = do
matches' <- getMatches pattern
return $ PatternDependency pattern matches'