Damn You, Emacs Autoinsert

Emacs can also be annoying when it autoinserts text without checking to see if it’s already there. Case in point, gettext .po files under Emacs 23.1. For whatever reason, my copy kept inserting this header whenever I opened a translation file:

If this header sneaks into a .po file, when you try to compile the file with Django’s compilemessages command, you get the following error:

What seems to happen is that this header is only inserted when you use C-x C-f to open a file. When opening a file directly from the command line, this corruption does not seem to occur.

If you look at the PO Group configuration options, this text is listed there as the default PO file header. I haven’t validated this as a solution, but you should be able to switch this value to be the comment character “#”, and that should take care of the problem.

Also, why the hell does the PO major mode not allow you to destroy entire msgid’s and their translations? This is really annoying. Yes, it’s nice sometimes when programs limit your options, but in this case, Emacs was messing up by inserting the bad header, then refusing me the option of removing it.

Custom Pystache Template And Loader Classes For Django

Update: I’ve posted the code at github here.

Wanting to keep my server-side and client-side templates identical, I noticed there wasn’t a good solution for using Mustache/Pystache/Handlebars templates alongside the Django templating language.

The existing one that I saw used Pystache internals, and I’m not even sure they’re still valid in the latest Pystache versions.

The other existing one that I saw let you do the equivalent of #include “something.mustache” within the Django templates, which wasn’t what I was looking for either.

So I went ahead and wrote a set of PystacheTemplate and Pystache*Loader classes which do it all within the specifications of the Django infrastructure, and, I set them up so that you can specify the exact file extensions you want them to operate on, i.e. just .mustache, .handlebars, and .hbs files by default. This doesn’t mean you can mix Mustache directly into Django templates, which doesn’t make sense (and is mildly redundant), but it does mean you can render complete Mustache template files directly, which is awesome if you’re wanting to share those templates or partials between client and server.

Copy this file somewhere into your project or app folders:

Then you can just update the settings.py file to use the custom Loaders, putting them ahead of Django’s default Loaders in the TEMPLATE_LOADERS setting:

Then if you have, say, a file called index.handlebars somewhere in one of your app directory template/ folders or somewhere else in the TEMPLATE_DIRS list, you can just do this in one of your views:

And all will be good.

Please let me know in the comments if you have any problems with this. (Speed might one of them, I haven’t extensively tested this yet. But I plan to cache these using the Django template cache loader.)

Generating List Of Mustache Tags In A Template

Google falls over again looking for:

“generating list of mustache tags in a template”
“regular expression list of mustache tags in a template”
“grep list of mustache tags in a template”
“get list of mustache tags in a template”
“grep expression for mustache tags”

What am I trying to do? I have a Mustache template, and I want to pull out the list of tags I am using in it, because I need to filter a bigger chunk of JSON data to only contain those tags. This JSON data will eventually be used as the context that is then used to .render() the output.

Google needs to reinstate exact-quote indexing, the fuzzy searches of the past few years are making it really hard to find relevant links. (Yes, I probably should have the regex rules memorized, but this can’t be the first time someone’s been looking for this.)

For the record, the command I used to generate this list:

Update 2 March 2013:

The above command was perhaps a bit too simple, it gives the tags in the form of a list of “{{tag}}”.

To get rid of the mustache brackets, you have to run the point through sed.

In Python, the following regex will also give you all the tag names, sans brackets:

Implicit Things

As a learning exercise, I converted a piece of Javascript code i’d written into CoffeeScript.

So what’s wrong with this?

Oh that’s right, initialize doesn’t get called.

It must be “initialize()” when you want to call a function w/no params. But when you do have params, you can leave the parens off. What inconsistent bullshit is this? I guess it’s nice that “initialize” by itself is a statement w/o a side effect. But then when you run it through the compiler (when the hanging return isn’t there), it spits out “return initialize;”, which isn’t what I meant at all and could be a side effect for anyone trying to maintain this down the road. Better to make it explicit that this thing won’t return anything a caller can (ab)use, until and unless I choose to make that the case.

So I’ve been adding hanging returns so the compiled code returns nothing as often as it should.

Sigh, I don’t know that I dig the whole implicit “return whatever was on the last line” thing that so many of these terse scripting languages have. And it strikes me as a weird idea to try and save on parens.

So the CoffeeScript adventure begins, but I’ll be damned if I’m not already building the defensive programming idioms in.