on master help sign in
Add fromCapture(s)
commit672ecb077c7edd6a542958a2c9ede5c8ea14bbc4 authorJasper Van der Jeugt <jaspervdj@gmail.com> dateFri 7 Jan 2011, 15:09 (16 years ago) parent7bf3450
2 files changed+27−3
src/Hakyll/Core/Identifier.hs+3−1 src/Hakyll/Core/Identifier/Pattern.hs+24−2
src/Hakyll/Core/Identifier.hs+3−1
@@ -10,6 +10,7 @@
1010--
1111-- * @error/404@
1212--
13+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
1314module Hakyll.Core.Identifier
1415 ( Identifier (..)
1516 , parseIdentifier
@@ -17,6 +18,7 @@ module Hakyll.Core.Identifier
1718 ) where
1819
1920import Control.Arrow (second)
21+import Data.Monoid (Monoid)
2022
2123import GHC.Exts (IsString, fromString)
2224import System.FilePath (joinPath)
@@ -24,7 +26,7 @@ import System.FilePath (joinPath)
2426-- | An identifier used to uniquely identify a value
2527--
2628newtype Identifier = Identifier {unIdentifier :: [String]}
27− deriving (Eq, Ord)
29+ deriving (Eq, Ord, Monoid)
2830
2931instance Show Identifier where
3032 show = toFilePath
src/Hakyll/Core/Identifier/Pattern.hs+24−2
@@ -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