Permalink
Browse files

Add complement function for patterns

  • Loading branch information...
1 parent 2416c76 commit 1996052b110b8528e90d76c06e13c0b7e4a2e93a @jaspervdj committed Feb 19, 2012
Showing with 21 additions and 4 deletions.
  1. +16 −1 src/Hakyll/Core/Identifier/Pattern.hs
  2. +5 −3 tests/Hakyll/Core/Identifier/Tests.hs
@@ -36,13 +36,19 @@
-- function.
--
module Hakyll.Core.Identifier.Pattern
- ( Pattern
+ ( -- * The pattern type
+ Pattern
, castPattern
+
+ -- * Creating patterns
, parseGlob
, predicate
, list
, regex
, inGroup
+ , complement
+
+ -- * Applying patterns
, matches
, filterMatches
, capture
@@ -131,6 +137,15 @@ regex str = predicate $ fromMaybe False . (=~~ str) . toFilePath
inGroup :: Maybe String -> Pattern a
inGroup group = predicate $ (== group) . identifierGroup
+-- | Inverts a pattern, e.g.
+--
+-- > complement "foo/bar.html"
+--
+-- will match /anything/ except @\"foo\/bar.html\"@
+--
+complement :: Pattern a -> Pattern a
+complement p = predicate (not . matches p)
+
-- | Check if an identifier matches a pattern
--
matches :: Pattern a -> Identifier a -> Bool
@@ -37,8 +37,10 @@ captureTests = fromAssertions "capture"
matchesTests :: [Test]
matchesTests = fromAssertions "matches"
- [ True @=? matches (regex "^foo/[^x]*$") "foo/bar"
- , False @=? matches (regex "^foo/[^x]*$") "foo/barx"
- , True @=? matches (list ["foo.markdown"]) "foo.markdown"
+ [ True @=? matches (list ["foo.markdown"]) "foo.markdown"
, False @=? matches (list ["foo"]) (Identifier (Just "foo") "foo")
+ , True @=? matches (regex "^foo/[^x]*$") "foo/bar"
+ , False @=? matches (regex "^foo/[^x]*$") "foo/barx"
+ , True @=? matches (complement "foo.markdown") "bar.markdown"
+ , False @=? matches (complement "foo.markdown") "foo.markdown"
]

0 comments on commit 1996052

Please sign in to comment.