@@ -35,36 +35,42 @@ module Hakyll.Core.Routes
3535 , customRoute
3636 , constRoute
3737 , gsubRoute
38+ , metadataRoute
3839 , composeRoutes
3940 ) where
4041
4142
4243--------------------------------------------------------------------------------
43−import Data.Monoid (Monoid, mempty, mappend)
44−import Control.Monad (mplus)
45−import System.FilePath (replaceExtension)
44+import Data.Monoid (Monoid, mappend, mempty)
45+import System.FilePath (replaceExtension)
4646
4747
4848--------------------------------------------------------------------------------
49−import Hakyll.Core.Identifier
50−import Hakyll.Core.Identifier.Pattern
51−import Hakyll.Core.Util.String
49+import Hakyll.Core.Identifier
50+import Hakyll.Core.Identifier.Pattern
51+import Hakyll.Core.Metadata
52+import Hakyll.Core.Provider
53+import Hakyll.Core.Util.String
5254
5355
5456--------------------------------------------------------------------------------
5557-- | Type used for a route
56−newtype Routes = Routes {unRoutes :: Identifier -> Maybe FilePath}
58+newtype Routes = Routes
59+ { unRoutes :: Provider -> Identifier -> IO (Maybe FilePath)
60+ }
5761
5862
5963--------------------------------------------------------------------------------
6064instance Monoid Routes where
61− mempty = Routes $ const Nothing
62− mappend (Routes f) (Routes g) = Routes $ \id' -> f id' `mplus` g id'
65+ mempty = Routes $ \_ _ -> return Nothing
66+ mappend (Routes f) (Routes g) = Routes $ \p id' -> do
67+ mfp <- f p id'
68+ maybe (g p id') (return . Just) mfp
6369
6470
6571--------------------------------------------------------------------------------
6672-- | Apply a route to an identifier
67−runRoutes :: Routes -> Identifier -> Maybe FilePath
73+runRoutes :: Routes -> Provider -> Identifier -> IO (Maybe FilePath)
6874runRoutes = unRoutes
6975
7076
@@ -72,7 +78,7 @@ runRoutes = unRoutes
7278-- | A route that uses the identifier as filepath. For example, the target with
7379-- ID @foo\/bar@ will be written to the file @foo\/bar@.
7480idRoute :: Routes
75−idRoute = Routes $ Just . toFilePath
81+idRoute = customRoute toFilePath
7682
7783
7884--------------------------------------------------------------------------------
@@ -94,23 +100,23 @@ idRoute = Routes $ Just . toFilePath
94100--
95101-- > Just "posts/the-art-of-trolling.html"
96102setExtension :: String -> Routes
97−setExtension extension = Routes $
98− fmap (`replaceExtension` extension) . unRoutes idRoute
103+setExtension extension = customRoute $
104+ (`replaceExtension` extension) . toFilePath
99105
100106
101107--------------------------------------------------------------------------------
102108-- | Apply the route if the identifier matches the given pattern, fail
103109-- otherwise
104110matchRoute :: Pattern -> Routes -> Routes
105−matchRoute pattern (Routes route) = Routes $ \id' ->
106− if matches pattern id' then route id' else Nothing
111+matchRoute pattern (Routes route) = Routes $ \p id' ->
112+ if matches pattern id' then route p id' else return Nothing
107113
108114
109115--------------------------------------------------------------------------------
110116-- | Create a custom route. This should almost always be used with
111117-- 'matchRoute'
112118customRoute :: (Identifier -> FilePath) -> Routes
113−customRoute f = Routes $ Just . f
119+customRoute f = Routes $ const $ return . Just . f
114120
115121
116122--------------------------------------------------------------------------------
@@ -137,6 +143,14 @@ gsubRoute pattern replacement = customRoute $
137143 replaceAll pattern replacement . toFilePath
138144
139145
146+--------------------------------------------------------------------------------
147+-- | Get access to the metadata in order to determine the route
148+metadataRoute :: (Metadata -> Routes) -> Routes
149+metadataRoute f = Routes $ \p i -> do
150+ metadata <- resourceMetadata p i
151+ unRoutes (f metadata) p i
152+
153+
140154--------------------------------------------------------------------------------
141155-- | Compose routes so that @f \`composeRoutes\` g@ is more or less equivalent
142156-- with @g . f@.
@@ -154,6 +168,6 @@ gsubRoute pattern replacement = customRoute $
154168composeRoutes :: Routes -- ^ First route to apply
155169 -> Routes -- ^ Second route to apply
156170 -> Routes -- ^ Resulting route
157−composeRoutes (Routes f) (Routes g) = Routes $ \i -> do
158− p <- f i
159− g $ fromFilePath p
171+composeRoutes (Routes f) (Routes g) = Routes $ \p i -> do
172+ mfp <- f p i
173+ maybe (return Nothing) (g p . fromFilePath) mfp