on master help sign in
Add preprocess rule
commit50371ab5c198509c710a289430e1ad752eac786a authorJasper Van der Jeugt <m@jaspervdj.be> dateTue 8 Jan 2013, 12:47 (14 years ago) parent91da790
2 files changed+20−3
src/Hakyll/Core/Rules.hs+10 tests/Hakyll/Core/Rules/Tests.hs+10−3
src/Hakyll/Core/Rules.hs+10
@@ -25,6 +25,8 @@ module Hakyll.Core.Rules
2525 , route
2626
2727 -- * Advanced usage
28+ , preprocess
29+ , Dependency (..)
2830 , rulesExtraDependencies
2931 ) where
3032
@@ -33,6 +35,7 @@ module Hakyll.Core.Rules
3335import Control.Applicative ((<$>))
3436import Control.Monad.Reader (ask, local)
3537import Control.Monad.State (get, modify, put)
38+import Control.Monad.Trans (liftIO)
3639import Control.Monad.Writer (censor, tell)
3740import Data.Maybe (fromMaybe)
3841import Data.Monoid (mempty)
@@ -157,6 +160,13 @@ route :: Routes -> Rules ()
157160route route' = Rules $ modify $ \s -> s {rulesRoute = Just route'}
158161
159162
163+--------------------------------------------------------------------------------
164+-- | Execute an 'IO' action immediately while the rules are being evaluated.
165+-- This should be avoided if possible, but occasionally comes in useful.
166+preprocess :: IO a -> Rules a
167+preprocess = Rules . liftIO
168+
169+
160170--------------------------------------------------------------------------------
161171-- | Advanced usage: add extra dependencies to compilers. Basically this is
162172-- needed when you're doing unsafe tricky stuff in the rules monad, but you
tests/Hakyll/Core/Rules/Tests.hs+10−3
@@ -6,6 +6,8 @@ module Hakyll.Core.Rules.Tests
66
77
88--------------------------------------------------------------------------------
9+import Data.IORef (IORef, newIORef, readIORef,
10+ writeIORef)
911import qualified Data.Set as S
1012import Test.Framework (Test, testGroup)
1113import Test.Framework.Providers.HUnit (testCase)
@@ -34,9 +36,10 @@ tests = testGroup "Hakyll.Core.Rules.Tests"
3436--------------------------------------------------------------------------------
3537rulesTest :: Assertion
3638rulesTest = do
39+ ioref <- newIORef False
3740 store <- newTestStore
3841 provider <- newTestProvider store
39− ruleSet <- runRules rules provider
42+ ruleSet <- runRules (rules ioref) provider
4043 let identifiers = S.fromList $ map fst $ rulesCompilers ruleSet
4144 routes = rulesRoutes ruleSet
4245
@@ -47,6 +50,7 @@ rulesTest = do
4750 Just "example.md" @=? runRoutes routes (sv "nav" "example.md")
4851 Just "example.mv1" @=? runRoutes routes (sv "mv1" "example.md")
4952 Just "example.mv2" @=? runRoutes routes (sv "mv2" "example.md")
53+ readIORef ioref >>= assert
5054 where
5155 sv g = setVersion (Just g)
5256 expected =
@@ -59,13 +63,16 @@ rulesTest = do
5963
6064
6165--------------------------------------------------------------------------------
62−rules :: Rules ()
63−rules = do
66+rules :: IORef Bool -> Rules ()
67+rules ioref = do
6468 -- Compile some posts
6569 match "*.md" $ do
6670 route $ setExtension "html"
6771 compile pandocCompiler
6872
73+ -- Yeah. I don't know how else to test this stuff?
74+ preprocess $ writeIORef ioref True
75+
6976 -- Compile them, raw
7077 match "*.md" $ version "raw" $ do
7178 route idRoute