Technical Pickles

Josh on Programming, Linux, and Other Technical Stuff

Using Markdown in vim

My posts are written in Markdown, by using the wonderful BlueCloth library, as I discussed in this earlier post.

Considering this is my first Rails project, it definitely lacks a certain... sophistication. For example, posts are either published and live, or don't exist yet. And also, I don't have any autosave mechanism.

These two areas of want combined to make me a very sad panda one day. After writing up the post of the day, I may have restarted my computer, forgetting that I had a lovely work of writing in progress. Whoops. Got a little upset by that.

Until I have time to implement those two features, I found a suitable stop-gap: vim! So I've taken to simply writing stuff up in vim, and when it's ready, copy, paste, and publish. Easy peasy.

But where would vim be without some syntax highlighting? NOWHERE! Well, it's still pretty awesome regardless, but I don't think many would disagree that syntax highlighting is a bad thing.

While there isn't support out of box, some clever fellow wrote something up to do the trick. While I'm sure you could easily browse there to see how to do this, I'll reproduce it here as well.

Download this, and place it in ~/.vim/syntax/ (of course, creating it if it doesn't exist).

Now create, or add to ~/.vim/filetype.vim:

" markdown filetype file
if exists("did_load_filetypes")
 finish
endif
augroup markdown
 au! BufRead,BufNewFile *.mkd   setfiletype mkd
augroup END

And then to ~/.vimrc and/or ~/.gvimrc

 augroup mkd
  autocmd BufRead *.mkd  set ai formatoptions=tcroqn2 comments=n:>
 augroup END

I kind of feel like a plugin, or something, could take care of this for you, but I don't know enough vim voodoo to sure.

Now you have vim markdown goodness... Enjoy!

Tags
vim markdown bluecloth rails
Published
August 23, 2007 at 00:24

Switched templating engine to BlueCloth

Originally, I had implemented the main data entry using TinyMCE. TinyMCE itself is a pretty cool piece of software. I mean, it gives you a full WYSIWYG (mostly) form, and the output is actual HTML. But in the end, it felt like a bit too much

So, the issues I had were:

  • It was more like WYSISWYG (what you see is sort of what you get)
  • Icky markup
  • A bit of overhead from the extra javascript files loading
  • Not any way to do code highlighting. This was the breaking point, since I plan on having lots of these

After deciding to ditch TinyMCE, the next step was to figure out what to replace it with. There are a few out there for rails:

Sometimes, I hate how developers name stuff :) Here's a break down of much comparison:

  • Pretty equivalent as far as support/ease of use in rails goes.
  • Both support straight-up HTML, so old posts will still work, and you can also drop down to HTML when a particular tricky display issue comes up.
  • Markdown seems a bit more straight forward syntax, but I suspect it's because it's very similar to the syntax used for Trac's syntax, which I'm particularly fond of

So I went with BlueCloth.

Changes to my code were pretty trivial. I added this to my BlogPost model:

      def to_html
        BlueCloth.new(self.body).to_html
      end

And in my view, I changed the following bit:

      <div class="blogbody">
        <%= blog_post.body %>
      </div>

to...

      <div class="blogbody">
        <%= blog_post.to_html %>
      </div>

Easy peezy.

Now, I can use nifty code snippets as you can see. Unfortunately, it's not quite as nifty as it could be, as it, it could use some nice syntax highlighting.

My idea is to use Ultraviolet to do that. So, I could add bits to BlogPost#to_html, to parse out the contents of <code> blocks, run it through Ultraviolet, and return that. I'll save that for another day though.

Tags
rails tinymce redcloth bluecloth ultraviolet
Published
June 18, 2007 at 23:20