on master help sign in
Add identifierGroup, clean up Pattern API
commit15045d313c9d23014fcf284625a90d67d3a10d4d authorJasper Van der Jeugt <jaspervdj@gmail.com> dateMon 11 Apr 2011, 18:07 (16 years ago) parent9d42e54
2 files changed+30−29
src/Hakyll/Core/Identifier.hs+20−6 src/Hakyll/Core/Identifier/Pattern.hs+10−23
src/Hakyll/Core/Identifier.hs+20−6
@@ -28,17 +28,30 @@ module Hakyll.Core.Identifier
2828 ) where
2929
3030import Control.Arrow (second)
31−import Data.Monoid (Monoid)
31+import Control.Applicative ((<$>), (<*>))
32+import Control.Monad (mplus)
33+import Data.Monoid (Monoid, mempty, mappend)
3234import Data.List (intercalate)
3335
34−import Data.Binary (Binary)
36+import Data.Binary (Binary, get, put)
3537import GHC.Exts (IsString, fromString)
3638import Data.Typeable (Typeable)
3739
3840-- | An identifier used to uniquely identify a value
3941--
40−newtype Identifier = Identifier {unIdentifier :: String}
41− deriving (Eq, Ord, Monoid, Binary, Typeable)
42+data Identifier = Identifier
43+ { identifierGroup :: Maybe String
44+ , identifierPath :: String
45+ } deriving (Eq, Ord, Typeable)
46+
47+instance Monoid Identifier where
48+ mempty = Identifier Nothing ""
49+ Identifier g1 p1 `mappend` Identifier g2 p2 =
50+ Identifier (g1 `mplus` g2) (p1 `mappend` p2)
51+
52+instance Binary Identifier where
53+ put (Identifier g p) = put g >> put p
54+ get = Identifier <$> get <*> get
4255
4356instance Show Identifier where
4457 show = toFilePath
@@ -49,7 +62,8 @@ instance IsString Identifier where
4962-- | Parse an identifier from a string
5063--
5164parseIdentifier :: String -> Identifier
52−parseIdentifier = Identifier . intercalate "/" . filter (not . null) . split'
65+parseIdentifier = Identifier Nothing
66+ . intercalate "/" . filter (not . null) . split'
5367 where
5468 split' [] = [[]]
5569 split' str = let (pre, post) = second (drop 1) $ break (== '/') str
@@ -58,4 +72,4 @@ parseIdentifier = Identifier . intercalate "/" . filter (not . null) . split'
5872-- | Convert an identifier to a relative 'FilePath'
5973--
6074toFilePath :: Identifier -> FilePath
61−toFilePath = unIdentifier
75+toFilePath = identifierPath
src/Hakyll/Core/Identifier/Pattern.hs+10−23
@@ -40,7 +40,6 @@ module Hakyll.Core.Identifier.Pattern
4040 , filterMatches
4141 , capture
4242 , fromCapture
43− , fromCaptureString
4443 , fromCaptures
4544 ) where
4645
@@ -126,9 +125,9 @@ splits = inits &&& tails >>> uncurry zip >>> reverse
126125
127126-- | Match a glob against a pattern, generating a list of captures
128127--
129−capture :: Pattern -> Identifier -> Maybe [Identifier]
130−capture (Glob p) (Identifier i) = fmap (map Identifier) $ capture' p i
131−capture (Predicate _) _ = Nothing
128+capture :: Pattern -> Identifier -> Maybe [String]
129+capture (Glob p) (Identifier _ i) = capture' p i
130+capture (Predicate _) _ = Nothing
132131
133132-- | Internal verion of 'capture'
134133--
@@ -152,44 +151,32 @@ capture' (CaptureMany : ms) str =
152151--
153152-- Example:
154153--
155−-- > fromCapture (parseGlob "tags/*") (parseIdentifier "foo")
154+-- > fromCapture (parseGlob "tags/*") "foo"
156155--
157156-- Result:
158157--
159158-- > "tags/foo"
160159--
161−fromCapture :: Pattern -> Identifier -> Identifier
160+fromCapture :: Pattern -> String -> Identifier
162161fromCapture pattern = fromCaptures pattern . repeat
163162
164−-- | Simplified version of 'fromCapture' which takes a 'String' instead of an
165−-- 'Identifier'
166−--
167−-- > fromCaptureString (parseGlob "tags/*") "foo"
168−--
169−-- Result:
170−--
171−-- > "tags/foo"
172−--
173−fromCaptureString :: Pattern -> String -> Identifier
174−fromCaptureString pattern = fromCapture pattern . parseIdentifier
175−
176163-- | Create an identifier from a pattern by filling in the captures with the
177164-- given list of strings
178165--
179−fromCaptures :: Pattern -> [Identifier] -> Identifier
180−fromCaptures (Glob p) = fromCaptures' p
166+fromCaptures :: Pattern -> [String] -> Identifier
167+fromCaptures (Glob p) = Identifier Nothing . fromCaptures' p
181168fromCaptures (Predicate _) = error $
182169 "Hakyll.Core.Identifier.Pattern.fromCaptures: fromCaptures called on a " ++
183170 "predicate instead of a glob"
184171
185172-- | Internally used version of 'fromCaptures'
186173--
187−fromCaptures' :: [GlobComponent] -> [Identifier] -> Identifier
174+fromCaptures' :: [GlobComponent] -> [String] -> String
188175fromCaptures' [] _ = mempty
189176fromCaptures' (m : ms) [] = case m of
190− Literal l -> Identifier l `mappend` fromCaptures' ms []
177+ Literal l -> l `mappend` fromCaptures' ms []
191178 _ -> error $ "Hakyll.Core.Identifier.Pattern.fromCaptures': "
192179 ++ "identifier list exhausted"
193180fromCaptures' (m : ms) ids@(i : is) = case m of
194− Literal l -> Identifier l `mappend` fromCaptures' ms ids
181+ Literal l -> l `mappend` fromCaptures' ms ids
195182 _ -> i `mappend` fromCaptures' ms is