Extension:BBCodeSyntax

From MediaWiki.org
Jump to: navigation, search


MediaWiki extensions manual - list
Crystal Clear app error.png
BBCode for MediaWiki

Release status: unstable

BBCodeSyntax.png
Implementation Parser extension, Extended syntax
Description Adds support for BBCode syntax as used by many popular bulletin board software packages.
Author(s) Paul Deveau (KaizenNekoTalk)
Last version 0.1 (February 10, 2008)
MediaWiki 1.10
License LGPL: GNU Lesser General Public License
Download Get the code
Hooks used
ParserAfterTidy

Check usage (experimental)

Contents

[edit] Extension's Function

This extension adds some basic support for BBCodes to MediaWiki. Originally developed as a 'bridge' for users of my forum to be able to edit a MediaWiki-based wiki should they not be familiar with wiki formatting.


[edit] Extension's (Known) Limitations

Does not currently support IMG tags.

Not sure how to handle them at this time since the way I would like to do them requires wgAllowExternalImages to be enabled, and even then it's kind of dodgy.

Does not currently support HTML-style +/- and integer only sizes.

Working on a solution for this, but as of right now only CSS2.1 values are supported.

Identical tags that are nested in other tags are not parsed.

This one needs an example. [i]Some [b]test[/b][/i] text to be [i]parsed[/i]. is displayed as Some test[/i] text to be [i]parsed.


[edit] Usage

Valid BBCodes are as follows: (Note, they are NOT case-sensitive)

NOPARSE: Tells the parser not to convert any BBCodes between these tags. Useful for posting examples.
IMG: Not enabled in this version
FONT: Changes the font-face of the text.
SIZE: Changes the font-size of the text. Does not allow for HTML +/- or integer only sizes. Must be valid CSS (i.e. px, pt, %, em, etc.)
COLOR: Changes the foreground color of the text.
BGCOLOR: Changes the background color of the text. Good for highlighting.
B: Bold text.
I: Italic text.
U: Underline text.
S: Strikeout text.
SUP: Superscript.
SUB: Subscript.
HR: Insert a horizontal rule.
LINE: Insert a horizontal line. Duplicates HR because my users hate the term Horizontal Rule.


[edit] Download instructions

Please cut and paste the code found below and place it in $IP/extensions/BBCodeSyntax/BBCodeSyntax.php. Note: $IP stands for the root directory of your MediaWiki installation, the same directory that holds LocalSettings.php.

[edit] Installation

To install this extension, add the following to LocalSettings.php:

require_once("$IP/extensions/BBCodeSyntax/BBCodeSyntax.php");


[edit] Code

<?php
// Confirm MediaWiki environment
if (!defined('MEDIAWIKI')) die();
 
// Credits
$wgExtensionCredits['other'][] = array(
  'name'=>'BBCodeSyntax',
  'author'=>'Paul Deveau &lt;kaizenneko@onederangedneko.net&gt;',
  'url'=>'http://www.mediawiki.org/wiki/Extension:BBCodeSyntax',
  'description'=>'Adds support for BBCode syntax as used by many popular bulletin board software packages, along with a few I use on my site often.',
  'version'=>'0.1'
  );
 
// Attach Hook
$wgHooks['ParserAfterTidy'][] = 'wfBBCodeSyntax';
 
function wfBBCodeSyntax(&$parser, &$text) {
    // noparse - Render raw BBCode without being parsed
    $holdNP = $text;
    preg_match('%\[noparse\](.+)\[/noparse\]%i', $holdNP, $matches);
    $workNP = $matches[1];
    $workNP = preg_replace('/\[/i', '&#91;', $workNP);
    $workNP = preg_replace('/\]/i', '&#93;', $workNP);
    $text = preg_replace('%\[noparse\](.+)\[/noparse\]%i', $workNP, $text);
 
    // img - Renders an image.  However, this doesn't quite the way it should.  The image will not be displayed at all if wgAllowExternalImages is not set to true.
    // On the otherhand, setting this to true causes ALL external images to be displayed regardless of the noparse tag.
    // NOTE: This is disabled for now.  I am not sure how I want to handle the tag since this method requires a configuration file change.
 
    //$text = preg_replace('%\[img\]([^][]+)\[/img\]%i', '$1', $text);
 
    // font - Changes the font face
    $text = preg_replace('%\[font=([^][]+)\](.+)\[/font\]%i', '<span style="font-family:$1;font-size:inherit;color:inherit;background-color:inherit;">$2</span>', $text);
 
    // size - Changes the font size.  Can be any acceptable CSS declaration but does NOT accept HTML +, - and integer only values.
    $text = preg_replace('%\[size=([^][]+)\]([^][]+)\[/size\]%i', '<span style="font-family:inherit;font-size:$1;color:inherit;background-color:inherit;">$2</span>', $text);
 
    // color - Colorizes text.  Accepts named colors, hex, and RGB notaion.
    $text = preg_replace('%\[color=([^][]+)\](.+)\[/color\]%i', '<span style="font-family:inherit;font-size:inherit;color:$1;background-color:inherit;">$2</span>', $text);
 
    // bgcolor - Colorizes the background of text.  Accepts named colors, hex, and RGB notaion.
    $text = preg_replace('%\[bgcolor=([^][]+)\](.+)\[/bgcolor\]%i', '<span style="font-family:inherit;font-size:inherit;color:inherit;background-color:$1;">$2</span>', $text);
 
    // b - Bold text.
    $text = preg_replace('%\[b\](.+)\[/b\]%i', '<b>$1</b>', $text);
 
    // i - Italics.
    $text = preg_replace('%\[i\](.+)\[/i\]%i', '<i>$1</i>', $text);
 
    // u - Underline
    $text = preg_replace('%\[u\](.+)\[/u\]%i', '<u>$1</u>', $text);
 
    // s - Strikeout
    $text = preg_replace('%\[s\](.+)\[/s\]%i', '<span style="text-decoration: line-through;">$1</span>', $text);
 
    // sup - Superscript
    $text = preg_replace('%\[sup\](.+)\[/sup\]%i', '<span style="vertical-align: super; font-size: .65em;">$1</span>', $text);
 
    // sub - Subscript
    $text = preg_replace('%\[sub\](.+)\[/sub\]%i', '<span style="vertical-align: sub; font-size: .65em;">$1</span>', $text);
 
    // hr & line - Horizontal Rule
    // LINE does the exact same thing as HR, but my users decided to buck HTML and voted to use LINE in it's place.
    $text = preg_replace('%\[hr\]%i', '<hr />', $text);
    $text = preg_replace('%\[line\]%i', '<hr />', $text);
 
    // Make sure that new lines if copied from a bulletin board are not parsed by MediaWiki as a <p>aragraph
    $text = preg_replace('/(\r|\n)/i', '<br />', $text);
 
    return true;
  }
Personal tools
Namespaces
Variants
Actions
Site
Support
Download
Development
Communication
Print/export
Toolbox