on master help sign in
Add regex predicate helper
78dbe8aJasper Van der Jeugt
src/Hakyll/Core/Identifier/Pattern.hs+12−1
@@ -35,6 +35,7 @@ module Hakyll.Core.Identifier.Pattern
35 ( Pattern
35 ( Pattern
36 , parseGlob
36 , parseGlob
37 , predicate
37 , predicate
38+ , regex
38 , matches
39 , matches
39 , filterMatches
40 , filterMatches
40 , capture
41 , capture
@@ -46,10 +47,11 @@ module Hakyll.Core.Identifier.Pattern
46import Data.List (isPrefixOf, inits, tails)
47import Data.List (isPrefixOf, inits, tails)
47import Control.Arrow ((&&&), (>>>))
48import Control.Arrow ((&&&), (>>>))
48import Control.Monad (msum)
49import Control.Monad (msum)
49−import Data.Maybe (isJust)
50+import Data.Maybe (isJust, fromMaybe)
50import Data.Monoid (Monoid, mempty, mappend)
51import Data.Monoid (Monoid, mempty, mappend)
51
52
52import GHC.Exts (IsString, fromString)
53import GHC.Exts (IsString, fromString)
54+import Text.Regex.PCRE ((=~~))
53
55
54import Hakyll.Core.Identifier
56import Hakyll.Core.Identifier
55
57
@@ -96,6 +98,15 @@ parseGlob = Glob . parse'
96predicate :: (Identifier -> Bool) -> Pattern
98predicate :: (Identifier -> Bool) -> Pattern
97predicate = Predicate
99predicate = Predicate
98
100
101+-- | Create a 'Pattern' from a regex
102+--
103+-- Example:
104+--
105+-- > regex "^foo/[^x]*$
106+--
107+regex :: String -> Pattern
108+regex str = predicate $ fromMaybe False . (=~~ str) . toFilePath
109+
99-- | Check if an identifier matches a pattern
110-- | Check if an identifier matches a pattern
100--
111--
101matches :: Pattern -> Identifier -> Bool
112matches :: Pattern -> Identifier -> Bool
tests/Hakyll/Core/Identifier/Tests.hs+13−1
@@ -10,7 +10,13 @@ import Hakyll.Core.Identifier.Pattern
10import TestSuite.Util
10import TestSuite.Util
11
11
12tests :: [Test]
12tests :: [Test]
13+tests = concat
14+ [ captureTests
15+ , regexTests
16+ ]
17+
18+captureTests :: [Test]
13−tests = fromAssertions "capture"
19+captureTests = fromAssertions "capture"
14 [ Just ["bar"] @=? capture "foo/**" "foo/bar"
20 [ Just ["bar"] @=? capture "foo/**" "foo/bar"
15 , Just ["foo/bar"] @=? capture "**" "foo/bar"
21 , Just ["foo/bar"] @=? capture "**" "foo/bar"
16 , Nothing @=? capture "*" "foo/bar"
22 , Nothing @=? capture "*" "foo/bar"
@@ -25,3 +31,9 @@ tests = fromAssertions "capture"
25 , Just ["foo/bar", "wut"] @=? capture "**/qux/*" "foo/bar/qux/wut"
31 , Just ["foo/bar", "wut"] @=? capture "**/qux/*" "foo/bar/qux/wut"
26 , Just ["lol", "fun/large"] @=? capture "*cat/**.jpg" "lolcat/fun/large.jpg"
32 , Just ["lol", "fun/large"] @=? capture "*cat/**.jpg" "lolcat/fun/large.jpg"
27 ]
33 ]
34+
35+regexTests :: [Test]
36+regexTests = fromAssertions "regex"
37+ [ True @=? matches (regex "^foo/[^x]*$") "foo/bar"
38+ , False @=? matches (regex "^foo/[^x]*$") "foo/barx"
39+ ]