summaryrefslogtreecommitdiff
path: root/src/Hakyll/Web/Urls/Relativize.hs
diff options
context:
space:
mode:
authorJasper Van der Jeugt <m@jaspervdj.be>2011-09-06 22:26:07 +0200
committerJasper Van der Jeugt <m@jaspervdj.be>2011-09-06 22:27:53 +0200
commit75f157ca8c319d770f02c38d65226bb3de495a0e (patch)
tree51c92ac2658e3f265dc3971651dd89817f4e6cc9 /src/Hakyll/Web/Urls/Relativize.hs
parentbf4115eb0fad1a3b7a0ce5dc71b55045df30995b (diff)
downloadhakyll-75f157ca8c319d770f02c38d65226bb3de495a0e.tar.gz
Add some URL utilities
Diffstat (limited to 'src/Hakyll/Web/Urls/Relativize.hs')
-rw-r--r--src/Hakyll/Web/Urls/Relativize.hs47
1 files changed, 47 insertions, 0 deletions
diff --git a/src/Hakyll/Web/Urls/Relativize.hs b/src/Hakyll/Web/Urls/Relativize.hs
new file mode 100644
index 0000000..f4b7a6c
--- /dev/null
+++ b/src/Hakyll/Web/Urls/Relativize.hs
@@ -0,0 +1,47 @@
+-- | This module exposes a function which can relativize URL's on a webpage.
+--
+-- This means that one can deploy the resulting site on
+-- @http:\/\/example.com\/@, but also on @http:\/\/example.com\/some-folder\/@
+-- without having to change anything (simply copy over the files).
+--
+-- To use it, you should use absolute URL's from the site root everywhere. For
+-- example, use
+--
+-- > <img src="/images/lolcat.png" alt="Funny zomgroflcopter" />
+--
+-- in a blogpost. When running this through the relativize URL's module, this
+-- will result in (suppose your blogpost is located at @\/posts\/foo.html@:
+--
+-- > <img src="../images/lolcat.png" alt="Funny zomgroflcopter" />
+--
+module Hakyll.Web.Urls.Relativize
+ ( relativizeUrlsCompiler
+ , relativizeUrls
+ ) where
+
+import Prelude hiding (id)
+import Control.Category (id)
+import Control.Arrow ((&&&), (>>^))
+import Data.List (isPrefixOf)
+
+import Hakyll.Core.Compiler
+import Hakyll.Web.Page
+import Hakyll.Web.Urls
+
+-- | Compiler form of 'relativizeUrls' which automatically picks the right root
+-- path
+--
+relativizeUrlsCompiler :: Compiler (Page String) (Page String)
+relativizeUrlsCompiler = getRoute &&& id >>^ uncurry relativize
+ where
+ relativize Nothing = id
+ relativize (Just r) = fmap (relativizeUrls $ toSiteRoot r)
+
+-- | Relativize URL's in HTML
+--
+relativizeUrls :: String -- ^ Path to the site root
+ -> String -- ^ HTML to relativize
+ -> String -- ^ Resulting HTML
+relativizeUrls root = withUrls rel
+ where
+ rel x = if "/" `isPrefixOf` x then root ++ x else x