diff options
| -rw-r--r-- | src/Hakyll/Web/Blaze.hs | 4 | ||||
| -rw-r--r-- | src/Hakyll/Web/Util/Html.hs | 17 | ||||
| -rw-r--r-- | 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 "<p>foo</p> bar" , "foo" @=? stripTags "<p>foo</p" ] + , fromAssertions "escapeHtml" + [ "Me & Dean" @=? escapeHtml "Me & Dean" + , "<img>" @=? escapeHtml "<img>" + ] ] |
