summaryrefslogtreecommitdiff
path: root/src/Hakyll/Web/Util/Url.hs
blob: 7ab67172b895a9925e30fcd920373ab76b747fbc (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
30
31
32
33
34
35
-- | Miscellaneous URL manipulation functions.
--
module Hakyll.Web.Util.Url
    ( toUrl
    , toSiteRoot
    ) where

import System.FilePath (splitPath, takeDirectory, joinPath)

-- | 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
    parent = const ".."
    emptyException [] = "."
    emptyException x  = x
    relevant "." = False
    relevant "/" = False
    relevant _   = True