~ overflow ~

Archive for July 10th, 2010

BBCode php parser class

by z3n on Jul.10, 2010, under Coding, Tips & Hints

This class is based on jquery’s markitup plugin function, i did some code compressions and added youtube handler to it. If you need base_defines function, just search the site.

class BBCodeParser {
	private $text;

	public function __construct() {
		base_defines(array(
			"EMOTICONS_DIR" => "/img/emoticons/"
		));
	}

	public function escape($s) {
		$this->text = strip_tags($this->text);
		$code = $s[1];
		$code = htmlspecialchars($code);
		$code = str_replace("[", "[", $code);
		$code = str_replace("]", "]", $code);
		return '<pre><code>'.$code.'</code></pre>';
	}	

	public function removeBr($s) {
		return str_replace("<br />", "", $s[0]);
	}

	public function parse($text) {
		$this->text = trim($text);
		$this->text = preg_replace(array(
				'/\[b\](.*?)\[\/b\]/ms',
				'/\[i\](.*?)\[\/i\]/ms',
				'/\[u\](.*?)\[\/u\]/ms',
				'/\[img\](.*?)\[\/img\]/ms',
				'/\[email\](.*?)\[\/email\]/ms',
				'/\[url\="?(.*?)"?\](.*?)\[\/url\]/ms',
				'/\[size\="?(.*?)"?\](.*?)\[\/size\]/ms',
				'/\[youtube\](.*?)\[\/youtube\]/ms',
				'/\[color\="?(.*?)"?\](.*?)\[\/color\]/ms',
				'/\[quote](.*?)\[\/quote\]/ms',
				'/\[list\=(.*?)\](.*?)\[\/list\]/ms',
				'/\[list\](.*?)\[\/list\]/ms',
				'/\[\*\]\s?(.*?)\n/ms'
			),array(
				'<strong>\1</strong>',
				'<em>\1</em>',
				'<u>\1</u>',
				'<img src="\1" alt="\1" />',
				'<a href="mailto:\1">\1</a>',
				'<a href="\1">\2</a>',
				'<span style="font-size:\1%">\2</span>',
				'<object width="450" height="350"><param name="movie" value="\1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="\1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="450" height="350"></embed></object>',
				'<span style="color:\1">\2</span>',
				'<blockquote>\1</blockquote>',
				'<ol start="\1">\2</ol>',
				'<ul>\1</ul>',
				'<li>\1</li>'
			),
			str_replace(array(
					':)',
					':D',
					':o',
					':p',
					':P',
					':(',
					';)'
				),array(
					'<img alt=":)" src="'.EMOTICONS_DIR.'emoticon-happy.png" />',
					'<img alt=":D" src="'.EMOTICONS_DIR.'emoticon-smile.png" />',
					'<img alt=":o" src="'.EMOTICONS_DIR.'emoticon-surprised.png" />',
					'<img alt=":p" src="'.EMOTICONS_DIR.'emoticon-tongue.png" />',
					'<img alt=":P" src="'.EMOTICONS_DIR.'emoticon-tongue.png" />',
					'<img alt=":(" src="'.EMOTICONS_DIR.'emoticon-unhappy.png" />',
					'<img alt=";)" src="'.EMOTICONS_DIR.'emoticon-wink.png" />'
				),
				preg_replace_callback(
					'/\[code\](.*?)\[\/code\]/ms',
					array(&$this, "escape"), // http://www.php.net/manual/en/function.preg-replace-callback.php#90849
					//"BBCodeParser::escape", // http://www.php.net/manual/en/function.preg-replace-callback.php#92548
					$this->text
				)
			)
		);

		// paragraphs
		$this->text = str_replace("\r", "", $this->text);
		$this->text = "<p>".preg_replace("/(\n){2,}/", "</p><p>", $this->text)."</p>";
		$this->text = nl2br($this->text);

		// clean some tags to remain strict
		// not very elegant, but it works. No time to do better ;)

		$this->text = preg_replace_callback('/<pre>(.*?)<\/pre>/ms', array(&$this,"removeBr"), $this->text);
		$this->text = preg_replace('/<p><pre>(.*?)<\/pre><\/p>/ms', "<pre>\\1</pre>", $this->text);

		$this->text = preg_replace_callback('/<ul>(.*?)<\/ul>/ms', array(&$this,"removeBr"), $this->text);
		$this->text = preg_replace('/<p><ul>(.*?)<\/ul><\/p>/ms', "<ul>\\1</ul>", $this->text);

		return $this->text;
	}
}
1 Comment :, , , more...

MarkItUp set em português (pt-br)

by z3n on Jul.10, 2010, under Coding, Tips & Hints

Variáveis de markitup em português com youtube

markitup_bbcode = {
  nameSpace:          "bbcode", // Useful to prevent multi-instances CSS conflict
  previewParserPath:  "~/sets/bbcode/preview.php",
  markupSet: [
      {name:'Negrito', key:'B', openWith:'[b]', closeWith:'[/b]'},
      {name:'Italico', key:'I', openWith:'[i]', closeWith:'[/i]'},
      {name:'Sublinhado', key:'U', openWith:'[u]', closeWith:'[/u]'},
      {separator:'---------------' },
      {name:'Imagem', key:'P', replaceWith:'[img][![Url]!][/img]'},
      {name:'YouTube', key:'Y', openWith:'[youtube]', closeWith:'[/youtube]', placeHolder:'[![Url do YouTube]!]'},
      {name:'Link', key:'L', openWith:'[url=[![Url]!]]', closeWith:'[/url]', placeHolder:'Coloque o texto do link aqui...'},
      {separator:'---------------' },
      {name:'Cores', openWith:'[color=[![Cor]!]]', closeWith:'[/color]', dropMenu: [
          {name:'Amarelo', openWith:'[color=yellow]', closeWith:'[/color]', className:"col1-1" },
          {name:'Laranja', openWith:'[color=orange]', closeWith:'[/color]', className:"col1-2" },
          {name:'Vermelho', openWith:'[color=red]', closeWith:'[/color]', className:"col1-3" },
          {name:'Azul', openWith:'[color=blue]', closeWith:'[/color]', className:"col2-1" },
          {name:'Roxo', openWith:'[color=purple]', closeWith:'[/color]', className:"col2-2" },
          {name:'Verde', openWith:'[color=green]', closeWith:'[/color]', className:"col2-3" },
          {name:'Branco', openWith:'[color=white]', closeWith:'[/color]', className:"col3-1" },
          {name:'Cinza', openWith:'[color=gray]', closeWith:'[/color]', className:"col3-2" },
          {name:'Preto', openWith:'[color=black]', closeWith:'[/color]', className:"col3-3" }
      ]},
      {name:'Tamanho', key:'S', openWith:'[size=[![Tamanho do texto]!]]', closeWith:'[/size]', dropMenu :[
          {name:'Grande', openWith:'[size=200]', closeWith:'[/size]' },
          {name:'Normal', openWith:'[size=100]', closeWith:'[/size]' },
          {name:'Pequeno', openWith:'[size=50]', closeWith:'[/size]' }
      ]},
      {separator:'---------------' },
      {name:'Quotes', openWith:'[quote]', closeWith:'[/quote]'},
      {separator:'---------------' },
      {name:'Limpar', className:"clean", replaceWith:function(h) { return h.selection.replace(/\[(.*?)\]/g, "") } }
   ]
};
Leave a Comment :, , , more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!