In the last few weeks I have been tinkering with a dynamic website created with Turbogears, but that's not what this blog entry is about. The website I have in mind is similar to a forum in that most of the content come from the users (can't tell you exactly what it is just yet). I wanted a way for users to post comments with simple formatting, but I didn't want to let them enter straight html - for all the problems that would cause. No doubt, some wise-guy would figure out that he could enter the <blink> tag! So I decided to implement something like BBCode, which I dubbed 'Post Markup'.

But once I came close to finishing Post Markup, I realised it was so much like BBCode, it was BBCode (which doesn't seem to have any strict definition anyway). My BBCode parser is deliberately quite relaxed, it will try to make sense of the BBCode rather than throwing errors. It will close open tags as well as handle overlapping tags so that it always produces valid XHTML snippets.

You can download postmarkup.py here. This code is 'politeware', you may use it for any purpose you want as long as you say 'thank you' and you promise not to sue me if it breaks! Let me know if you have any suggestions or bug-fixes.

Here's a quick example of basic use.

import postmarkup

markup = postmarkup.PostMarkup().default_tags()
bbcode = "[b]Hello, World![/b]"

print markup.render_to_html(bbcode)

There are comments in the module. If you have any questions, please email or (better) post a comment here so I can build up documentation.

The following is a cut and paste of the test output. It shows the basic tags (bold, italic etc) and more advanced tags.


[b]Hello[/b]
Hello
[s]Strike through[/s]
Strike through
[b]bold [i]bold and italic[/b] italic[/i]
bold bold and italic italic
[google]Will McGugan[/google]
Will McGugan [Google]
[wiki Will McGugan]Look up my name in Wikipedia[/wiki]
Look up my name in Wikipedia [Wikipedia]
[link http://www.willmcgugan.com]My homepage[/link]
My homepage [willmcgugan.com]
[link]http://www.willmcgugan.com[/link]
http://www.willmcgugan.com [willmcgugan.com]
[quote Will said...]BBCode is very cool[/quote]
Will said...

BBCode is very cool

[b]Long test[/b]

New lines characters are converted to breaks. Tags my be [b]ove[i]rl[/b]apped[/i].

[i]Open tags will be closed.
Long test

New lines characters are converted to breaks. Tags my be overlapped.

Open tags will be closed.
This blog post was posted to It's All Geek to Me on Saturday March 10th, 2007 at 3:22PM
 

16 Responses to "BBCode Python Module"

  • Jan Pöschko
    March 17th, 2007, 8:59 p.m.

    This is great! Thank you a lot!

  • March 18th, 2007, 4:29 p.m.

    BBCode Python Module...

    Will McGugan recently wrote about the BBCode python module he created.  This could useful for my (yet to be released) blog software ....

  • BatCreat
    May 30th, 2007, 10:27 a.m.

    Thank you! Your Module will be very useful for me.

  • ugo pozo
    September 6th, 2007, 2:45 a.m.

    Thank you a lot. This will save me a great amount of time!

  • Mika
    September 29th, 2007, 3:15 p.m.

    Thanks a lot! I love it!

  • October 18th, 2007, 6:23 p.m.

    Thank you, your BBCode Parser works pretty fine.
    There's a newer package around at: http://code.google.com/p/postmarkup/

  • g4b
    June 16th, 2008, 2:27 p.m.

    thanx a lot...! i can use this very well in my next django app...

    everything i now need is the same for wiki syntax and i will rock the world! :D

  • August 18th, 2008, 11:05 p.m.

    If the purpose of expanding BBCode into full HTML is to save effort, typing and learning then my favoured format for links is:

    [http://www.somelink.com/|anchor text]

    to expand into

    anchor text

    which is the method used on e2 http://everything2.com/

    I am working on my own "html shorthand to longhand converter" and your approach has been helpful... thanks.

  • August 18th, 2008, 11:06 p.m.

    ermm last post didnt work but you should get the idea

  • django
    September 14th, 2008, 7:58 p.m.

    Thanks (as required by the politeware license you stated), I'll be using this quite extensively :)

  • April 6th, 2009, 7:16 p.m.

    A Thousand Thanks (and shall flowers blossom in your garden) !

    Politeware… mmmh… neat licence for a neat work !

  • nux
    September 2nd, 2009, 11:24 p.m.

    Thanxs a lot ! i think it will be very usefull for me !

    good work !

  • December 6th, 2009, 1:04 p.m.

    Thanks much :) Will use it for our new clan page. Wondering if I should start contributing a C version of it for more speeeeeeeed :D

  • January 14th, 2010, 9:03 p.m.

    Thanks, very useful. But is there any package for transforming html back to bbcode?

  • Ahlywog
    February 25th, 2010, 4:31 a.m.

    Thanks for this. I started writing my own and was starting to get a little frustrated when a friend pointed me to this site.

  • Atkin
    July 5th, 2010, 8:39 a.m.

    This will save me a lot of time :)

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
http://www.iclshoes.com/alexander-mcqueen-c-13.html [iclshoes.com] http://www.iclshoes.com/jimmy-choo-shoes-c-2.html [iclshoes.com] http://www.iclshoes.com/ [iclshoes.com] http://www.zentai-mart.com/Play-Costumes-c-5.html [zentai-mart.com] http://www.zentai-mart.com/Latex-Catsuits-Clothes-c-3.html [zentai-mart.com] http://www.zentai-mart.com/PVC-Catsuits-Clothes-c-6.html [zentai-mart.com] http://www.zentai-mart.com/ [zentai-mart.com] http://www.hereshoes.com/miu-miu-shoes-c-31.html [hereshoes.com] http://www.hereshoes.com/giuseppe-zanotti-c-43.html [hereshoes.com] http://www.hereshoes.com/lanvin-shoes-c-50.html [hereshoes.com] ...
- Christian Louboutin on Turning website favicons in to 3D
What are the charmings of?a href="http://www.iclshoes.com [iclshoes.com]cl shoes/a?They are quality,comfort and style.The a href=http://www.iclshoes.com/jimmy-choo-shoes-c-2.html [iclshoes.com]Jimmy Choo shoes/a are made from ...
- Christian Louboutin on Turning website favicons in to 3D
Jay, the reason Creationists, and their slightly more dishonest variant intelligent design, are bit of a worry, is their tendency ...
- Shayne O'Neill on Creationists in Oxford
Thanks a lot for that: I had first tried sudo aptitude purge adobe-flashplugin then sudo aptitude install flashplugin-nonfree but that ...
Andre, the name is derived from the class name (camel case converted to lower case with underscores). But, you can ...
 
© 2008 Will McGugan.

A technoblog blog, design by Will McGugan