diff options
Diffstat (limited to 'src/Hakyll')
-rw-r--r-- | src/Hakyll/Core/Util/String.hs | 43 | ||||
-rw-r--r-- | src/Hakyll/Web/Html.hs | 14 | ||||
-rw-r--r-- | src/Hakyll/Web/Template/Context.hs | 10 |
3 files changed, 45 insertions, 22 deletions
diff --git a/src/Hakyll/Core/Util/String.hs b/src/Hakyll/Core/Util/String.hs index d9ec91c..b46f7b8 100644 --- a/src/Hakyll/Core/Util/String.hs +++ b/src/Hakyll/Core/Util/String.hs @@ -1,5 +1,5 @@ +-------------------------------------------------------------------------------- -- | Miscellaneous string manipulation functions. --- module Hakyll.Core.Util.String ( trim , replaceAll @@ -7,21 +7,24 @@ module Hakyll.Core.Util.String , needlePrefix ) where + +-------------------------------------------------------------------------------- import Data.Char (isSpace) import Data.List (isPrefixOf) import Data.Maybe (listToMaybe) - import Text.Regex.TDFA ((=~~)) + +-------------------------------------------------------------------------------- -- | 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 @@ -35,9 +38,10 @@ replaceAll pattern f source = replaceAll' source (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 @@ -50,19 +54,24 @@ splitAll pattern = filter (not . null) . splitAll' in before : splitAll' (drop l tmp) --- | Find the first instance of needle (must be non-empty) in --- haystack. We return the prefix of haystack before needle is --- matched. + +-------------------------------------------------------------------------------- +-- | Find the first instance of needle (must be non-empty) in haystack. We +-- return the prefix of haystack before needle is matched. -- -- Examples: --- needlePrefix "cd" "abcde" = "ab" --- needlePrefix "ab" "abc" = "" --- needlePrefix "ab" "xxab" = "xx" --- needlePrefix "a" "xx" = "xx" -- -needlePrefix :: String -> String -> String -needlePrefix needle haystack = go haystack +-- > needlePrefix "cd" "abcde" = "ab" +-- +-- > needlePrefix "ab" "abc" = "" +-- +-- > needlePrefix "ab" "xxab" = "xx" +-- +-- > needlePrefix "a" "xx" = "xx" +needlePrefix :: String -> String -> Maybe String +needlePrefix needle haystack = go [] haystack where - go [] = [] - go xss@(x:xs) | needle `isPrefixOf` xss = [] - | otherwise = x : go xs + go _ [] = Nothing + go acc xss@(x:xs) + | needle `isPrefixOf` xss = Just $ reverse acc + | otherwise = go (x : acc) xs diff --git a/src/Hakyll/Web/Html.hs b/src/Hakyll/Web/Html.hs index 58b5c43..3a0aa3b 100644 --- a/src/Hakyll/Web/Html.hs +++ b/src/Hakyll/Web/Html.hs @@ -30,6 +30,7 @@ import System.FilePath (joinPath, splitPath, import Text.Blaze.Html (toHtml) import Text.Blaze.Html.Renderer.String (renderHtml) import qualified Text.HTML.TagSoup as TS +import Network.URI (isUnreserved, escapeURIString) -------------------------------------------------------------------------------- @@ -98,9 +99,18 @@ renderTags' = TS.renderTagsOptions TS.renderOptions -- Result: -- -- > "/foo/bar.html" +-- +-- This also sanitizes the URL, e.g. converting spaces into '%20' toUrl :: FilePath -> String -toUrl ('/' : xs) = '/' : xs -toUrl url = '/' : url +toUrl url = case url of + ('/' : xs) -> '/' : sanitize xs + xs -> '/' : sanitize xs + where + -- Everything but unreserved characters should be escaped as we are + -- sanitising the path therefore reserved characters which have a + -- meaning in URI does not appear. Special casing for `/`, because it has + -- a special meaning in FilePath as well as in URI. + sanitize = escapeURIString (\c -> c == '/' || isUnreserved c) -------------------------------------------------------------------------------- diff --git a/src/Hakyll/Web/Template/Context.hs b/src/Hakyll/Web/Template/Context.hs index 3fde93b..ecf769d 100644 --- a/src/Hakyll/Web/Template/Context.hs +++ b/src/Hakyll/Web/Template/Context.hs @@ -242,9 +242,13 @@ modificationTimeFieldWith locale key fmt = field key $ \i -> do teaserField :: String -- ^ Key to use -> Snapshot -- ^ Snapshot to load -> Context String -- ^ Resulting context -teaserField key snapshot = field key $ \item -> - (needlePrefix teaserSeparator . itemBody) <$> - loadSnapshot (itemIdentifier item) snapshot +teaserField key snapshot = field key $ \item -> do + body <- itemBody <$> loadSnapshot (itemIdentifier item) snapshot + case needlePrefix teaserSeparator body of + Nothing -> fail $ + "Hakyll.Web.Template.Context: no teaser defined for " ++ + show (itemIdentifier item) + Just t -> return t -------------------------------------------------------------------------------- |