@@ -14,6 +14,8 @@
1414module Hakyll.Core.Rules
1515 ( RulesM
1616 , Rules
17+ , matchPattern
18+ , matchPredicate
1719 , compile
1820 , create
1921 , route
@@ -23,7 +25,7 @@ module Hakyll.Core.Rules
2325
2426import Control.Applicative ((<$>))
2527import Control.Monad.Writer (tell)
26−import Control.Monad.Reader (ask)
28+import Control.Monad.Reader (ask, local)
2729import Control.Arrow (second, (>>>), arr, (>>^))
2830import Control.Monad.State (get, put)
2931import Data.Monoid (mempty)
@@ -63,21 +65,37 @@ tellResources :: [Resource]
6365 -> Rules
6466tellResources resources = RulesM $ tell $ RuleSet mempty mempty $ S.fromList resources
6567
68+-- | Only compile/route items matching the given pattern
69+--
70+matchPattern :: Pattern -> Rules -> Rules
71+matchPattern pattern = matchPredicate (doesMatch pattern)
72+
73+-- | Only compile/route items satisfying the given predicate
74+--
75+matchPredicate :: (Identifier -> Bool) -> Rules -> Rules
76+matchPredicate predicate = RulesM . local addPredicate . unRulesM
77+ where
78+ addPredicate env = env
79+ { rulesMatcher = \id' -> rulesMatcher env id' && predicate id'
80+ }
81+
6682-- | Add a compilation rule to the rules.
6783--
68−-- This instructs all resources matching the given pattern to be compiled using
69−-- the given compiler. When no resources match the given pattern, nothing will
70−-- happen. In this case, you might want to have a look at 'create'.
84+-- This instructs all resources to be compiled using the given compiler. When
85+-- no resources match the current selection, nothing will happen. In this case,
86+-- you might want to have a look at 'create'.
7187--
7288compile :: (Binary a, Typeable a, Writable a)
73− => Pattern -> Compiler Resource a -> Rules
74−compile pattern compiler = RulesM $ do
75− identifiers <- matches pattern . map unResource . resourceList <$> ask
89+ => Compiler Resource a -> Rules
90+compile compiler = RulesM $ do
91+ matcher <- rulesMatcher <$> ask
92+ provider <- rulesResourceProvider <$> ask
93+ let identifiers = filter matcher $ map unResource $ resourceList provider
7694 unRulesM $ do
7795 tellCompilers $ flip map identifiers $ \identifier ->
7896 (identifier, constA (Resource identifier) >>> compiler)
7997 tellResources $ map Resource identifiers
80−
98+
8199-- | Add a compilation rule
82100--
83101-- This sets a compiler for the given identifier. No resource is needed, since
@@ -91,10 +109,12 @@ create identifier compiler = tellCompilers [(identifier, compiler)]
91109
92110-- | Add a route.
93111--
94−-- This adds a route for all items matching the given pattern.
112+-- This adds a route for all items matching the current pattern.
95113--
96−route :: Pattern -> Routes -> Rules
97−route pattern route' = tellRoute $ ifMatch pattern route'
114+route :: Routes -> Rules
115+route route' = RulesM $ do
116+ matcher <- rulesMatcher <$> ask
117+ unRulesM $ tellRoute $ matchRoute matcher route'
98118
99119-- | Apart from regular compilers, one is also able to specify metacompilers.
100120-- Metacompilers are a special class of compilers: they are compilers which