on master help sign in
Add `composeRoutes`
fa057f3Jasper Van der Jeugt
src/Hakyll/Core/Routes.hs+22
@@ -33,6 +33,7 @@ module Hakyll.Core.Routes
33 , ifMatch
33 , ifMatch
34 , customRoute
34 , customRoute
35 , gsubRoute
35 , gsubRoute
36+ , composeRoutes
36 ) where
37 ) where
37
38
38import Data.Monoid (Monoid, mempty, mappend)
39import Data.Monoid (Monoid, mempty, mappend)
@@ -112,3 +113,24 @@ gsubRoute :: String -- ^ Pattern
112 -> Routes -- ^ Resulting route
113 -> Routes -- ^ Resulting route
113gsubRoute pattern replacement = customRoute $
114gsubRoute pattern replacement = customRoute $
114 replaceAll pattern replacement . toFilePath
115 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"
18
18
19 , Just "tags/bar.xml" @=?
19 , Just "tags/bar.xml" @=?
20 runRoutes (gsubRoute "rss/" (const "")) "tags/rss/bar.xml"
20 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"
21 ]
24 ]