Jump to content

Not pharsing syntax in certan blocks - Custom Syntax Parser


Recommended Posts

Hi i am working on my very own cms so far its almost complete but one thing that still has a few issues is the syntax parser. the thing is it works fine but it works to well :P basicly what i need is to be able to insert say #code[some code here] and any syntax that the parser is designed to parse simply wont be but everything below and above it will function normaly.

 

parser class snippent:

class Parser{
//Wiki Syntax
var $sand = array(
	'===' => 'h2',
	'==' => 'h3'
);
//Link System
var $internal = array(
	'\-' => array('a href')
);
//BBCode Tags
var $tags = array(
	'b' => 'strong',
	'i' => 'em',
	'u' => 'span style="text-decoration:underline"',
	'quote' => 'blockquote',
	's' => 'span style="text-decoration: line-through"',
	'list' => 'ul',
	'\*' => 'li',
	'code' => 'pre'
);
//Tags that must be mapped to diffierent parts
var $mapped = array(
	'url' => array('a','href',true),
	'img' => array('img','src',false)
);
//Tags with atributes
var $tags_with_att = array('color' => array('font','color'),'size' => array('font','size'),'url' => array('a','href'));
//Gotta have smilies
var $smilies = array(
	'' => 'smileys/smile.gif',
	'' => 'smileys/sad.gif',
	'XD' => 'smileys/biggrin.gif',
	':"(' => 'smileys/cry.gif',
	'' => 'smileys/tongue.gif',
	'' => 'smileys/suprised.gif',
	':@' => 'smileys/mad.gif',
	':|' => 'smileys/confused.gif'
);
//Config Variables
//Convert new line charactes to linebreaks?
var $convert_newlines = false;
//Parse For smilies?
var $parse_smilies = true;
//auto link urls(http and ftp), and email addresses?
var $auto_links = true;
//Internal Storage
var $_code = '';
function BB_Code($new=true,$parse=true,$links=true){
	$this->convert_newlines = $new;
	$this->parse_smilies = $parse;
	$this->auto_links = $links;
}
function parse($code){
	$this->_code = $code;
	//$this->_strip_html();// Maybe you will need this...
	$this->_parse_sand();
	$this->_parse_internal();
	$this->_parse_tags();
	$this->_parse_mapped();
	$this->_parse_tags_with_att();
	$this->_parse_smilies();
	$this->_parse_links();
	$this->_convert_nl();
	return $this->_code;
}
function _strip_html(){
	$this->_code = strip_tags($this->_code);
}
function _convert_nl(){
	if($this->convert_newlines){
		$this->_code = nl2br($this->_code);
	}
}
function _parse_sand(){
	foreach($this->sand as $old=>$new){
		$ex = explode(' ',$new);
		$this->_code = preg_replace('/'.$old.'(.+?)'.$old.'/is','<'.$new.'>$1</'.$ex[0].'>',$this->_code);
	}
}
function _parse_internal(){
	foreach($this->internal as $tag=>$data){
		//Use URL rewrite engine?
		$rewrite = false;
		if($rewrite == true){
			$this->_code = preg_replace('/\['.$tag.'(.+?)\|(.+?)\]/is','<'.$data[0].'="/$1">$2</a>',$this->_code);
		}else{
			$this->_code = preg_replace('/\['.$tag.'(.+?)\|(.+?)\]/is','<'.$data[0].'="?page=$1">$2</a>',$this->_code);
		}
		//$this->_code = preg_replace('/\['.$tag.'(.+?)\]/is','<'.$data[0].'="/$1">$1</a>',$this->_code);//FIX ME
	}
}
function _parse_tags(){
	foreach($this->tags as $old=>$new){
		$ex = explode(' ',$new);
		$this->_code = preg_replace('/\['.$old.'\](.+?)\[\/'.$old.'\]/is','<'.$new.'>$1</'.$ex[0].'>',$this->_code);
	}
}
function _parse_mapped(){
	foreach($this->mapped as $tag=>$data){
		$reg = '/\['.$tag.'\](.+?)\[\/'.$tag.'\]/is';
		if($data[2]){
			$this->_code = preg_replace($reg,'<'.$data[0].' '.$data[1].'="$1">$1</'.$data[0].'>',$this->_code);
		}
		else{
			$this->_code = preg_replace($reg,'<'.$data[0].' '.$data[1].'="$1">',$this->_code);
		}
	}
}
function _parse_tags_with_att(){
	foreach($this->tags_with_att as $tag=>$data){
		$this->_code = preg_replace('/\['.$tag.'=(.+?)\](.+?)\[\/'.$tag.'\]/is','<'.$data[0].' '.$data[1].'="$1">$2</'.$data[0].'>',$this->_code);
	}
}
function _parse_smilies(){
	if($this->parse_smilies){
		foreach($this->smilies as $s=>$im){
			$this->_code = str_replace($s,'<img src="'.$im.'" alt="" />',$this->_code);
		}
	}
}
function _parse_links(){
	if($this->auto_links){
		$this->_code = preg_replace('/([^"])(http:\/\/|ftp:\/\/)([^\s,]*)/i','$1<a href="$2$3">$2$3</a>',$this->_code);
		$this->_code = preg_replace('/([^"])([A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4})/i','$1<a href="mailto:$2">$2</a>',$this->_code);
	}
}
function addSand($old,$new){
	$this->tags[$old] = $new;
}
function addinternal($bb,$html,$att){
	$this->internal[$bb] = array($html,$att);
}
function addTag($old,$new){
	$this->tags[$old] = $new;
}
function addMapped($bb,$html,$att,$end=true){
	$this->mapped[$bb] = array($html,$att,$end);
}
function addTagWithAttribute($bb,$html,$att){
	$this->tags_with_att[$bb] = array($html,$att);
}
function addSmiley($code,$src){
	$this->smilies[$code] = $src;
}
}

 

As you can see all it really dose is gos though the code and replaces the tags with there html alteritives but the thing is i want to be able to add a function to it that will protect certan sections of the code from being parsed.

 

the page is call with file_get_contents so here is what i use to call it below

$full = $config["path"] . $page . $config["ext"];
if (!is_file($full) || is_dir($full) || !file_exists($full)){
echo $fail['404'];
} else { // Send to parser or render page contents //
if(!$_REQUEST['act'] == 'Edit'){
	if($config["syntax"] == 'Enabled'){
    		$raw_syntax = file_get_contents($full);
		$_syntax = new Parser();
		$res = $_syntax->parse($raw_syntax);
		echo $res;
	}else{
		require_once($full);
	}
}
}

 

if you solve this for me many many thanks!!!

 

Perhaps something along the lines of HTML's <pre> tag? Why not [pre]...[/pre] ??

 

A

 

<pre> wont work because the parser dosen't listen to html it just maches srtings i need a defenit way of getting it to not do it it vertan areas

Whoops, my post was supposed to say.. "Why not [ pre ]...[ /pre ] ??"

 

.. But I see what you mean now, in a sense the parser is 'greedy' and wants to parse everything. I've never done anything like this so totally going off an idea, but why not extract everything between [pre ][ /pre] tags into an array, replacing with some special identifier such as "::pre::", parsing the rest of the code, then using str_replace() to replace the occurences of ::pre:: with your array?? Technically it should work, but somebody else might have a better idea?

 

A

Whoops, my post was supposed to say.. "Why not [ pre ]...[ /pre ] ??"

 

.. But I see what you mean now, in a sense the parser is 'greedy' and wants to parse everything. I've never done anything like this so totally going off an idea, but why not extract everything between [pre ][ /pre] tags into an array, replacing with some special identifier such as "::pre::", parsing the rest of the code, then using str_replace() to replace the occurences of ::pre:: with your array?? Technically it should work, but somebody else might have a better idea?

 

A

 

yes thats the answer but i am a noob at php so doing that is almost imposable can you help a little with the coding of such a function?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.