From 49989eab5767b24cb3e917b95137ae05566e34a8 Mon Sep 17 00:00:00 2001 From: Jasper Van der Jeugt Date: Mon, 28 Feb 2011 22:33:28 +0100 Subject: Web.Util.String → {Web.Util.Url, Core.Util.String} MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Hakyll.hs | 6 ++-- src/Hakyll/Core/Util/String.hs | 48 ++++++++++++++++++++++++++ src/Hakyll/Web/CompressCss.hs | 2 +- src/Hakyll/Web/Feed.hs | 2 +- src/Hakyll/Web/Page.hs | 2 +- src/Hakyll/Web/Page/Metadata.hs | 2 +- src/Hakyll/Web/Page/Read.hs | 2 +- src/Hakyll/Web/Preview/Server.hs | 2 +- src/Hakyll/Web/RelativizeUrls.hs | 2 +- src/Hakyll/Web/Tags.hs | 3 +- src/Hakyll/Web/Util/String.hs | 73 ---------------------------------------- src/Hakyll/Web/Util/Url.hs | 30 +++++++++++++++++ 12 files changed, 91 insertions(+), 83 deletions(-) create mode 100644 src/Hakyll/Core/Util/String.hs delete mode 100644 src/Hakyll/Web/Util/String.hs create mode 100644 src/Hakyll/Web/Util/Url.hs (limited to 'src') diff --git a/src/Hakyll.hs b/src/Hakyll.hs index 9a17479..0261044 100644 --- a/src/Hakyll.hs +++ b/src/Hakyll.hs @@ -12,6 +12,7 @@ module Hakyll , module Hakyll.Core.UnixFilter , module Hakyll.Core.Util.Arrow , module Hakyll.Core.Util.File + , module Hakyll.Core.Util.String , module Hakyll.Core.Writable , module Hakyll.Main , module Hakyll.Web.CompressCss @@ -24,7 +25,7 @@ module Hakyll , module Hakyll.Web.RelativizeUrls , module Hakyll.Web.Tags , module Hakyll.Web.Template - , module Hakyll.Web.Util.String + , module Hakyll.Web.Util.Url ) where import Hakyll.Core.Compiler @@ -38,6 +39,7 @@ import Hakyll.Core.Rules import Hakyll.Core.UnixFilter import Hakyll.Core.Util.Arrow import Hakyll.Core.Util.File +import Hakyll.Core.Util.String import Hakyll.Core.Writable import Hakyll.Main import Hakyll.Web.CompressCss @@ -50,4 +52,4 @@ import Hakyll.Web.Pandoc import Hakyll.Web.RelativizeUrls import Hakyll.Web.Tags import Hakyll.Web.Template -import Hakyll.Web.Util.String +import Hakyll.Web.Util.Url diff --git a/src/Hakyll/Core/Util/String.hs b/src/Hakyll/Core/Util/String.hs new file mode 100644 index 0000000..7f75a36 --- /dev/null +++ b/src/Hakyll/Core/Util/String.hs @@ -0,0 +1,48 @@ +-- | Miscellaneous string manipulation functions. +-- +module Hakyll.Core.Util.String + ( trim + , replaceAll + , splitAll + ) where + +import Data.Char (isSpace) +import Data.Maybe (listToMaybe) + +import Text.Regex.PCRE ((=~~)) + +-- | Trim a string (drop spaces, tabs and newlines at both sides). +-- +trim :: String -> String +trim = reverse . trim' . reverse . trim' + where + trim' = dropWhile isSpace + +-- | A simple (but inefficient) regex replace funcion +-- +replaceAll :: String -- ^ Pattern + -> (String -> String) -- ^ Replacement (called on capture) + -> String -- ^ Source string + -> String -- ^ Result +replaceAll pattern f source = replaceAll' source + where + replaceAll' src = case listToMaybe (src =~~ pattern) of + Nothing -> src + Just (o, l) -> + let (before, tmp) = splitAt o src + (capture, after) = splitAt l tmp + in before ++ f capture ++ replaceAll' after + +-- | A simple regex split function. The resulting list will contain no empty +-- strings. +-- +splitAll :: String -- ^ Pattern + -> String -- ^ String to split + -> [String] -- ^ Result +splitAll pattern = filter (not . null) . splitAll' + where + splitAll' src = case listToMaybe (src =~~ pattern) of + Nothing -> [src] + Just (o, l) -> + let (before, tmp) = splitAt o src + in before : splitAll' (drop l tmp) diff --git a/src/Hakyll/Web/CompressCss.hs b/src/Hakyll/Web/CompressCss.hs index 94ba9a9..2df08fd 100644 --- a/src/Hakyll/Web/CompressCss.hs +++ b/src/Hakyll/Web/CompressCss.hs @@ -12,7 +12,7 @@ import Control.Arrow ((>>^)) import Hakyll.Core.Compiler import Hakyll.Core.ResourceProvider -import Hakyll.Web.Util.String +import Hakyll.Core.Util.String -- | Compiler form of 'compressCss' -- diff --git a/src/Hakyll/Web/Feed.hs b/src/Hakyll/Web/Feed.hs index d91a60f..85674c6 100644 --- a/src/Hakyll/Web/Feed.hs +++ b/src/Hakyll/Web/Feed.hs @@ -33,7 +33,7 @@ import Hakyll.Web.Page import Hakyll.Web.Page.Metadata import Hakyll.Web.Template import Hakyll.Web.Template.Read.Hakyll (readTemplate) -import Hakyll.Web.Util.String +import Hakyll.Web.Util.Url import Paths_hakyll diff --git a/src/Hakyll/Web/Page.hs b/src/Hakyll/Web/Page.hs index 8a16ef8..955e1a8 100644 --- a/src/Hakyll/Web/Page.hs +++ b/src/Hakyll/Web/Page.hs @@ -73,7 +73,7 @@ import Hakyll.Web.Page.Read import Hakyll.Web.Page.Metadata import Hakyll.Web.Pandoc import Hakyll.Web.Template -import Hakyll.Web.Util.String +import Hakyll.Web.Util.Url -- | Create a page from a body, without metadata -- diff --git a/src/Hakyll/Web/Page/Metadata.hs b/src/Hakyll/Web/Page/Metadata.hs index 23d98a4..72742e6 100644 --- a/src/Hakyll/Web/Page/Metadata.hs +++ b/src/Hakyll/Web/Page/Metadata.hs @@ -24,7 +24,7 @@ import System.FilePath (takeFileName) import System.Locale (TimeLocale, defaultTimeLocale) import Hakyll.Web.Page.Internal -import Hakyll.Web.Util.String +import Hakyll.Core.Util.String -- | Get a metadata field. If the field does not exist, the empty string is -- returned. diff --git a/src/Hakyll/Web/Page/Read.hs b/src/Hakyll/Web/Page/Read.hs index d72f32a..cf39ddd 100644 --- a/src/Hakyll/Web/Page/Read.hs +++ b/src/Hakyll/Web/Page/Read.hs @@ -12,7 +12,7 @@ import Data.Map (Map) import qualified Data.Map as M import Hakyll.Web.Page.Internal -import Hakyll.Web.Util.String +import Hakyll.Core.Util.String -- | We're using a simple state monad as parser -- diff --git a/src/Hakyll/Web/Preview/Server.hs b/src/Hakyll/Web/Preview/Server.hs index 77b3cb0..c550b69 100644 --- a/src/Hakyll/Web/Preview/Server.hs +++ b/src/Hakyll/Web/Preview/Server.hs @@ -18,7 +18,7 @@ import Snap.Http.Server ( httpServe, setAccessLog, setErrorLog, addListen , ConfigListen (..), emptyConfig ) -import Hakyll.Web.Util.String (replaceAll) +import Hakyll.Core.Util.String (replaceAll) -- | The first file in the list that actually exists is returned -- diff --git a/src/Hakyll/Web/RelativizeUrls.hs b/src/Hakyll/Web/RelativizeUrls.hs index 1df4fea..2de4a0e 100644 --- a/src/Hakyll/Web/RelativizeUrls.hs +++ b/src/Hakyll/Web/RelativizeUrls.hs @@ -29,7 +29,7 @@ import Text.HTML.TagSoup import Hakyll.Core.Compiler import Hakyll.Web.Page -import Hakyll.Web.Util.String +import Hakyll.Web.Util.Url -- | Compiler form of 'compressCss' which automatically picks the right root -- path diff --git a/src/Hakyll/Web/Tags.hs b/src/Hakyll/Web/Tags.hs index 77dc440..211a06b 100644 --- a/src/Hakyll/Web/Tags.hs +++ b/src/Hakyll/Web/Tags.hs @@ -55,10 +55,11 @@ import qualified Text.Blaze.Html5.Attributes as A import Hakyll.Web.Page import Hakyll.Web.Page.Metadata -import Hakyll.Web.Util.String +import Hakyll.Web.Util.Url import Hakyll.Core.Writable import Hakyll.Core.Identifier import Hakyll.Core.Compiler +import Hakyll.Core.Util.String -- | Data about tags -- diff --git a/src/Hakyll/Web/Util/String.hs b/src/Hakyll/Web/Util/String.hs deleted file mode 100644 index 0dde74a..0000000 --- a/src/Hakyll/Web/Util/String.hs +++ /dev/null @@ -1,73 +0,0 @@ --- | Miscellaneous string manipulation functions. --- -module Hakyll.Web.Util.String - ( trim - , replaceAll - , splitAll - , toUrl - , toSiteRoot - ) where - -import Data.Char (isSpace) -import Data.Maybe (listToMaybe) - -import System.FilePath (splitPath, takeDirectory, joinPath) -import Text.Regex.PCRE ((=~~)) - --- | Trim a string (drop spaces, tabs and newlines at both sides). --- -trim :: String -> String -trim = reverse . trim' . reverse . trim' - where - trim' = dropWhile isSpace - --- | A simple (but inefficient) regex replace funcion --- -replaceAll :: String -- ^ Pattern - -> (String -> String) -- ^ Replacement (called on capture) - -> String -- ^ Source string - -> String -- ^ Result -replaceAll pattern f source = replaceAll' source - where - replaceAll' src = case listToMaybe (src =~~ pattern) of - Nothing -> src - Just (o, l) -> - let (before, tmp) = splitAt o src - (capture, after) = splitAt l tmp - in before ++ f capture ++ replaceAll' after - --- | A simple regex split function. The resulting list will contain no empty --- strings. --- -splitAll :: String -- ^ Pattern - -> String -- ^ String to split - -> [String] -- ^ Result -splitAll pattern = filter (not . null) . splitAll' - where - splitAll' src = case listToMaybe (src =~~ pattern) of - Nothing -> [src] - Just (o, l) -> - let (before, tmp) = splitAt o src - in before : splitAll' (drop l tmp) - --- | 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 diff --git a/src/Hakyll/Web/Util/Url.hs b/src/Hakyll/Web/Util/Url.hs new file mode 100644 index 0000000..54a361e --- /dev/null +++ b/src/Hakyll/Web/Util/Url.hs @@ -0,0 +1,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 -- cgit v1.2.3