@@ -34,11 +34,14 @@ module Hakyll.Core.Identifier.Pattern
35 , doesMatch
35 , doesMatch
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)
43import GHC.Exts (IsString, fromString)
46import GHC.Exts (IsString, fromString)
@@ -78,8 +81,8 @@ parsePattern = Pattern . map toPattern . unIdentifier . parseIdentifier
79-- | Match an identifier against a pattern, generating a list of captures
82-- | Match an identifier against a pattern, generating a list of captures
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
84-- | Check if an identifier matches a pattern
87-- | Check if an identifier matches a pattern
@@ -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)
122+-- | Create an identifier from a pattern by filling in the captures with a given
125+fromCapture :: Pattern -> Identifier -> Identifier
126+fromCapture pattern = fromCaptures pattern . repeat
128+-- | Create an identifier from a pattern by filling in the captures with the
129+-- given list of strings
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