RedCloth and WordPress

I’ve been a bit lax posting anything substantive programming-wise; I think it’s largely because I have found myself reiterating the same code over and over at work, so there hasn’t been anything new to contribute (at least nothing that hadn’t already been contributed by programmers far superior than I). I thought this might be worth sharing, however. It’s a little script I put together today to generate an HTML file from a textile input. Nothing groundbreaking, I know, but I find it useful for blog posts, and here’s why.

  • I don’t like writing in HTML. It’s cumbersome.
  • I’m not really a big fan of the visual editors, since I find myself wanting to alter the HTML output anyway. (I think it offends my programmer’s sensibilities.)
  • I really like using textile for markup.
  • Writing programs is fun, and makes you a better programmer.

I found myself consistently writing the articles in TextMate or Vim using textile formatting, then manually converting the output to HTML, etc. I figured my output would be improved with some minor automation and so, with the joy that is Ruby, I set it up. All my posts are stored in a single directory (Articles) with a subdirectory for each website to which they get posted (e.g., each of which then has an ‘out’ directory for the HTML. The textile source goes in, and the following script in the base directory:

#!/usr/bin/env ruby

require 'rubygems'
require 'redcloth'

unless ARGV.empty?
  # Set up the files for conversion
  input_file = ARGV[0]
  directory = File.dirname(input_file)
  # FIXME: Retaining extension on filename
  filename  = File.basename(input_file).gsub(/(.*)\.#{File.extname(input_file)}$/,'\1')
  output_file = "#{directory}/out/#{filename}.html", "r+") do |input|, "w+") do |output|
      # TODO: Unescape HTML codes?
      output <<
  # Fail gracefully if no input file is specified
  puts "You need to specify an input file"

Then I just call ./prepare.rb and voilà! HTML output to / Copy and paste to the HTML editor in WordPress and it’s ready for any final customisation, or just posting.

I’m still tweaking it to remove the original extension from the filename (I wrote this script in less time than it took to write this article) and I was thinking of adding --[no]-escape as a command line option for whether RedCloth or WordPress should handle HTML escape codes. This can be achieved by changing the RedCloth call to

output <<\&\#8217\;/, "'").gsub(/\&\#822(0|1)\;/, '"')

and so on for the remaining escapees. Hell, I haven’t really looked at the RedCloth documentation much — there may be a way to do it already.

Now, before I get a dozen (I wish!) comments telling me how there’s already a way to do this with [insert plugin/editor of choice here], let me state this clearly: this is about writing code. If there’s a simple job to do, sometimes it’s more fun to create a custom solution than to search for an existing one. I’m enjoying (and getting better at) command-line Ruby and scripting, and I’ve got something that does exactly what I want it to, no more, no less!

Oh, and you’ll need to chmod +x prepare.rb to make it command-line executable, but you knew that already. 🙂


~ by MattR on August 28, 2009.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: