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.

8 Responses to “BBCode Python Module II”

  1. Jesper Says:

    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.

  2. Jesper Says:

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

  3. Jesper Says:

    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

  4. Will Says:

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

  5. David Cramer Says:

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

  6. Will Says:

    Glad you like it!

    The latest version is on Google code:

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

  7. Chris Says:

    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 “:-[”.

  8. Will Says:

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

Leave a Reply


Close
E-mail It
Socialized through Gregarious 42