summaryrefslogtreecommitdiff
path: root/src/Hakyll/Web/Util/Url.hs
blob: 54a361e49f44d08a108ebe691ac3d820a9234b3d (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
-- | 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 = ('/' :)

-- | Get the relative url to the site root, for a given (absolute) url
--
toSiteRoot :: String -> String
toSiteRoot = emptyException . joinPath . map parent . splitPath . takeDirectory
  where
    parent = const ".."
    emptyException [] = "."
    emptyException x  = x