1--------------------------------------------------------------------------------
2-- | Module providing pattern matching and capturing on 'Identifier's.
3-- 'Pattern's come in two kinds:
5-- * Simple glob patterns, like @foo\/*@;
7-- * Custom, arbitrary predicates of the type @Identifier -> Bool@.
9-- They both have advantages and disadvantages. By default, globs are used,
10-- unless you construct your 'Pattern' using the 'predicate' function.
12-- A very simple pattern could be, for example, @foo\/bar@. This pattern will
13-- only match the exact @foo\/bar@ identifier.
15-- To match more than one identifier, there are different captures that one can
18-- * @*@: matches at most one element of an identifier;
20-- * @**@: matches one or more elements of an identifier.
24-- * @foo\/*@ will match @foo\/bar@ and @foo\/foo@, but not @foo\/bar\/qux@;
26-- * @**@ will match any identifier;
28-- * @foo\/**@ will match @foo\/bar@ and @foo\/bar\/qux@, but not @bar\/foo@;
30-- * @foo\/*.html@ will match all HTML files in the @foo\/@ directory.
32-- The 'capture' function allows the user to get access to the elements captured
33-- by the capture elements in the pattern.
35-- Like an 'Identifier', a 'Pattern' also has a type parameter. This is simply
36-- an extra layer of safety, and can be discarded using the 'castPattern'
38module Hakyll.Core.Identifier.Pattern
39 ( -- * The pattern type
42 -- * Creating patterns
47 -- * Manipulating patterns
52 -- * Applying patterns
56 -- * Capturing strings
63--------------------------------------------------------------------------------
64import Control.Applicative (pure, (<$>), (<*>))
65import Control.Arrow ((&&&), (>>>))
66import Control.Monad (msum)
67import Data.Binary (Binary (..), getWord8, putWord8)
68import Data.List (inits, isPrefixOf, tails)
69import Data.Maybe (isJust)
70import Data.Monoid (Monoid, mappend, mempty)
73--------------------------------------------------------------------------------
74import GHC.Exts (IsString, fromString)
75import Text.Regex.TDFA ((=~))
78--------------------------------------------------------------------------------
79import Hakyll.Core.Identifier
82--------------------------------------------------------------------------------
83-- | Elements of a glob pattern
91--------------------------------------------------------------------------------
92instance Binary GlobComponent where
93 put Capture = putWord8 0
94 put CaptureMany = putWord8 1
95 put (Literal s) = putWord8 2 >> put s
97 get = getWord8 >>= \t -> case t of
101 _ -> error "Data.Binary.get: Invalid GlobComponent"
104--------------------------------------------------------------------------------
105-- | Type that allows matching on identifiers
108 | Complement (Pattern a)
109 | And (Pattern a) (Pattern a)
110 | Glob [GlobComponent]
111 | List [Identifier a] -- TODO Maybe use a set here
113 | Version (Maybe String)
117--------------------------------------------------------------------------------
118instance Binary (Pattern a) where
119 put Everything = putWord8 0
120 put (Complement p) = putWord8 1 >> put p
121 put (And x y) = putWord8 2 >> put x >> put y
122 put (Glob g) = putWord8 3 >> put g
123 put (List is) = putWord8 4 >> put is
124 put (Regex r) = putWord8 5 >> put r
125 put (Version v) = putWord8 6 >> put v
127 get = getWord8 >>= \t -> case t of
129 1 -> Complement <$> get
130 2 -> And <$> get <*> get
137--------------------------------------------------------------------------------
138instance IsString (Pattern a) where
139 fromString = fromGlob
142--------------------------------------------------------------------------------
143instance Monoid (Pattern a) where
148--------------------------------------------------------------------------------
149-- | Parse a pattern from a string
150fromGlob :: String -> Pattern a
151fromGlob = Glob . parse'
154 let (chunk, rest) = break (`elem` "\\*") str
156 ('\\' : x : xs) -> Literal (chunk ++ [x]) : parse' xs
157 ('*' : '*' : xs) -> Literal chunk : CaptureMany : parse' xs
158 ('*' : xs) -> Literal chunk : Capture : parse' xs
159 xs -> Literal chunk : Literal xs : []
162--------------------------------------------------------------------------------
163-- | Create a 'Pattern' from a list of 'Identifier's it should match
164fromList :: [Identifier a] -> Pattern a
168--------------------------------------------------------------------------------
169-- | Create a 'Pattern' from a regex
173-- > regex "^foo/[^x]*$
174fromRegex :: String -> Pattern a
178--------------------------------------------------------------------------------
179-- | Inverts a pattern, e.g.
181-- > complement "foo/bar.html"
183-- will match /anything/ except @\"foo\/bar.html\"@
184complement :: Pattern a -> Pattern a
185complement = Complement
188--------------------------------------------------------------------------------
189-- | Specify a version, e.g.
191-- > "foo/*.markdown" `withVersion` "pdf"
192withVersion :: Pattern a -> String -> Pattern a
193withVersion p v = And p $ Version $ Just v
196--------------------------------------------------------------------------------
197-- | Discard the phantom type parameter
198castPattern :: Pattern a -> Pattern b
199castPattern Everything = Everything
200castPattern (Complement x) = Complement (castPattern x)
201castPattern (And x y) = And (castPattern x) (castPattern y)
202castPattern (Glob g) = Glob g
203castPattern (List l) = List $ map castIdentifier l
204castPattern (Regex r) = Regex r
205castPattern (Version v) = Version v
208--------------------------------------------------------------------------------
209-- | Check if an identifier matches a pattern
210matches :: Pattern a -> Identifier a -> Bool
211matches Everything _ = True
212matches (Complement p) i = not $ matches p i
213matches (And x y) i = matches x i && matches y i
214matches (Glob p) i = isJust $ capture (Glob p) i
215matches (List l) i = i `elem` l
216matches (Regex r) i = toFilePath i =~ r
217matches (Version v) i = identifierVersion i == v
220--------------------------------------------------------------------------------
221-- | Given a list of identifiers, retain only those who match the given pattern
222filterMatches :: Pattern a -> [Identifier a] -> [Identifier a]
223filterMatches = filter . matches
226--------------------------------------------------------------------------------
227-- | Split a list at every possible point, generate a list of (init, tail)
228-- cases. The result is sorted with inits decreasing in length.
229splits :: [a] -> [([a], [a])]
230splits = inits &&& tails >>> uncurry zip >>> reverse
233--------------------------------------------------------------------------------
234-- | Match a glob against a pattern, generating a list of captures
235capture :: Pattern a -> Identifier a -> Maybe [String]
236capture (Glob p) i = capture' p (toFilePath i)
240--------------------------------------------------------------------------------
241-- | Internal verion of 'capture'
242capture' :: [GlobComponent] -> String -> Maybe [String]
243capture' [] [] = Just [] -- An empty match
244capture' [] _ = Nothing -- No match
245capture' (Literal l : ms) str
246 -- Match the literal against the string
247 | l `isPrefixOf` str = capture' ms $ drop (length l) str
248 | otherwise = Nothing
249capture' (Capture : ms) str =
250 -- Match until the next /
251 let (chunk, rest) = break (== '/') str
252 in msum $ [ fmap (i :) (capture' ms (t ++ rest)) | (i, t) <- splits chunk ]
253capture' (CaptureMany : ms) str =
255 msum $ [ fmap (i :) (capture' ms t) | (i, t) <- splits str ]
258--------------------------------------------------------------------------------
259-- | Create an identifier from a pattern by filling in the captures with a given
264-- > fromCapture (parseGlob "tags/*") "foo"
269fromCapture :: Pattern a -> String -> Identifier a
270fromCapture pattern = fromCaptures pattern . repeat
273--------------------------------------------------------------------------------
274-- | Create an identifier from a pattern by filling in the captures with the
275-- given list of strings
276fromCaptures :: Pattern a -> [String] -> Identifier a
277fromCaptures (Glob p) = fromFilePath . fromCaptures' p
278fromCaptures _ = error $
279 "Hakyll.Core.Identifier.Pattern.fromCaptures: fromCaptures only works " ++
283--------------------------------------------------------------------------------
284-- | Internally used version of 'fromCaptures'
285fromCaptures' :: [GlobComponent] -> [String] -> String
286fromCaptures' [] _ = mempty
287fromCaptures' (m : ms) [] = case m of
288 Literal l -> l `mappend` fromCaptures' ms []
289 _ -> error $ "Hakyll.Core.Identifier.Pattern.fromCaptures': "
290 ++ "identifier list exhausted"
291fromCaptures' (m : ms) ids@(i : is) = case m of
292 Literal l -> l `mappend` fromCaptures' ms ids
293 _ -> i `mappend` fromCaptures' ms is
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list