@@ -15,57 +15,69 @@ module Hakyll.Core.Rules
15import Control.Applicative (Applicative, (<$>))
15import Control.Applicative (Applicative, (<$>))
16import Control.Monad.Writer
16import Control.Monad.Writer
17import Control.Monad.Reader
17import Control.Monad.Reader
18+import Control.Arrow (second)
20+import Data.Typeable (Typeable)
21+import Data.Binary (Binary)
19import Hakyll.Core.ResourceProvider
23import Hakyll.Core.ResourceProvider
20import Hakyll.Core.Identifier
24import Hakyll.Core.Identifier
21import Hakyll.Core.Identifier.Pattern
25import Hakyll.Core.Identifier.Pattern
22import Hakyll.Core.Compiler
26import Hakyll.Core.Compiler
23import Hakyll.Core.Route
27import Hakyll.Core.Route
28+import Hakyll.Core.CompiledItem
29+import Hakyll.Core.Writable
25-- | A collection of rules for the compilation process
31-- | A collection of rules for the compilation process
27−data RuleSet a = RuleSet
33+data RuleSet = RuleSet
28 { rulesRoute :: Route
34 { rulesRoute :: Route
29− , rulesCompilers :: [(Identifier, Compiler a)]
35+ , rulesCompilers :: [(Identifier, Compiler CompiledItem)]
32−instance Monoid (RuleSet a) where
38+instance Monoid RuleSet where
33 mempty = RuleSet mempty mempty
39 mempty = RuleSet mempty mempty
34 mappend (RuleSet r1 c1) (RuleSet r2 c2) =
40 mappend (RuleSet r1 c1) (RuleSet r2 c2) =
35 RuleSet (mappend r1 r2) (mappend c1 c2)
41 RuleSet (mappend r1 r2) (mappend c1 c2)
37-- | The monad used to compose rules
43-- | The monad used to compose rules
39−newtype RulesM a b = RulesM
45+newtype RulesM a = RulesM
40− { unRulesM :: ReaderT ResourceProvider (Writer (RuleSet a)) b
46+ { unRulesM :: ReaderT ResourceProvider (Writer RuleSet) a
41 } deriving (Monad, Functor, Applicative)
47 } deriving (Monad, Functor, Applicative)
43-- | Simplification of the RulesM type; usually, it will not return any
49-- | Simplification of the RulesM type; usually, it will not return any
46−type Rules a = RulesM a ()
52+type Rules = RulesM ()
48-- | Run a Rules monad, resulting in a 'RuleSet'
54-- | Run a Rules monad, resulting in a 'RuleSet'
50−runRules :: Rules a -> ResourceProvider -> RuleSet a
56+runRules :: Rules -> ResourceProvider -> RuleSet
51runRules rules provider = execWriter $ runReaderT (unRulesM rules) provider
57runRules rules provider = execWriter $ runReaderT (unRulesM rules) provider
53-- | Add a route
59-- | Add a route
55−addRoute :: Route -> Rules a
61+addRoute :: Route -> Rules
56addRoute route' = RulesM $ tell $ RuleSet route' mempty
62addRoute route' = RulesM $ tell $ RuleSet route' mempty
58-- | Add a number of compilers
64-- | Add a number of compilers
66+addCompilers :: (Binary a, Typeable a, Writable a)
60−addCompilers :: [(Identifier, Compiler a)] -> Rules a
67+ => [(Identifier, Compiler a)]
61−addCompilers compilers = RulesM $ tell $ RuleSet mempty compilers
69+addCompilers compilers = RulesM $ tell $ RuleSet mempty $
70+ map (second boxCompiler) compilers
72+ boxCompiler = fmap (fmap compiledItem)
63-- | Add a compilation rule
74-- | Add a compilation rule
65-- This instructs all resources matching the given pattern to be compiled using
76-- This instructs all resources matching the given pattern to be compiled using
66-- the given compiler
77-- the given compiler
79+compile :: (Binary a, Typeable a, Writable a)
68−compile :: Pattern -> Compiler a -> Rules a
80+ => Pattern -> Compiler a -> Rules
69compile pattern compiler = RulesM $ do
81compile pattern compiler = RulesM $ do
70 identifiers <- matches pattern . resourceList <$> ask
82 identifiers <- matches pattern . resourceList <$> ask
71 unRulesM $ addCompilers $ zip identifiers (repeat compiler)
83 unRulesM $ addCompilers $ zip identifiers (repeat compiler)
@@ -74,10 +86,11 @@ compile pattern compiler = RulesM $ do
75-- This sets a compiler for the given identifier
87-- This sets a compiler for the given identifier
89+create :: (Binary a, Typeable a, Writable a)
77−create :: Identifier -> Compiler a -> RulesM a ()
90+ => Identifier -> Compiler a -> Rules
78create identifier compiler = addCompilers [(identifier, compiler)]
91create identifier compiler = addCompilers [(identifier, compiler)]
80-- | Add a route
93-- | Add a route
82−route :: Pattern -> Route -> RulesM a ()
95+route :: Pattern -> Route -> Rules
83route pattern route' = addRoute $ ifMatch pattern route'
96route pattern route' = addRoute $ ifMatch pattern route'