module Hakyll.Web.Html.RelativizeUrls
( relativizeUrls
, relativizeUrlsWith
) where
import Data.List (isPrefixOf)
import Hakyll.Core.Compiler
import Hakyll.Core.Item
import Hakyll.Web.Html
relativizeUrls :: Item String -> Compiler (Item String)
relativizeUrls :: Item String -> Compiler (Item String)
relativizeUrls Item String
item = do
Maybe String
route <- Identifier -> Compiler (Maybe String)
getRoute forall a b. (a -> b) -> a -> b
$ forall a. Item a -> Identifier
itemIdentifier Item String
item
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ case Maybe String
route of
Maybe String
Nothing -> Item String
item
Just String
r -> forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (String -> String -> String
relativizeUrlsWith forall a b. (a -> b) -> a -> b
$ String -> String
toSiteRoot String
r) Item String
item
relativizeUrlsWith :: String
-> String
-> String
relativizeUrlsWith :: String -> String -> String
relativizeUrlsWith String
root = (String -> String) -> String -> String
withUrls String -> String
rel
where
isRel :: String -> Bool
isRel String
x = String
"/" forall a. Eq a => [a] -> [a] -> Bool
`isPrefixOf` String
x Bool -> Bool -> Bool
&& Bool -> Bool
not (String
"//" forall a. Eq a => [a] -> [a] -> Bool
`isPrefixOf` String
x)
rel :: String -> String
rel String
x = if String -> Bool
isRel String
x then String
root forall a. [a] -> [a] -> [a]
++ String
x else String
x