@@ -14,6 +14,8 @@
14module Hakyll.Core.Rules
14module Hakyll.Core.Rules
@@ -23,7 +25,7 @@ module Hakyll.Core.Rules
24import Control.Applicative ((<$>))
26import Control.Applicative ((<$>))
25import Control.Monad.Writer (tell)
27import Control.Monad.Writer (tell)
26−import Control.Monad.Reader (ask)
28+import Control.Monad.Reader (ask, local)
27import Control.Arrow (second, (>>>), arr, (>>^))
29import Control.Arrow (second, (>>>), arr, (>>^))
28import Control.Monad.State (get, put)
30import Control.Monad.State (get, put)
29import Data.Monoid (mempty)
31import Data.Monoid (mempty)
@@ -63,21 +65,37 @@ tellResources :: [Resource]
64tellResources resources = RulesM $ tell $ RuleSet mempty mempty $ S.fromList resources
66tellResources resources = RulesM $ tell $ RuleSet mempty mempty $ S.fromList resources
68+-- | Only compile/route items matching the given pattern
70+matchPattern :: Pattern -> Rules -> Rules
71+matchPattern pattern = matchPredicate (doesMatch pattern)
73+-- | Only compile/route items satisfying the given predicate
75+matchPredicate :: (Identifier -> Bool) -> Rules -> Rules
76+matchPredicate predicate = RulesM . local addPredicate . unRulesM
78+ addPredicate env = env
79+ { rulesMatcher = \id' -> rulesMatcher env id' && predicate id'
66-- | Add a compilation rule to the rules.
82-- | Add a compilation rule to the rules.
68−-- This instructs all resources matching the given pattern to be compiled using
84+-- This instructs all resources to be compiled using the given compiler. When
69−-- the given compiler. When no resources match the given pattern, nothing will
85+-- no resources match the current selection, nothing will happen. In this case,
70−-- happen. In this case, you might want to have a look at 'create'.
86+-- you might want to have a look at 'create'.
72compile :: (Binary a, Typeable a, Writable a)
88compile :: (Binary a, Typeable a, Writable a)
73− => Pattern -> Compiler Resource a -> Rules
89+ => Compiler Resource a -> Rules
74−compile pattern compiler = RulesM $ do
90+compile compiler = RulesM $ do
91+ matcher <- rulesMatcher <$> ask
92+ provider <- rulesResourceProvider <$> ask
75− identifiers <- matches pattern . map unResource . resourceList <$> ask
93+ let identifiers = filter matcher $ map unResource $ resourceList provider
76 unRulesM $ do
94 unRulesM $ do
77 tellCompilers $ flip map identifiers $ \identifier ->
95 tellCompilers $ flip map identifiers $ \identifier ->
78 (identifier, constA (Resource identifier) >>> compiler)
96 (identifier, constA (Resource identifier) >>> compiler)
79 tellResources $ map Resource identifiers
97 tellResources $ map Resource identifiers
81-- | Add a compilation rule
99-- | Add a compilation rule
83-- This sets a compiler for the given identifier. No resource is needed, since
101-- This sets a compiler for the given identifier. No resource is needed, since
@@ -91,10 +109,12 @@ create identifier compiler = tellCompilers [(identifier, compiler)]
92-- | Add a route.
110-- | Add a route.
94−-- This adds a route for all items matching the given pattern.
112+-- This adds a route for all items matching the current pattern.
96−route :: Pattern -> Routes -> Rules
114+route :: Routes -> Rules
97−route pattern route' = tellRoute $ ifMatch pattern route'
115+route route' = RulesM $ do
116+ matcher <- rulesMatcher <$> ask
117+ unRulesM $ tellRoute $ matchRoute matcher route'
99-- | Apart from regular compilers, one is also able to specify metacompilers.
119-- | Apart from regular compilers, one is also able to specify metacompilers.
100-- Metacompilers are a special class of compilers: they are compilers which
120-- Metacompilers are a special class of compilers: they are compilers which