summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Hakyll/Core/Metadata.hs22
-rw-r--r--src/Hakyll/Web/Template/Context.hs4
2 files changed, 24 insertions, 2 deletions
diff --git a/src/Hakyll/Core/Metadata.hs b/src/Hakyll/Core/Metadata.hs
index 9fc2d3e..7902b94 100644
--- a/src/Hakyll/Core/Metadata.hs
+++ b/src/Hakyll/Core/Metadata.hs
@@ -2,6 +2,8 @@
module Hakyll.Core.Metadata
( Metadata
, MonadMetadata (..)
+ , getMetadataField
+ , getMetadataField'
, makePatternDependency
) where
@@ -9,6 +11,7 @@ module Hakyll.Core.Metadata
--------------------------------------------------------------------------------
import Control.Monad (forM)
import Data.Map (Map)
+import qualified Data.Map as M
--------------------------------------------------------------------------------
@@ -35,6 +38,25 @@ class Monad m => MonadMetadata m where
--------------------------------------------------------------------------------
+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
+
+
+--------------------------------------------------------------------------------
makePatternDependency :: MonadMetadata m => Pattern -> m Dependency
makePatternDependency pattern = do
matches' <- getMatches pattern
diff --git a/src/Hakyll/Web/Template/Context.hs b/src/Hakyll/Web/Template/Context.hs
index 1a9aba3..f9c96d0 100644
--- a/src/Hakyll/Web/Template/Context.hs
+++ b/src/Hakyll/Web/Template/Context.hs
@@ -100,8 +100,8 @@ bodyField key = field key $ return . itemBody
-- | Map any field to its metadata value, if present
metadataField :: Context String
metadataField = Context $ \k i -> do
- metadata <- getMetadata $ itemIdentifier i
- maybe empty return $ M.lookup k metadata
+ value <- getMetadataField (itemIdentifier i) k
+ maybe empty return value
--------------------------------------------------------------------------------