@@ -0,0 +1,71 @@
1+-- | 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
5+-- When a route is applied (using 'runRoute'), it either returns a 'Just'
6+-- 'FilePath' (meaning the target should be written to that file path), or
7+-- 'Nothing' (meaning this target should not be written anywhere).
9+module Hakyll.Core.Route
17+import Data.Monoid (Monoid, mempty, mappend)
18+import Control.Monad (mplus)
19+import System.FilePath (replaceExtension)
21+import Hakyll.Core.Identifier
22+import Hakyll.Core.Identifier.Pattern
24+-- | Type used for a route
26+newtype Route = Route {unRoute :: Identifier -> Maybe FilePath}
28+instance Monoid Route where
29+ mempty = Route $ const Nothing
30+ mappend (Route f) (Route g) = Route $ \id' -> f id' `mplus` g id'
32+-- | Apply a route to an identifier
34+runRoute :: Route -> Identifier -> Maybe FilePath
37+-- | A route that uses the identifier as filepath. For example, the target with
38+-- ID @foo\/bar@ will be written to the file @foo\/bar@.
41+idRoute = Route $ Just . toFilePath
43+-- | Set (or replace) the extension of a route.
47+-- > runRoute (setExtension "html") "foo/bar"
51+-- > Just "foo/bar.html"
55+-- > runRoute (setExtension "html") "posts/the-art-of-trolling.markdown"
59+-- > Just "posts/the-art-of-trolling.html"
61+setExtension :: String -> Route
62+setExtension exension = Route $ fmap (flip replaceExtension exension)
65+-- | Modify a route: apply the route if the identifier matches the given
66+-- pattern, fail otherwise.
68+ifMatch :: Pattern -> Route -> Route
69+ifMatch pattern (Route route) = Route $ \id' ->
70+ if doesMatch pattern id' then route id'