on master help sign in
Add escapeHtml function
12b4e8aJasper Van der Jeugt
src/Hakyll/Web/Blaze.hs+2−2
@@ -12,8 +12,8 @@ import Text.Blaze (Html, toHtml, preEscapedString)
12import Hakyll.Web.Page
12import Hakyll.Web.Page
13import Hakyll.Web.Page.Metadata
13import Hakyll.Web.Page.Metadata
14
14
15−-- | Get a field from a page and convert it to HTML. This version does escape
15+-- | Get a field from a page and convert it to HTML. This version does not
16−-- the given HTML
16+-- escape the given HTML
17--
17--
18getFieldHtml :: String -> Page a -> Html
18getFieldHtml :: String -> Page a -> Html
19getFieldHtml key = preEscapedString . getField key
19getFieldHtml key = preEscapedString . getField key
src/Hakyll/Web/Util/Html.hs+17
@@ -2,8 +2,12 @@
2--
2--
3module Hakyll.Web.Util.Html
3module Hakyll.Web.Util.Html
4 ( stripTags
4 ( stripTags
5+ , escapeHtml
5 ) where
6 ) where
6
7
8+import Text.Blaze (toHtml)
9+import Text.Blaze.Renderer.String (renderHtml)
10+
7-- | Strip all HTML tags from a string
11-- | Strip all HTML tags from a string
8--
12--
9-- Example:
13-- Example:
@@ -28,3 +32,16 @@ stripTags :: String -> String
28stripTags [] = []
32stripTags [] = []
29stripTags ('<' : xs) = stripTags $ drop 1 $ dropWhile (/= '>') xs
33stripTags ('<' : xs) = stripTags $ drop 1 $ dropWhile (/= '>') xs
30stripTags (x : xs) = x : stripTags xs
34stripTags (x : xs) = x : stripTags xs
35+
36+-- | HTML-escape a string
37+--
38+-- Example:
39+--
40+-- > escapeHtml "Me & Dean"
41+--
42+-- Result:
43+--
44+-- > "Me &amp; Dean"
45+--
46+escapeHtml :: String -> String
47+escapeHtml = renderHtml . toHtml
tests/Hakyll/Web/Util/Html/Tests.hs+4
@@ -15,4 +15,8 @@ tests = concat
15 , "foo bar" @=? stripTags "<p>foo</p> bar"
15 , "foo bar" @=? stripTags "<p>foo</p> bar"
16 , "foo" @=? stripTags "<p>foo</p"
16 , "foo" @=? stripTags "<p>foo</p"
17 ]
17 ]
18+ , fromAssertions "escapeHtml"
19+ [ "Me &amp; Dean" @=? escapeHtml "Me & Dean"
20+ , "&lt;img&gt;" @=? escapeHtml "<img>"
21+ ]
18 ]
22 ]