@@ -15,57 +15,69 @@ module Hakyll.Core.Rules
1515import Control.Applicative (Applicative, (<$>))
1616import Control.Monad.Writer
1717import Control.Monad.Reader
18+import Control.Arrow (second)
19+
20+import Data.Typeable (Typeable)
21+import Data.Binary (Binary)
1822
1923import Hakyll.Core.ResourceProvider
2024import Hakyll.Core.Identifier
2125import Hakyll.Core.Identifier.Pattern
2226import Hakyll.Core.Compiler
2327import Hakyll.Core.Route
28+import Hakyll.Core.CompiledItem
29+import Hakyll.Core.Writable
2430
2531-- | A collection of rules for the compilation process
2632--
27−data RuleSet a = RuleSet
33+data RuleSet = RuleSet
2834 { rulesRoute :: Route
29− , rulesCompilers :: [(Identifier, Compiler a)]
35+ , rulesCompilers :: [(Identifier, Compiler CompiledItem)]
3036 }
3137
32−instance Monoid (RuleSet a) where
38+instance Monoid RuleSet where
3339 mempty = RuleSet mempty mempty
3440 mappend (RuleSet r1 c1) (RuleSet r2 c2) =
3541 RuleSet (mappend r1 r2) (mappend c1 c2)
3642
3743-- | The monad used to compose rules
3844--
39−newtype RulesM a b = RulesM
40− { unRulesM :: ReaderT ResourceProvider (Writer (RuleSet a)) b
45+newtype RulesM a = RulesM
46+ { unRulesM :: ReaderT ResourceProvider (Writer RuleSet) a
4147 } deriving (Monad, Functor, Applicative)
4248
4349-- | Simplification of the RulesM type; usually, it will not return any
4450-- result.
4551--
46−type Rules a = RulesM a ()
52+type Rules = RulesM ()
4753
4854-- | Run a Rules monad, resulting in a 'RuleSet'
4955--
50−runRules :: Rules a -> ResourceProvider -> RuleSet a
56+runRules :: Rules -> ResourceProvider -> RuleSet
5157runRules rules provider = execWriter $ runReaderT (unRulesM rules) provider
5258
5359-- | Add a route
5460--
55−addRoute :: Route -> Rules a
61+addRoute :: Route -> Rules
5662addRoute route' = RulesM $ tell $ RuleSet route' mempty
5763
5864-- | Add a number of compilers
5965--
60−addCompilers :: [(Identifier, Compiler a)] -> Rules a
61−addCompilers compilers = RulesM $ tell $ RuleSet mempty compilers
66+addCompilers :: (Binary a, Typeable a, Writable a)
67+ => [(Identifier, Compiler a)]
68+ -> Rules
69+addCompilers compilers = RulesM $ tell $ RuleSet mempty $
70+ map (second boxCompiler) compilers
71+ where
72+ boxCompiler = fmap (fmap compiledItem)
6273
6374-- | Add a compilation rule
6475--
6576-- This instructs all resources matching the given pattern to be compiled using
6677-- the given compiler
6778--
68−compile :: Pattern -> Compiler a -> Rules a
79+compile :: (Binary a, Typeable a, Writable a)
80+ => Pattern -> Compiler a -> Rules
6981compile pattern compiler = RulesM $ do
7082 identifiers <- matches pattern . resourceList <$> ask
7183 unRulesM $ addCompilers $ zip identifiers (repeat compiler)
@@ -74,10 +86,11 @@ compile pattern compiler = RulesM $ do
7486--
7587-- This sets a compiler for the given identifier
7688--
77−create :: Identifier -> Compiler a -> RulesM a ()
89+create :: (Binary a, Typeable a, Writable a)
90+ => Identifier -> Compiler a -> Rules
7891create identifier compiler = addCompilers [(identifier, compiler)]
7992
8093-- | Add a route
8194--
82−route :: Pattern -> Route -> RulesM a ()
95+route :: Pattern -> Route -> Rules
8396route pattern route' = addRoute $ ifMatch pattern route'