hakyll/src/Hakyll/Core/Rules.hs

156 lines
5.5 KiB
Haskell
Raw Normal View History

2012-11-08 12:50:08 +00:00
--------------------------------------------------------------------------------
2011-02-11 12:30:55 +00:00
-- | This module provides a declarative DSL in which the user can specify the
-- different rules used to run the compilers.
--
2012-11-13 12:13:17 +00:00
-- The convention is to just list all items in the 'Rules' monad, routes and
2011-02-11 12:30:55 +00:00
-- compilation rules.
--
-- A typical usage example would be:
--
-- > main = hakyll $ do
2011-04-05 09:58:26 +00:00
-- > match "posts/*" $ do
-- > route (setExtension "html")
-- > compile someCompiler
-- > match "css/*" $ do
-- > route idRoute
-- > compile compressCssCompiler
2012-11-08 12:50:08 +00:00
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE OverloadedStrings #-}
2010-12-25 18:37:21 +00:00
module Hakyll.Core.Rules
2012-11-13 12:13:17 +00:00
( Rules
2011-04-05 09:58:26 +00:00
, match
2012-11-24 17:45:43 +00:00
, version
2010-12-25 18:37:21 +00:00
, compile
, route
2012-11-29 15:22:08 +00:00
-- * Advanced usage
, rulesExtraDependencies
2010-12-25 18:37:21 +00:00
) where
2012-11-08 12:50:08 +00:00
--------------------------------------------------------------------------------
import Control.Applicative ((<$>))
2012-11-13 12:13:17 +00:00
import Control.Arrow (second)
2012-11-08 12:50:08 +00:00
import Control.Monad.Reader (ask, local)
2012-11-29 15:22:08 +00:00
import Control.Monad.Writer (censor, tell)
2012-11-08 12:50:08 +00:00
import Data.Monoid (mappend, mempty)
import qualified Data.Set as S
--------------------------------------------------------------------------------
import Data.Binary (Binary)
import Data.Typeable (Typeable)
--------------------------------------------------------------------------------
import Hakyll.Core.Compiler.Internal
2012-11-29 15:22:08 +00:00
import Hakyll.Core.Dependencies
2012-11-08 12:50:08 +00:00
import Hakyll.Core.Identifier
import Hakyll.Core.Identifier.Pattern
2012-11-18 20:56:52 +00:00
import Hakyll.Core.Item
import Hakyll.Core.Item.SomeItem
2012-11-24 17:45:43 +00:00
import Hakyll.Core.Metadata
2012-11-08 12:50:08 +00:00
import Hakyll.Core.Routes
import Hakyll.Core.Rules.Internal
import Hakyll.Core.Writable
--------------------------------------------------------------------------------
2010-12-25 18:37:21 +00:00
-- | Add a route
2012-11-13 12:13:17 +00:00
tellRoute :: Routes -> Rules ()
tellRoute route' = Rules $ tell $ RuleSet route' mempty mempty
2010-12-25 18:37:21 +00:00
2012-11-08 12:50:08 +00:00
--------------------------------------------------------------------------------
2010-12-25 18:37:21 +00:00
-- | Add a number of compilers
2011-01-07 11:12:13 +00:00
tellCompilers :: (Binary a, Typeable a, Writable a)
2012-11-18 20:56:52 +00:00
=> [(Identifier, Compiler (Item a))]
2012-11-13 12:13:17 +00:00
-> Rules ()
tellCompilers compilers = Rules $ do
2011-05-24 12:39:21 +00:00
-- We box the compilers so they have a more simple type
2012-11-18 20:56:52 +00:00
let compilers' = map (second $ fmap SomeItem) compilers
2011-04-11 19:57:33 +00:00
tell $ RuleSet mempty compilers' mempty
2010-12-25 18:37:21 +00:00
2012-11-08 12:50:08 +00:00
--------------------------------------------------------------------------------
2011-02-15 17:32:55 +00:00
-- | Add resources
2012-11-13 12:13:17 +00:00
tellResources :: [Identifier]
-> Rules ()
tellResources resources' = Rules $ tell $
RuleSet mempty mempty $ S.fromList resources'
2011-02-15 17:32:55 +00:00
2012-11-08 12:50:08 +00:00
--------------------------------------------------------------------------------
2012-12-25 22:26:16 +00:00
-- | Make sure routes are consistent with the compilers in this section
fixRoutes :: Rules b -> Rules b
fixRoutes = Rules . censor matchRoutes . unRules
where
-- Create a fast pattern for routing that matches exactly the compilers
-- created in the block given to match
matchRoutes ruleSet = ruleSet
{ rulesRoutes = matchRoute fastPattern (rulesRoutes ruleSet)
}
where
fastPattern = fromList [id' | (id', _) <- rulesCompilers ruleSet]
2012-12-25 22:26:16 +00:00
--------------------------------------------------------------------------------
match :: Pattern -> Rules b -> Rules b
match pattern = fixRoutes . Rules . local addPattern . unRules
where
addPattern env = env
{ rulesPattern = rulesPattern env `mappend` pattern
}
2012-11-08 12:50:08 +00:00
--------------------------------------------------------------------------------
2012-11-24 17:45:43 +00:00
version :: String -> Rules a -> Rules a
2012-12-25 22:26:16 +00:00
version v = fixRoutes . Rules . local setVersion' . unRules
2011-04-11 19:57:33 +00:00
where
2012-11-24 17:45:43 +00:00
setVersion' env = env {rulesVersion = Just v}
2011-04-11 19:57:33 +00:00
2012-11-08 12:50:08 +00:00
--------------------------------------------------------------------------------
2011-02-11 12:30:55 +00:00
-- | Add a compilation rule to the rules.
2010-12-25 18:37:21 +00:00
--
-- 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'.
2010-12-28 10:12:45 +00:00
compile :: (Binary a, Typeable a, Writable a)
2012-11-18 20:56:52 +00:00
=> Compiler (Item a) -> Rules ()
2011-04-14 08:17:08 +00:00
compile compiler = do
2012-11-24 17:45:43 +00:00
pattern <- Rules $ rulesPattern <$> ask
version' <- Rules $ rulesVersion <$> ask
ids <- case fromLiteral pattern of
2012-11-24 12:34:50 +00:00
Just id' -> return [id']
Nothing -> do
2012-11-24 17:45:43 +00:00
ids <- getMatches pattern
2012-11-24 12:34:50 +00:00
tellResources ids
return ids
2012-11-08 12:50:08 +00:00
2012-11-24 17:45:43 +00:00
tellCompilers [(setVersion version' id', compiler) | id' <- ids]
2010-12-25 18:37:21 +00:00
2012-11-08 12:50:08 +00:00
--------------------------------------------------------------------------------
2011-02-11 12:30:55 +00:00
-- | Add a route.
--
-- This adds a route for all items matching the current pattern.
2012-11-13 12:13:17 +00:00
route :: Routes -> Rules ()
route = tellRoute
2012-11-29 15:22:08 +00:00
--------------------------------------------------------------------------------
-- | Advanced usage: add extra dependencies to compilers. Basically this is
-- needed when you're doing unsafe tricky stuff in the rules monad, but you
-- still want correct builds.
rulesExtraDependencies :: [Dependency] -> Rules a -> Rules a
rulesExtraDependencies deps = Rules . censor addDependencies . unRules
where
-- Adds the dependencies to the compilers in the ruleset
addDependencies ruleSet = ruleSet
{ rulesCompilers =
[ (i, compilerTellDependencies deps >> c)
| (i, c) <- rulesCompilers ruleSet
]
}