summaryrefslogtreecommitdiff
path: root/src/Hakyll/Web/Util/String.hs
blob: e48580bb3e6c779394901c032615dcc65749f663 (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
-- | Miscellaneous string manipulation functions.
--
module Hakyll.Web.Util.String
    ( trim
    , toSiteRoot
    ) where

import Data.Char (isSpace)

import System.FilePath (splitPath, takeDirectory, joinPath)

-- | Trim a string (drop spaces, tabs and newlines at both sides).
--
trim :: String -> String
trim = reverse . trim' . reverse . trim'
  where
    trim' = dropWhile isSpace

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