on master help sign in
Add `composeRoutes`
commitfa057f30117e02f13f4a788eb3f52660ab8ab440 authorJasper Van der Jeugt <jaspervdj@gmail.com> dateTue 1 Mar 2011, 09:40 (16 years ago) parentd460fd8
2 files changed+25
src/Hakyll/Core/Routes.hs+22 tests/Hakyll/Core/Routes/Tests.hs+3
src/Hakyll/Core/Routes.hs+22
@@ -33,6 +33,7 @@ module Hakyll.Core.Routes
3333 , ifMatch
3434 , customRoute
3535 , gsubRoute
36+ , composeRoutes
3637 ) where
3738
3839import Data.Monoid (Monoid, mempty, mappend)
@@ -112,3 +113,24 @@ gsubRoute :: String -- ^ Pattern
112113 -> Routes -- ^ Resulting route
113114gsubRoute pattern replacement = customRoute $
114115 replaceAll pattern replacement . toFilePath
116+
117+-- | Compose routes so that @f `composeRoutes` g@ is more or less equivalent
118+-- with @f >>> g@.
119+--
120+-- Example:
121+--
122+-- > let routes = gsubRoute "rss/" (const "") `composeRoutes` setExtension "xml"
123+-- > in runRoutes routes "tags/rss/bar"
124+--
125+-- Result:
126+--
127+-- > Just "tags/bar.xml"
128+--
129+-- If the first route given fails, Hakyll will not apply the second route.
130+--
131+composeRoutes :: Routes -- ^ First route to apply
132+ -> Routes -- ^ Second route to apply
133+ -> Routes -- ^ Resulting route
134+composeRoutes (Routes f) (Routes g) = Routes $ \i -> do
135+ p <- f i
136+ g $ parseIdentifier p
tests/Hakyll/Core/Routes/Tests.hs+3
@@ -18,4 +18,7 @@ tests = fromAssertions "runRoutes"
1818
1919 , Just "tags/bar.xml" @=?
2020 runRoutes (gsubRoute "rss/" (const "")) "tags/rss/bar.xml"
21+ , Just "tags/bar.xml" @=?
22+ runRoutes (gsubRoute "rss/" (const "") `composeRoutes`
23+ setExtension "xml") "tags/rss/bar"
2124 ]