@@ -3,19 +3,21 @@
4{-# LANGUAGE GeneralizedNewtypeDeriving #-}
4{-# LANGUAGE GeneralizedNewtypeDeriving #-}
5module Hakyll.Core.Rules
5module Hakyll.Core.Rules
6− ( RuleSet (..)
7+ , RuleSet (..)
9 , runRules
10 , runRules
15import Control.Applicative (Applicative, (<$>))
17import Control.Applicative (Applicative, (<$>))
16import Control.Monad.Writer
18import Control.Monad.Writer
17import Control.Monad.Reader
19import Control.Monad.Reader
18−import Control.Arrow (second, (>>>), arr)
20+import Control.Arrow (second, (>>>), arr, (>>^))
20import Data.Typeable (Typeable)
22import Data.Typeable (Typeable)
21import Data.Binary (Binary)
23import Data.Binary (Binary)
@@ -23,16 +25,26 @@ import Data.Binary (Binary)
23import Hakyll.Core.ResourceProvider
25import Hakyll.Core.ResourceProvider
24import Hakyll.Core.Identifier
26import Hakyll.Core.Identifier
25import Hakyll.Core.Identifier.Pattern
27import Hakyll.Core.Identifier.Pattern
26−import Hakyll.Core.Compiler
28+import Hakyll.Core.Compiler.Internal
27import Hakyll.Core.Route
29import Hakyll.Core.Route
28import Hakyll.Core.CompiledItem
30import Hakyll.Core.CompiledItem
29import Hakyll.Core.Writable
31import Hakyll.Core.Writable
33+-- | Output of a compiler rule
35+-- * The compiler will produce a simple item. This is the most common case.
37+-- * The compiler will produce more compilers. These new compilers need to be
38+-- added to the runtime if possible, since other items might depend upon them.
40+data CompileRule = ItemRule CompiledItem
41+ | AddCompilersRule [(Identifier, Compiler () CompiledItem)]
31-- | A collection of rules for the compilation process
43-- | A collection of rules for the compilation process
33data RuleSet = RuleSet
45data RuleSet = RuleSet
34 { rulesRoute :: Route
46 { rulesRoute :: Route
35− , rulesCompilers :: [(Identifier, Compiler () CompiledItem)]
47+ , rulesCompilers :: [(Identifier, Compiler () CompileRule)]
38instance Monoid RuleSet where
50instance Monoid RuleSet where
@@ -58,18 +70,18 @@ runRules rules provider = execWriter $ runReaderT (unRulesM rules) provider
59-- | Add a route
71-- | Add a route
61−addRoute :: Route -> Rules
73+tellRoute :: Route -> Rules
62−addRoute route' = RulesM $ tell $ RuleSet route' mempty
74+tellRoute route' = RulesM $ tell $ RuleSet route' mempty
64-- | Add a number of compilers
76-- | Add a number of compilers
66−addCompilers :: (Binary a, Typeable a, Writable a)
78+tellCompilers :: (Binary a, Typeable a, Writable a)
67 => [(Identifier, Compiler () a)]
79 => [(Identifier, Compiler () a)]
69−addCompilers compilers = RulesM $ tell $ RuleSet mempty $
81+tellCompilers compilers = RulesM $ tell $ RuleSet mempty $
70 map (second boxCompiler) compilers
82 map (second boxCompiler) compilers
72− boxCompiler = (>>> arr compiledItem)
84+ boxCompiler = (>>> arr compiledItem >>> arr ItemRule)
74-- | Add a compilation rule
86-- | Add a compilation rule
@@ -80,7 +92,7 @@ compile :: (Binary a, Typeable a, Writable a)
80 => Pattern -> Compiler () a -> Rules
92 => Pattern -> Compiler () a -> Rules
81compile pattern compiler = RulesM $ do
93compile pattern compiler = RulesM $ do
82 identifiers <- matches pattern . resourceList <$> ask
94 identifiers <- matches pattern . resourceList <$> ask
83− unRulesM $ addCompilers $ zip identifiers (repeat compiler)
95+ unRulesM $ tellCompilers $ zip identifiers (repeat compiler)
85-- | Add a compilation rule
97-- | Add a compilation rule
@@ -88,9 +100,23 @@ compile pattern compiler = RulesM $ do
89create :: (Binary a, Typeable a, Writable a)
101create :: (Binary a, Typeable a, Writable a)
90 => Identifier -> Compiler () a -> Rules
102 => Identifier -> Compiler () a -> Rules
91−create identifier compiler = addCompilers [(identifier, compiler)]
103+create identifier compiler = tellCompilers [(identifier, compiler)]
93-- | Add a route
105-- | Add a route
95route :: Pattern -> Route -> Rules
107route :: Pattern -> Route -> Rules
96−route pattern route' = addRoute $ ifMatch pattern route'
108+route pattern route' = tellRoute $ ifMatch pattern route'
110+-- | Add a compiler that produces other compilers over time
112+addCompilers :: (Binary a, Typeable a, Writable a)
114+ -- ^ Identifier for this compiler
115+ -> Compiler () [(Identifier, Compiler () a)]
116+ -- ^ Compiler generating the other compilers
118+ -- ^ Resulting rules
119+addCompilers identifier compiler = RulesM $ tell $ RuleSet mempty $
120+ [(identifier, compiler >>^ makeRule)]
122+ makeRule = AddCompilersRule . map (second (>>^ compiledItem))