on master help sign in
Add `freshIdentifier` to Hakyll.Core.Rules
commitdabaa4532b767d5ab10204e6efa6f71c8eb96b7b authorJasper Van der Jeugt <m@jaspervdj.be> dateMon 16 Jan 2012, 14:35 (15 years ago) parent0182626
2 files changed+16−13
src/Hakyll/Core/Rules.hs+14−11 src/Hakyll/Core/Rules/Internal.hs+2−2
src/Hakyll/Core/Rules.hs+14−11
@@ -26,6 +26,7 @@ module Hakyll.Core.Rules
2626 , resources
2727 , metaCompile
2828 , metaCompileWith
29+ , freshIdentifier
2930 ) where
3031
3132import Control.Applicative ((<$>))
@@ -208,17 +209,9 @@ metaCompile :: (Binary a, Typeable a, Writable a)
208209 -- ^ Compiler generating the other compilers
209210 -> Rules
210211 -- ^ Resulting rules
211−metaCompile compiler = RulesM $ do
212− -- Create an identifier from the state
213− state <- get
214− let index = rulesMetaCompilerIndex state
215− id' = fromCapture "Hakyll.Core.Rules.metaCompile/*" (show index)
216−
217− -- Update the state with a new identifier
218− put $ state {rulesMetaCompilerIndex = index + 1}
219−
220− -- Fallback to 'metaCompileWith' with now known identifier
221− unRulesM $ metaCompileWith id' compiler
212+metaCompile compiler = do
213+ id' <- freshIdentifier "Hakyll.Core.Rules.metaCompile"
214+ metaCompileWith id' compiler
222215
223216-- | Version of 'metaCompile' that allows you to specify a custom identifier for
224217-- the metacompiler.
@@ -243,3 +236,13 @@ metaCompileWith identifier compiler = RulesM $ do
243236 compilers = [(id', compiler >>> arr makeRule )]
244237
245238 tell $ RuleSet mempty compilers mempty
239+
240+-- | Generate a fresh Identifier with a given prefix
241+freshIdentifier :: String -- ^ Prefix
242+ -> RulesM (Identifier a) -- ^ Fresh identifier
243+freshIdentifier prefix = RulesM $ do
244+ state <- get
245+ let index = rulesNextIdentifier state
246+ id' = parseIdentifier $ prefix ++ "/" ++ show index
247+ put $ state {rulesNextIdentifier = index + 1}
248+ return id'
src/Hakyll/Core/Rules/Internal.hs+2−2
@@ -56,7 +56,7 @@ instance Monoid RuleSet where
5656-- | Rule state
5757--
5858data RuleState = RuleState
59− { rulesMetaCompilerIndex :: Int
59+ { rulesNextIdentifier :: Int
6060 } deriving (Show)
6161
6262-- | Rule environment
@@ -84,7 +84,7 @@ runRules :: RulesM a -> ResourceProvider -> RuleSet
8484runRules rules provider = nubCompilers $
8585 evalState (execWriterT $ runReaderT (unRulesM rules) env) state
8686 where
87− state = RuleState {rulesMetaCompilerIndex = 0}
87+ state = RuleState {rulesNextIdentifier = 0}
8888 env = RuleEnvironment { rulesResourceProvider = provider
8989 , rulesPattern = mempty
9090 , rulesGroup = Nothing