A lot of my day-to-day haskell scripts produce html that’s intended to go on my stock market blog. Because I generally just hack stuff together, I’m producing the html along with the output via hPutStr. If I wanted to be a little more fancy and pure about it, I would put another step between my domain and the final HTML output. Looking at the hierarchical haskell libraries, I see Text.XHtml just waiting for me to use it.
So, I played with it for a few minutes. Here, for my own reference, is a very basic use of Text.XHtml:
import Text.XHtml.Strict
myheader = thetitle << "My First Page"
mybody = (h1 << "First Off...") +++
(paragraph << "Hello, there!")
myhtml = (header << myheader) +++ (body << mybody)
main = do
putStrLn $ showHtml myhtml
You can also use showHtmlFragment to get a just the html you wrote, with no doctype stuff prepended. I was surprised that it doesn’t seem to put the <?xml … > tag at the top, since it’s xhtml, after all. But, it doesn’t.
Had I been familiar with this library when I wrote all those scripts, I would have used it. Since I wasn’t, I didn’t, and I’m not sure it’s worth my time to replace that stuff now. Maybe, whenever I update one of them, I’ll go ahead and convert it. That way, they’ll eventually all get converted… you know… lazily. And what could be more in the haskell spirit than lazy conversion