on master help sign in
Merge branch 'nested-rules'
commit433f36e6f3efdf95276fe0a5f486db3be2824445 authorJasper Van der Jeugt <jaspervdj@gmail.com> dateTue 5 Apr 2011, 22:02 (16 years ago) parent041ec5c19dc9f5
13 files changed+285−218
examples/brochure/hakyll.hs+10−8 examples/feedblog/hakyll.hs+22−26 examples/hakyll/hakyll.hs+18−16 examples/hakyll/tutorial.markdown+33−22 examples/morepages/hakyll.hs+12−11 examples/simpleblog/hakyll.hs+21−25 examples/tagblog/hakyll.hs+24−28 src/Hakyll/Core/Compiler.hs+1−1 src/Hakyll/Core/Identifier/Pattern.hs+74−41 src/Hakyll/Core/Routes.hs+8−8 src/Hakyll/Core/Rules.hs+34−16 src/Hakyll/Core/Rules/Internal.hs+14−2 tests/Hakyll/Core/Identifier/Tests.hs+14−14
examples/brochure/hakyll.hs+10−8
@@ -6,13 +6,15 @@ import Hakyll
66
77main :: IO ()
88main = hakyll $ do
9− route "css/*" idRoute
10− compile "css/*" compressCssCompiler
9+ match "css/*" $ do
10+ route idRoute
11+ compile compressCssCompiler
1112
12− compile "templates/*" templateCompiler
13+ match "templates/*" $ compile templateCompiler
1314
14− forM_ ["about.rst", "index.markdown", "code.lhs"] $ \page -> do
15− route page $ setExtension "html"
16− compile page $ pageCompiler
17− >>> applyTemplateCompiler "templates/default.html"
18− >>> relativizeUrlsCompiler
15+ forM_ ["about.rst", "index.markdown", "code.lhs"] $ \page ->
16+ match page $ do
17+ route $ setExtension "html"
18+ compile $ pageCompiler
19+ >>> applyTemplateCompiler "templates/default.html"
20+ >>> relativizeUrlsCompiler
examples/feedblog/hakyll.hs+22−26
@@ -11,47 +11,43 @@ import Hakyll
1111main :: IO ()
1212main = hakyll $ do
1313 -- Compress CSS
14− route "css/*" idRoute
15− compile "css/*" compressCssCompiler
14+ match "css/*" $ do
15+ route idRoute
16+ compile compressCssCompiler
1617
1718 -- Render posts
18− route "posts/*" $ setExtension ".html"
19− compile "posts/*" $
20− pageCompiler
19+ match "posts/*" $ do
20+ route $ setExtension ".html"
21+ compile $ pageCompiler
2122 >>> applyTemplateCompiler "templates/post.html"
2223 >>> applyTemplateCompiler "templates/default.html"
2324 >>> relativizeUrlsCompiler
2425
2526 -- Render posts list
26− route "posts.html" $ idRoute
27− create "posts.html" $
28− constA mempty
29− >>> arr (setField "title" "All posts")
30− >>> requireAllA "posts/*" addPostList
31− >>> applyTemplateCompiler "templates/posts.html"
32− >>> applyTemplateCompiler "templates/default.html"
33− >>> relativizeUrlsCompiler
27+ match "posts.html" $ route idRoute
28+ create "posts.html" $ constA mempty
29+ >>> arr (setField "title" "All posts")
30+ >>> requireAllA "posts/*" addPostList
31+ >>> applyTemplateCompiler "templates/posts.html"
32+ >>> applyTemplateCompiler "templates/default.html"
33+ >>> relativizeUrlsCompiler
3434
3535 -- Index
36− route "index.html" $ idRoute
37− create "index.html" $
38− constA mempty
39− >>> arr (setField "title" "Home")
40− >>> requireAllA "posts/*" (id *** arr (take 3 . reverse . sortByBaseName) >>> addPostList)
41− >>> applyTemplateCompiler "templates/index.html"
42− >>> applyTemplateCompiler "templates/default.html"
43− >>> relativizeUrlsCompiler
36+ match "index.html" $ route idRoute
37+ create "index.html" $ constA mempty
38+ >>> arr (setField "title" "Home")
39+ >>> requireAllA "posts/*" (id *** arr (take 3 . reverse . sortByBaseName) >>> addPostList)
40+ >>> applyTemplateCompiler "templates/index.html"
41+ >>> applyTemplateCompiler "templates/default.html"
42+ >>> relativizeUrlsCompiler
4443
4544 -- Render RSS feed
46− route "rss.xml" $ idRoute
45+ match "rss.xml" $ route idRoute
4746 create "rss.xml" $
4847 requireAll_ "posts/*" >>> renderRss feedConfiguration
4948
5049 -- Read templates
51− compile "templates/*" templateCompiler
52−
53− -- End
54− return ()
50+ match "templates/*" $ compile templateCompiler
5551
5652-- | Auxiliary compiler: generate a post list from a list of given posts, and
5753-- add it to the current page under @$posts@
examples/hakyll/hakyll.hs+18−16
@@ -6,35 +6,37 @@ import Text.Pandoc
66
77main :: IO ()
88main = hakyll $ do
9− route "css/*" idRoute
10− compile "css/*" compressCssCompiler
9+ match "css/*" $ do
10+ route idRoute
11+ compile compressCssCompiler
1112
1213 -- Static directories
13− forM_ ["images/*", "examples/*", "reference/*"] $ \f -> do
14− route f idRoute
15− compile f copyFileCompiler
14+ forM_ ["images/*", "examples/*", "reference/*"] $ \f -> match f $ do
15+ route idRoute
16+ compile copyFileCompiler
1617
1718 -- Pages
18− forM_ pages $ \p -> do
19− route p $ setExtension "html"
20− compile p $ pageCompiler
19+ forM_ pages $ \p -> match p $ do
20+ route $ setExtension "html"
21+ compile $ pageCompiler
2122 >>> requireA "sidebar.markdown" (setFieldA "sidebar" $ arr pageBody)
2223 >>> applyTemplateCompiler "templates/default.html"
2324 >>> relativizeUrlsCompiler
2425
2526 -- Tutorial
26− route "tutorial.markdown" $ setExtension "html"
27− compile "tutorial.markdown" $ readPageCompiler
28− >>> pageRenderPandocWith defaultHakyllParserState withToc
29− >>> requireA "sidebar.markdown" (setFieldA "sidebar" $ arr pageBody)
30− >>> applyTemplateCompiler "templates/default.html"
31− >>> relativizeUrlsCompiler
27+ match "tutorial.markdown" $ do
28+ route $ setExtension "html"
29+ compile $ readPageCompiler
30+ >>> pageRenderPandocWith defaultHakyllParserState withToc
31+ >>> requireA "sidebar.markdown" (setFieldA "sidebar" $ arr pageBody)
32+ >>> applyTemplateCompiler "templates/default.html"
33+ >>> relativizeUrlsCompiler
3234
3335 -- Sidebar
34− compile "sidebar.markdown" pageCompiler
36+ match "sidebar.markdown" $ compile pageCompiler
3537
3638 -- Templates
37− compile "templates/*" templateCompiler
39+ match "templates/*" $ compile templateCompiler
3840 where
3941 withToc = defaultHakyllWriterOptions
4042 { writerTableOfContents = True
examples/hakyll/tutorial.markdown+33−22
@@ -65,16 +65,18 @@ import Hakyll
6565
6666main :: IO ()
6767main = hakyll $ do
68− route "css/*" idRoute
69− compile "css/*" compressCssCompiler
70−
71− compile "templates/*" templateCompiler
72−
73− forM_ ["about.rst", "index.markdown", "code.lhs"] $ \page -> do
74− route page $ setExtension "html"
75− compile page $ pageCompiler
76− >>> applyTemplateCompiler "templates/default.html"
77− >>> relativizeUrlsCompiler
68+ match "css/*" $ do
69+ route idRoute
70+ compile compressCssCompiler
71+
72+ match "templates/*" $ compile templateCompiler
73+
74+ forM_ ["about.rst", "index.markdown", "code.lhs"] $ \page ->
75+ match page $ do
76+ route $ setExtension "html"
77+ compile $ pageCompiler
78+ >>> applyTemplateCompiler "templates/default.html"
79+ >>> relativizeUrlsCompiler
7880~~~~~
7981
8082This is enough code to create a small brochure site! You can find all code
@@ -111,12 +113,11 @@ main :: IO ()
111113main = hakyll $ do
112114~~~~~
113115
114−The `RulesM` monad is composed of a few functions. A first important one is
115−`route`: this creates a new rule for routing items. This rule is applied to all
116−items it matches -- and matching is done using the `"css/*"` [pattern].
117−`idRoute` simply means that an item will be routed to it's own filename. For
118−example, `css/screen.css` will be routed to `css/screen.css` -- not very
119−exciting.
116+The `RulesM` monad is composed of a few functions. Seldomly, you want to apply a
117+compiler to *all* resources. You want to apply a compiler to certain files
118+instead. That's why the `match` function exists. First, let's handle the CSS of
119+our file. We use a `"css/*"` [Pattern] to match all files in the `css/`
120+directory.
120121
121122Note that a [Pattern] matches an [Identifier], it doesn't match filenames.
122123
@@ -124,7 +125,16 @@ Note that a [Pattern] matches an [Identifier], it doesn't match filenames.
124125[Identifier]: /reference/Hakyll-Core-Identifier.html
125126
126127~~~~~{.haskell}
127−route "css/*" idRoute
128+match "css/*" $ do
129+~~~~~
130+
131+`route` creates a new rule for routing items. This rule is applied to all items
132+that are currently matched -- in this case, `"css/*"`. `idRoute` simply means
133+that an item will be routed to it's own filename. For example, `css/screen.css`
134+will be routed to `css/screen.css` -- not very exciting.
135+
136+~~~~~{.haskell}
137+route idRoute
128138~~~~~
129139
130140Apart from specifying where the items should go (using `route`), we also have to
@@ -135,12 +145,12 @@ good default compilers. The `compressCssCompiler` compiler will simply compress
135145the CSS found in the files.
136146
137147~~~~~{.haskell}
138−compile "css/*" compressCssCompiler
148+compile compressCssCompiler
139149~~~~~
140150
141151Next, we're going to render some pages. We're going to style the results a
142152little, so we're going to need a [Template]. We simply compile a template using
143−the `defaultTemplateRead` compiler, it's good enough in most cases.
153+the `templateCompiler` compiler, it's good enough in most cases.
144154
145155[Template]: /reference/Hakyll-Web-Template.html
146156
@@ -148,7 +158,7 @@ We don't use a route for these templates, after all, we don't want to route them
148158anywhere, we just want to use them to style our pages a little.
149159
150160~~~~~{.haskell}
151−compile "templates/*" templateCompiler
161+match "templates/*" $ compile templateCompiler
152162~~~~~
153163
154164We can conclude that some rules do not *directly* add an output page on our
@@ -164,13 +174,14 @@ manually).
164174
165175~~~~~{.haskell}
166176forM_ ["about.rst", "index.markdown", "code.lhs"] $ \page -> do
177+ match page $ do
167178~~~~~
168179
169180The pages all have different extensions. In our website, we only want to see
170181`.html` files. Hakyll provides a route to do just that:
171182
172183~~~~~{.haskell}
173−route page $ setExtension "html"
184+route setExtension "html"
174185~~~~~
175186
176187The [Rules] reference page has a complete listing of the API used.
@@ -189,7 +200,7 @@ reference page has some more readable information on this subject.
189200[Compiler]: /reference/Hakyll-Core-Compiler.html
190201
191202~~~~~{.haskell}
192−compile page $ pageCompiler
203+compile pageCompiler
193204 >>> applyTemplateCompiler "templates/default.html"
194205 >>> relativizeUrlsCompiler
195206~~~~~
examples/morepages/hakyll.hs+12−11
@@ -9,20 +9,21 @@ import Hakyll
99main :: IO ()
1010main = hakyll $ do
1111 -- Compress CSS
12− route "css/*" idRoute
13− compile "css/*" compressCssCompiler
12+ match "css/*" $ do
13+ route idRoute
14+ compile compressCssCompiler
1415
1516 -- Render static pages
16− forM_ ["about.markdown", "index.markdown", "products.markdown"] $ \p -> do
17− route p $ setExtension ".html"
18− compile p $
19− pageCompiler
20− >>> requireA "footer.markdown" (setFieldA "footer" $ arr pageBody)
21− >>> applyTemplateCompiler "templates/default.html"
22− >>> relativizeUrlsCompiler
17+ forM_ ["about.markdown", "index.markdown", "products.markdown"] $ \p ->
18+ match p $ do
19+ route $ setExtension ".html"
20+ compile $ pageCompiler
21+ >>> requireA "footer.markdown" (setFieldA "footer" $ arr pageBody)
22+ >>> applyTemplateCompiler "templates/default.html"
23+ >>> relativizeUrlsCompiler
2324
2425 -- Compile footer
25− compile "footer.markdown" pageCompiler
26+ match "footer.markdown" $ compile pageCompiler
2627
2728 -- Read templates
28− compile "templates/*" templateCompiler
29+ match "templates/*" $ compile templateCompiler
examples/simpleblog/hakyll.hs+21−25
@@ -11,42 +11,38 @@ import Hakyll
1111main :: IO ()
1212main = hakyll $ do
1313 -- Compress CSS
14− route "css/*" idRoute
15− compile "css/*" compressCssCompiler
14+ match "css/*" $ do
15+ route idRoute
16+ compile compressCssCompiler
1617
1718 -- Render posts
18− route "posts/*" $ setExtension ".html"
19− compile "posts/*" $
20− pageCompiler
19+ match "posts/*" $ do
20+ route $ setExtension ".html"
21+ compile $ pageCompiler
2122 >>> applyTemplateCompiler "templates/post.html"
2223 >>> applyTemplateCompiler "templates/default.html"
2324 >>> relativizeUrlsCompiler
2425
2526 -- Render posts list
26− route "posts.html" $ idRoute
27− create "posts.html" $
28− constA mempty
29− >>> arr (setField "title" "All posts")
30− >>> requireAllA "posts/*" addPostList
31− >>> applyTemplateCompiler "templates/posts.html"
32− >>> applyTemplateCompiler "templates/default.html"
33− >>> relativizeUrlsCompiler
27+ match "posts.html" $ route idRoute
28+ create "posts.html" $ constA mempty
29+ >>> arr (setField "title" "All posts")
30+ >>> requireAllA "posts/*" addPostList
31+ >>> applyTemplateCompiler "templates/posts.html"
32+ >>> applyTemplateCompiler "templates/default.html"
33+ >>> relativizeUrlsCompiler
3434
3535 -- Index
36− route "index.html" idRoute
37− create "index.html" $
38− constA mempty
39− >>> arr (setField "title" "Home")
40− >>> requireAllA "posts/*" (id *** arr (take 3 . reverse . sortByBaseName) >>> addPostList)
41− >>> applyTemplateCompiler "templates/index.html"
42− >>> applyTemplateCompiler "templates/default.html"
43− >>> relativizeUrlsCompiler
36+ match "index.html" $ route idRoute
37+ create "index.html" $ constA mempty
38+ >>> arr (setField "title" "Home")
39+ >>> requireAllA "posts/*" (id *** arr (take 3 . reverse . sortByBaseName) >>> addPostList)
40+ >>> applyTemplateCompiler "templates/index.html"
41+ >>> applyTemplateCompiler "templates/default.html"
42+ >>> relativizeUrlsCompiler
4443
4544 -- Read templates
46− compile "templates/*" templateCompiler
47−
48− -- End
49− return ()
45+ match "templates/*" $ compile templateCompiler
5046
5147-- | Auxiliary compiler: generate a post list from a list of given posts, and
5248-- add it to the current page under @$posts@
examples/tagblog/hakyll.hs+24−28
@@ -12,13 +12,14 @@ import Hakyll
1212main :: IO ()
1313main = hakyll $ do
1414 -- Compress CSS
15− route "css/*" idRoute
16− compile "css/*" compressCssCompiler
15+ match "css/*" $ do
16+ route idRoute
17+ compile compressCssCompiler
1718
1819 -- Render posts
19− route "posts/*" $ setExtension ".html"
20− compile "posts/*" $
21− pageCompiler
20+ match "posts/*" $ do
21+ route $ setExtension ".html"
22+ compile $ pageCompiler
2223 >>> arr (renderDateField "date" "%B %e, %Y" "Date unknown")
2324 >>> renderTagsField "prettytags" (fromCaptureString "tags/*")
2425 >>> applyTemplateCompiler "templates/post.html"
@@ -26,48 +27,43 @@ main = hakyll $ do
2627 >>> relativizeUrlsCompiler
2728
2829 -- Render posts list
29− route "posts.html" $ idRoute
30− create "posts.html" $
31− constA mempty
32− >>> arr (setField "title" "All posts")
33− >>> requireAllA "posts/*" addPostList
34− >>> applyTemplateCompiler "templates/posts.html"
35− >>> applyTemplateCompiler "templates/default.html"
36− >>> relativizeUrlsCompiler
30+ match "posts.html" $ route idRoute
31+ create "posts.html" $ constA mempty
32+ >>> arr (setField "title" "All posts")
33+ >>> requireAllA "posts/*" addPostList
34+ >>> applyTemplateCompiler "templates/posts.html"
35+ >>> applyTemplateCompiler "templates/default.html"
36+ >>> relativizeUrlsCompiler
3737
3838 -- Index
39− route "index.html" $ idRoute
40− create "index.html" $
41− constA mempty
42− >>> arr (setField "title" "Home")
43− >>> requireA "tags" (setFieldA "tagcloud" (renderTagCloud'))
44− >>> requireAllA "posts/*" (id *** arr (take 3 . reverse . sortByBaseName) >>> addPostList)
45− >>> applyTemplateCompiler "templates/index.html"
46− >>> applyTemplateCompiler "templates/default.html"
47− >>> relativizeUrlsCompiler
39+ match "index.html" $ route idRoute
40+ create "index.html" $ constA mempty
41+ >>> arr (setField "title" "Home")
42+ >>> requireA "tags" (setFieldA "tagcloud" (renderTagCloud'))
43+ >>> requireAllA "posts/*" (id *** arr (take 3 . reverse . sortByBaseName) >>> addPostList)
44+ >>> applyTemplateCompiler "templates/index.html"
45+ >>> applyTemplateCompiler "templates/default.html"
46+ >>> relativizeUrlsCompiler
4847
4948 -- Tags
5049 create "tags" $
5150 requireAll "posts/*" (\_ ps -> readTags ps :: Tags String)
5251
5352 -- Add a tag list compiler for every tag
54− route "tags/*" $ setExtension ".html"
53+ match "tags/*" $ route $ setExtension ".html"
5554 metaCompile $ require_ "tags"
5655 >>> arr (M.toList . tagsMap)
5756 >>> arr (map (\(t, p) -> (tagIdentifier t, makeTagList t p)))
5857
5958 -- Render RSS feed
60− route "rss.xml" $ idRoute
59+ match "rss.xml" $ route idRoute
6160 create "rss.xml" $
6261 requireAll_ "posts/*"
6362 >>> mapCompiler (arr $ copyBodyToField "description")
6463 >>> renderRss feedConfiguration
6564
6665 -- Read templates
67− compile "templates/*" templateCompiler
68−
69− -- End
70− return ()
66+ match "templates/*" $ compile templateCompiler
7167 where
7268 renderTagCloud' :: Compiler (Tags String) String
7369 renderTagCloud' = renderTagCloud tagIdentifier 100 120
src/Hakyll/Core/Compiler.hs+1−1
@@ -245,7 +245,7 @@ requireAll_ :: (Binary a, Typeable a, Writable a)
245245 -> Compiler b [a]
246246requireAll_ pattern = fromDependencies (const getDeps) >>> fromJob requireAll_'
247247 where
248− getDeps = matches pattern . map unResource . resourceList
248+ getDeps = filterMatches pattern . map unResource . resourceList
249249 requireAll_' = const $ CompilerM $ do
250250 deps <- getDeps . compilerResourceProvider <$> ask
251251 mapM (unCompilerM . getDependency) deps
src/Hakyll/Core/Identifier/Pattern.hs+74−41
@@ -1,4 +1,12 @@
11-- | Module providing pattern matching and capturing on 'Identifier's.
2+-- 'Pattern's come in two kinds:
3+--
4+-- * Simple glob patterns, like @foo\/*@;
5+--
6+-- * Custom, arbitrary predicates of the type @Identifier -> Bool@.
7+--
8+-- They both have advantages and disadvantages. By default, globs are used,
9+-- unless you construct your 'Pattern' using the 'predicate' function.
210--
311-- A very simple pattern could be, for example, @foo\/bar@. This pattern will
412-- only match the exact @foo\/bar@ identifier.
@@ -20,15 +28,16 @@
2028--
2129-- * @foo\/*.html@ will match all HTML files in the @foo\/@ directory.
2230--
23−-- The 'match' function allows the user to get access to the elements captured
31+-- The 'capture' function allows the user to get access to the elements captured
2432-- by the capture elements in the pattern.
2533--
2634module Hakyll.Core.Identifier.Pattern
2735 ( Pattern
28− , parsePattern
29− , match
30− , doesMatch
36+ , parseGlob
37+ , predicate
3138 , matches
39+ , filterMatches
40+ , capture
3241 , fromCapture
3342 , fromCaptureString
3443 , fromCaptures
@@ -38,7 +47,7 @@ import Data.List (isPrefixOf, inits, tails)
3847import Control.Arrow ((&&&), (>>>))
3948import Control.Monad (msum)
4049import Data.Maybe (isJust)
41−import Data.Monoid (mempty, mappend)
50+import Data.Monoid (Monoid, mempty, mappend)
4251
4352import GHC.Exts (IsString, fromString)
4453
@@ -46,23 +55,29 @@ import Hakyll.Core.Identifier
4655
4756-- | One base element of a pattern
4857--
49−data PatternComponent = Capture
50− | CaptureMany
51− | Literal String
52− deriving (Eq, Show)
58+data GlobComponent = Capture
59+ | CaptureMany
60+ | Literal String
61+ deriving (Eq, Show)
5362
5463-- | Type that allows matching on identifiers
5564--
56−newtype Pattern = Pattern {unPattern :: [PatternComponent]}
57− deriving (Eq, Show)
65+data Pattern = Glob [GlobComponent]
66+ | Predicate (Identifier -> Bool)
5867
5968instance IsString Pattern where
60− fromString = parsePattern
69+ fromString = parseGlob
70+
71+instance Monoid Pattern where
72+ mempty = Predicate (const True)
73+ g@(Glob _) `mappend` x = Predicate (matches g) `mappend` x
74+ x `mappend` g@(Glob _) = x `mappend` Predicate (matches g)
75+ Predicate f `mappend` Predicate g = Predicate $ \i -> f i && g i
6176
6277-- | Parse a pattern from a string
6378--
64−parsePattern :: String -> Pattern
65−parsePattern = Pattern . parse' -- undefined -- Pattern . map toPattern . unIdentifier . parseIdentifier
79+parseGlob :: String -> Pattern
80+parseGlob = Glob . parse'
6681 where
6782 parse' str =
6883 let (chunk, rest) = break (`elem` "\\*") str
@@ -72,20 +87,25 @@ parsePattern = Pattern . parse' -- undefined -- Pattern . map toPattern . unIden
7287 ('*' : xs) -> Literal chunk : Capture : parse' xs
7388 xs -> Literal chunk : Literal xs : []
7489
75−-- | Match an identifier against a pattern, generating a list of captures
90+-- | Create a 'Pattern' from an arbitrary predicate
91+--
92+-- Example:
7693--
77−match :: Pattern -> Identifier -> Maybe [Identifier]
78−match p (Identifier i) = fmap (map Identifier) $ match' (unPattern p) i
94+-- > predicate (\i -> matches "foo/*" i && not (matches "foo/bar" i))
95+--
96+predicate :: (Identifier -> Bool) -> Pattern
97+predicate = Predicate
7998
8099-- | Check if an identifier matches a pattern
81100--
82−doesMatch :: Pattern -> Identifier -> Bool
83−doesMatch p = isJust . match p
101+matches :: Pattern -> Identifier -> Bool
102+matches (Glob p) = isJust . capture (Glob p)
103+matches (Predicate p) = (p $)
84104
85105-- | Given a list of identifiers, retain only those who match the given pattern
86106--
87−matches :: Pattern -> [Identifier] -> [Identifier]
88−matches p = filter (doesMatch p)
107+filterMatches :: Pattern -> [Identifier] -> [Identifier]
108+filterMatches = filter . matches
89109
90110-- | Split a list at every possible point, generate a list of (init, tail)
91111-- cases. The result is sorted with inits decreasing in length.
@@ -93,30 +113,35 @@ matches p = filter (doesMatch p)
93113splits :: [a] -> [([a], [a])]
94114splits = inits &&& tails >>> uncurry zip >>> reverse
95115
96−-- | Internal verion of 'match'
116+-- | Match a glob against a pattern, generating a list of captures
117+--
118+capture :: Pattern -> Identifier -> Maybe [Identifier]
119+capture (Glob p) (Identifier i) = fmap (map Identifier) $ capture' p i
120+capture (Predicate _) _ = Nothing
121+
122+-- | Internal verion of 'capture'
97123--
98−match' :: [PatternComponent] -> String -> Maybe [String]
99−match' [] [] = Just [] -- An empty match
100−match' [] _ = Nothing -- No match
101−-- match' _ [] = Nothing -- No match
102−match' (Literal l : ms) str
124+capture' :: [GlobComponent] -> String -> Maybe [String]
125+capture' [] [] = Just [] -- An empty match
126+capture' [] _ = Nothing -- No match
127+capture' (Literal l : ms) str
103128 -- Match the literal against the string
104− | l `isPrefixOf` str = match' ms $ drop (length l) str
129+ | l `isPrefixOf` str = capture' ms $ drop (length l) str
105130 | otherwise = Nothing
106−match' (Capture : ms) str =
131+capture' (Capture : ms) str =
107132 -- Match until the next /
108133 let (chunk, rest) = break (== '/') str
109− in msum $ [ fmap (i :) (match' ms (t ++ rest)) | (i, t) <- splits chunk ]
110−match' (CaptureMany : ms) str =
134+ in msum $ [ fmap (i :) (capture' ms (t ++ rest)) | (i, t) <- splits chunk ]
135+capture' (CaptureMany : ms) str =
111136 -- Match everything
112− msum $ [ fmap (i :) (match' ms t) | (i, t) <- splits str ]
137+ msum $ [ fmap (i :) (capture' ms t) | (i, t) <- splits str ]
113138
114139-- | Create an identifier from a pattern by filling in the captures with a given
115140-- string
116141--
117142-- Example:
118143--
119−-- > fromCapture (parsePattern "tags/*") (parseIdentifier "foo")
144+-- > fromCapture (parseGlob "tags/*") (parseIdentifier "foo")
120145--
121146-- Result:
122147--
@@ -128,7 +153,7 @@ fromCapture pattern = fromCaptures pattern . repeat
128153-- | Simplified version of 'fromCapture' which takes a 'String' instead of an
129154-- 'Identifier'
130155--
131−-- > fromCaptureString (parsePattern "tags/*") "foo"
156+-- > fromCaptureString (parseGlob "tags/*") "foo"
132157--
133158-- Result:
134159--
@@ -141,11 +166,19 @@ fromCaptureString pattern = fromCapture pattern . parseIdentifier
141166-- given list of strings
142167--
143168fromCaptures :: Pattern -> [Identifier] -> Identifier
144−fromCaptures (Pattern []) _ = mempty
145−fromCaptures (Pattern (m : ms)) [] = case m of
146− Literal l -> Identifier l `mappend` fromCaptures (Pattern ms) []
147− _ -> error $ "Hakyll.Core.Identifier.Pattern.fromCaptures: "
169+fromCaptures (Glob p) = fromCaptures' p
170+fromCaptures (Predicate _) = error $
171+ "Hakyll.Core.Identifier.Pattern.fromCaptures: fromCaptures called on a " ++
172+ "predicate instead of a glob"
173+
174+-- | Internally used version of 'fromCaptures'
175+--
176+fromCaptures' :: [GlobComponent] -> [Identifier] -> Identifier
177+fromCaptures' [] _ = mempty
178+fromCaptures' (m : ms) [] = case m of
179+ Literal l -> Identifier l `mappend` fromCaptures' ms []
180+ _ -> error $ "Hakyll.Core.Identifier.Pattern.fromCaptures': "
148181 ++ "identifier list exhausted"
149−fromCaptures (Pattern (m : ms)) ids@(i : is) = case m of
150− Literal l -> Identifier l `mappend` fromCaptures (Pattern ms) ids
151− _ -> i `mappend` fromCaptures (Pattern ms) is
182+fromCaptures' (m : ms) ids@(i : is) = case m of
183+ Literal l -> Identifier l `mappend` fromCaptures' ms ids
184+ _ -> i `mappend` fromCaptures' ms is
src/Hakyll/Core/Routes.hs+8−8
@@ -30,7 +30,7 @@ module Hakyll.Core.Routes
3030 , runRoutes
3131 , idRoute
3232 , setExtension
33− , ifMatch
33+ , matchRoute
3434 , customRoute
3535 , gsubRoute
3636 , composeRoutes
@@ -85,15 +85,15 @@ setExtension :: String -> Routes
8585setExtension extension = Routes $ fmap (`replaceExtension` extension)
8686 . unRoutes idRoute
8787
88−-- | Modify a route: apply the route if the identifier matches the given
89−-- pattern, fail otherwise.
88+-- | Apply the route if the identifier matches the given pattern, fail
89+-- otherwise
9090--
91−ifMatch :: Pattern -> Routes -> Routes
92−ifMatch pattern (Routes route) = Routes $ \id' ->
93− if doesMatch pattern id' then route id'
94− else Nothing
91+matchRoute :: Pattern -> Routes -> Routes
92+matchRoute pattern (Routes route) = Routes $ \id' ->
93+ if matches pattern id' then route id' else Nothing
9594
96−-- | Create a custom route. This should almost always be used with 'ifMatch'.
95+-- | Create a custom route. This should almost always be used with
96+-- 'matchRoute'
9797--
9898customRoute :: (Identifier -> FilePath) -> Routes
9999customRoute f = Routes $ Just . f
src/Hakyll/Core/Rules.hs+34−16
@@ -7,13 +7,18 @@
77-- A typical usage example would be:
88--
99-- > main = hakyll $ do
10−-- > route "posts/*" (setExtension "html")
11−-- > compile "posts/*" someCompiler
10+-- > match "posts/*" $ do
11+-- > route (setExtension "html")
12+-- > compile someCompiler
13+-- > match "css/*" $ do
14+-- > route idRoute
15+-- > compile compressCssCompiler
1216--
1317{-# LANGUAGE GeneralizedNewtypeDeriving, OverloadedStrings #-}
1418module Hakyll.Core.Rules
1519 ( RulesM
1620 , Rules
21+ , match
1722 , compile
1823 , create
1924 , route
@@ -23,10 +28,10 @@ module Hakyll.Core.Rules
2328
2429import Control.Applicative ((<$>))
2530import Control.Monad.Writer (tell)
26−import Control.Monad.Reader (ask)
31+import Control.Monad.Reader (ask, local)
2732import Control.Arrow (second, (>>>), arr, (>>^))
2833import Control.Monad.State (get, put)
29−import Data.Monoid (mempty)
34+import Data.Monoid (mempty, mappend)
3035import qualified Data.Set as S
3136
3237import Data.Typeable (Typeable)
@@ -63,21 +68,32 @@ tellResources :: [Resource]
6368 -> Rules
6469tellResources resources = RulesM $ tell $ RuleSet mempty mempty $ S.fromList resources
6570
71+-- | Only compile/route items satisfying the given predicate
72+--
73+match :: Pattern -> Rules -> Rules
74+match pattern = RulesM . local addPredicate . unRulesM
75+ where
76+ addPredicate env = env
77+ { rulesPattern = rulesPattern env `mappend` pattern
78+ }
79+
6680-- | Add a compilation rule to the rules.
6781--
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'.
82+-- This instructs all resources to be compiled using the given compiler. When
83+-- no resources match the current selection, nothing will happen. In this case,
84+-- you might want to have a look at 'create'.
7185--
7286compile :: (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
87+ => Compiler Resource a -> Rules
88+compile compiler = RulesM $ do
89+ pattern <- rulesPattern <$> ask
90+ provider <- rulesResourceProvider <$> ask
91+ let ids = filterMatches pattern $ map unResource $ resourceList provider
7692 unRulesM $ do
77− tellCompilers $ flip map identifiers $ \identifier ->
93+ tellCompilers $ flip map ids $ \identifier ->
7894 (identifier, constA (Resource identifier) >>> compiler)
79− tellResources $ map Resource identifiers
80−
95+ tellResources $ map Resource ids
96+
8197-- | Add a compilation rule
8298--
8399-- This sets a compiler for the given identifier. No resource is needed, since
@@ -91,10 +107,12 @@ create identifier compiler = tellCompilers [(identifier, compiler)]
91107
92108-- | Add a route.
93109--
94−-- This adds a route for all items matching the given pattern.
110+-- This adds a route for all items matching the current pattern.
95111--
96−route :: Pattern -> Routes -> Rules
97−route pattern route' = tellRoute $ ifMatch pattern route'
112+route :: Routes -> Rules
113+route route' = RulesM $ do
114+ pattern <- rulesPattern <$> ask
115+ unRulesM $ tellRoute $ matchRoute pattern route'
98116
99117-- | Apart from regular compilers, one is also able to specify metacompilers.
100118-- Metacompilers are a special class of compilers: they are compilers which
src/Hakyll/Core/Rules/Internal.hs+14−2
@@ -5,6 +5,7 @@ module Hakyll.Core.Rules.Internal
55 ( CompileRule (..)
66 , RuleSet (..)
77 , RuleState (..)
8+ , RuleEnvironment (..)
89 , RulesM (..)
910 , Rules
1011 , runRules
@@ -19,6 +20,7 @@ import Data.Set (Set)
1920
2021import Hakyll.Core.ResourceProvider
2122import Hakyll.Core.Identifier
23+import Hakyll.Core.Identifier.Pattern
2224import Hakyll.Core.Compiler.Internal
2325import Hakyll.Core.Routes
2426import Hakyll.Core.CompiledItem
@@ -55,10 +57,17 @@ data RuleState = RuleState
5557 { rulesMetaCompilerIndex :: Int
5658 } deriving (Show)
5759
60+-- | Rule environment
61+--
62+data RuleEnvironment = RuleEnvironment
63+ { rulesResourceProvider :: ResourceProvider
64+ , rulesPattern :: Pattern
65+ }
66+
5867-- | The monad used to compose rules
5968--
6069newtype RulesM a = RulesM
61− { unRulesM :: ReaderT ResourceProvider (WriterT RuleSet (State RuleState)) a
70+ { unRulesM :: ReaderT RuleEnvironment (WriterT RuleSet (State RuleState)) a
6271 } deriving (Monad, Functor, Applicative)
6372
6473-- | Simplification of the RulesM type; usually, it will not return any
@@ -70,6 +79,9 @@ type Rules = RulesM ()
7079--
7180runRules :: Rules -> ResourceProvider -> RuleSet
7281runRules rules provider =
73− evalState (execWriterT $ runReaderT (unRulesM rules) provider) state
82+ evalState (execWriterT $ runReaderT (unRulesM rules) env) state
7483 where
7584 state = RuleState {rulesMetaCompilerIndex = 0}
85+ env = RuleEnvironment { rulesResourceProvider = provider
86+ , rulesPattern = mempty
87+ }
tests/Hakyll/Core/Identifier/Tests.hs+14−14
@@ -10,18 +10,18 @@ import Hakyll.Core.Identifier.Pattern
1010import TestSuite.Util
1111
1212tests :: [Test]
13−tests = fromAssertions "match"
14− [ Just ["bar"] @=? match "foo/**" "foo/bar"
15− , Just ["foo/bar"] @=? match "**" "foo/bar"
16− , Nothing @=? match "*" "foo/bar"
17− , Just [] @=? match "foo" "foo"
18− , Just ["foo"] @=? match "*/bar" "foo/bar"
19− , Just ["foo/bar"] @=? match "**/qux" "foo/bar/qux"
20− , Just ["foo/bar", "qux"] @=? match "**/*" "foo/bar/qux"
21− , Just ["foo", "bar/qux"] @=? match "*/**" "foo/bar/qux"
22− , Just ["foo"] @=? match "*.html" "foo.html"
23− , Nothing @=? match "*.html" "foo/bar.html"
24− , Just ["foo/bar"] @=? match "**.html" "foo/bar.html"
25− , Just ["foo/bar", "wut"] @=? match "**/qux/*" "foo/bar/qux/wut"
26− , Just ["lol", "fun/large"] @=? match "*cat/**.jpg" "lolcat/fun/large.jpg"
13+tests = fromAssertions "capture"
14+ [ Just ["bar"] @=? capture "foo/**" "foo/bar"
15+ , Just ["foo/bar"] @=? capture "**" "foo/bar"
16+ , Nothing @=? capture "*" "foo/bar"
17+ , Just [] @=? capture "foo" "foo"
18+ , Just ["foo"] @=? capture "*/bar" "foo/bar"
19+ , Just ["foo/bar"] @=? capture "**/qux" "foo/bar/qux"
20+ , Just ["foo/bar", "qux"] @=? capture "**/*" "foo/bar/qux"
21+ , Just ["foo", "bar/qux"] @=? capture "*/**" "foo/bar/qux"
22+ , Just ["foo"] @=? capture "*.html" "foo.html"
23+ , Nothing @=? capture "*.html" "foo/bar.html"
24+ , Just ["foo/bar"] @=? capture "**.html" "foo/bar.html"
25+ , Just ["foo/bar", "wut"] @=? capture "**/qux/*" "foo/bar/qux/wut"
26+ , Just ["lol", "fun/large"] @=? capture "*cat/**.jpg" "lolcat/fun/large.jpg"
2727 ]