diff options
author | frederik-h <frederik-h@users.noreply.github.com> | 2017-12-25 15:42:21 +0100 |
---|---|---|
committer | Jasper Van der Jeugt <jaspervdj@gmail.com> | 2017-12-25 15:42:21 +0100 |
commit | 293c379e1634b39315cf6a8cab80470aa3c50ad0 (patch) | |
tree | d2981d9090007c901599f67cab8b8c9caaffba3d | |
parent | e18de17f4f313483193daf68419a73f23aa79de1 (diff) | |
download | hakyll-293c379e1634b39315cf6a8cab80470aa3c50ad0.tar.gz |
Extend capture with Regex handling
-rw-r--r-- | lib/Hakyll/Core/Identifier/Pattern.hs | 6 | ||||
-rw-r--r-- | tests/Hakyll/Core/Identifier/Tests.hs | 1 |
2 files changed, 5 insertions, 2 deletions
diff --git a/lib/Hakyll/Core/Identifier/Pattern.hs b/lib/Hakyll/Core/Identifier/Pattern.hs index 47ad21b..83d5adc 100644 --- a/lib/Hakyll/Core/Identifier/Pattern.hs +++ b/lib/Hakyll/Core/Identifier/Pattern.hs @@ -27,7 +27,7 @@ -- * @\"foo\/*.html\"@ will match all HTML files in the @\"foo\/\"@ directory. -- -- The 'capture' function allows the user to get access to the elements captured --- by the capture elements in the pattern. +-- by the capture elements in a glob or regex pattern. module Hakyll.Core.Identifier.Pattern ( -- * The pattern type Pattern @@ -260,9 +260,11 @@ splits = inits &&& tails >>> uncurry zip >>> reverse -------------------------------------------------------------------------------- --- | Match a glob against a pattern, generating a list of captures +-- | Match a glob or regex pattern against an identifier, generating a list of captures capture :: Pattern -> Identifier -> Maybe [String] capture (Glob p) i = capture' p (toFilePath i) +capture (Regex pat) i = Just groups + where (_, _, _, groups) = ((toFilePath i) =~ pat) :: (String, String, String, [String]) capture _ _ = Nothing diff --git a/tests/Hakyll/Core/Identifier/Tests.hs b/tests/Hakyll/Core/Identifier/Tests.hs index a5de85a..2829927 100644 --- a/tests/Hakyll/Core/Identifier/Tests.hs +++ b/tests/Hakyll/Core/Identifier/Tests.hs @@ -42,6 +42,7 @@ captureTests = fromAssertions "capture" , Just ["lol", "fun/large"] @=? capture "*cat/**.jpg" "lolcat/fun/large.jpg" , Just [] @=? capture "\\*.jpg" "*.jpg" , Nothing @=? capture "\\*.jpg" "foo.jpg" + , Just ["xyz","42"] @=? capture (fromRegex "cat-([a-z]+)/foo([0-9]+).jpg") "cat-xyz/foo42.jpg" ] |