summaryrefslogtreecommitdiff
path: root/src/Hakyll/Web/RelativizeUrls.hs
blob: 2a3b98feb438e0ec877f15d0fff9e50f51f5cf1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
module Hakyll.Web.RelativizeUrls
    ( relativizeUrls
    ) where

import Data.List (isPrefixOf)
import qualified Data.Set as S

import Text.HTML.TagSoup

-- | Relativize URL's in HTML
--
relativizeUrls :: String  -- ^ Path to the site root
               -> String  -- ^ HTML to relativize
               -> String  -- ^ Resulting HTML
relativizeUrls root = renderTags . map relativizeUrls' . parseTags
  where
    relativizeUrls' (TagOpen s a) = TagOpen s $ map (relativizeUrlsAttrs root) a
    relativizeUrls' x = x

-- | Relativize URL's in attributes
--
relativizeUrlsAttrs :: String            -- ^ Path to the site root
                    -> Attribute String  -- ^ Attribute to relativize
                    -> Attribute String  -- ^ Resulting attribute
relativizeUrlsAttrs root (key, value)
    | key `S.member` urls && "/" `isPrefixOf` value = (key, root ++ value)
    | otherwise = (key, value)
  where
    urls = S.fromList ["src", "href"]