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)
|
2012-06-10 12:21:01 +00:00
|
|
|
import Data.Char (toLower)
|
2011-09-06 20:26:07 +00:00
|
|
|
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-06-10 12:21:01 +00:00
|
|
|
withUrls f = renderTags' . 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"]
|
2011-09-06 20:26:07 +00:00
|
|
|
|
2012-06-10 14:05:50 +00:00
|
|
|
-- | Customized TagSoup renderer. (The default TagSoup renderer escape CSS
|
|
|
|
-- within style tags.)
|
|
|
|
--
|
2012-06-10 12:21:01 +00:00
|
|
|
renderTags' :: [TS.Tag String] -> String
|
|
|
|
renderTags' = TS.renderTagsOptions TS.renderOptions
|
2012-06-10 14:05:50 +00:00
|
|
|
{ TS.optRawTag = (`elem` ["script", "style"]) . map toLower
|
|
|
|
}
|
2012-06-10 12:21:01 +00:00
|
|
|
|
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://"]
|