2010-12-23 18:20:05 +00:00
|
|
|
-- | Once a target is compiled, the user usually wants to save it to the disk.
|
2011-02-03 15:07:49 +00:00
|
|
|
-- This is where the 'Routes' type comes in; it determines where a certain
|
|
|
|
-- target should be written.
|
2010-12-23 18:20:05 +00:00
|
|
|
--
|
|
|
|
-- When a route is applied (using 'runRoute'), it either returns a 'Just'
|
|
|
|
-- 'FilePath' (meaning the target should be written to that file path), or
|
|
|
|
-- 'Nothing' (meaning this target should not be written anywhere).
|
|
|
|
--
|
2011-02-03 15:07:49 +00:00
|
|
|
module Hakyll.Core.Routes
|
|
|
|
( Routes
|
|
|
|
, runRoutes
|
2010-12-23 18:20:05 +00:00
|
|
|
, idRoute
|
|
|
|
, setExtension
|
|
|
|
, ifMatch
|
2011-02-11 07:20:35 +00:00
|
|
|
, customRoute
|
2010-12-23 18:20:05 +00:00
|
|
|
) where
|
|
|
|
|
|
|
|
import Data.Monoid (Monoid, mempty, mappend)
|
|
|
|
import Control.Monad (mplus)
|
|
|
|
import System.FilePath (replaceExtension)
|
|
|
|
|
|
|
|
import Hakyll.Core.Identifier
|
|
|
|
import Hakyll.Core.Identifier.Pattern
|
|
|
|
|
|
|
|
-- | Type used for a route
|
|
|
|
--
|
2011-02-03 15:07:49 +00:00
|
|
|
newtype Routes = Routes {unRoutes :: Identifier -> Maybe FilePath}
|
2010-12-23 18:20:05 +00:00
|
|
|
|
2011-02-03 15:07:49 +00:00
|
|
|
instance Monoid Routes where
|
|
|
|
mempty = Routes $ const Nothing
|
|
|
|
mappend (Routes f) (Routes g) = Routes $ \id' -> f id' `mplus` g id'
|
2010-12-23 18:20:05 +00:00
|
|
|
|
|
|
|
-- | Apply a route to an identifier
|
|
|
|
--
|
2011-02-03 15:07:49 +00:00
|
|
|
runRoutes :: Routes -> Identifier -> Maybe FilePath
|
|
|
|
runRoutes = unRoutes
|
2010-12-23 18:20:05 +00:00
|
|
|
|
|
|
|
-- | A route that uses the identifier as filepath. For example, the target with
|
|
|
|
-- ID @foo\/bar@ will be written to the file @foo\/bar@.
|
|
|
|
--
|
2011-02-03 15:07:49 +00:00
|
|
|
idRoute :: Routes
|
|
|
|
idRoute = Routes $ Just . toFilePath
|
2010-12-23 18:20:05 +00:00
|
|
|
|
|
|
|
-- | Set (or replace) the extension of a route.
|
|
|
|
--
|
|
|
|
-- Example:
|
|
|
|
--
|
|
|
|
-- > runRoute (setExtension "html") "foo/bar"
|
|
|
|
--
|
|
|
|
-- Result:
|
|
|
|
--
|
|
|
|
-- > Just "foo/bar.html"
|
|
|
|
--
|
|
|
|
-- Example:
|
|
|
|
--
|
|
|
|
-- > runRoute (setExtension "html") "posts/the-art-of-trolling.markdown"
|
|
|
|
--
|
|
|
|
-- Result:
|
|
|
|
--
|
|
|
|
-- > Just "posts/the-art-of-trolling.html"
|
|
|
|
--
|
2011-02-03 15:07:49 +00:00
|
|
|
setExtension :: String -> Routes
|
|
|
|
setExtension extension = Routes $ fmap (`replaceExtension` extension)
|
|
|
|
. unRoutes idRoute
|
2010-12-23 18:20:05 +00:00
|
|
|
|
|
|
|
-- | Modify a route: apply the route if the identifier matches the given
|
|
|
|
-- pattern, fail otherwise.
|
|
|
|
--
|
2011-02-03 15:07:49 +00:00
|
|
|
ifMatch :: Pattern -> Routes -> Routes
|
|
|
|
ifMatch pattern (Routes route) = Routes $ \id' ->
|
2010-12-23 18:20:05 +00:00
|
|
|
if doesMatch pattern id' then route id'
|
|
|
|
else Nothing
|
2011-02-11 07:20:35 +00:00
|
|
|
|
|
|
|
-- | Create a custom route. This should almost always be used with 'ifMatch'.
|
|
|
|
--
|
|
|
|
customRoute :: (Identifier -> FilePath) -> Routes
|
|
|
|
customRoute f = Routes $ Just . f
|