on master help sign in
Add .&&. and .||. operators
commit8c0a609d6a8437b129228cfaad592d839b60b2c8 authorJasper Van der Jeugt <m@jaspervdj.be> dateThu 3 Jan 2013, 19:02 (14 years ago) parent9ea75c1
2 files changed+33−9
src/Hakyll/Core/Identifier/Pattern.hs+32−9 tests/Hakyll/Core/Identifier/Tests.hs+1
src/Hakyll/Core/Identifier/Pattern.hs+32−9
@@ -39,6 +39,8 @@ module Hakyll.Core.Identifier.Pattern
3939 , fromVersion
4040
4141 -- * Manipulating patterns
42+ , (.&&.)
43+ , (.||.)
4244 , complement
4345 , withVersion
4446 , fromLiteral
@@ -103,6 +105,7 @@ data Pattern
103105 = Everything
104106 | Complement Pattern
105107 | And Pattern Pattern
108+ | Or Pattern Pattern
106109 | Glob [GlobComponent]
107110 | List (Set Identifier)
108111 | Regex String
@@ -115,18 +118,20 @@ instance Binary Pattern where
115118 put Everything = putWord8 0
116119 put (Complement p) = putWord8 1 >> put p
117120 put (And x y) = putWord8 2 >> put x >> put y
118− put (Glob g) = putWord8 3 >> put g
119− put (List is) = putWord8 4 >> put is
120− put (Regex r) = putWord8 5 >> put r
121− put (Version v) = putWord8 6 >> put v
121+ put (Or x y) = putWord8 3 >> put x >> put y
122+ put (Glob g) = putWord8 4 >> put g
123+ put (List is) = putWord8 5 >> put is
124+ put (Regex r) = putWord8 6 >> put r
125+ put (Version v) = putWord8 7 >> put v
122126
123127 get = getWord8 >>= \t -> case t of
124128 0 -> pure Everything
125129 1 -> Complement <$> get
126130 2 -> And <$> get <*> get
127− 3 -> Glob <$> get
128− 4 -> List <$> get
129− 5 -> Regex <$> get
131+ 3 -> Or <$> get <*> get
132+ 4 -> Glob <$> get
133+ 5 -> List <$> get
134+ 6 -> Regex <$> get
130135 _ -> Version <$> get
131136
132137
@@ -137,8 +142,8 @@ instance IsString Pattern where
137142
138143--------------------------------------------------------------------------------
139144instance Monoid Pattern where
140− mempty = Everything
141− mappend x y = optimize $ And x y
145+ mempty = Everything
146+ mappend = (.&&.)
142147
143148
144149--------------------------------------------------------------------------------
@@ -148,6 +153,9 @@ optimize (Complement x) = Complement (optimize x)
148153optimize (And x Everything) = x
149154optimize (And Everything y) = y
150155optimize (And x y) = And (optimize x) (optimize y)
156+optimize (Or _ Everything) = Everything
157+optimize (Or Everything _) = Everything
158+optimize (Or x y) = Or (optimize x) (optimize y)
151159optimize p = p
152160
153161
@@ -187,6 +195,20 @@ fromVersion :: Maybe String -> Pattern
187195fromVersion = Version
188196
189197
198+--------------------------------------------------------------------------------
199+-- | '&&' for patterns: the given identifier must match both subterms
200+(.&&.) :: Pattern -> Pattern -> Pattern
201+x .&&. y = optimize (And x y)
202+infixr 3 .&&.
203+
204+
205+--------------------------------------------------------------------------------
206+-- | '||' for patterns: the given identifier must match any subterm
207+(.||.) :: Pattern -> Pattern -> Pattern
208+x .||. y = optimize (Or x y)
209+infixr 2 .||.
210+
211+
190212--------------------------------------------------------------------------------
191213-- | Inverts a pattern, e.g.
192214--
@@ -224,6 +246,7 @@ matches :: Pattern -> Identifier -> Bool
224246matches Everything _ = True
225247matches (Complement p) i = not $ matches p i
226248matches (And x y) i = matches x i && matches y i
249+matches (Or x y) i = matches x i || matches y i
227250matches (Glob p) i = isJust $ capture (Glob p) i
228251matches (List l) i = i `S.member` l
229252matches (Regex r) i = toFilePath i =~ r
tests/Hakyll/Core/Identifier/Tests.hs+1
@@ -66,4 +66,5 @@ matchesTests = fromAssertions "matches"
6666 , False @=? matches (fromRegex "^foo/[^x]*$") "foo/barx"
6767 , True @=? matches (complement "foo.markdown") "bar.markdown"
6868 , False @=? matches (complement "foo.markdown") "foo.markdown"
69+ , True @=? matches ("foo" .||. "bar") "bar"
6970 ]