on master help sign in
Route → Routes
commitc093761e8941c1605b6131c411ca995588c10c2e authorJasper Van der Jeugt <jaspervdj@gmail.com> dateThu 3 Feb 2011, 16:07 (16 years ago) parent5705bb8
5 files changed+46−44
src/Hakyll/Core/Compiler.hs+8−4 src/Hakyll/Core/Compiler/Internal.hs+5−4 src/Hakyll/Core/Route.hs → src/Hakyll/Core/Routes.hs+18−18 src/Hakyll/Core/Rules.hs+4−4 src/Hakyll/Core/Run.hs+11−14
src/Hakyll/Core/Compiler.hs+8−4
@@ -37,6 +37,7 @@ import Hakyll.Core.ResourceProvider
3737import Hakyll.Core.Compiler.Internal
3838import Hakyll.Core.Store
3939import Hakyll.Core.Rules
40+import Hakyll.Core.Routes
4041
4142-- | Run a compiler, yielding the resulting target and it's dependencies. This
4243-- version of 'runCompilerJob' also stores the result
@@ -44,13 +45,13 @@ import Hakyll.Core.Rules
4445runCompiler :: Compiler () CompileRule -- ^ Compiler to run
4546 -> Identifier -- ^ Target identifier
4647 -> ResourceProvider -- ^ Resource provider
47− -> Maybe FilePath -- ^ Route
48+ -> Routes -- ^ Route
4849 -> Store -- ^ Store
4950 -> Bool -- ^ Was the resource modified?
5051 -> IO CompileRule -- ^ Resulting item
51−runCompiler compiler identifier provider route' store modified = do
52+runCompiler compiler identifier provider routes store modified = do
5253 -- Run the compiler job
53− result <- runCompilerJob compiler identifier provider route' store modified
54+ result <- runCompilerJob compiler identifier provider routes store modified
5455
5556 -- Inspect the result
5657 case result of
@@ -73,7 +74,10 @@ getIdentifier = fromJob $ const $ CompilerM $ compilerIdentifier <$> ask
7374-- | Get the route we are using for this item
7475--
7576getRoute :: Compiler a (Maybe FilePath)
76−getRoute = fromJob $ const $ CompilerM $ compilerRoute <$> ask
77+getRoute = fromJob $ const $ CompilerM $ do
78+ identifier <- compilerIdentifier <$> ask
79+ routes <- compilerRoutes <$> ask
80+ return $ runRoutes routes identifier
7781
7882-- | Get the resource we are compiling as a string
7983--
src/Hakyll/Core/Compiler/Internal.hs+5−4
@@ -25,6 +25,7 @@ import Control.Arrow (Arrow, arr, first)
2525import Hakyll.Core.Identifier
2626import Hakyll.Core.ResourceProvider
2727import Hakyll.Core.Store
28+import Hakyll.Core.Routes
2829
2930-- | A set of dependencies
3031--
@@ -37,8 +38,8 @@ data CompilerEnvironment = CompilerEnvironment
3738 compilerIdentifier :: Identifier
3839 , -- | Resource provider
3940 compilerResourceProvider :: ResourceProvider
40− , -- | Site route
41− compilerRoute :: Maybe FilePath
41+ , -- | Site routes
42+ compilerRoutes :: Routes
4243 , -- | Compiler store
4344 compilerStore :: Store
4445 , -- | Flag indicating if the underlying resource was modified
@@ -82,7 +83,7 @@ instance Arrow Compiler where
8283runCompilerJob :: Compiler () a -- ^ Compiler to run
8384 -> Identifier -- ^ Target identifier
8485 -> ResourceProvider -- ^ Resource provider
85− -> Maybe FilePath -- ^ Route
86+ -> Routes -- ^ Route
8687 -> Store -- ^ Store
8788 -> Bool -- ^ Was the resource modified?
8889 -> IO a
@@ -92,7 +93,7 @@ runCompilerJob compiler identifier provider route store modified =
9293 env = CompilerEnvironment
9394 { compilerIdentifier = identifier
9495 , compilerResourceProvider = provider
95− , compilerRoute = route
96+ , compilerRoutes = route
9697 , compilerStore = store
9798 , compilerResourceModified = modified
9899 }
src/Hakyll/Core/Route.hs → src/Hakyll/Core/Routes.hsrenamed+18−18
@@ -1,14 +1,14 @@
11-- | Once a target is compiled, the user usually wants to save it to the disk.
2−-- This is where the 'Route' type comes in; it determines where a certain target
3−-- should be written.
2+-- This is where the 'Routes' type comes in; it determines where a certain
3+-- target should be written.
44--
55-- When a route is applied (using 'runRoute'), it either returns a 'Just'
66-- 'FilePath' (meaning the target should be written to that file path), or
77-- 'Nothing' (meaning this target should not be written anywhere).
88--
9−module Hakyll.Core.Route
10− ( Route
11− , runRoute
9+module Hakyll.Core.Routes
10+ ( Routes
11+ , runRoutes
1212 , idRoute
1313 , setExtension
1414 , ifMatch
@@ -23,22 +23,22 @@ import Hakyll.Core.Identifier.Pattern
2323
2424-- | Type used for a route
2525--
26−newtype Route = Route {unRoute :: Identifier -> Maybe FilePath}
26+newtype Routes = Routes {unRoutes :: Identifier -> Maybe FilePath}
2727
28−instance Monoid Route where
29− mempty = Route $ const Nothing
30− mappend (Route f) (Route g) = Route $ \id' -> f id' `mplus` g id'
28+instance Monoid Routes where
29+ mempty = Routes $ const Nothing
30+ mappend (Routes f) (Routes g) = Routes $ \id' -> f id' `mplus` g id'
3131
3232-- | Apply a route to an identifier
3333--
34−runRoute :: Route -> Identifier -> Maybe FilePath
35−runRoute = unRoute
34+runRoutes :: Routes -> Identifier -> Maybe FilePath
35+runRoutes = unRoutes
3636
3737-- | A route that uses the identifier as filepath. For example, the target with
3838-- ID @foo\/bar@ will be written to the file @foo\/bar@.
3939--
40−idRoute :: Route
41−idRoute = Route $ Just . toFilePath
40+idRoute :: Routes
41+idRoute = Routes $ Just . toFilePath
4242
4343-- | Set (or replace) the extension of a route.
4444--
@@ -58,14 +58,14 @@ idRoute = Route $ Just . toFilePath
5858--
5959-- > Just "posts/the-art-of-trolling.html"
6060--
61−setExtension :: String -> Route
62−setExtension extension = Route $ fmap (`replaceExtension` extension)
63− . unRoute idRoute
61+setExtension :: String -> Routes
62+setExtension extension = Routes $ fmap (`replaceExtension` extension)
63+ . unRoutes idRoute
6464
6565-- | Modify a route: apply the route if the identifier matches the given
6666-- pattern, fail otherwise.
6767--
68−ifMatch :: Pattern -> Route -> Route
69−ifMatch pattern (Route route) = Route $ \id' ->
68+ifMatch :: Pattern -> Routes -> Routes
69+ifMatch pattern (Routes route) = Routes $ \id' ->
7070 if doesMatch pattern id' then route id'
7171 else Nothing
src/Hakyll/Core/Rules.hs+4−4
@@ -28,7 +28,7 @@ import Hakyll.Core.ResourceProvider
2828import Hakyll.Core.Identifier
2929import Hakyll.Core.Identifier.Pattern
3030import Hakyll.Core.Compiler.Internal
31−import Hakyll.Core.Route
31+import Hakyll.Core.Routes
3232import Hakyll.Core.CompiledItem
3333import Hakyll.Core.Writable
3434
@@ -45,7 +45,7 @@ data CompileRule = CompileRule CompiledItem
4545-- | A collection of rules for the compilation process
4646--
4747data RuleSet = RuleSet
48− { rulesRoute :: Route
48+ { rulesRoutes :: Routes
4949 , rulesCompilers :: [(Identifier, Compiler () CompileRule)]
5050 }
5151
@@ -81,7 +81,7 @@ runRules rules provider =
8181
8282-- | Add a route
8383--
84−tellRoute :: Route -> Rules
84+tellRoute :: Routes -> Rules
8585tellRoute route' = RulesM $ tell $ RuleSet route' mempty
8686
8787-- | Add a number of compilers
@@ -115,7 +115,7 @@ create identifier compiler = tellCompilers [(identifier, compiler)]
115115
116116-- | Add a route
117117--
118−route :: Pattern -> Route -> Rules
118+route :: Pattern -> Routes -> Rules
119119route pattern route' = tellRoute $ ifMatch pattern route'
120120
121121-- | Add a compiler that produces other compilers over time
src/Hakyll/Core/Run.hs+11−14
@@ -16,7 +16,7 @@ import System.FilePath ((</>))
1616import Data.Set (Set)
1717import qualified Data.Set as S
1818
19−import Hakyll.Core.Route
19+import Hakyll.Core.Routes
2020import Hakyll.Core.Identifier
2121import Hakyll.Core.Util.File
2222import Hakyll.Core.Compiler
@@ -48,7 +48,7 @@ run configuration rules = do
4848 where
4949 env ruleSet provider store = RuntimeEnvironment
5050 { hakyllConfiguration = configuration
51− , hakyllRoute = rulesRoute ruleSet
51+ , hakyllRoutes = rulesRoutes ruleSet
5252 , hakyllResourceProvider = provider
5353 , hakyllStore = store
5454 }
@@ -60,7 +60,7 @@ run configuration rules = do
6060
6161data RuntimeEnvironment = RuntimeEnvironment
6262 { hakyllConfiguration :: HakyllConfiguration
63− , hakyllRoute :: Route
63+ , hakyllRoutes :: Routes
6464 , hakyllResourceProvider :: ResourceProvider
6565 , hakyllStore :: Store
6666 }
@@ -156,31 +156,28 @@ runCompilers :: [(Identifier, Compiler () CompileRule)]
156156runCompilers [] = return ()
157157runCompilers ((id', compiler) : compilers) = Runtime $ do
158158 -- Obtain information
159− route' <- hakyllRoute <$> ask
159+ routes <- hakyllRoutes <$> ask
160160 provider <- hakyllResourceProvider <$> ask
161161 store <- hakyllStore <$> ask
162162 modified' <- hakyllModified <$> get
163163
164− let -- Determine the URL
165− url = runRoute route' id'
166−
167− -- Check if the resource was modified
164+ let -- Check if the resource was modified
168165 isModified = id' `S.member` modified'
169166
170167 -- Run the compiler
171− result <- liftIO $ runCompiler compiler id' provider url store isModified
168+ result <- liftIO $ runCompiler compiler id' provider routes store isModified
172169 liftIO $ putStrLn $ "Generated target: " ++ show id'
173170
174171 case result of
175172 -- Compile rule for one item, easy stuff
176173 CompileRule compiled -> do
177− case url of
178− Nothing -> return ()
179− Just r -> do
180− liftIO $ putStrLn $ "Routing " ++ show id' ++ " to " ++ r
174+ case runRoutes routes id' of
175+ Nothing -> return ()
176+ Just url -> do
177+ liftIO $ putStrLn $ "Routing " ++ show id' ++ " to " ++ url
181178 destination <-
182179 destinationDirectory . hakyllConfiguration <$> ask
183− let path = destination </> r
180+ let path = destination </> url
184181 liftIO $ makeDirectories path
185182 liftIO $ write path compiled
186183