Baked Blog

So, it looks like I’ve taken the route that quite a few others are taking lately. Although not really based on the post by Brent Simmons: A plea for baked weblogs, it probably got me thinking. Marco Arment also talked about this concept in a couple of episodes of Build and Analyze, one of which I haven’t even listened to yet.

More though, it was wanting to really be in control of what goes on the site. I moved the data from Blogger to Blogsome ages ago, and then onto a self-hosted WordPress installation. I never managed to keep the installation updated, and was forced to use PHP if I wanted to change the look.

So, this re-publish, using Jekyll, was a way to simplify the design, and generally clean everything up. I managed to pull all of the posts down using the Jekyll-WordPress tool, although since I had a very old WordPress installation, I needed to tweak the script somewhat. Mainly that was to get the tags and categories. The database structure they had to associate them was royally fucked. Eventually I got there, though.

I designed the layout myself, with a real focus on simplicity. It’s probably not quite finished, but tweaking will happen. I have a few custom (based on others work) Jekyll plugins: notably I have a generators that create the yearly/monthly/daily archives, and the tag pages. I override the highlight template tag to cache the pygments output, and add a filename if it is included.

I probably would have not had the permalinks the way I do if I had started from scratch, but didn’t really want to break incoming links. I also need to find any more missing images, and incorrectly indented code blocks.

What I haven’t done yet is get my workflow complete. I would like to be able to edit a post in my iPad (when it arrives), or from any DropBox linked computer, and when I mark it as publish, it automatically moves it to the _posts/ folder in the Jekyll directory, runs jekyll, and then deploys the data to the site. This script can take care of the file naming, from the date and the title in the yaml header, or the file modification date/time if none provided in the header. It would be nice to come up with some workflow for creating new files, but I think I’d have to write my own app for that to work on iPad.

I do have a deployment script, which just uses rsync to copy changed files:

1#! /bin/bash
2rsync -rcvuzm --delete /path/to/site/_site/ user@host:

I think I may create a new TextMate jekyll bundle, as the one that exists (a) cannot be installed properly from GetBundles, and (b) is missing some features. Like creating a new post, publish and deploy.

Finally, I am a bit peeved at how long it takes to build the jekyll site. With around 1500 posts, it takes about 4 minutes to generate the files. This is caching the pygments files, but the markdown files need to be re-read each time, as the several pages will change if a new post is added. That is, the index, any paginated root pages, plus all archives for the current year, and any tag pages with tags the same as the new post has. Jekyll currently recreates all pages.

Highlighting Liquid Template Blocks in

For many years, I’ve used an old version of Jekyll to write this blog. For previewing, I use, and one of the things I like about it is how you can get it to preprocess your Markdown files before processing by the markdown processor, or use a custom markdown processor altogether.

In my case, I use Liquid Templates, although the only part of them I use often are the syntax highlighting features. I have some neat TextMate language extensions so that I see the code blocks for Python, SQL and other languages syntax highlighted in the “proper” way for that code block.

Until recently, I think I had a custom markdown processor which used to apply the syntax highlighting so I saw them in as I would in the browser after rendering using Jekyll, but that stopped working. So tonight, I wrote a small tool in python to use Pygments to apply the syntax highlighting.

There’s not much to it: it’s more glue code: it uses re.sub to switch out the highlight block with the syntax highlighted version. Something like:

import pathlib
import sys

from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters.html import HtmlFormatter

def highlight_block(match):
    data = match.groupdict()
    formatter = HtmlFormatter(noclasses=True, linenos=False)
    lexer = get_lexer_by_name(data['language'], stripall=True)
    return highlight(data['code'], lexer, formatter)

  r'{% highlight (?P<language>.*?) %}\n(?P<code>.*?)\n{% endhighlight %}',

However, it is a bit slow to syntax highlight the files. It might be nice to cache them somewhere:

import pathlib

CACHE_DIR = pathlib.Path('/tmp/pygments-cache/')

def highlight_block(match):
    data = match.groupdict()
    cache = CACHE_DIR / '{language}.{hash}.html'.format(

    if cache.exists():

    formatter = HtmlFormatter(
        linenos='linenos' in data
    lexer = get_lexer_by_name(data['language'], stripall=True)
    output = pygments.highlight(data['code'], lexer, formatter)'w').write(output)
    return output

Now it doesn’t need to rebuild syntax highlighting for blocks that have already been highlighted, and the cache automatically invalidates when there are changes to the block.

This is almost the same solution I implemented as a Jekyll plugin to make that run a bunch faster: although this version does inline styles, which means I don’t have to use the same CSS from my blog.

This is packaged up into a command line tool, and installed using:

$ pipx install --spec \
        hg+ \

(or would be if sourcehut’s public urls worked).

Highlight 'highlight' blocks in Markdown/Textmate

The other day, I mentioned that I had nicely handling my {% highlight %} blocks, and syntax highlighting them. In passing at the end, I mentioned that TextMate was still formatting them as if they were Markdown.

Now, one way around this is to indent them, but then within the code block they are indented further, and that offends my sensibilities.

Now, within TextMate, syntax highlighting is based on scopes, so to do what I want (which is the same as how HTML may have CSS or JS embedded in it), we just need a language grammar pattern that matches, and applies the relevant scope.

TextMate 2 has even nicer features, where you can set the scope (but not, as it turns out, include rules) dynamically based on a match in the pattern.

Anyway, on to the rules.

Rather than edit the Markdown rules, I wanted to just inject the language grammars in from a bundle of my own, but had no luck with this. Instead, I decided to extend the Jekyll bundle.

This is what I wanted to put in the patterns (simplified a little):

2  begin = ' "%\}\n';
3  end = ' "%\}\n';
4  name = 'source.$1.embedded.html.markdown';
5  patterns = ( { include = 'source.$1'; } );

However, as I mentioned above, the expression on line 5 does not actually include source.js patterns in this case.

Instead, I needed to have a seperate pattern for each language I wanted to include patterns from. Since mostly I work in python, html and javascript, for now those ones will do.

Oh, and the last thing is that html needs to include text.html.basic.

You can see my fork at jekyll-tmbundle. The current code is: previews of jekyll site is pretty sweet. What I like most about it is that it takes about 2 minutes for my site to regenerate, so doing things like previewing a post is a bit of a pain in the arse: so I can use to have previews every time I save.

But most of my posts are technical, and have code fragments. I’m using Liquid Templating within Jekyll (indeed, a custom highlighter that caches the files), and these were not rendered well by Fair enough, too, as it doesn’t know anything about them.

So, I needed a way to have the {% highlight %} tags handled by Albino, which in turn uses Pygments.

There are posts on the site that talk about Jekyll, but they don’t actually handle the Liquid Templating syntax. For example: github-flavored-markdown.rb

But this one does:

Now all I need is for TextMate to recognise that those block delimiters mean that it is source code and to highlight it as such.

Liquid Templates and Django Templates

Note to self: when I get the error:

/Library/Ruby/Gems/1.8/gems/jekyll-0.11.2/bin/../lib/jekyll/convertible.rb:81:in `do_layout': undefined method `name' for #<Jekyll::Post:0x10f842e88> (NoMethodError)
	from /Library/Ruby/Gems/1.8/gems/jekyll-0.11.2/bin/../lib/jekyll/post.rb:189:in `render'
	from /Library/Ruby/Gems/1.8/gems/jekyll-0.11.2/bin/../lib/jekyll/site.rb:193:in `render'
	from /Library/Ruby/Gems/1.8/gems/jekyll-0.11.2/bin/../lib/jekyll/site.rb:192:in `each'
	from /Library/Ruby/Gems/1.8/gems/jekyll-0.11.2/bin/../lib/jekyll/site.rb:192:in `render'
	from /Library/Ruby/Gems/1.8/gems/jekyll-0.11.2/bin/../lib/jekyll/site.rb:40:in `_draft_process'
	from /Users/matt/Dropbox/Blog/_plugins/drafts.rb:15:in `process'
	from /Library/Ruby/Gems/1.8/gems/jekyll-0.11.2/bin/jekyll:250
	from /usr/local/bin/jekyll:19:in `load'
	from /usr/local/bin/jekyll:19

It’s probably because I have used django template tags in the {% (end)highlight %} blocks, and omitted the {% (end)raw %} stuff.