<?php
// Confirm MediaWiki environment
if (!defined('MEDIAWIKI')) die();

// Credits
$wgExtensionCredits['other'][] = array(
  'name'=>'BBCodeSyntax',
  'author'=>'Paul Deveau &lt;nekoouterverse@black-blade.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;
  }