on master help sign in
Add toUrl, move & optimize replaceAll a bit
commit2d1225104cb5a050a84bc70916e1aadfff25fb00 authorJasper Van der Jeugt <jaspervdj@gmail.com> dateSun 2 Jan 2011, 10:22 (16 years ago) parente1aa960
3 files changed+36−18
src/Hakyll/Web/CompressCss.hs+1−15 src/Hakyll/Web/Page.hs+2−2 src/Hakyll/Web/Util/String.hs+33−1
src/Hakyll/Web/CompressCss.hs+1−15
@@ -6,23 +6,9 @@ module Text.Hakyll.Internal.CompressCss
66 ) where
77
88import Data.Char (isSpace)
9−import Data.Maybe (listToMaybe)
109import Data.List (isPrefixOf)
11−import Text.Regex.Posix ((=~~))
1210
13−-- | A simple (but inefficient) regex replace funcion
14−--
15−replaceAll :: String -- ^ Pattern
16− -> (String -> String) -- ^ Replacement (called on capture)
17− -> String -- ^ Source string
18− -> String -- ^ Result
19−replaceAll pattern f source =
20− case listToMaybe (source =~~ pattern) of
21− Nothing -> source
22− Just (o, l) ->
23− let (before, tmp) = splitAt o source
24− (capture, after) = splitAt l tmp
25− in before ++ f capture ++ replaceAll pattern f after
11+import Hakyll.Web.Util.String
2612
2713-- | Compress CSS to speed up your site.
2814--
src/Hakyll/Web/Page.hs+2−2
@@ -56,8 +56,8 @@ addDefaultFields = (getRoute &&& id >>^ uncurry addRoute)
5656 where
5757 -- Add root and url, based on route
5858 addRoute Nothing = id
59− addRoute (Just r) = addField "url" r
60− . addField "root" (toSiteRoot r)
59+ addRoute (Just r) = addField "url" (toUrl r)
60+ . addField "root" (toSiteRoot $ toUrl r)
6161
6262 -- Add title and category, based on identifier
6363 addIdentifier i = addField "title" (takeBaseName p)
src/Hakyll/Web/Util/String.hs+33−1
@@ -2,12 +2,16 @@
22--
33module Hakyll.Web.Util.String
44 ( trim
5+ , replaceAll
6+ , toUrl
57 , toSiteRoot
68 ) where
79
810import Data.Char (isSpace)
11+import Data.Maybe (listToMaybe)
912
1013import System.FilePath (splitPath, takeDirectory, joinPath)
14+import Text.Regex.PCRE ((=~~))
1115
1216-- | Trim a string (drop spaces, tabs and newlines at both sides).
1317--
@@ -16,9 +20,37 @@ trim = reverse . trim' . reverse . trim'
1620 where
1721 trim' = dropWhile isSpace
1822
23+-- | A simple (but inefficient) regex replace funcion
24+--
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
30+ where
31+ replaceAll' src = case listToMaybe (src =~~ pattern) of
32+ Nothing -> src
33+ Just (o, l) ->
34+ let (before, tmp) = splitAt o src
35+ (capture, after) = splitAt l tmp
36+ in before ++ f capture ++ replaceAll' after
37+
38+-- | Convert a filepath to an URL starting from the site root
39+--
40+-- Example:
41+--
42+-- > toUrl "foo/bar.html"
43+--
44+-- Result:
45+--
46+-- > "/foo/bar.html"
47+--
48+toUrl :: FilePath -> String
49+toUrl = ('/' :)
50+
1951-- | Get the relative url to the site root, for a given (absolute) url
2052--
21−toSiteRoot :: FilePath -> FilePath
53+toSiteRoot :: String -> String
2254toSiteRoot = emptyException . joinPath . map parent . splitPath . takeDirectory
2355 where
2456 parent = const ".."