@@ -1,3 +1,4 @@
1+--------------------------------------------------------------------------------
12-- | This module provides a declarative DSL in which the user can specify the
23-- different rules used to run the compilers.
34--
@@ -13,8 +14,8 @@
1314-- > match "css/*" $ do
1415-- > route idRoute
1516-- > compile compressCssCompiler
16−--
17−{-# LANGUAGE GeneralizedNewtypeDeriving, OverloadedStrings #-}
17+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
18+{-# LANGUAGE OverloadedStrings #-}
1819module Hakyll.Core.Rules
1920 ( RulesM
2021 , Rules
@@ -29,35 +30,43 @@ module Hakyll.Core.Rules
2930 , freshIdentifier
3031 ) where
3132
32−import Control.Applicative ((<$>))
33−import Control.Monad.Writer (tell)
34−import Control.Monad.Reader (ask, local)
35−import Control.Arrow ((>>>), arr, (>>^), (***))
36−import Control.Monad.State (get, put)
37−import Data.Monoid (mempty, mappend)
38−import qualified Data.Set as S
39−
40−import Data.Typeable (Typeable)
41−import Data.Binary (Binary)
42−
43−import Hakyll.Core.Resource
44−import Hakyll.Core.Resource.Provider
45−import Hakyll.Core.Identifier
46−import Hakyll.Core.Identifier.Pattern
47−import Hakyll.Core.Compiler.Internal
48−import Hakyll.Core.Routes
49−import Hakyll.Core.CompiledItem
50−import Hakyll.Core.Writable
51−import Hakyll.Core.Rules.Internal
52−import Hakyll.Core.Util.Arrow
5333
34+--------------------------------------------------------------------------------
35+import Control.Applicative ((<$>))
36+import Control.Arrow (arr, (***), (>>>), (>>^))
37+import Control.Monad.Reader (ask, local)
38+import Control.Monad.State (get, put)
39+import Control.Monad.Writer (tell)
40+import Data.Monoid (mappend, mempty)
41+import qualified Data.Set as S
42+
43+
44+--------------------------------------------------------------------------------
45+import Data.Binary (Binary)
46+import Data.Typeable (Typeable)
47+
48+
49+--------------------------------------------------------------------------------
50+import Hakyll.Core.CompiledItem
51+import Hakyll.Core.Compiler.Internal
52+import Hakyll.Core.Identifier
53+import Hakyll.Core.Identifier.Pattern
54+import Hakyll.Core.Resource
55+import Hakyll.Core.Resource.Provider
56+import Hakyll.Core.Routes
57+import Hakyll.Core.Rules.Internal
58+import Hakyll.Core.Util.Arrow
59+import Hakyll.Core.Writable
60+
61+
62+--------------------------------------------------------------------------------
5463-- | Add a route
55−--
5664tellRoute :: Routes -> Rules
5765tellRoute route' = RulesM $ tell $ RuleSet route' mempty mempty
5866
67+
68+--------------------------------------------------------------------------------
5969-- | Add a number of compilers
60−--
6170tellCompilers :: (Binary a, Typeable a, Writable a)
6271 => [(Identifier a, Compiler () a)]
6372 -> Rules
@@ -68,15 +77,17 @@ tellCompilers compilers = RulesM $ do
6877 where
6978 boxCompiler = (>>> arr compiledItem >>> arr CompileRule)
7079
80+
81+--------------------------------------------------------------------------------
7182-- | Add resources
72−--
7383tellResources :: [Resource]
7484 -> Rules
7585tellResources resources' = RulesM $ tell $
7686 RuleSet mempty mempty $ S.fromList resources'
7787
88+
89+--------------------------------------------------------------------------------
7890-- | Only compile/route items satisfying the given predicate
79−--
8091match :: Pattern a -> RulesM b -> RulesM b
8192match pattern = RulesM . local addPredicate . unRulesM
8293 where
@@ -84,8 +95,9 @@ match pattern = RulesM . local addPredicate . unRulesM
8495 { rulesPattern = rulesPattern env `mappend` castPattern pattern
8596 }
8697
98+
99+--------------------------------------------------------------------------------
87100-- | Greate a group of compilers
88−--
89101-- Imagine you have a page that you want to render, but you also want the raw
90102-- content available on your site.
91103--
@@ -114,18 +126,18 @@ match pattern = RulesM . local addPredicate . unRulesM
114126--
115127-- This will put the compiler for the raw content in a separate group
116128-- (@\"raw\"@), which causes it to be compiled as well.
117−--
118129group :: String -> RulesM a -> RulesM a
119130group g = RulesM . local setGroup' . unRulesM
120131 where
121132 setGroup' env = env { rulesGroup = Just g }
122133
134+
135+--------------------------------------------------------------------------------
123136-- | Add a compilation rule to the rules.
124137--
125138-- This instructs all resources to be compiled using the given compiler. When
126139-- no resources match the current selection, nothing will happen. In this case,
127140-- you might want to have a look at 'create'.
128−--
129141compile :: (Binary a, Typeable a, Writable a)
130142 => Compiler Resource a -> RulesM (Pattern a)
131143compile compiler = do
@@ -134,7 +146,9 @@ compile compiler = do
134146 (identifier, constA (fromIdentifier identifier) >>> compiler)
135147 tellResources $ map fromIdentifier ids
136148 return $ list ids
137−
149+
150+
151+--------------------------------------------------------------------------------
138152-- | Add a compilation rule
139153--
140154-- This sets a compiler for the given identifier. No resource is needed, since
@@ -143,7 +157,6 @@ compile compiler = do
143157-- actual content itself. Note that the group of the given identifier is
144158-- replaced by the group set via 'group' (or 'Nothing', if 'group' has not been
145159-- used).
146−--
147160create :: (Binary a, Typeable a, Writable a)
148161 => Identifier a -> Compiler () a -> RulesM (Identifier a)
149162create id' compiler = RulesM $ do
@@ -152,10 +165,11 @@ create id' compiler = RulesM $ do
152165 unRulesM $ tellCompilers [(id'', compiler)]
153166 return id''
154167
168+
169+--------------------------------------------------------------------------------
155170-- | Add a route.
156171--
157172-- This adds a route for all items matching the current pattern.
158−--
159173route :: Routes -> Rules
160174route route' = RulesM $ do
161175 -- We want the route only to be applied if we match the current pattern and
@@ -164,9 +178,10 @@ route route' = RulesM $ do
164178 group' <- rulesGroup <$> ask
165179 unRulesM $ tellRoute $ matchRoute (pattern `mappend` inGroup group') route'
166180
181+
182+--------------------------------------------------------------------------------
167183-- | Get a list of resources matching the current pattern. This will also set
168184-- the correct group to the identifiers.
169−--
170185resources :: RulesM [Identifier a]
171186resources = RulesM $ do
172187 pattern <- rulesPattern <$> ask
@@ -176,6 +191,8 @@ resources = RulesM $ do
176191 where
177192 toId g = setGroup g . toIdentifier
178193
194+
195+--------------------------------------------------------------------------------
179196-- | Apart from regular compilers, one is also able to specify metacompilers.
180197-- Metacompilers are a special class of compilers: they are compilers which
181198-- produce other compilers.
@@ -205,9 +222,8 @@ resources = RulesM $ do
205222-- For simple hakyll systems, it is no need for this construction. More
206223-- formally, it is only needed when the content of one or more items determines
207224-- which items must be rendered.
208−--
209225metaCompile :: (Binary a, Typeable a, Writable a)
210− => Compiler () [(Identifier a, Compiler () a)]
226+ => Compiler () [(Identifier a, Compiler () a)]
211227 -- ^ Compiler generating the other compilers
212228 -> Rules
213229 -- ^ Resulting rules
@@ -215,13 +231,14 @@ metaCompile compiler = do
215231 id' <- freshIdentifier "Hakyll.Core.Rules.metaCompile"
216232 metaCompileWith id' compiler
217233
234+
235+--------------------------------------------------------------------------------
218236-- | Version of 'metaCompile' that allows you to specify a custom identifier for
219237-- the metacompiler.
220−--
221238metaCompileWith :: (Binary a, Typeable a, Writable a)
222239 => Identifier ()
223240 -- ^ Identifier for this compiler
224− -> Compiler () [(Identifier a, Compiler () a)]
241+ -> Compiler () [(Identifier a, Compiler () a)]
225242 -- ^ Compiler generating the other compilers
226243 -> Rules
227244 -- ^ Resulting rules
@@ -239,6 +256,8 @@ metaCompileWith identifier compiler = RulesM $ do
239256
240257 tell $ RuleSet mempty compilers mempty
241258
259+
260+--------------------------------------------------------------------------------
242261-- | Generate a fresh Identifier with a given prefix
243262freshIdentifier :: String -- ^ Prefix
244263 -> RulesM (Identifier a) -- ^ Fresh identifier