haskellsvgblaze-html

How do I use svg-builder Elements in blaze-html?


I'm using svg-builder to create some simple drawings eg

import Graphics.Svg

main :: IO ()
main = do
  print $ svg contents

svg :: Element -> Element
svg content =
    doctype
    <> with (svg11_ content) [Version_ <<- "1.1", Width_ <<- "300", Height_ <<- "200"]

contents :: Element
contents =
    circle_ [Cx_ <<- "50", Cy_ <<- "50", R_ <<-"40", Stroke_ <<- "green", Stroke_width_ <<- "4", Fill_ <<- "yellow" ]

Now I want to embed such drawings into a normal Html page, eg constructed with blaze-html. The difficulty is, that blaze-html uses data types like Html and Builder, however svg-builder uses a data type called Element. How can I use svg-builder Elements in blaze-html?

Note : There is this similar so question but it uses blaze-svg and I would like to use svg-builder.


Solution

  • Use renderText from svg-builder, pass the resulting text into preEscapedToHtml from blaze-html. Note that you do not want to include the doctype here.