diff options
-rw-r--r-- | src/Hakyll/Core/Routes.hs | 18 | ||||
-rw-r--r-- | tests/Hakyll/Core/Routes/Tests.hs | 3 |
2 files changed, 21 insertions, 0 deletions
diff --git a/src/Hakyll/Core/Routes.hs b/src/Hakyll/Core/Routes.hs index 250536a..eba35ff 100644 --- a/src/Hakyll/Core/Routes.hs +++ b/src/Hakyll/Core/Routes.hs @@ -32,6 +32,7 @@ module Hakyll.Core.Routes , setExtension , ifMatch , customRoute + , gsubRoute ) where import Data.Monoid (Monoid, mempty, mappend) @@ -40,6 +41,7 @@ import System.FilePath (replaceExtension) import Hakyll.Core.Identifier import Hakyll.Core.Identifier.Pattern +import Hakyll.Core.Util.String -- | Type used for a route -- @@ -94,3 +96,19 @@ ifMatch pattern (Routes route) = Routes $ \id' -> -- customRoute :: (Identifier -> FilePath) -> Routes customRoute f = Routes $ Just . f + +-- | Create a gsub route +-- +-- Example: +-- +-- > runRoutes (gsubRoute "rss/" (const "")) "tags/rss/bar.xml" +-- +-- Result: +-- +-- > Just "tags/bar.xml" +-- +gsubRoute :: String -- ^ Pattern + -> (String -> String) -- ^ Replacement + -> Routes -- ^ Resulting route +gsubRoute pattern replacement = customRoute $ + replaceAll pattern replacement . toFilePath diff --git a/tests/Hakyll/Core/Routes/Tests.hs b/tests/Hakyll/Core/Routes/Tests.hs index 5aa6dbd..201c656 100644 --- a/tests/Hakyll/Core/Routes/Tests.hs +++ b/tests/Hakyll/Core/Routes/Tests.hs @@ -15,4 +15,7 @@ tests = fromAssertions "runRoutes" , Just "foo.html" @=? runRoutes (setExtension ".html") "foo" , Just "foo.html" @=? runRoutes (setExtension "html") "foo.markdown" , Just "foo.html" @=? runRoutes (setExtension ".html") "foo.markdown" + + , Just "tags/bar.xml" @=? + runRoutes (gsubRoute "rss/" (const "")) "tags/rss/bar.xml" ] |