diff options
Diffstat (limited to 'src/Hakyll/Core/Rules.hs')
-rw-r--r-- | src/Hakyll/Core/Rules.hs | 95 |
1 files changed, 57 insertions, 38 deletions
diff --git a/src/Hakyll/Core/Rules.hs b/src/Hakyll/Core/Rules.hs index bef480a..ff68c56 100644 --- a/src/Hakyll/Core/Rules.hs +++ b/src/Hakyll/Core/Rules.hs @@ -1,3 +1,4 @@ +-------------------------------------------------------------------------------- -- | This module provides a declarative DSL in which the user can specify the -- different rules used to run the compilers. -- @@ -13,8 +14,8 @@ -- > match "css/*" $ do -- > route idRoute -- > compile compressCssCompiler --- -{-# LANGUAGE GeneralizedNewtypeDeriving, OverloadedStrings #-} +{-# LANGUAGE GeneralizedNewtypeDeriving #-} +{-# LANGUAGE OverloadedStrings #-} module Hakyll.Core.Rules ( RulesM , Rules @@ -29,35 +30,43 @@ module Hakyll.Core.Rules , freshIdentifier ) where -import Control.Applicative ((<$>)) -import Control.Monad.Writer (tell) -import Control.Monad.Reader (ask, local) -import Control.Arrow ((>>>), arr, (>>^), (***)) -import Control.Monad.State (get, put) -import Data.Monoid (mempty, mappend) -import qualified Data.Set as S - -import Data.Typeable (Typeable) -import Data.Binary (Binary) - -import Hakyll.Core.Resource -import Hakyll.Core.Resource.Provider -import Hakyll.Core.Identifier -import Hakyll.Core.Identifier.Pattern -import Hakyll.Core.Compiler.Internal -import Hakyll.Core.Routes -import Hakyll.Core.CompiledItem -import Hakyll.Core.Writable -import Hakyll.Core.Rules.Internal -import Hakyll.Core.Util.Arrow +-------------------------------------------------------------------------------- +import Control.Applicative ((<$>)) +import Control.Arrow (arr, (***), (>>>), (>>^)) +import Control.Monad.Reader (ask, local) +import Control.Monad.State (get, put) +import Control.Monad.Writer (tell) +import Data.Monoid (mappend, mempty) +import qualified Data.Set as S + + +-------------------------------------------------------------------------------- +import Data.Binary (Binary) +import Data.Typeable (Typeable) + + +-------------------------------------------------------------------------------- +import Hakyll.Core.CompiledItem +import Hakyll.Core.Compiler.Internal +import Hakyll.Core.Identifier +import Hakyll.Core.Identifier.Pattern +import Hakyll.Core.Resource +import Hakyll.Core.Resource.Provider +import Hakyll.Core.Routes +import Hakyll.Core.Rules.Internal +import Hakyll.Core.Util.Arrow +import Hakyll.Core.Writable + + +-------------------------------------------------------------------------------- -- | Add a route --- tellRoute :: Routes -> Rules tellRoute route' = RulesM $ tell $ RuleSet route' mempty mempty + +-------------------------------------------------------------------------------- -- | Add a number of compilers --- tellCompilers :: (Binary a, Typeable a, Writable a) => [(Identifier a, Compiler () a)] -> Rules @@ -68,15 +77,17 @@ tellCompilers compilers = RulesM $ do where boxCompiler = (>>> arr compiledItem >>> arr CompileRule) + +-------------------------------------------------------------------------------- -- | Add resources --- tellResources :: [Resource] -> Rules tellResources resources' = RulesM $ tell $ RuleSet mempty mempty $ S.fromList resources' + +-------------------------------------------------------------------------------- -- | Only compile/route items satisfying the given predicate --- match :: Pattern a -> RulesM b -> RulesM b match pattern = RulesM . local addPredicate . unRulesM where @@ -84,8 +95,9 @@ match pattern = RulesM . local addPredicate . unRulesM { rulesPattern = rulesPattern env `mappend` castPattern pattern } + +-------------------------------------------------------------------------------- -- | Greate a group of compilers --- -- Imagine you have a page that you want to render, but you also want the raw -- content available on your site. -- @@ -114,18 +126,18 @@ match pattern = RulesM . local addPredicate . unRulesM -- -- This will put the compiler for the raw content in a separate group -- (@\"raw\"@), which causes it to be compiled as well. --- group :: String -> RulesM a -> RulesM a group g = RulesM . local setGroup' . unRulesM where setGroup' env = env { rulesGroup = Just g } + +-------------------------------------------------------------------------------- -- | Add a compilation rule to the rules. -- -- This instructs all resources to be compiled using the given compiler. When -- no resources match the current selection, nothing will happen. In this case, -- you might want to have a look at 'create'. --- compile :: (Binary a, Typeable a, Writable a) => Compiler Resource a -> RulesM (Pattern a) compile compiler = do @@ -134,7 +146,9 @@ compile compiler = do (identifier, constA (fromIdentifier identifier) >>> compiler) tellResources $ map fromIdentifier ids return $ list ids - + + +-------------------------------------------------------------------------------- -- | Add a compilation rule -- -- This sets a compiler for the given identifier. No resource is needed, since @@ -143,7 +157,6 @@ compile compiler = do -- actual content itself. Note that the group of the given identifier is -- replaced by the group set via 'group' (or 'Nothing', if 'group' has not been -- used). --- create :: (Binary a, Typeable a, Writable a) => Identifier a -> Compiler () a -> RulesM (Identifier a) create id' compiler = RulesM $ do @@ -152,10 +165,11 @@ create id' compiler = RulesM $ do unRulesM $ tellCompilers [(id'', compiler)] return id'' + +-------------------------------------------------------------------------------- -- | Add a route. -- -- This adds a route for all items matching the current pattern. --- route :: Routes -> Rules route route' = RulesM $ do -- We want the route only to be applied if we match the current pattern and @@ -164,9 +178,10 @@ route route' = RulesM $ do group' <- rulesGroup <$> ask unRulesM $ tellRoute $ matchRoute (pattern `mappend` inGroup group') route' + +-------------------------------------------------------------------------------- -- | Get a list of resources matching the current pattern. This will also set -- the correct group to the identifiers. --- resources :: RulesM [Identifier a] resources = RulesM $ do pattern <- rulesPattern <$> ask @@ -176,6 +191,8 @@ resources = RulesM $ do where toId g = setGroup g . toIdentifier + +-------------------------------------------------------------------------------- -- | Apart from regular compilers, one is also able to specify metacompilers. -- Metacompilers are a special class of compilers: they are compilers which -- produce other compilers. @@ -205,9 +222,8 @@ resources = RulesM $ do -- For simple hakyll systems, it is no need for this construction. More -- formally, it is only needed when the content of one or more items determines -- which items must be rendered. --- metaCompile :: (Binary a, Typeable a, Writable a) - => Compiler () [(Identifier a, Compiler () a)] + => Compiler () [(Identifier a, Compiler () a)] -- ^ Compiler generating the other compilers -> Rules -- ^ Resulting rules @@ -215,13 +231,14 @@ metaCompile compiler = do id' <- freshIdentifier "Hakyll.Core.Rules.metaCompile" metaCompileWith id' compiler + +-------------------------------------------------------------------------------- -- | Version of 'metaCompile' that allows you to specify a custom identifier for -- the metacompiler. --- metaCompileWith :: (Binary a, Typeable a, Writable a) => Identifier () -- ^ Identifier for this compiler - -> Compiler () [(Identifier a, Compiler () a)] + -> Compiler () [(Identifier a, Compiler () a)] -- ^ Compiler generating the other compilers -> Rules -- ^ Resulting rules @@ -239,6 +256,8 @@ metaCompileWith identifier compiler = RulesM $ do tell $ RuleSet mempty compilers mempty + +-------------------------------------------------------------------------------- -- | Generate a fresh Identifier with a given prefix freshIdentifier :: String -- ^ Prefix -> RulesM (Identifier a) -- ^ Fresh identifier |