sfBBCodeParserPlugin - 1.0.2

This is a plugin to parse BBCode, it is based on the HTML_BBCodeParser PEAR library

You are currently browsing
the website for symfony 1

Visit the Symfony2 website


« Back to the Plugins Home

Signin


Forgot your password?
Create an account

Tools

Stats

advanced search
Information Readme Releases Changelog Contribute
Show source

sfBBCodeParserPlugin

The sfBBCodeParserPlugin allows you to parse BBCode and get the corresponding html output. There are 6 available filters which provide the following tags:

  • Basic (b, i, u, s, sub, sup)
  • Extended (color, size, font, align, quote, code, h1 to h6)
  • Images (img)
  • Links (url)
  • Lists (list, ulist, li)
  • Email (email)

You can activate the filters you want. For each filter you can also decide which tags can be used. And finally you can make your own filters with your own tags. Almost everything can be configured in the plugin, filters, tags, attributes, opening and closing character...

The plugin is based on the PEAR library HTML_BBCodeParser but the PEAR dependencies were removed so you don't have to install it in order to use the plugin.

Installation

  • Install the plugin

    $ symfony plugin:install sfBBCodeParserPlugin
    

(or download and unzip in your /plugins directory or checkout with SVN)

  • Clear your cache

    $ symfony cc
    

Configuration

The plugins comes with 2 configurations files:

  • config/bb_code_parser_config.yml: contains the main configuration

    config:
      quotestyle: "double"
      quotewhat:  "all"
      open:       "["
      close:      "]"
      xmlclose:   true
      filters:    "Basic,Extended,Images,Links,Lists,Email"
    
  • config/bb_code_parser_tags.yml: contains the configuration for the filters and their tags.

    filters:
      Basic:
        b:
          htmlopen:   "strong"
          htmlclose:  "strong"
          allowed:    "all"
          attributes: { }
        i:
          htmlopen:   "em"
          htmlclose:  "em"
          allowed:    "all"
          attributes: { }
     # ...
    

To customize the configuration, just copy these 2 files into the config folder of your project or your application. They will automatically override the one provided by the plugin. Then, feel free to add/delete filters, add/delete tags...

Usage

  • Instantiate a sfBBCodeParser object:

    // actions.class.php
    $this->bb_parser = new sfBBCodeParser();
  • Then use the qparse function of the object:

    // indexSuccess.php
    <h1>sfBBCodeParser demo</h1>
     
    <table>
      <tr>
        <td colspan="3"><h2>Basic filter</h2></td>
      </tr>
      <tr>
        <td>[b]Bold[/b]</td>
        <td>:</td>
        <td><?php echo $bb_parser->qparse('[b]Bold[/b]'); ?></td>
      </tr>
      <tr>
        <td>[i]Italic[/i]</td>
        <td>:</td>
        <td><?php echo $bb_parser->qparse('[i]Italic[/i]'); ?></td>
      </tr>
      <tr>
        <td>[i]Underline[/i]</td>
        <td>:</td>
        <td><?php echo $bb_parser->qparse('[i]Underline[/i]'); ?></td>
      </tr>
      <tr>
        <td>[s]Strike[/s]</td>
        <td>:</td>
        <td><?php echo $bb_parser->qparse('[s]Strike[/s]'); ?></td>
      </tr>
      <tr>
        <td>Normal [sub]Sub[/sub]</td>
        <td>:</td>
        <td><?php echo $bb_parser->qparse('Normal [sub]Sub[/sub]'); ?></td>
      </tr>
      <tr>
        <td>Normal [sup]Sup[/sup]</td>
        <td>:</td>
        <td><?php echo $bb_parser->qparse('Normal [sup]Sup[/sup]'); ?></td>
      </tr>
      <tr>
        <td colspan="3"><hr/><h2>Extended filter</h2></td>
      </tr>
      <tr>
        <td>[color="#FF7F50"]Color #FF7F50[/color]</td>
        <td>:</td>
        <td><?php echo $bb_parser->qparse('[color="#FF7F50"]Color #FF7F50[/color]'); ?></td>
      </tr>
      <tr>
        <td>[size="18"]Size 18[/size]</td>
        <td>:</td>
        <td><?php echo $bb_parser->qparse('[size="18"]Size 18[/size]'); ?></td>
      </tr>
      <tr>
        <td>[font="Tahoma"]font Tahoma[/font]</td>
        <td>:</td>
        <td><?php echo $bb_parser->qparse('[font="Tahoma"]font Tahoma[/font]'); ?></td>
      </tr>
      <tr>
        <td>[align="center"]Align center[/align]</td>
        <td>:</td>
        <td><?php echo $bb_parser->qparse('[align="center"]Align center[/align]'); ?></td>
      </tr>
      <tr>
        <td>[code]$php = "good"[/code]</td>
        <td>:</td>
        <td><?php echo $bb_parser->qparse('[code]$php = "good"[/code]'); ?></td>
      </tr>
      <tr>
        <td>[quote="COil"]This is a good plugin ! :)[/quote]</td>
        <td>:</td>
        <td><?php echo $bb_parser->qparse('[quote="COil"]This is a good plugin ! :)[/quote]'); ?></td>
      </tr>
      <tr>
        <td>[h1]Title 1[/h1]</td>
        <td>:</td>
        <td><?php echo $bb_parser->qparse('[h1]Title 1[/h1]'); ?></td>
      </tr>
      <tr>
        <td>[h2]Title 2[/h2]</td>
        <td>:</td>
        <td><?php echo $bb_parser->qparse('[h2]Title 2[/h2]'); ?></td>
      </tr>
      <tr>
        <td>[h3]Title 3[/h3]</td>
        <td>:</td>
        <td><?php echo $bb_parser->qparse('[h3]Title 3[/h3]'); ?></td>
      </tr>
      <tr>
        <td>[h4]Title 4[/h4]</td>
        <td>:</td>
        <td><?php echo $bb_parser->qparse('[h4]Title 4[/h4]'); ?></td>
      </tr>
      <tr>
        <td>[h5]Title 5[/h5]</td>
        <td>:</td>
        <td><?php echo $bb_parser->qparse('[h5]Title 5[/h5]'); ?></td>
      </tr>  
      <tr>
        <td>[h6]Title 6[/h6]</td>
        <td>:</td>
        <td><?php echo $bb_parser->qparse('[h6]Title 6[/h6]'); ?></td>
      </tr>
      <tr>    
        <td colspan="3"><hr/><h2>Images</h2></td>
      </tr>
      <tr>
        <td>
          [img 
            src="http://www.symfony-project.org/downloads/logos/symfony.gif" 
            alt="Symfony_logo" 
            title="Symfony_rocks!"][/img]</td>
        <td>:</td>
        <td><?php echo $bb_parser->qparse('[img src="http://www.symfony-project.org/downloads/logos/symfony.gif" alt="Symfony_logo" title="Symfony_rocks!"][/img]'); ?></td>
      </tr>
      <tr>
        <td colspan="3"><hr/><h2>Links</h2></td>
      </tr>
      <tr>
        <td>
        [url 
          href="http://www.symfony-project.org/plugins/sfBBCodeParserPlugin"]
            Back to plugin homepage
        [/url]
        </td>
        <td>:</td>
        <td><?php echo $bb_parser->qparse('[url href="http://www.symfony-project.org/plugins/sfBBCodeParserPlugin"]Back to plugin homepage[/url]'); ?></td>
      </tr>
      <tr>
        <td colspan="3"><hr/><h2>Lists</h2></td>
      </tr>
      <tr>
        <td>[list][*]Element 1 [*]Element 2[/list]</td>
        <td>:</td>
        <td><?php echo $bb_parser->qparse('[list][*]Element 1 [*]Element 2[/list]'); ?></td>
      </tr>
      <tr>
        <td>[ulist][*]Element 1 [*]Element 2[/list]</td>
        <td>:</td>
        <td><?php echo $bb_parser->qparse('[ulist][*]Element 1 [*]Element 2[/ulist]'); ?></td>
      </tr>
      <tr>
        <td>[li]Element 1[/li][li]Element 2[/li]</td>
        <td>:</td>
        <td><?php echo $bb_parser->qparse('[li]Element 3[/li][li]Element 4[/li]'); ?></td>
      </tr>
      <tr>
        <td colspan="3"><hr/><h2>Email</h2></td>
      </tr>
      <tr>
        <td>[email="toto@domain.com"]toto@domain.com[/email]</td>
        <td></td>
        <td><?php echo $bb_parser->qparse('[email="toto@domain.com"]toto@domain.com[/email]'); ?></td>
      </tr>
    </table>        

If your escaping strategy is set to on: (this is the default case for symfony 1.3 and 1.4)

# settings.yml
# Output escaping settings
escaping_strategy:      on   # Determines how variables are made ...

You will have to call the getRawValue() function on your parser object in order not to have the output escaped:

<?php echo $bb_parser->getRawValue()->qparse('[b]Bold[/b]'); ?>

Extend the plugin

If you want to create your own filters, let's say the filter called myFilter:

  • Create the class `sfBBCodeParser_Filter_myFilter``in your lib/ directory
  • Add this filter in the config/bb_code_parser_config.yml file
  • Add the related tags in the config/bb_code_parser_tags.yml

To know how to implement your filter class, look at the standard filter classes like sfBBCodeParser_Filter_Email provided by the plugin and check the library API on the PEAR website.

Demo

If you want to see the demo, enable the sfBBCodeParser module in your settings.yml file, then call the /sfBBCodeParser URL. (If you have desactivated the default symfony routes, a routing.yml file is included in the config folder of the plugin. (copy/paste it in the routing.yml of your application.

You can also check the live demo on my symfony blog.

If the demo doesn't work, check the note above about the escaping strategy of your application.

TODO

Support

Please report bugs on the symfony TRAC, I can also answer if you ask on the symfony mailing list.

See you. COil :)

Changelog

Check the changelog TAB

Bugs

  • The img tag doesn't allow to have spaces in alt or title attributes.

This plugin is sponsored by SQL Technologies

SQL Technologies