on warp/3.0.7 help sign in
38 lines 1.3 kB history raw
1{-# LANGUAGE OverloadedStrings #-}
2module Util
3 ( relativeDirFromPieces
4 , defaultMkRedirect
5 , replace
6 , remove
7 ) where
9import WaiAppStatic.Types
10import qualified Data.Text as T
11import Data.ByteString (ByteString)
12import qualified Data.ByteString.Char8 as S8
13import qualified Data.Text.Encoding as TE
15-- alist helper functions
16replace :: Eq a => a -> b -> [(a, b)] -> [(a, b)]
17replace k v [] = [(k,v)]
18replace k v (x:xs) | fst x == k = (k,v):xs
19 | otherwise = x:replace k v xs
21remove :: Eq a => a -> [(a, b)] -> [(a, b)]
22remove _ [] = []
23remove k (x:xs) | fst x == k = xs
24 | otherwise = x:remove k xs
26-- | Turn a list of pieces into a relative path to the root folder.
27relativeDirFromPieces :: Pieces -> T.Text
28relativeDirFromPieces pieces = T.concat $ map (const "../") (drop 1 pieces) -- last piece is not a dir
30-- | Construct redirects with relative paths.
31defaultMkRedirect :: Pieces -> ByteString -> S8.ByteString
32defaultMkRedirect pieces newPath
33 | S8.null newPath || S8.null relDir ||
34 S8.last relDir /= '/' || S8.head newPath /= '/' =
35 relDir `S8.append` newPath
36 | otherwise = relDir `S8.append` S8.tail newPath
37 where
38 relDir = TE.encodeUtf8 (relativeDirFromPieces pieces)