summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/Hakyll/Core/Compiler.hs12
-rw-r--r--src/Hakyll/Core/Compiler/Internal.hs33
-rw-r--r--src/Hakyll/Core/Compiler/Require.hs5
-rw-r--r--src/Hakyll/Core/Metadata.hs20
-rw-r--r--src/Hakyll/Core/Rules.hs1
-rw-r--r--src/Hakyll/Core/Rules/Internal.hs16
-rw-r--r--src/Hakyll/Web/Template/Context.hs1
7 files changed, 68 insertions, 20 deletions
diff --git a/src/Hakyll/Core/Compiler.hs b/src/Hakyll/Core/Compiler.hs
index 7193e4f..c542ce7 100644
--- a/src/Hakyll/Core/Compiler.hs
+++ b/src/Hakyll/Core/Compiler.hs
@@ -6,7 +6,6 @@ module Hakyll.Core.Compiler
, getUnderlying
, makeItem
, getRoute
- , getMetadata
, getResourceBody
, getResourceString
, getResourceLBS
@@ -32,11 +31,9 @@ import System.Environment (getProgName)
--------------------------------------------------------------------------------
import Hakyll.Core.Compiler.Internal
import Hakyll.Core.Compiler.Require
-import Hakyll.Core.Dependencies
import Hakyll.Core.Identifier
import Hakyll.Core.Item
import Hakyll.Core.Logger as Logger
-import Hakyll.Core.Metadata
import Hakyll.Core.Provider
import Hakyll.Core.Routes
import qualified Hakyll.Core.Store as Store
@@ -63,15 +60,6 @@ getRoute identifier = do
return $ runRoutes routes identifier
-
---------------------------------------------------------------------------------
-getMetadata :: Identifier -> Compiler Metadata
-getMetadata identifier = do
- provider <- compilerProvider <$> compilerAsk
- compilerTellDependencies [IdentifierDependency identifier]
- compilerUnsafeIO $ resourceMetadata provider identifier
-
-
--------------------------------------------------------------------------------
-- | Get the body of the underlying resource
getResourceBody :: Compiler (Item String)
diff --git a/src/Hakyll/Core/Compiler/Internal.hs b/src/Hakyll/Core/Compiler/Internal.hs
index 89227ef..981ddda 100644
--- a/src/Hakyll/Core/Compiler/Internal.hs
+++ b/src/Hakyll/Core/Compiler/Internal.hs
@@ -25,16 +25,18 @@ module Hakyll.Core.Compiler.Internal
--------------------------------------------------------------------------------
-import Control.Applicative (Alternative (..),
- Applicative (..))
-import Control.Exception (SomeException, handle)
-import Data.Monoid (Monoid (..))
+import Control.Applicative (Alternative (..),
+ Applicative (..), (<$>))
+import Control.Exception (SomeException, handle)
+import Data.Monoid (Monoid (..))
--------------------------------------------------------------------------------
import Hakyll.Core.Dependencies
import Hakyll.Core.Identifier
+import Hakyll.Core.Identifier.Pattern
import Hakyll.Core.Logger
+import Hakyll.Core.Metadata
import Hakyll.Core.Provider
import Hakyll.Core.Routes
import Hakyll.Core.Store
@@ -128,6 +130,12 @@ instance Applicative Compiler where
--------------------------------------------------------------------------------
+instance MonadMetadata Compiler where
+ getMetadata = compilerGetMetadata
+ getMatches = compilerGetMatches
+
+
+--------------------------------------------------------------------------------
runCompiler :: Compiler a -> CompilerRead -> IO (CompilerResult a)
runCompiler compiler read' = handle handler $ unCompiler compiler read'
where
@@ -195,3 +203,20 @@ compilerTellDependencies ds = compilerTell mempty {compilerDependencies = ds}
compilerTellCacheHits :: Int -> Compiler ()
compilerTellCacheHits ch = compilerTell mempty {compilerCacheHits = ch}
{-# INLINE compilerTellCacheHits #-}
+
+
+--------------------------------------------------------------------------------
+compilerGetMetadata :: Identifier -> Compiler Metadata
+compilerGetMetadata identifier = do
+ provider <- compilerProvider <$> compilerAsk
+ compilerTellDependencies [IdentifierDependency identifier]
+ compilerUnsafeIO $ resourceMetadata provider identifier
+
+
+--------------------------------------------------------------------------------
+compilerGetMatches :: Pattern -> Compiler [Identifier]
+compilerGetMatches pattern = do
+ universe <- compilerUniverse <$> compilerAsk
+ let matching = filterMatches pattern universe
+ compilerTellDependencies [PatternDependency pattern matching]
+ return matching
diff --git a/src/Hakyll/Core/Compiler/Require.hs b/src/Hakyll/Core/Compiler/Require.hs
index a7c47ce..b9e0cc7 100644
--- a/src/Hakyll/Core/Compiler/Require.hs
+++ b/src/Hakyll/Core/Compiler/Require.hs
@@ -19,6 +19,7 @@ import Hakyll.Core.Dependencies
import Hakyll.Core.Identifier
import Hakyll.Core.Identifier.Pattern
import Hakyll.Core.Item
+import Hakyll.Core.Metadata
import Hakyll.Core.Store (Store)
import qualified Hakyll.Core.Store as Store
@@ -59,9 +60,7 @@ requireBody = fmap itemBody . require
--------------------------------------------------------------------------------
requireAll :: (Binary a, Typeable a) => Pattern -> Compiler [Item a]
requireAll pattern = do
- universe <- compilerUniverse <$> compilerAsk
- let matching = filterMatches pattern universe
- compilerTellDependencies [PatternDependency pattern matching]
+ matching <- getMatches pattern
mapM require matching
diff --git a/src/Hakyll/Core/Metadata.hs b/src/Hakyll/Core/Metadata.hs
index f3e99f5..884acd3 100644
--- a/src/Hakyll/Core/Metadata.hs
+++ b/src/Hakyll/Core/Metadata.hs
@@ -1,12 +1,32 @@
--------------------------------------------------------------------------------
module Hakyll.Core.Metadata
( Metadata
+ , MonadMetadata (..)
) where
--------------------------------------------------------------------------------
+import Control.Monad (forM)
import Data.Map (Map)
--------------------------------------------------------------------------------
+import Hakyll.Core.Identifier
+import Hakyll.Core.Identifier.Pattern
+
+
+--------------------------------------------------------------------------------
type Metadata = Map String String
+
+
+--------------------------------------------------------------------------------
+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)
diff --git a/src/Hakyll/Core/Rules.hs b/src/Hakyll/Core/Rules.hs
index d9dea01..0d9b7e2 100644
--- a/src/Hakyll/Core/Rules.hs
+++ b/src/Hakyll/Core/Rules.hs
@@ -174,6 +174,7 @@ route route' = Rules $ do
--------------------------------------------------------------------------------
-- | Get a list of resources matching the current pattern. This will also set
-- the correct group to the identifiers.
+-- TODO: Make this private?
resources :: Rules [Identifier]
resources = Rules $ do
pattern <- rulesPattern <$> ask
diff --git a/src/Hakyll/Core/Rules/Internal.hs b/src/Hakyll/Core/Rules/Internal.hs
index a067b02..249ae3b 100644
--- a/src/Hakyll/Core/Rules/Internal.hs
+++ b/src/Hakyll/Core/Rules/Internal.hs
@@ -12,8 +12,10 @@ module Hakyll.Core.Rules.Internal
--------------------------------------------------------------------------------
-import Control.Applicative (Applicative)
+import Control.Applicative (Applicative, (<$>))
+import Control.Monad.Reader (ask)
import Control.Monad.RWS (RWST, runRWST)
+import Control.Monad.Trans (liftIO)
import qualified Data.Map as M
import Data.Monoid (Monoid, mappend, mempty)
import Data.Set (Set)
@@ -24,6 +26,7 @@ import Hakyll.Core.Compiler.Internal
import Hakyll.Core.Identifier
import Hakyll.Core.Identifier.Pattern
import Hakyll.Core.Item.SomeItem
+import Hakyll.Core.Metadata
import Hakyll.Core.Provider
import Hakyll.Core.Routes
@@ -71,6 +74,17 @@ newtype Rules a = Rules
--------------------------------------------------------------------------------
+instance MonadMetadata Rules where
+ getMetadata identifier = Rules $ do
+ provider <- rulesProvider <$> ask
+ liftIO $ resourceMetadata provider identifier
+
+ getMatches pattern = Rules $ do
+ provider <- rulesProvider <$> ask
+ return $ filterMatches pattern $ resourceList provider
+
+
+--------------------------------------------------------------------------------
-- | Run a Rules monad, resulting in a 'RuleSet'
runRules :: Rules a -> Provider -> IO RuleSet
runRules rules provider = do
diff --git a/src/Hakyll/Web/Template/Context.hs b/src/Hakyll/Web/Template/Context.hs
index acc01bf..11491bf 100644
--- a/src/Hakyll/Web/Template/Context.hs
+++ b/src/Hakyll/Web/Template/Context.hs
@@ -39,6 +39,7 @@ import Hakyll.Core.Compiler
import Hakyll.Core.Compiler.Internal
import Hakyll.Core.Identifier
import Hakyll.Core.Item
+import Hakyll.Core.Metadata
import Hakyll.Core.Provider
import Hakyll.Core.Util.String (splitAll)
import Hakyll.Web.Urls