@@ -34,11 +34,14 @@ module Hakyll.Core.Identifier.Pattern
3434 , match
3535 , doesMatch
3636 , matches
37+ , fromCapture
38+ , fromCaptures
3739 ) where
3840
3941import Data.List (intercalate)
4042import Control.Monad (msum)
4143import Data.Maybe (isJust)
44+import Data.Monoid (mempty, mappend)
4245
4346import GHC.Exts (IsString, fromString)
4447
@@ -78,8 +81,8 @@ parsePattern = Pattern . map toPattern . unIdentifier . parseIdentifier
7881
7982-- | Match an identifier against a pattern, generating a list of captures
8083--
81−match :: Pattern -> Identifier -> Maybe [[String]]
82−match (Pattern p) (Identifier i) = match' p i
84+match :: Pattern -> Identifier -> Maybe [Identifier]
85+match (Pattern p) (Identifier i) = fmap (map Identifier) $ match' p i
8386
8487-- | Check if an identifier matches a pattern
8588--
@@ -115,3 +118,22 @@ match' (m : ms) (s : ss) = case m of
115118 CaptureMany ->
116119 let take' (i, t) = fmap (i :) $ match' ms t
117120 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