on 9939708 help sign in
hakyll/src/Hakyll/Core/Route.hs
71 lines 2.0 kB history raw
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--
9module Hakyll.Core.Route
10 ( Route
11 , runRoute
12 , idRoute
13 , setExtension
14 , ifMatch
15 ) where
17import Data.Monoid (Monoid, mempty, mappend)
18import Control.Monad (mplus)
19import System.FilePath (replaceExtension)
21import Hakyll.Core.Identifier
22import Hakyll.Core.Identifier.Pattern
24-- | Type used for a route
25--
26newtype Route = Route {unRoute :: Identifier -> Maybe FilePath}
28instance 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
33--
34runRoute :: Route -> Identifier -> Maybe FilePath
35runRoute = unRoute
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--
40idRoute :: Route
41idRoute = Route $ Just . toFilePath
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--
61setExtension :: String -> Route
62setExtension exension = Route $ fmap (flip replaceExtension exension)
63 . unRoute idRoute
65-- | Modify a route: apply the route if the identifier matches the given
66-- pattern, fail otherwise.
67--
68ifMatch :: Pattern -> Route -> Route
69ifMatch pattern (Route route) = Route $ \id' ->
70 if doesMatch pattern id' then route id'
71 else Nothing