BBCode Python Module II
Thursday, March 22nd, 2007It’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.