2011-02-11 12:30:55 +00:00
|
|
|
-- | Internal rules module for types which are not exposed to the user
|
|
|
|
--
|
|
|
|
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
|
|
|
|
module Hakyll.Core.Rules.Internal
|
|
|
|
( CompileRule (..)
|
|
|
|
, RuleSet (..)
|
|
|
|
, RuleState (..)
|
2011-04-04 18:49:22 +00:00
|
|
|
, 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
|
|
|
|
| MetaCompileRule [(Identifier, Compiler () CompileRule)]
|
|
|
|
|
|
|
|
-- | 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
|
|
|
|
rulesCompilers :: [(Identifier, Compiler () CompileRule)]
|
|
|
|
, -- | 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
|
|
|
|
{ rulesMetaCompilerIndex :: Int
|
|
|
|
} deriving (Show)
|
|
|
|
|
2011-04-04 18:49:22 +00:00
|
|
|
-- | Rule environment
|
|
|
|
--
|
|
|
|
data RuleEnvironment = RuleEnvironment
|
|
|
|
{ rulesResourceProvider :: ResourceProvider
|
2011-04-05 09:58:26 +00:00
|
|
|
, rulesPattern :: Pattern
|
2011-04-11 19:57:33 +00:00
|
|
|
, rulesGroup :: Maybe String
|
2011-04-04 18:49:22 +00:00
|
|
|
}
|
|
|
|
|
2011-02-11 12:30:55 +00:00
|
|
|
-- | The monad used to compose rules
|
|
|
|
--
|
|
|
|
newtype RulesM a = RulesM
|
2011-04-04 18:49:22 +00:00
|
|
|
{ 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'
|
|
|
|
--
|
|
|
|
runRules :: Rules -> ResourceProvider -> RuleSet
|
2011-04-13 15:42:57 +00:00
|
|
|
runRules rules provider = nubCompilers $
|
2011-04-04 18:49:22 +00:00
|
|
|
evalState (execWriterT $ runReaderT (unRulesM rules) env) state
|
2011-02-11 12:30:55 +00:00
|
|
|
where
|
|
|
|
state = RuleState {rulesMetaCompilerIndex = 0}
|
2011-04-04 18:49:22 +00:00
|
|
|
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-04 18:49:22 +00:00
|
|
|
}
|
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)
|