MimeTeX for those without CGI

For some reason CGI doesn’t work on this server at all. Trying to run things just ends up with the program itself being served, rather than its output. (Yes, I have the executable bit set.) This means that MimeTeX, which is a CGI program written in C, doesn’t work at all.

Of course, this doesn’t really matter since they have a public server, http://www.forkosh.dreamhost.com/mimetex.cgi, which apparently anyone and everyone can use. Of course I feel a little guilty leeching off their site like that even if Adrað’s bandwidth is pretty negligible, so I hacked together a filter for fauxML that uses their server once and saves the result locally. I’ve put the result here so anyone on the other end of a Google search or whatever can save a few minutes’ hassle by copypasting it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
function _mimetex($arr) {
    $mimetex_url = "http://www.forkosh.dreamhost.com/mimetex.cgi?";
    $cachedir = dirname(__FILE__) . '/cache/';
    $eq = $arr[1];
    $md5 = md5($eq);
    $fn = "$cachedir$md5.gif";
 
    if (!file_exists($fn)) {
        $img = file_get_contents($mimetex_url . urlencode($eq));
        file_put_contents($fn, $img);
    }
    $src = get_option('siteurl')
        . "/wp-content/plugins/fauxml/cache/$md5.gif";
 
    return "<img src='$src' alt='$eq' />";
}
 
// when it finds a sequence of the form $math (...)$ it will
// convert it to a MimeTeX image
wp_add_faux_ml('/\$math[ =]([^$]*?)\$/', '_mimetex');

Add the above to the end of the fauxML file, and create a directory called cache which is writable by the server. Et voilà!

Tags: , ,

Leave a Reply

You must be logged in to post a comment.