on master help sign in
Extend capture with Regex handling
commit293c379e1634b39315cf6a8cab80470aa3c50ad0 authorfrederik-h <frederik-h@users.noreply.github.com> dateMon 25 Dec 2017, 15:42 (9 years ago) parente18de17
2 files changed+5−2
lib/Hakyll/Core/Identifier/Pattern.hs+4−2 tests/Hakyll/Core/Identifier/Tests.hs+1
lib/Hakyll/Core/Identifier/Pattern.hs+4−2
@@ -27,7 +27,7 @@
2727-- * @\"foo\/*.html\"@ will match all HTML files in the @\"foo\/\"@ directory.
2828--
2929-- 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.
3131module Hakyll.Core.Identifier.Pattern
3232 ( -- * The pattern type
3333 Pattern
@@ -260,9 +260,11 @@ splits = inits &&& tails >>> uncurry zip >>> reverse
260260
261261
262262--------------------------------------------------------------------------------
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
264264capture :: Pattern -> Identifier -> Maybe [String]
265265capture (Glob p) i = capture' p (toFilePath i)
266+capture (Regex pat) i = Just groups
267+ where (_, _, _, groups) = ((toFilePath i) =~ pat) :: (String, String, String, [String])
266268capture _ _ = Nothing
267269
268270
tests/Hakyll/Core/Identifier/Tests.hs+1
@@ -42,6 +42,7 @@ captureTests = fromAssertions "capture"
4242 , Just ["lol", "fun/large"] @=? capture "*cat/**.jpg" "lolcat/fun/large.jpg"
4343 , Just [] @=? capture "\\*.jpg" "*.jpg"
4444 , Nothing @=? capture "\\*.jpg" "foo.jpg"
45+ , Just ["xyz","42"] @=? capture (fromRegex "cat-([a-z]+)/foo([0-9]+).jpg") "cat-xyz/foo42.jpg"
4546 ]
4647
4748