blaze-html 0.5
Published on April 21, 2012 under the tag haskell
After some work here at UHac, I’m releasing a new version of the blaze-html library. It has a number of changes, some of which slightly break compatibility with older versions of the library.
blaze-markup and blaze-svg
A few weeks ago, Deepak Jois emailed me that he would like to create a
blaze-html
package for SVG. He started doing so by first simply copying the
blaze-html
code and creating blaze-svg from there.
Obviously, blaze-html
and blaze-svg
contained a lot of common code, so we
(mostly Deepak, he really deserves all credit for this) extracted the shared
code into a new blaze-markup package, upon which both packages now depend.
Backwards-incompatible changes
This move introduced some backwards-incompatible changes, which we catch with deprecation warnings in a few places, but there have also been some module changes, which might break your code when you upgrade.
The Text.Blaze
module no longer contains an Html
type. Instead, it has a
Markup
type. However, in Text.Blaze.Html
, we have
type Html = Markup
so the best way to upgrade is to change every Text.Blaze
import into a
Text.Blaze.Html
import.
Additionally, the renderHtml
methods have been renamed to renderMarkup
. When
you use a renderHtml
method, you will see a deprecation warning. The best way
to resolve this warning is to import e.g., Text.Blaze.Html.Renderer.Utf8
instead of Text.Blaze.Renderer.Utf8
.
Easier creation of custom nodes
blaze-html
0.5 introduces easy creation of custom nodes. In the following
snippet, test1
and test2
generate the same HTML:
{-# LANGUAGE OverloadedStrings #-}
import Text.Blaze.Html (Html)
import Text.Blaze.Html5 (p)
import Text.Blaze.Internal
test1 :: Html
= p "Hello world"
test1
test2 :: Html
= customParent "p" "Hello world" test2
However, you should void usage of customParent
(and customLeaf
,
customAttribute
) whenever possible, which is why they are placed in the
Text.Blaze.Internal
module. They are much slower than their regular
counterparts, and also less safe since they’re vulnerable to typos.
Simple manipulations of the HTML tree
I’ve also decided I will add some very simple manipulations of the HTML tree
when needed. For now, a single manipulation contents
is included, since
another package (to be released in the future) depends on it.
This very fast function takes the text content of the HTML tree, so it is available for rendering. Let’s look at a simple example:
{-# LANGUAGE OverloadedStrings #-}
import Text.Blaze.Html (Html, contents, (!))
import Text.Blaze.Html.Renderer.Utf8 (renderHtml)
import qualified Text.Blaze.Html5 as H
import qualified Text.Blaze.Html5.Attributes as A
test :: Html
= do
test "Hello "
H.p ! A.src "foo.png"
H.img "World!"
H.p
-- Renders: <p>Hello </p><img src=\"foo.png\"><p>World!</p>
= renderHtml test
test1
-- Renders "Hello World!"
= renderHtml $ contents test test2
Installation
You can install the new version just by running
cabal update && cabal install blaze-html
You should definitely contact me if you run into any trouble, I’ll try to get back to you as fast as possible. In the meanwhile, I’m going to enjoy the rest of the Utrecht Hackathon!