Posts Tagged ‘textmate’

Markdown footnotes bundle for TextMate

There was a recent discussion on the TextMate mailing list about automating the creation of TextMate footnotes. Stephan Hugel, who started the discussion, likes having his footnote text at the bottom of the document and wanted some command/snippet/macro system that would do most of the fiddle work for him. I pointed out my Markdown reference link system for TextMate and suggested how it could be modified to do what he wanted. You can read about it by following the discussion thread. I won’t recapitulate it here, because that particular way of doing footnotes isn’t to my taste. I do, however, have a couple of footnote-making automations that I’d like to share.

First, you should recognize that footnotes aren’t a part of the official Gruber Markdown. They are, however, included in several Markdown variants/extensions, including Michel Fortin’s PHP Markdown Extra and Fletcher Penney’s MultiMarkdown.

Here’s an example of footnote syntax:

* First, it's kind of backwards. You do the footnote text first,
then set the position of the marker. I find this perfectly natural,
but you may think it's weird.
* Second, it replaces whatever was on your clipboard with the
marker. This doesn't bother me because I use [Jumpcut][8], a
[multi-clipboard tool][9] that saves the last 40 text clippings.
If you don't use a utility like Jumpcut, you'll find this
behavior *really* annoying.[^multiclip]

[^multiclip]: But you really should be using a multi-clipboard tool.
It makes working with text so much easier.

The footnote marker is placed where the [^marker] is, and the text of the footnote is the paragraph that starts with [^marker]:. The footnote text can go anywhere in the document; it can even be intermixed with reference-style links down at the bottom, although in my opinion that makes for an unreadable mess. I tend to put my footnote text just after the paragraph with the corresponding marker.

To semi-automate the insertion of footnotes, I use a snippet and a macro, which you can download in this zipped TextMate bundle. The source code of the snippet is just

[^${1:marker}]$0: ${2:Footnote}

The marker (default value: marker) is the first tab stop and the footnote text (default value: Footnote) is the second. Tabbing a third time puts the cursor at the end bracket of the marker, which is where the macro expects to start. The Tab Trigger for this snippet is, in a nod to LaTeX, fn.

The macro is similarly trivial. It selects the text from the current position (just after the marker’s end bracket if you invoke it right after the snippet) to the beginning of the line (just before the marker’s start bracket), copies it to the clipboard, and sets the cursor at the beginning of the footnote paragraph. The marker is now on the clipboard, ready to be pasted wherever I want to put it. The Key Equivalent for this macro is ⌃⌥⌘F.

You should note two things about this method of doing footnotes:

If these caveats don’t bother you, install the Markdown Footnotes bundle and give it a try. If you do a lot of footnotes, it can save you some time.


  1. But you really should be using a multi-clipboard tool. It makes working with text so much easier. 


The no-server notes wiki

I’ve just pushed a new version of my unnamed no-server notes wiki to its GitHub repository. Notes can now be organized in subdirectories instead of all being at the top level. Here’s the README.


This is a no-server personal wiki system that I created to keep track of project notes for work. I’ve put it here because it may be useful to others.

Goals

This is what I want:

  1. A self-contained file or folder of files that includes everything needed to write and view the notes. I want it to be easy to copy from one computer to another and to archive to DVD. This eliminates most of the available wiki systems, which store everything in a central database.
  2. The notes themselves to be written in Markdown rather than some specialized wiki markup. I write everything in Markdown and don’t want to shift context when switching from notes to a report. In fact, I’d like to be able to copy directly from my notes—markup included—when writing a report.
  3. To write the notes in my text editor of choice rather than in an HTML text input box or a word processor. Currently, that editor is TextMate, but TextMate itself isn’t the point. The point is to take advantage of the comfort I feel working in my normal editor. There’s a reason old Unix hackers like to do everything in Emacs or vi; it’s just more efficient to do all your text work in one environment.
  4. To be able to change the visual style of the notes as my needs or tastes change.
  5. To create new notes quickly and easily.

Requirements

Apart from what’s in the repository, you’ll need

If you need to include mathematical formulas in your notes, you should consider installing Davide Cervone’s jsMath. Once you’ve installed it, you can activate jsMath in the notes by uncommenting Line 10 of the header.tmpl file and adjusting the jsmathpath variable in the project.info file to point to jsMath’s easy.js file.

File structure

The top level of the notes directory contains all the support files, that is, all the files that are distinct from the notes themselves. These files are:

Notes files contain the actual content. These files should all have the extension .md and can be in both the top-level directory and in subdirectories. Two sample notes files are included: aa-overview.md in the top-level directory, and testing1.md in the Lab subdirectory.

Creating notes

As mentioned above, notes are just plain text files written in Markdown and saved with an .md extension. The first line will be the note’s title and will appear in the sidebar.

I use ATX-style headers, with hash marks indicating the header level, and I start each file with a first-level header, like this:

    # Overview #

The build system is smart enough to get rid of the hash marks when making up the sidebar.

Notes files in subdirectories appear with greater indentation under the name of the directory—like an outline. Within each directory, the notes are ordered alphabetically according to their file names, so you can rearrange the order in which the notes appear in the sidebar by changing the file names without changing their content. At present, there’s no way to change the order of the subdirectories.

Executing make from the top-level directory will generate all the HTML pages, which can be opened with any browser. Subsequent executions of make will generate only those pages whose .md files are new or have been modified. Executing make clean will erase all the HTML files, but will not touch the .md files.

Editing notes

You can, of course, open any .md file in any text editor to make changes. If you’re using TextMate on a Mac, there’s a faster way: click the Edit in TextMate link in the side bar to instantly open the .md file in TextMate—no need to switch to the Finder, open the folder, and double-click the file icon. If you’re a BBEdit user, you can do the same thing, but you’ll probably want to change the name of the link. It’s on Line 33 of header.tmpl.

More details

I wrote a three-part series of blog posts describing this system and its scripts, here, here, and here. The scripts have changed since then, but the basic ideas are the same.

License

This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License. Do what you want with it, but provide a link back to either my blog posts or to my repository.


An odd coincidence

Less than 24 hours after my last post, in which I said I wanted to explore build systems other than make to generate my serverless wiki project notes files, Allan Odgaard writes a post on make that leads me to think I shouldn’t be too hasty in abandoning a software tool that’s stood the test of time. Allan is a smart guy who’s written some great stuff and sounds very authoritative when he says computer sciencey things like “directed acyclic graph.” Also, the last example in his post is a frighteningly close match to what I’m doing, so I think I’ll listen to him.

He says this post is the first of two on the topic of make. I look forward to Post 2 almost as much as I do to TextMate 2. Hope I don’t have to wait as long.


Extending the Calendar Events bundle for TextMate

I’ve added a new command to my Calendar Events bundle for TextMate (described in this post and available in this GitHub repository). The new command saves the generated iCalendar document to a temporary file and opens that file in iCal (or, presumably, BusyCal, if that’s your default calendar program). When an iCalendar file is opened in iCal, it asks you which calendar you want to put the new events into:

Clicking the popup menu will let you choose any of your existing calendars and will also let you create a new one.

The new command is called “Add to calendar,” and it’s bound to the ⌃⌥⌘A keyboard shortcut.

So the process of adding events to your calendar is now:

  1. Open a new document in TextMate and press ⌃⌥⇧E to make it an Events document.
  2. Use the “ev” and/or “ad” snippets to add timed events or all-day events to the document in a simple pipe-separated format.
  3. Press ⌃⌥⌘C to create an iCalendar document with those events.
  4. Assuming the iCalendar document is correct, press ⌃⌥⌘A to add those events to an iCal calendar chosen via the window shown above.

Calendar events bundle for TextMate

I think most users would agree that entering new events in iCal has always been clumsy and the Snow Leopard update just made it clumsier. I mentioned in a post last week that BusyCal’s entry method was distinctly better, although overall, BusyCal wasn’t enough of an improvement to get me to switch. In the past, I’ve used TextMate to create files using a simple pipe-separated format for the events; these are converted through a TM command into iCalendar files, which can be dragged to iCal to add the events. I’ve now improved that command and made a TextMate bundle for generating calendar events.

You can get the bundle from its GitHub repository. You install it by either

Although it’s not necessary, it would help if you also install my fork of the iCalendar bundle. This adds just one feature to the standard iCalendar bundle: it recognizes the file type based on a first line of BEGIN:VCALENDAR. This makes saving the generated iCalendar file with the proper extension a little bit easier.

To use the Calendar Events bundle, open a new document and press ⌃⌥⇧E to change the document’s language to Event. You now have two tab-triggered snippets available to enter events. For timed events, type “ev” and tab to get

Event|01/07/10 9:00 am|1.00|Location|Notes

This is the pipe-separated format for defining an event. The five fields are:

  1. The event name, which is the top line in the usual iCal event bubble.
  2. The event’s starting time, which is both a date and time. There are three TextMate tab stops within this field: one for the month and day, one for the year, and one for the time. The date parts default to the current date, and the time defaults to 9:00 am.
  3. The event’s duration in hours, which defaults to 1.
  4. The event’s location.
  5. Any other notes you may want to describe the event.

The location and notes fields may be blank, but the pipe characters between them must be present.

For all-day events, type “ad” and tab to get

Event|01/07/10|all day|Location|Notes

There is no time associated with the start of the event and therefore only two tab stops in this field. The duration is set to “all day” and is skipped over when tabbing through the snippet. The other three fields are the same as before.

When you’ve entered all the events, invoke the “Make ICS” command, either by choosing it from the gear popup menu at the bottom of the window or by typing ⌃⌥⌘C. That will generate a new document with the events transformed into the iCalendar format. If you’ve made a formatting error in the Events document, it will (probably) be flagged as such in the iCalendar document. Here’s an example in which

Project conf call|1/7/10 9:00 am|1.00|office|Pull drawings from file
All day event|1/20/10|all day||
Wrong # of fields|8/23/10 9:00 am|1.00|Location
Project meeting|1/10/10 10am|2|Chicago|Bring drawings
Bad start time|1/40/10 9:00 am|1.00|home|
Personal day|1/30/10|all day||
Bad duration|1/12/10 9:00 am|bad|office|whatever
Basketball|1/15/10 7pm|2|Stadium|

is transformed into

BEGIN:VCALENDAR
VERSION:2.0
BEGIN:VEVENT
DTSTART:20100107T090000
DTEND::20100107T100000
SUMMARY:Project conf call
LOCATION:office
DESCRIPTION:Pull drawings from file
END:VEVENT
BEGIN:VEVENT
DTSTART;VALUE=DATE:20100120
DTEND:;VALUE=DATE:20100121
SUMMARY:All day event
LOCATION:
DESCRIPTION:
END:VEVENT
!!!!!!!!!!!!!!!!!!!! This event has the wrong number of fields
BEGIN:VEVENT
DTSTART:20100110T100000
DTEND::20100110T120000
SUMMARY:Project meeting
LOCATION:Chicago
DESCRIPTION:Bring drawings
END:VEVENT
!!!!!!!!!!!!!!!!!!!! This event has a bad start time
BEGIN:VEVENT
DTSTART;VALUE=DATE:20100130
DTEND:;VALUE=DATE:20100131
SUMMARY:Personal day
LOCATION:
DESCRIPTION:
END:VEVENT
!!!!!!!!!!!!!!!!!!!! This event has a bad duration
BEGIN:VEVENT
DTSTART:20100115T190000
DTEND::20100115T210000
SUMMARY:Basketball
LOCATION:Stadium
DESCRIPTION:
END:VEVENT
END:VCALENDAR

On my system, the two documents look like this:

Because I have my iCalendar bundle installed, the generated document is automatically recognized as an iCalendar file and is syntax-highlighted appropriately. When I save it, the .ics extension will be present by default. If you don’t have my iCalendar bundle, TextMate won’t recognize it as an iCalendar file and you’ll have to change the extension from whatever your default is (probably .txt).

You’ll also notice that the pipe characters are colored in my Events document. That’s because I added an entry for tabular separators to the theme (IDLE) that I use.

This highlighting will also work for CSV and TSV files if you have the Tabular bundle installed. If you want special highlighting for the field separators, just add a line with a scope of punctuation.separator.tabular.field to the theme you use. The highlighting isn’t a requirement; the snippets and command will work without it.

The Make ICS command uses the parse function from Python’s datetime the dateutil module to interpret the date and time in the second field. This is a fairly forgiving function; you don’t need to include leading zeros, and as you can see from the example above, even something like “7pm” is interpreted correctly.

Update 1/8/10
Not sure what I was thinking when I wrote datetime in the previous paragraph. The module used is dateutil, which isn’t a standard Python module. On my iMac, it’s in a subdirectory of /System/Library/Frameworks/Python.framework, which suggests that it’s supplied by Apple and not something I installed and later forgot about. I’ll look into it and update here when I get the answer…

OK, I’m back, and I’m pretty sure now that dateutils has been included by Apple since Leopard. So if you’re running OS X 10.5.x or 10.6.x, the Make ICS command will work. If you’re running 10.4.x or earlier, you have three choices. In decreasing order of desirability, they are:

  1. Upgrade to Leopard or Snow Leopard, i.e., get with the program. Look, my home computer is an iBook that came with Jaguar, and it’s perfectly happy with Leopard—I’d have upgraded it to Snow Leopard if it weren’t Intel-only. There’s no reason for you to be that far behind.
  2. Download dateutils from Labix and install it according to their directions.
  3. Just don’t bother with the Calendar Events bundle at all.

I deliberately added errors to three lines in the Events document so you can see how they are handled. The position of the error messages in the iCalendar document will tell you which lines need to be fixed.

After saving the iCalendar document, you can drag it onto one of the calendar names in iCal to get all the events added to that calendar. iCal won’t accept a file with formatting errors, so if you forget to make the necessary corrections, iCal will tell you.

Is this faster than iCal’s normal entry method? Certainly not if you’re just adding one or two events. But if you need to add several events, especially when the events span several weeks or months, it’s much faster. I also find that I make fewer errors because I don’t get frustrated with all the keystrokes and/or mouse clicks iCal demands.