hakyll/src/Hakyll/Core/Rules/Internal.hs

100 lines
3.1 KiB
Haskell
Raw Normal View History

2011-02-11 12:30:55 +00:00
-- | Internal rules module for types which are not exposed to the user
--
2011-05-24 08:12:10 +00:00
{-# LANGUAGE GeneralizedNewtypeDeriving, Rank2Types #-}
2011-02-11 12:30:55 +00:00
module Hakyll.Core.Rules.Internal
( CompileRule (..)
, RuleSet (..)
, RuleState (..)
, RuleEnvironment (..)
2011-02-11 12:30:55 +00:00
, RulesM (..)
, Rules
, runRules
) where
import Control.Applicative (Applicative)
import Control.Monad.Writer (WriterT, execWriterT)
import Control.Monad.Reader (ReaderT, runReaderT)
import Control.Monad.State (State, evalState)
import Data.Monoid (Monoid, mempty, mappend)
2011-02-15 17:32:55 +00:00
import Data.Set (Set)
2011-04-13 15:42:57 +00:00
import qualified Data.Map as M
2011-02-11 12:30:55 +00:00
2011-04-05 20:14:49 +00:00
import Hakyll.Core.Resource
import Hakyll.Core.Resource.Provider
2011-02-11 12:30:55 +00:00
import Hakyll.Core.Identifier
2011-04-05 09:58:26 +00:00
import Hakyll.Core.Identifier.Pattern
2011-02-11 12:30:55 +00:00
import Hakyll.Core.Compiler.Internal
import Hakyll.Core.Routes
import Hakyll.Core.CompiledItem
-- | Output of a compiler rule
--
-- * The compiler will produce a simple item. This is the most common case.
--
-- * The compiler will produce more compilers. These new compilers need to be
-- added to the runtime if possible, since other items might depend upon them.
--
data CompileRule = CompileRule CompiledItem
2011-05-24 09:58:13 +00:00
| MetaCompileRule [(Identifier (), Compiler () CompileRule)]
2011-02-11 12:30:55 +00:00
-- | A collection of rules for the compilation process
--
data RuleSet = RuleSet
2011-02-15 17:32:55 +00:00
{ -- | Routes used in the compilation structure
rulesRoutes :: Routes
, -- | Compilation rules
2011-05-24 09:58:13 +00:00
rulesCompilers :: [(Identifier (), Compiler () CompileRule)]
2011-02-15 17:32:55 +00:00
, -- | A list of the used resources
rulesResources :: Set Resource
2011-02-11 12:30:55 +00:00
}
instance Monoid RuleSet where
2011-02-15 17:32:55 +00:00
mempty = RuleSet mempty mempty mempty
mappend (RuleSet r1 c1 s1) (RuleSet r2 c2 s2) =
RuleSet (mappend r1 r2) (mappend c1 c2) (mappend s1 s2)
2011-02-11 12:30:55 +00:00
-- | Rule state
--
data RuleState = RuleState
{ rulesNextIdentifier :: Int
2011-02-11 12:30:55 +00:00
} deriving (Show)
-- | Rule environment
--
data RuleEnvironment = RuleEnvironment
{ rulesResourceProvider :: ResourceProvider
2011-05-24 08:12:10 +00:00
, rulesPattern :: forall a. Pattern a
2011-04-11 19:57:33 +00:00
, rulesGroup :: Maybe String
}
2011-02-11 12:30:55 +00:00
-- | The monad used to compose rules
--
newtype RulesM a = RulesM
{ unRulesM :: ReaderT RuleEnvironment (WriterT RuleSet (State RuleState)) a
2011-02-11 12:30:55 +00:00
} deriving (Monad, Functor, Applicative)
-- | Simplification of the RulesM type; usually, it will not return any
-- result.
--
type Rules = RulesM ()
-- | Run a Rules monad, resulting in a 'RuleSet'
--
2011-05-29 10:30:33 +00:00
runRules :: RulesM a -> ResourceProvider -> RuleSet
2011-04-13 15:42:57 +00:00
runRules rules provider = nubCompilers $
evalState (execWriterT $ runReaderT (unRulesM rules) env) state
2011-02-11 12:30:55 +00:00
where
state = RuleState {rulesNextIdentifier = 0}
env = RuleEnvironment { rulesResourceProvider = provider
2011-04-05 09:58:26 +00:00
, rulesPattern = mempty
2011-04-11 19:57:33 +00:00
, rulesGroup = Nothing
}
2011-04-13 15:42:57 +00:00
-- | Remove duplicate compilers from the 'RuleSet'. When two compilers match an
-- item, we prefer the first one
--
nubCompilers :: RuleSet -> RuleSet
nubCompilers set = set { rulesCompilers = nubCompilers' (rulesCompilers set) }
where
nubCompilers' = M.toList . M.fromListWith (flip const)