on master help sign in
Add regex predicate helper
commit78dbe8a3d127546c8c0cc5b464da0f2b8af7c9b0 authorJasper Van der Jeugt <jaspervdj@gmail.com> dateWed 6 Apr 2011, 09:39 (16 years ago) parent3d2b250
2 files changed+25−2
src/Hakyll/Core/Identifier/Pattern.hs+12−1 tests/Hakyll/Core/Identifier/Tests.hs+13−1
src/Hakyll/Core/Identifier/Pattern.hs+12−1
@@ -35,6 +35,7 @@ module Hakyll.Core.Identifier.Pattern
3535 ( Pattern
3636 , parseGlob
3737 , predicate
38+ , regex
3839 , matches
3940 , filterMatches
4041 , capture
@@ -46,10 +47,11 @@ module Hakyll.Core.Identifier.Pattern
4647import Data.List (isPrefixOf, inits, tails)
4748import Control.Arrow ((&&&), (>>>))
4849import Control.Monad (msum)
49−import Data.Maybe (isJust)
50+import Data.Maybe (isJust, fromMaybe)
5051import Data.Monoid (Monoid, mempty, mappend)
5152
5253import GHC.Exts (IsString, fromString)
54+import Text.Regex.PCRE ((=~~))
5355
5456import Hakyll.Core.Identifier
5557
@@ -96,6 +98,15 @@ parseGlob = Glob . parse'
9698predicate :: (Identifier -> Bool) -> Pattern
9799predicate = Predicate
98100
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+
99110-- | Check if an identifier matches a pattern
100111--
101112matches :: Pattern -> Identifier -> Bool
tests/Hakyll/Core/Identifier/Tests.hs+13−1
@@ -10,7 +10,13 @@ import Hakyll.Core.Identifier.Pattern
1010import TestSuite.Util
1111
1212tests :: [Test]
13−tests = fromAssertions "capture"
13+tests = concat
14+ [ captureTests
15+ , regexTests
16+ ]
17+
18+captureTests :: [Test]
19+captureTests = fromAssertions "capture"
1420 [ Just ["bar"] @=? capture "foo/**" "foo/bar"
1521 , Just ["foo/bar"] @=? capture "**" "foo/bar"
1622 , Nothing @=? capture "*" "foo/bar"
@@ -25,3 +31,9 @@ tests = fromAssertions "capture"
2531 , Just ["foo/bar", "wut"] @=? capture "**/qux/*" "foo/bar/qux/wut"
2632 , Just ["lol", "fun/large"] @=? capture "*cat/**.jpg" "lolcat/fun/large.jpg"
2733 ]
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+ ]