Posterous theme by Cory Watilo

Filed under: haskell

HTML Conditional Comments with blaze-html

Why

A couple of days ago I ran across a really neat boilerplate for mobile-friendly development called Skeleton. This seemed great, and because I do my web development in Haskell and use Jasper’s excellent blaze-html, I wanted the index.html coded in Haskell.

No problem, right? Wrong.

As soon as you look at the index.html file, these top lines raise concern:

Can blaze-html do comments like that, or more complex still, are its combinators capable of closing the final if statement but leaving the HTML tag unclosed?

How

The answer to this is yes, although I admit my solution is kind of hackish. As I got farther into this, I began to realize that perhaps I should have just hard-coded these if statements with preEscapedText and moved on with my life, but it was too late at this point. I was already deep in Text.Blaze.Internal’s source.

The first realization is that, due to the nature of the combinators, I had to define my own html and body tags that would allow me to:

  • Open the html tag without closing it
  • Open the body tag but close both it and html

This resulted in:

Here, Parent is used to define the opening and closing tags. Note that the missing > is intentional in the opening string: blaze adds it automatically after appending any attributes.

Next came actually allowing for the creation of the comments. This was actually quite simple after StaticString/OverloadedStrings were understood.

comment is very similar to html' and htmlBody from above, but with one key difference: the addition of ss. This is needed to repack the StaticString that blaze uses for efficiency, which is typically created automagically via the OverloadedStrings language pragma.

Usage

Usage is actually quite simple once the combinators are setup, which was of course my goal all along. First, I created htmlTag, a function with the specific task of outputting just the conditional HTML tags:

I was then able to use this effortlessly in the skeletonBase code that actually output the template:

And at last, we have HTML conditional comments in blaze!

Notes

As mentioned before, I know this seems a bit hackish, but it feels less hackish (and more fun!) than the alternative of just shoving a hard-coded string into the template via preEscapedText. Plus, it was a great reason to get to know blaze’s internals a bit better.

If you can think of a better way to do this, please do let me know.

Announcing hs-vcard

I recently began working on a new venture, QR Card Us, to help support my education and fund another venture–it just launched tonight. This isn't so much a sales pitch about that though as it is an announcement of an open-sourced Haskell module I wrote in the process (though, you're certainly welcome to go check it out, tell your friends, and order some!).

As I was reading up on vCards, I found it most helpful to read RFC 2426. I wanted to easily play around with vCards in my favorite language, but didn't care for the existing vCard module, so I decided to write my own instead, thus letting me announce hs-vcard. I think it's fairly straight forward and well-documented, so I'll end with an example input and the output.

This is the vCard for Frank Dawson, one of the RFC 2426 authors, constructed in Haskell:

and printed out as:

Please let me know if you find any problems, and even better: fix them and submit patches to the GitHub repository. I also chose to not implement reading them in, primarily because I had absolutely no use for that, but people are again welcome to contribute and do so.

I hope you find it useful! Please feel free to comment with any questions or feedback you might have on how I could do things better.

Announcing accentuateus-0.9 (Haskell implementation of the Accentuate.us API)

Introduction

As some of you know, I co-created the Accentuate.us project with Kevin Scannell and have been hacking away at various client implementations ever since (including our flagship Firefox client).

Haskell

So, since I have been working on so many implementations of the API, it only seemed natural that I give some attention to my favourite language.

This is my first release on Hackage and it is available at http://hackage.haskell.org/package/accentuateus. I appreciate any feedback you guys have so that I can improve both it and any future code I write.

Code

The source code is available on Spearhead’s GitHub.

Usage

The API implementation boils down to Accentuate.us' three calls:

  • langs — Accepting an optional locale and mandatory version number (0 if unknown), langs will tell you whether or not you are in sync with the API servers' current version. If not (supplied version != server version), it will supply a new list of (ISO-639, Language Name) pairs. When possible, we will return the language names localized to the supplied locale.
  • accentuate — This is the magic call in the service: given a language, optional locale (to localize any error messages), and input text, it will use statistics to add diacritics.
  • feedback — The other part of the Accentuate.us service that we really want to make more use of is feedback. Since we use statistics, the diacritic restorations will not always be correct (especially for the more under-resourced languages we support). By supplying a language name and corrected text, we will add it to our training text so that results will improve by user contributions.

Feedback

Again, this is my first upload to Hackage and I’m really excited about it. I appreciate any feedback you guys have. The latest source can be found at https://github.com/spearhead/hs-accentuateus/blob/master/Text/AccentuateUs.hs.