1-- | This module provides a monadic DSL in which the user can specify the
2-- different rules used to run the compilers
4{-# LANGUAGE GeneralizedNewtypeDeriving #-}
5module Hakyll.Core.Rules
15import Control.Applicative (Applicative, (<$>))
16import Control.Monad.Writer
17import Control.Monad.Reader
19import Hakyll.Core.ResourceProvider
20import Hakyll.Core.Identifier
21import Hakyll.Core.Identifier.Pattern
22import Hakyll.Core.Compiler
23import Hakyll.Core.Route
25-- | A collection of rules for the compilation process
27data RuleSet a = RuleSet
29 , rulesCompilers :: [(Identifier, Compiler a)]
32instance Monoid (RuleSet a) where
33 mempty = RuleSet mempty mempty
34 mappend (RuleSet r1 c1) (RuleSet r2 c2) =
35 RuleSet (mappend r1 r2) (mappend c1 c2)
37-- | The monad used to compose rules
39newtype RulesM a b = RulesM
40 { unRulesM :: ReaderT ResourceProvider (Writer (RuleSet a)) b
41 } deriving (Monad, Functor, Applicative)
43-- | Simplification of the RulesM type; usually, it will not return any
46type Rules a = RulesM a ()
48-- | Run a Rules monad, resulting in a 'RuleSet'
50runRules :: Rules a -> ResourceProvider -> RuleSet a
51runRules rules provider = execWriter $ runReaderT (unRulesM rules) provider
55addRoute :: Route -> Rules a
56addRoute route' = RulesM $ tell $ RuleSet route' mempty
58-- | Add a number of compilers
60addCompilers :: [(Identifier, Compiler a)] -> Rules a
61addCompilers compilers = RulesM $ tell $ RuleSet mempty compilers
63-- | Add a compilation rule
65-- This instructs all resources matching the given pattern to be compiled using
68compile :: Pattern -> Compiler a -> Rules a
69compile pattern compiler = RulesM $ do
70 identifiers <- matches pattern . resourceList <$> ask
71 unRulesM $ addCompilers $ zip identifiers (repeat compiler)
73-- | Add a compilation rule
75-- This sets a compiler for the given identifier
77create :: Identifier -> Compiler a -> RulesM a ()
78create identifier compiler = addCompilers [(identifier, compiler)]
82route :: Pattern -> Route -> RulesM a ()
83route pattern route' = addRoute $ ifMatch pattern route'
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list