summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJasper Van der Jeugt <m@jaspervdj.be>2011-11-25 23:05:50 +0100
committerJasper Van der Jeugt <m@jaspervdj.be>2011-11-25 23:05:50 +0100
commit12b4e8a0b276829747dffd45ddb6fd89d0c61454 (patch)
tree5ad2cb5b1784c57967431f9f4fb23cd21a1b0a30
parent3a4b45ef29cebf9f7110412e005a3dac5d6d6262 (diff)
downloadhakyll-12b4e8a0b276829747dffd45ddb6fd89d0c61454.tar.gz
Add escapeHtml function
-rw-r--r--src/Hakyll/Web/Blaze.hs4
-rw-r--r--src/Hakyll/Web/Util/Html.hs17
-rw-r--r--tests/Hakyll/Web/Util/Html/Tests.hs4
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 &amp; 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 &amp; Dean" @=? escapeHtml "Me & Dean"
+ , "&lt;img&gt;" @=? escapeHtml "<img>"
+ ]
]