From 12b4e8a0b276829747dffd45ddb6fd89d0c61454 Mon Sep 17 00:00:00 2001 From: Jasper Van der Jeugt Date: Fri, 25 Nov 2011 23:05:50 +0100 Subject: Add escapeHtml function --- src/Hakyll/Web/Blaze.hs | 4 ++-- src/Hakyll/Web/Util/Html.hs | 17 +++++++++++++++++ tests/Hakyll/Web/Util/Html/Tests.hs | 4 ++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/Hakyll/Web/Blaze.hs b/src/Hakyll/Web/Blaze.hs index e83b340..f79f036 100644 --- a/src/Hakyll/Web/Blaze.hs +++ b/src/Hakyll/Web/Blaze.hs @@ -12,8 +12,8 @@ import Text.Blaze (Html, toHtml, preEscapedString) import Hakyll.Web.Page import Hakyll.Web.Page.Metadata --- | Get a field from a page and convert it to HTML. This version does escape --- the given HTML +-- | Get a field from a page and convert it to HTML. This version does not +-- escape the given HTML -- getFieldHtml :: String -> Page a -> Html getFieldHtml key = preEscapedString . getField key diff --git a/src/Hakyll/Web/Util/Html.hs b/src/Hakyll/Web/Util/Html.hs index 5330cdd..2a42608 100644 --- a/src/Hakyll/Web/Util/Html.hs +++ b/src/Hakyll/Web/Util/Html.hs @@ -2,8 +2,12 @@ -- module Hakyll.Web.Util.Html ( stripTags + , escapeHtml ) where +import Text.Blaze (toHtml) +import Text.Blaze.Renderer.String (renderHtml) + -- | Strip all HTML tags from a string -- -- Example: @@ -28,3 +32,16 @@ stripTags :: String -> String stripTags [] = [] stripTags ('<' : xs) = stripTags $ drop 1 $ dropWhile (/= '>') xs stripTags (x : xs) = x : stripTags xs + +-- | HTML-escape a string +-- +-- Example: +-- +-- > escapeHtml "Me & Dean" +-- +-- Result: +-- +-- > "Me & Dean" +-- +escapeHtml :: String -> String +escapeHtml = renderHtml . toHtml diff --git a/tests/Hakyll/Web/Util/Html/Tests.hs b/tests/Hakyll/Web/Util/Html/Tests.hs index fc72cdf..e73c88b 100644 --- a/tests/Hakyll/Web/Util/Html/Tests.hs +++ b/tests/Hakyll/Web/Util/Html/Tests.hs @@ -15,4 +15,8 @@ tests = concat , "foo bar" @=? stripTags "

foo

bar" , "foo" @=? stripTags "

foo" + ] ] -- cgit v1.2.3