Jump to content

Module: BBCode Text Formatter


Recommended Posts

We already have Markdown and Textile text formatters, so I thought we should have BBCode too. BBCode is a good text formatter to use when you'll be outputting untrusted user-supplied input. I think it's also a little simpler and more widely understood than Markdown and Textile, since it is used by many forums (including the ProcessWire forum, before we switched to IP.Board). More about the BBCode format can be found here: http://en.wikipedia.org/wiki/BBCode

This module uses the NBBC Library to handle BBCode parsing. I selected this one because it seems to be really well documented and also includes it's own set of smileys, for those that like that sort of thing. :rolleyes:

To install, clone it from the TextformatterBBCode GitHub page or download the ZIP file.

  • Like 1
Link to comment
Share on other sites

Nice addition - could be very useful for user comment fields.

You can still type bbcode into these forums too by the way, just the editor makes it a little pointless really.

Link to comment
Share on other sites

  • 10 months later...
So if you can't use this in a field set as TinyMCE (or CKEditor) how would one place/use the BBCode from Flickr for example?

Not sure I understand the question. What BBCode from Flickr? :)

Link to comment
Share on other sites

On Flickr you can either choose HTML embed code or BBCode.

Here's a BBCode example:



    ]Pike reflection[/url] by [url=http://www.flickr.com/people/sparth/]sparth[/url], on Flickr

Here's the HTML example:

<a href="

    " title="Pike reflection by sparth, on Flickr"><img src="http://farm6.staticflickr.com/5506/9061328126_bac63857c2_z.jpg" width="640" height="426" alt="Pike reflection"></a>

I had that thought as well about using Hanna Code for this but couldn't figure out how to a) get that image url out of that BBCode and b) get around TinyMCE stripping it out completely.

Link to comment
Share on other sites

Hanna code probably wouldn't work here looking at the code. We could write a Hanna code to do it, but you'd have to specify the href and src differently, like this:
[[flickr href="

    Pike reflection" src="http://farm6.staticflickr.com/5506/9061328126_bac63857c2_z.jpg"]]
It would be easy to make a Hanna code to do this, but of course you couldn't copy/paste a BBCode directly from Flickr using that method. So that kind of defeats the convenience purpose. You also probably don't want to apply the existing BBCode text formatter because it'll strip out your HTML. A new textformatter would be needed, that does nothing but convert BBCode without stripping HTML. Or, you could simply do this before outputting your $body field:
$body = $page->body;
if(strpos($body, '[url=') !== false) {
  $body = preg_replace(
    '<a href="$1"><img src="$2" alt=""></a>', 
echo $body;  

Not sure how we'd determine attributes like title, alt, width and height (or if you even need it). I'm guessing that would have to use an oEmbed service like the TextformatterVideoEmbed module does. 

Link to comment
Share on other sites

  • 6 years later...

For anyone using this (not many i guess), I have updated the module for compatibility with newer versions of PHP.


More info at the repo, but in short, this uses the new Vanilla Forums maintained version of Nbbc. The module is also PW 3.x only, as it uses namespaces.

One item of possible interest to module developers in this version is the use of the composer file loader (https://github.com/Wilkins/composer-file-loader) which allows the vendor folder to be loaded without any modification, and without needing to use Composer, sort of an on-the-fly package loader...


With any newer version of PHP, and where the current/old version of module is instantiated, the following error was occurring.

PHP Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; BBCodeLexer has a deprecated constructor in .../site/assets/cache/FileCompiler/site/modules/TextformatterBBCode/nbbc.php:67
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...