on master help sign in
Restructure pattern internals
commit091014a60e29700017c8d77b7561f37ae4a611be authorJasper Van der Jeugt <jaspervdj@gmail.com> dateTue 5 Apr 2011, 10:55 (16 years ago) parentf6c65aa
1 file changed+59−42
src/Hakyll/Core/Identifier/Pattern.hs+59−42
src/Hakyll/Core/Identifier/Pattern.hs+59−42
@@ -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,15 @@
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
3137 , matches
38+ , filterMatches
39+ , capture
3240 , fromCapture
3341 , fromCaptureString
3442 , fromCaptures
@@ -46,23 +54,23 @@ import Hakyll.Core.Identifier
4654
4755-- | One base element of a pattern
4856--
49−data PatternComponent = Capture
50− | CaptureMany
51− | Literal String
52− deriving (Eq, Show)
57+data GlobComponent = Capture
58+ | CaptureMany
59+ | Literal String
60+ deriving (Eq, Show)
5361
5462-- | Type that allows matching on identifiers
5563--
56−newtype Pattern = Pattern {unPattern :: [PatternComponent]}
57− deriving (Eq, Show)
64+data Pattern = Glob [GlobComponent]
65+ | Predicate (Identifier -> Bool)
5866
5967instance IsString Pattern where
60− fromString = parsePattern
68+ fromString = parseGlob
6169
6270-- | Parse a pattern from a string
6371--
64−parsePattern :: String -> Pattern
65−parsePattern = Pattern . parse' -- undefined -- Pattern . map toPattern . unIdentifier . parseIdentifier
72+parseGlob :: String -> Pattern
73+parseGlob = Glob . parse'
6674 where
6775 parse' str =
6876 let (chunk, rest) = break (`elem` "\\*") str
@@ -72,20 +80,16 @@ parsePattern = Pattern . parse' -- undefined -- Pattern . map toPattern . unIden
7280 ('*' : xs) -> Literal chunk : Capture : parse' xs
7381 xs -> Literal chunk : Literal xs : []
7482
75−-- | Match an identifier against a pattern, generating a list of captures
76−--
77−match :: Pattern -> Identifier -> Maybe [Identifier]
78−match p (Identifier i) = fmap (map Identifier) $ match' (unPattern p) i
79−
8083-- | Check if an identifier matches a pattern
8184--
82−doesMatch :: Pattern -> Identifier -> Bool
83−doesMatch p = isJust . match p
85+matches :: Pattern -> Identifier -> Bool
86+matches (Glob p) = isJust . capture (Glob p)
87+matches (Predicate p) = (p $)
8488
8589-- | Given a list of identifiers, retain only those who match the given pattern
8690--
87−matches :: Pattern -> [Identifier] -> [Identifier]
88−matches p = filter (doesMatch p)
91+filterMatches :: Pattern -> [Identifier] -> [Identifier]
92+filterMatches = filter . matches
8993
9094-- | Split a list at every possible point, generate a list of (init, tail)
9195-- cases. The result is sorted with inits decreasing in length.
@@ -93,30 +97,35 @@ matches p = filter (doesMatch p)
9397splits :: [a] -> [([a], [a])]
9498splits = inits &&& tails >>> uncurry zip >>> reverse
9599
96−-- | Internal verion of 'match'
100+-- | Match a glob against a pattern, generating a list of captures
97101--
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
102+capture :: Pattern -> Identifier -> Maybe [Identifier]
103+capture (Glob p) (Identifier i) = fmap (map Identifier) $ capture' p i
104+capture (Predicate _) _ = Nothing
105+
106+-- | Internal verion of 'capture'
107+--
108+capture' :: [GlobComponent] -> String -> Maybe [String]
109+capture' [] [] = Just [] -- An empty match
110+capture' [] _ = Nothing -- No match
111+capture' (Literal l : ms) str
103112 -- Match the literal against the string
104− | l `isPrefixOf` str = match' ms $ drop (length l) str
113+ | l `isPrefixOf` str = capture' ms $ drop (length l) str
105114 | otherwise = Nothing
106−match' (Capture : ms) str =
115+capture' (Capture : ms) str =
107116 -- Match until the next /
108117 let (chunk, rest) = break (== '/') str
109− in msum $ [ fmap (i :) (match' ms (t ++ rest)) | (i, t) <- splits chunk ]
110−match' (CaptureMany : ms) str =
118+ in msum $ [ fmap (i :) (capture' ms (t ++ rest)) | (i, t) <- splits chunk ]
119+capture' (CaptureMany : ms) str =
111120 -- Match everything
112− msum $ [ fmap (i :) (match' ms t) | (i, t) <- splits str ]
121+ msum $ [ fmap (i :) (capture' ms t) | (i, t) <- splits str ]
113122
114123-- | Create an identifier from a pattern by filling in the captures with a given
115124-- string
116125--
117126-- Example:
118127--
119−-- > fromCapture (parsePattern "tags/*") (parseIdentifier "foo")
128+-- > fromCapture (parseGlob "tags/*") (parseIdentifier "foo")
120129--
121130-- Result:
122131--
@@ -128,7 +137,7 @@ fromCapture pattern = fromCaptures pattern . repeat
128137-- | Simplified version of 'fromCapture' which takes a 'String' instead of an
129138-- 'Identifier'
130139--
131−-- > fromCaptureString (parsePattern "tags/*") "foo"
140+-- > fromCaptureString (parseGlob "tags/*") "foo"
132141--
133142-- Result:
134143--
@@ -141,11 +150,19 @@ fromCaptureString pattern = fromCapture pattern . parseIdentifier
141150-- given list of strings
142151--
143152fromCaptures :: 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: "
153+fromCaptures (Glob p) = fromCaptures' p
154+fromCaptures (Predicate _) = error $
155+ "Hakyll.Core.Identifier.Pattern.fromCaptures: fromCaptures called on a " ++
156+ "predicate instead of a glob"
157+
158+-- | Internally used version of 'fromCaptures'
159+--
160+fromCaptures' :: [GlobComponent] -> [Identifier] -> Identifier
161+fromCaptures' [] _ = mempty
162+fromCaptures' (m : ms) [] = case m of
163+ Literal l -> Identifier l `mappend` fromCaptures' ms []
164+ _ -> error $ "Hakyll.Core.Identifier.Pattern.fromCaptures': "
148165 ++ "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
166+fromCaptures' (m : ms) ids@(i : is) = case m of
167+ Literal l -> Identifier l `mappend` fromCaptures' ms ids
168+ _ -> i `mappend` fromCaptures' ms is