@@ -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