summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJasper Van der Jeugt <jaspervdj@gmail.com>2010-12-23 19:20:05 +0100
committerJasper Van der Jeugt <jaspervdj@gmail.com>2010-12-23 19:20:05 +0100
commit4bdd93b331eec642873262a524b6b3a6132eb1c9 (patch)
tree56958e64e49b039afc1962d638715210ed082d3c /src
parent07ca8954a5afef265253dd7fa2b77561a8b7470a (diff)
downloadhakyll-4bdd93b331eec642873262a524b6b3a6132eb1c9.tar.gz
Add Route code
Diffstat (limited to 'src')
-rw-r--r--src/Hakyll/Core/Route.hs71
1 files changed, 71 insertions, 0 deletions
diff --git a/src/Hakyll/Core/Route.hs b/src/Hakyll/Core/Route.hs
new file mode 100644
index 0000000..195768c
--- /dev/null
+++ b/src/Hakyll/Core/Route.hs
@@ -0,0 +1,71 @@
+-- | Once a target is compiled, the user usually wants to save it to the disk.
+-- This is where the 'Route' type comes in; it determines where a certain target
+-- should be written.
+--
+-- 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).
+--
+module Hakyll.Core.Route
+ ( Route
+ , runRoute
+ , 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
+--
+newtype Route = Route {unRoute :: Identifier -> Maybe FilePath}
+
+instance Monoid Route where
+ mempty = Route $ const Nothing
+ mappend (Route f) (Route g) = Route $ \id' -> f id' `mplus` g id'
+
+-- | Apply a route to an identifier
+--
+runRoute :: Route -> Identifier -> Maybe FilePath
+runRoute = unRoute
+
+-- | A route that uses the identifier as filepath. For example, the target with
+-- ID @foo\/bar@ will be written to the file @foo\/bar@.
+--
+idRoute :: Route
+idRoute = Route $ Just . toFilePath
+
+-- | 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"
+--
+setExtension :: String -> Route
+setExtension exension = Route $ fmap (flip replaceExtension exension)
+ . unRoute idRoute
+
+-- | Modify a route: apply the route if the identifier matches the given
+-- pattern, fail otherwise.
+--
+ifMatch :: Pattern -> Route -> Route
+ifMatch pattern (Route route) = Route $ \id' ->
+ if doesMatch pattern id' then route id'
+ else Nothing