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.

This blog post was posted to It's All Geek to Me on Thursday March 22nd, 2007 at 8:36PM
 

8 Responses to "BBCode Python Module II"

  • Jesper
    May 26th, 2007, 5:55 p.m.

    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.

  • Jesper
    May 26th, 2007, 6:06 p.m.

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

  • Jesper
    May 27th, 2007, 1:51 p.m.

    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

  • May 31st, 2007, 11:09 p.m.

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

  • October 2nd, 2007, 9:17 p.m.

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

  • October 2nd, 2007, 10:45 p.m.

    Glad you like it!

    The latest version is on Google code:

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

  • Chris
    November 29th, 2007, 8:10 p.m.

    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 ":-[".

  • November 29th, 2007, 10:17 p.m.

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

Leave a Comment

You can use bbcode in the comment: e.g. [b]This is bold[/b], [url]http://www.willmcgugan.com[/url], [code python]import this[/code]
Preview Posting...
Previewing comment, please wait a moment...

My Tweets

Will McGugan

My name is Will McGugan. I am an unabashed geek, an author, a hacker and a Python expert – amongst other things!

Search for Posts
Possibly related posts
Tags
 
Popular Tags
 
Archives
2010
 
Recent Comments
This is very true, i believe that people need to be able to use a domain if they register it, ...
Hehe Thats a good tatoo idea ;) Have a nice day Joel
- Joel Shapiro on Powered by Ubuntu
Weirdly enough i am a junior dev on an internship. I am @ work and i was way too bored/out ...
I love the idea of locidesktop, and even more the minimal UI. Hope you'll keep it up!
Many thanks! I tried several solutions but the only one that worked was yours.
 
© 2008 Will McGugan.

A technoblog blog, design by Will McGugan