Jump to content

Forum Extension badly in need of refactoring...


tmallen

Recommended Posts

This extension loads some stylesheets and scripts, then extends the StringFormatter class to provide my own forum post parser. The parser does only four things:

 

1,2. Replace < with < and > with >

3. Replace

[code/] with <pre title="code"/>, and
 with <pre title="code" class="$1"/>
4. Replace all newlines not occurring in a "pre" block with an HTML line break

Judging by how complex the Regex needed to be for step four, I know I'm doing something very wrong. Here's the class:

[code]<?php
class SyntaxHighlighterFormatter extends StringFormatter {
    function Parse($String, $Object, $FormatPurpose) {
        if ($FormatPurpose == FORMAT_STRING_FOR_DISPLAY) {
            $hlString = $String;
            // Steps 1 and 2
            $hlString = str_replace('<', '<', $hlString);
            $hlString = str_replace('>', '>', $hlString);
            // Step 3
            $hlString = preg_replace("/\[code( lang=\"([A-Za-z]+)\")?\]/", "<pre title=\"code\" class=\"$2\">", $hlString);
            // I had to remove part of the close "code" tag because the PHPFreaks forum was
            // butchering the code otherwise.
            $hlString = str_replace('/code]', '</pre>', $hlString);
            // Step 4
            $hlString = preg_replace("#(?:\r\n|[\r\n])(?=(?:[^<]|<(?!/pre))*(?:<pre|\Z))#", "<br />", $hlString);
            return $hlString;
        }
        // This extension only needs to deal with 'FORMAT_STRING_FOR_DISPLAY'
        else {
            return $String;
        }
    }
}
?>

 

Any advice? Specifically, I'm interested in the best way to parse the Post string, as I'm sure that my current method is impractical. Note that it does execute steps 1-4 perfectly every time, so nothing's functionally broken.

Edit: I've replaced the manual < and > substitution lines with the simpler:

 

<?php             
$hlString = htmlspecialchars($String, ENT_NOQUOTES);
?>

 

It seems to be working fine.

 

A big hurdle for me is that I want to further build this parser. I'd like it to, at the very least, convert URLs to links and maybe even add some basic HTML/markdown/BBCode support.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.