Page 1 of 1

Not pharsing syntax in certan blocks - Custom Syntax Parser

Posted: Fri Dec 26, 2008 7:32 am
by thetooth
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 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:
Code: [Select]

Code: Select all

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',
        ':p' => 'smileys/tongue.gif',
        ':o' => '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
Code: [Select]

Code: Select all

$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!!!