@@ -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
3+-- should be written.
4+--
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).
8+--
9+module Hakyll.Core.Route
10+ ( Route
11+ , runRoute
12+ , idRoute
13+ , setExtension
14+ , ifMatch
15+ ) where
16+
17+import Data.Monoid (Monoid, mempty, mappend)
18+import Control.Monad (mplus)
19+import System.FilePath (replaceExtension)
20+
21+import Hakyll.Core.Identifier
22+import Hakyll.Core.Identifier.Pattern
23+
24+-- | Type used for a route
25+--
26+newtype Route = Route {unRoute :: Identifier -> Maybe FilePath}
27+
28+instance Monoid Route where
29+ mempty = Route $ const Nothing
30+ mappend (Route f) (Route g) = Route $ \id' -> f id' `mplus` g id'
31+
32+-- | Apply a route to an identifier
33+--
34+runRoute :: Route -> Identifier -> Maybe FilePath
35+runRoute = unRoute
36+
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@.
39+--
40+idRoute :: Route
41+idRoute = Route $ Just . toFilePath
42+
43+-- | Set (or replace) the extension of a route.
44+--
45+-- Example:
46+--
47+-- > runRoute (setExtension "html") "foo/bar"
48+--
49+-- Result:
50+--
51+-- > Just "foo/bar.html"
52+--
53+-- Example:
54+--
55+-- > runRoute (setExtension "html") "posts/the-art-of-trolling.markdown"
56+--
57+-- Result:
58+--
59+-- > Just "posts/the-art-of-trolling.html"
60+--
61+setExtension :: String -> Route
62+setExtension exension = Route $ fmap (flip replaceExtension exension)
63+ . unRoute idRoute
64+
65+-- | Modify a route: apply the route if the identifier matches the given
66+-- pattern, fail otherwise.
67+--
68+ifMatch :: Pattern -> Route -> Route
69+ifMatch pattern (Route route) = Route $ \id' ->
70+ if doesMatch pattern id' then route id'
71+ else Nothing