on master help sign in
Add fromCapture(s)
672ecb0Jasper Van der Jeugt
src/Hakyll/Core/Identifier.hs+3−1
@@ -10,6 +10,7 @@
10--
10--
11-- * @error/404@
11-- * @error/404@
12--
12--
13+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
13module Hakyll.Core.Identifier
14module Hakyll.Core.Identifier
14 ( Identifier (..)
15 ( Identifier (..)
15 , parseIdentifier
16 , parseIdentifier
@@ -17,6 +18,7 @@ module Hakyll.Core.Identifier
17 ) where
18 ) where
18
19
19import Control.Arrow (second)
20import Control.Arrow (second)
21+import Data.Monoid (Monoid)
20
22
21import GHC.Exts (IsString, fromString)
23import GHC.Exts (IsString, fromString)
22import System.FilePath (joinPath)
24import System.FilePath (joinPath)
@@ -24,7 +26,7 @@ import System.FilePath (joinPath)
24-- | An identifier used to uniquely identify a value
26-- | An identifier used to uniquely identify a value
25--
27--
26newtype Identifier = Identifier {unIdentifier :: [String]}
28newtype Identifier = Identifier {unIdentifier :: [String]}
27− deriving (Eq, Ord)
29+ deriving (Eq, Ord, Monoid)
28
30
29instance Show Identifier where
31instance Show Identifier where
30 show = toFilePath
32 show = toFilePath
src/Hakyll/Core/Identifier/Pattern.hs+24−2
@@ -34,11 +34,14 @@ module Hakyll.Core.Identifier.Pattern
34 , match
34 , match
35 , doesMatch
35 , doesMatch
36 , matches
36 , matches
37+ , fromCapture
38+ , fromCaptures
37 ) where
39 ) where
38
40
39import Data.List (intercalate)
41import Data.List (intercalate)
40import Control.Monad (msum)
42import Control.Monad (msum)
41import Data.Maybe (isJust)
43import Data.Maybe (isJust)
44+import Data.Monoid (mempty, mappend)
42
45
43import GHC.Exts (IsString, fromString)
46import GHC.Exts (IsString, fromString)
44
47
@@ -78,8 +81,8 @@ parsePattern = Pattern . map toPattern . unIdentifier . parseIdentifier
78
81
79-- | Match an identifier against a pattern, generating a list of captures
82-- | Match an identifier against a pattern, generating a list of captures
80--
83--
81−match :: Pattern -> Identifier -> Maybe [[String]]
84+match :: Pattern -> Identifier -> Maybe [Identifier]
82−match (Pattern p) (Identifier i) = match' p i
85+match (Pattern p) (Identifier i) = fmap (map Identifier) $ match' p i
83
86
84-- | Check if an identifier matches a pattern
87-- | Check if an identifier matches a pattern
85--
88--
@@ -115,3 +118,22 @@ match' (m : ms) (s : ss) = case m of
115 CaptureMany ->
118 CaptureMany ->
116 let take' (i, t) = fmap (i :) $ match' ms t
119 let take' (i, t) = fmap (i :) $ match' ms t
117 in msum $ map take' $ splits (s : ss)
120 in msum $ map take' $ splits (s : ss)
121+
122+-- | Create an identifier from a pattern by filling in the captures with a given
123+-- string
124+--
125+fromCapture :: Pattern -> Identifier -> Identifier
126+fromCapture pattern = fromCaptures pattern . repeat
127+
128+-- | Create an identifier from a pattern by filling in the captures with the
129+-- given list of strings
130+--
131+fromCaptures :: Pattern -> [Identifier] -> Identifier
132+fromCaptures (Pattern []) _ = mempty
133+fromCaptures (Pattern (m : ms)) [] = case m of
134+ Literal l -> Identifier [l] `mappend` fromCaptures (Pattern ms) []
135+ _ -> error $ "Hakyll.Core.Identifier.Pattern.fromCaptures: "
136+ ++ "identifier list exhausted"
137+fromCaptures (Pattern (m : ms)) ids@(i : is) = case m of
138+ Literal l -> Identifier [l] `mappend` fromCaptures (Pattern ms) ids
139+ _ -> i `mappend` fromCaptures (Pattern ms) is