Type Slowly
Making happs-tutorial stop whinging about encodings
(I’m reposting this little tip, first posted back in May because I’ve somehow managed to delete it since then, and it appears to have been useful to a few other people. I’m afraid I never did submit that patch.)
So, as of last week, all of us at Harmonypark have started taking 10% time every Friday afternoon to work on our own projects, learn new things, and generally do interesting stuff that will make us better at our work.
This afternoon was my first ten percent time session, and my plan was to work my way through the Happstack tutorial, then to make something interesting for the web in Haskell (I’ve got some idea of what, but want to see how I get on before talking about it). However, for reasons that are unclear to me, my OS (Ubuntu 10.04 Lucid AMD64), version of GHC (6.12.1) and locale settings (en_GB.utf8) all conspired to stop the tutorial server from running, failing with the message:
./introductiontomacid.st: hGetContents: invalid argument (Invalid or incomplete multibyte or wide character)
The way to get around this is to batch convert all the template files to UTF-8 using iconv. As iconv won’t edit in place, this is a three-step operation. First convert all the files, creating new files with the extension ‘.utf8’, then delete all the old ones, then move the new ones into their place. To accomplish this, run these three commands from inside your checked-out happs-tutorial darcs repository:
find . -name '*.st' -exec sh -c 'iconv -f ISO-8859-1 -t UTF-8 "$1" > "$1.utf8"' -- {} \;
find . -name '*.st' -exec rm {} \;
find . -name '*.utf8' -exec sh -c 'echo "$1" | sed s/\.utf8// | xargs mv "$1"' -- {} \;
This wasn’t all wasted time though, while I haven’t learnt much about Haskell and Happstack, I’ve got to grips with ‘find’ and learnt about some interesting bits of the shell I didn’t know about before. I’m gonna make a patch and submit it back to the maintainers shortly (I’m getting back to actually doing the tutorial for the rest of the afternoon instead), but hopefully this will help out anyone in the meantime.