tmallen Posted August 4, 2008 Share Posted August 4, 2008 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. Quote Link to comment Share on other sites More sharing options...
tmallen Posted August 4, 2008 Author Share Posted August 4, 2008 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. Quote Link to comment Share on other sites More sharing options...
tmallen Posted August 4, 2008 Author Share Posted August 4, 2008 Also, this is for escaping/transforming output, not filtering input. Quote Link to comment Share on other sites More sharing options...
tmallen Posted August 5, 2008 Author Share Posted August 5, 2008 Any advice? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.