@@ -2,12 +2,16 @@
3module Hakyll.Web.Util.String
3module Hakyll.Web.Util.String
5 , toSiteRoot
7 , toSiteRoot
8import Data.Char (isSpace)
10import Data.Char (isSpace)
11+import Data.Maybe (listToMaybe)
10import System.FilePath (splitPath, takeDirectory, joinPath)
13import System.FilePath (splitPath, takeDirectory, joinPath)
14+import Text.Regex.PCRE ((=~~))
12-- | Trim a string (drop spaces, tabs and newlines at both sides).
16-- | Trim a string (drop spaces, tabs and newlines at both sides).
@@ -16,9 +20,37 @@ trim = reverse . trim' . reverse . trim'
17 trim' = dropWhile isSpace
21 trim' = dropWhile isSpace
23+-- | A simple (but inefficient) regex replace funcion
25+replaceAll :: String -- ^ Pattern
26+ -> (String -> String) -- ^ Replacement (called on capture)
27+ -> String -- ^ Source string
28+ -> String -- ^ Result
29+replaceAll pattern f source = replaceAll' source
31+ replaceAll' src = case listToMaybe (src =~~ pattern) of
34+ let (before, tmp) = splitAt o src
35+ (capture, after) = splitAt l tmp
36+ in before ++ f capture ++ replaceAll' after
38+-- | Convert a filepath to an URL starting from the site root
42+-- > toUrl "foo/bar.html"
48+toUrl :: FilePath -> String
19-- | Get the relative url to the site root, for a given (absolute) url
51-- | Get the relative url to the site root, for a given (absolute) url
21−toSiteRoot :: FilePath -> FilePath
53+toSiteRoot :: String -> String
22toSiteRoot = emptyException . joinPath . map parent . splitPath . takeDirectory
54toSiteRoot = emptyException . joinPath . map parent . splitPath . takeDirectory
24 parent = const ".."
56 parent = const ".."