Tapirtype Blog: Index

« The son of cruftless links | Main | Tip from Daring Fireball: Auto-Completion »

A footnote solution: fixing MT-Textile

A footnote solution: fixing MT-Textile

I wrote a whole entry about my solution to the footnote problems that I’m having since I de-crufted my links. Basically I worked out that it isn’t that hard (or long winded) to use Textile directly to make footnotes instead of using the specific Textile footnote syntax. It was great, a little longer than the simple [1] type of input you are supposed to use, but it worked… Except it didn’t work1.

Something was mysteriously replacing my relative links with absolute links, absolute links that went to the /index.php page that I want to keep people away from. Argh… So I tested making the link in html directly: <a href="#fn1">1</a>… Nope, still gets replaced. At first I thought that maybe there was some setting in the Movable Type preferences that automatically turned relative links into absolute links, cleverly outsmarting me in the name of making my links bulletproof. But no, it’s Textile doing it. If I remove Textile as the text formatting option the links stay happily relative.

In spite of the fact that there’s no mention in the documentation, relative links get turned into absolute links unless you preface them with a ./2. Now that I was on the right track a quick google search set me straight with the ./ behavior.

Actually this explained a lot because I was very puzzled when both the documentation and the perl code of the plugin seemed to me to be saying that the footnotes should generate a link reading: <a href="#fn1">1</a>. So the answer is that, yes, that is exactly what it does. However later on there’s some code that translates that link into an absolute link like: <a href="http://blog.tapirtype.com/2006/10/a_footnote_solution/index.php#fn1">1</a>.

Fortunately it’s easy to fix by telling Textile to generate the footnote link as <a href="./#fn1">. Just search the perl code of the Textile plugin for the text “fntag” and you should see the following bit of code:

# footnotes
  if ($text =~ m/[^ ]\[\d+\]/) {
    my $fntag = '<sup';
    $fntag .= ' class="'.$self->{css}{class_footnote}.'"'
        if $self->{css}{class_footnote};
    $fntag .= '><a href="#'.($self->{css}{id_footnote_prefix}||'fn');
    $text =~ s|([^ ])\[(\d+)\]|$1$fntag$2">$2</a></sup>|g;

This is the code responsible for replacing the footnote tag with the anchor. All you have to do to fix it is add a ./ into the anchor so that the line reads:

$fntag .= '><a href="./#'.($self->{css}{id_footnote_prefix}||'fn');

Problem solved! Textile is now generating footnotes that link correctly! The only problem with this is that the old behavior would save you if you inserted a footnote into the excerpt because it would generate an absolute link to the footnote in the individual entry archive. Now it will try to find the footnote on the index page which probably isn’t what you want. If you want to put a footnote link in the excerpt you could just make it manually like:

<sup class="footnote">

You don’t have to do anything special to make the footnote itself, just use fn1. like normal. I’ve demonstrated this with a useless footnote at the end of the excerpt in this entry that I created exactly as I just described.

Update 10/29/06: Want footnotes in the excerpt sometimes (I mean the footnotes themselves, not just a link to a footnote)? I’ve got it all worked out. For the footnote link just write <sup class="footnote">"1":./#fn1-10-29-06-footnote-sln</sup>. Yeah, it’s kind of awkward, but we’re only doing this occasionally right? The important part is giving it a unique id. For the footnote itself write: p(footnote fn1). <sup id="10-29-06-footnote-sln-fn1">1</sup> footnote text if it is the first footnote, or just p(footnote#fn1-10-29-06-footnote-sln). <sup>2</sup> footnote text if it is a lower footnote.

The important thing here is that I was using the id fn1 to put extra spacing above the footnote block. This will get fragged if I make the ids unique. To preserve the spacing I just added a class fn1 to the style sheet doing all the same stuff I had the id fn1 doing before. So for the first footnote, the paragraph as both the class footnote and the class fn1 and I just put the id for the link target on the <sup>. Probably all pretty obvious stuff, but hey, I figured it might be useful to someone someday.

Update 10/30/06: I corrected the last update in which I gave the link target id a value that started with a digit which won’t validate. Start with a letter instead.

1 This is a footnote for the sake of demonstrating the manual footnote link from the excerpt.

2 I don’t mean to be hard on Brad Choate or Dean Allen, the authors of the Movable Type plugin and original version of textile respectively. They are both demigods of this stuff who have contributed these really useful tools without asking anything in return. And yes, this was mostly just an excuse to test out a footnote.

You are visiting Tapirtype Blog. Unless otherwise noted, all content is © 2006-2008 by Sasha Kopf and Michael Boyle, some rights reserved. Site design by Michael Boyle modified from the standard Movable Type templates. I've made an attempt to generate standards compliant content which should look best in Safari or, otherwise, Firefox. Use of Internet Explorer may be harmful to your sanity and I've made little attempt to support it.

If you like you can subscribe to Tapirtype Blog's feed. That way you can be the first to know when more things burble from our brains.

This page is published using Movable Type 4.1