Using ReStructured Text with WordPress

Writing long documents in a browser input box isn’t the most pleasant thing to do. The WordPress builtin editor makes things even more inconvenient by forcing you to write your articles in HTML. It would be pretty cool if you could edit articles in a simple, readable format using your favorite text editor. Fortunately, there’s ReStructured Text, a document format frequently used in the Python community.

ReStructured Text (rst) is a pleasant plain text format that can be converted into different formats like HTML or LaTeX. The format is pretty intuitive and well suited for typical blog postings with a bit of formatting and a few links (see this example). Of course, there are also more powerful features, but I don’t use many of them in my articles.

The rst distribution comes with several converter scripts, but none of them is perfectly suited for WordPress. The HTML syntax WordPress uses is slightly simplified and we need just the body, not a full HTML document. Using the rst2s5 converter as a reference I wrote my own converter: rst2wp.

The rst distribution contains a library that parses the text format and turns it into a document tree. A writer class then translates the document tree into a writer-specific format (HTML, LaTeX, S5, …). All I had to do was to inherit from the standard HTMLTranslator class and override all parts of the document I wasn’t interested in. The HTMLTranslator follows the visitor pattern so it’s pretty easy to understand.

Using rst2wp my workflow for posting an article works like this:

  1. Write the article in rst format.
  2. Check it into my Mercurial repository.
  3. Run rst2wp to turn the article into HTML.
  4. Paste the output into the browser’s input box.

The script works well for me, but I didn’t test the more advanced features of rst so there’s a good chance that they won’t work. Feel free to tweak the script to your needs, the code is pretty simple. A possible next step would be to write a tool for posting the article directly via WordPress’ web service. But I’ll leave that for another day.

Update: There’s a modified version available for Blogspot’s input format.

Advertisements
This entry was posted in python and tagged , , . Bookmark the permalink.

8 Responses to Using ReStructured Text with WordPress

  1. Simon says:

    Do you think reST is better than markdown or pandoc?

    • mafr says:

      ReStructured text is the only ASCII format I’m really familiar with. I needed a format where I could easily adjust the output. For reST there’s good tool support in Python and since I like Python, it was an easy choice.

  2. Pingback: Nara's Blog

  3. Pingback: Redescubriendo reStructuredText « Keymon's snippets and brushstrokes

  4. Pingback: Writing long post on wordpress.com (+Español) | Manuel on Software

  5. Pingback: qtm con reStructuredText » Hyperreals *R

  6. Pingback: Python Automation of Wordpress With XMLRPC | TechVersat

  7. Pingback: Automation of Wordpress With Python XMLRPC | TechVersat

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s