March 22, 2007 will

BBCode Python Module II

It's good to see a few people using my BBCode module. I have since made some small fixes and added a feature. I've added a PygmentsCodeTag that uses the excellent Pygments module to syntax highlight code within a [code][/code] tag. It supports all the languages that Pygments supports, simply specify the language in the tag (i.e. [code python][/code]). You can still use the code tag that generates <pre></pre>, if you have another syntax highlighting solution.

I have also simplified usage a little. Previously you had to add tags individually to the markup object, which was only a cut and paste from the sample code - but I don't like boiler-plate code when it can be moved to a function. So I added a Create function that creates a markup object with all the tags. Here's an example of how to use it, see the code for details.

import postmarkup
markup = postmarkup.Create()
bbcode = "[b] Hello, World! [/b]"
print markup.render_to_html(bbcode)

Being a lazy programmer I also implemented the __call__ function in the postmarkup object and had it call render_to_html, so the last line can be written as print markup(bbcode).

Something I forgot to mention in the earlier post is that in addition to this form [url http://www.willmcgugan.com], postmarkup also supports the more usual BBCode syntax of [url="http://www.willmcgugan.com"]. I prefer the former, because it's easier to type and I think it looks better.

You can download postmarkup.zip from the usual place. The zip also contains code.css, which you will need if you use the Pygments code tag. I will eventualy put this and my other Python projects on Sourceforge or Google code when I have the time. But I've got another more urgent project I'll be working on.

Use Markdown for formatting
*Italic* **Bold** `inline code` Links to [Google](http://www.google.com) > This is a quote > ```python import this ```
your comment will be previewed here
gravatar
Jesper

Cool module.

Why do you have 2 different "wiki" tags, though? Is there any way you can select either? It seems to me like the reference.com takes presence over wikipedia's.

gravatar
Jesper

Oh, also, can you add an [img]-tag?

gravatar
Jesper

Module doesn't deal well with unicode. If you do a [google Jesper Nøhr]google[/google], it will croak. urllib.quote_plus dies on trying to quote the 'ø'.

The fix is to explicitly cast the 'search'-var to a string;

return link % quote_plus(str(search)) # On line 273

gravatar
Will

There's a new version with these issues fixed. :-)

gravatar
David Cramer

Nice work on this -- I think we're going to put this into use to save some dev time of our own :)

gravatar
Will

Glad you like it!

The latest version is on Google code:

http://code.google.com/p/postmarkup/

gravatar
Chris

Really nice. Thank you.
Found a little bug though: If you input a single "[" it throws an error.
In my case it broke when somebody used the smilie ":-[".

gravatar
Will

Thanks for the bug report. I've checked in a fix to SVN. I'll release a new version soon.

gravatar
Somename
I wish I could figure out how to rewrite the code tag so I can format it the way I need it with CSS classes. >.<