When I was first investigating John Gruber‘s excellent text-formatting system Markdown, one of the things that caught my eye on his demonstration pages was the ability to see the ‘source’ for any of the pages by simply replacing the .html extension with .text. I’d been wondering if it would be possible to pull such a trick for my site for a while, and got it figured out tonight.

You’ll now notice that just after the post date for each entry, there’s the option to go to a ‘text’ version of the entry. The URL is the same as the normal archive, except that it ends with .txt rather than .html. Clicking on the link will send you to the text-only version of the entry, which is simply the entry without any formatting applied to it whatsoever — just what I’ve typed, nothing more, nothing less.

For instance, here’s the text version of this entry.

I’m honestly not sure if there’s a huge use for this, actually, but that’s never stopped me from trying something before. ;) The biggest benefit I can see is that it allows for very easy copy-and-paste operations without having to worry about “smart quotes” fouling things up along the line. It also allows visitors to see the posts as they were written, of course — and thanks to Markdown, the text-only versions are generally just as readable as the formatted HTML versions, without lots of HTML code cluttering things up. Essentially, they look very similar to what a text e-mail might look like, with URLs placed after each paragraph, and references to each link at the appropriate point in the text.

I have noticed some caveats to this technique, however, which may put the usefulness of this entire technique into question.

  1. Safari doesn’t seem to display text files as pure text — rather, it treats them as HTML. This has the effect of running all paragraphs together as a single line, and rendering any HTML it might find. This has the rather unfortunate effect of defeating the purpose. If anyone has any suggestions as how to force Safari to actually display the text as text rather than rendering the HTML, I’d love to hear them.

    Update: Well, now Safari’s behaving and displaying the text versions as I’d expect them to display — as pure text, with un-rendered HTML. I have no idea why it didn’t do so the first time. This first caveat may be moot, then (which is a good thing).

    Update: John Gruber was kind enough to fill me in on why Safari will sometimes display the text as text, and other times will render it as HTML:

    Oh, and the reason that Safari sometimes refuses to show your text
    pages as plain text is because it tries to be clever. If anything
    that resembles an HTML tag appears in the first 100 KB or so of your
    document, Safari treats it as HTML, even if the HTTP headers state
    that it should be “text/plain”.

    Very frustrating, IMO. Apparently it’s a workaround for
    misconfigured servers that send HTML as “text/plain”, and it matches
    a similar workaround in IE/Win.

  2. Firefox will not wrap text files at the end of the screen, so each paragraph ends up as a single long line. Admittedly, this is technically correct, but without the word wrap, it’s a bit difficult to find something in the midst of a long paragraph. You could, of course, copy-and-paste the entire thing into a text editor before doing anything else, but that adds another step when working with anything.

  3. I have no idea what Internet Explorer will do with this, as I don’t have any version of IE on my computer.

If you’re still interested in implementing this yourself — or just curious — read on for the gory details. This is written for Movable Type users, of course, other systems will have to find their own techniques.

And here we go…

First, you’ll need to create the template that Movable Type will use when writing the text files. In your weblog’s menu in Movable Type, go to the Templates editing screen. Scroll down to Archive-Related Templates, and click on Create new archive template.

In the template editing screen, give your template an obvious name (I used ‘Individual Text-Only’), and if you use template files, enter a filename to link the new template to.

The template itself is very simple. Here’s what I used:

: 
Posted by:  on 

When the template is built, the finished text file will include the URL for the entry; the weblog name and entry title; the author’s name and the date the entry was posted; and the entry itself. Setting the convert_breaks tag to 0 ensures that no text formatting will be applied — Movable Type will output only what you wrote.

Once that’s done, save the template.

Next, go to your Weblog Config section, and switch to the Archive Files section. Under Create A New Template/Archive Type Association, set the first menu to Archive Type: Individual, and the second to Template: Individual Text-Only (or whatever name you chose for the template you just created), then hit the Add button.

Now you’ll want to make sure that Movable Type saves the text version of your entry in the same directory as the normal version, only with the a .txt extension. If you look at the Archives section of the page, you’ll see that there are now two items listed under the Individual archive type: the default Individual Entry Archive, and the new Individual Text-Only. The field directly to the right labeled Archive File Template lets you set where the archive file will be saved, and what extension will be appended to the file.

You’ll want to leave the default Individual Entry Archive‘s field blank, and edit the field next to the new template you’ve just enabled. Here’s what I used, in order to duplicate the default Movable Type file structure (as a single line, I’ve broken it into two lines here):

//
.txt

Movable Type will save all archive files underneath whatever directory you’ve set to hold your archives. From that starting point, the string above tells Movable Type to save the file in a directory named for the full four-digit year, a directory for the two-digit month, then the filename ‘dirified’ (any non-appropriate characters stripped out and converted to underscores) and trimmed to fifteen characters if necessary, with the .txt extension.

And with that, the majority of the work is done. The last thing you’ll want to do is add a link somewhere in your Individual Entry Archive and Main Index templates that links to the text version of the entry.

In my case, I added the following line to my templates (again, as a single line, I’ve broken it up here):

(<a>/
/.txt"
title="Text-only version of '' rel="nofollow"">text</a>)

Aside from the addition of the “ tag added to the beginning to create a fully-qualified URL, the rest of the link is the same as I used in the Archive File Template field above. I also added the rel="nofollow" argument to the link, as I figured that Google‘s already archiving my entries once, they don’t really need to do so a second time.

And there we have it. Rebuild your site, and you’re all set.

As I said in my introduction, the actual usefulness of this idea may be somewhat questionable. But then, I’ve never been one to shy away from doing something just because it may not be useful — “because I can” is often good enough reason for me!