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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.