hakyll/src/Hakyll/Web/Urls.hs

58 lines
1.5 KiB
Haskell
Raw Normal View History

2011-09-06 20:26:07 +00:00
-- | Provides utilities to manipulate URL's
--
module Hakyll.Web.Urls
( withUrls
, toUrl
, toSiteRoot
, isExternal
) where
import Data.List (isPrefixOf)
import System.FilePath (splitPath, takeDirectory, joinPath)
import qualified Data.Set as S
2012-01-19 18:46:06 +00:00
import qualified Text.HTML.TagSoup as TS
2011-09-06 20:26:07 +00:00
-- | Apply a function to each URL on a webpage
--
withUrls :: (String -> String) -> String -> String
2012-01-19 18:46:06 +00:00
withUrls f = TS.renderTagsOptions opts . map tag . TS.parseTags
2011-09-06 20:26:07 +00:00
where
2012-01-19 18:46:06 +00:00
tag (TS.TagOpen s a) = TS.TagOpen s $ map attr a
tag x = x
attr (k, v) = (k, if k `S.member` refs then f v else v)
refs = S.fromList ["src", "href"]
opts = TS.renderOptions {TS.optEscape = id}
2011-09-06 20:26:07 +00:00
-- | Convert a filepath to an URL starting from the site root
--
-- Example:
--
-- > toUrl "foo/bar.html"
--
-- Result:
--
-- > "/foo/bar.html"
--
toUrl :: FilePath -> String
toUrl ('/' : xs) = '/' : xs
toUrl url = '/' : url
-- | Get the relative url to the site root, for a given (absolute) url
--
toSiteRoot :: String -> String
toSiteRoot = emptyException . joinPath . map parent
. filter relevant . splitPath . takeDirectory
where
2012-01-19 18:46:06 +00:00
parent = const ".."
2011-09-06 20:26:07 +00:00
emptyException [] = "."
emptyException x = x
2012-01-19 18:46:06 +00:00
relevant "." = False
relevant "/" = False
relevant _ = True
2011-09-06 20:26:07 +00:00
-- | Check if an URL links to an external HTTP(S) source
--
isExternal :: String -> Bool
isExternal url = any (flip isPrefixOf url) ["http://", "https://"]