on master help sign in
Extend capture with Regex handling
293c379frederik-h
lib/Hakyll/Core/Identifier/Pattern.hs+4−2
@@ -27,7 +27,7 @@
27-- * @\"foo\/*.html\"@ will match all HTML files in the @\"foo\/\"@ directory.
27-- * @\"foo\/*.html\"@ will match all HTML files in the @\"foo\/\"@ directory.
28--
28--
29-- The 'capture' function allows the user to get access to the elements captured
29-- The 'capture' function allows the user to get access to the elements captured
30−-- by the capture elements in the pattern.
30+-- by the capture elements in a glob or regex pattern.
31module Hakyll.Core.Identifier.Pattern
31module Hakyll.Core.Identifier.Pattern
32 ( -- * The pattern type
32 ( -- * The pattern type
33 Pattern
33 Pattern
@@ -260,9 +260,11 @@ splits = inits &&& tails >>> uncurry zip >>> reverse
260
260
261
261
262--------------------------------------------------------------------------------
262--------------------------------------------------------------------------------
263−-- | Match a glob against a pattern, generating a list of captures
263+-- | Match a glob or regex pattern against an identifier, generating a list of captures
264capture :: Pattern -> Identifier -> Maybe [String]
264capture :: Pattern -> Identifier -> Maybe [String]
265capture (Glob p) i = capture' p (toFilePath i)
265capture (Glob p) i = capture' p (toFilePath i)
266+capture (Regex pat) i = Just groups
267+ where (_, _, _, groups) = ((toFilePath i) =~ pat) :: (String, String, String, [String])
266capture _ _ = Nothing
268capture _ _ = Nothing
267
269
268
270
tests/Hakyll/Core/Identifier/Tests.hs+1
@@ -42,6 +42,7 @@ captureTests = fromAssertions "capture"
42 , Just ["lol", "fun/large"] @=? capture "*cat/**.jpg" "lolcat/fun/large.jpg"
42 , Just ["lol", "fun/large"] @=? capture "*cat/**.jpg" "lolcat/fun/large.jpg"
43 , Just [] @=? capture "\\*.jpg" "*.jpg"
43 , Just [] @=? capture "\\*.jpg" "*.jpg"
44 , Nothing @=? capture "\\*.jpg" "foo.jpg"
44 , Nothing @=? capture "\\*.jpg" "foo.jpg"
45+ , Just ["xyz","42"] @=? capture (fromRegex "cat-([a-z]+)/foo([0-9]+).jpg") "cat-xyz/foo42.jpg"
45 ]
46 ]
46
47
47
48