hakyll/src/Hakyll/Core/Routes.hs

72 lines
1.9 KiB
Haskell
Raw Normal View History

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
) 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