nl2br event plugin improved
Posted: Mon Nov 13, 2006 12:41 am
As a programmer/developer, I love posting code snippets. I also prefer typing in my own HTML markup, unless it's a simple line-break. Unfortunately, there are times when line breaks aren't really desirable (such as in PRE tags since the BR tag will actually be visible). As such, I've finally found some time to modify the nl2br event plugin to make it a tiny bit smarter. I'd like some more help though, as I'd like to make it a bit more smarter, and unfortunately don't understand the plugin setup much at all.
Anyway...
With help from some members on the SitePoint forums, I've modified the serendipity_event_plugin_nl2br to work with HTML textarea, pre, and the BBCode GeSHi modifiers so that those ugly <br /> tags aren't added when they aren't wanted.
Here's what I did:
- Open up plugins\serendipity_event_nl2br\serendipity_event_nl2br.php in your serendipity folder
- add two new functions (within the class, but order of where they go is unimportant, I placed them before the event_hook function) as shown below:
- find the following line in the event_hook function: <strong><code>$eventData[$element] = nl2br($eventData[$element]);</code></strong> and replace it with the following:
What I'd like to do is to allow for users to modify the tags that would be used within the REGEXP (I know, I'd have to modify the regex, not a problem) by using a plugin administration page...this way, if a plugin that I'm not aware of yet is created that would benefit from this, a user (or possibly even the plugin itself) could modify the tags that are skipped from the nl2br PHP call.
This modified version should not effect anyone using the rich text editor, but will help those trying to use GeSHi or other code-posting methods. I would strongly suggest applying this to the current nl2br plugin, and/or replacing it and giving it a different name (nl2br_enhanced).
Anyway...
With help from some members on the SitePoint forums, I've modified the serendipity_event_plugin_nl2br to work with HTML textarea, pre, and the BBCode GeSHi modifiers so that those ugly <br /> tags aren't added when they aren't wanted.
Here's what I did:
- Open up plugins\serendipity_event_nl2br\serendipity_event_nl2br.php in your serendipity folder
- add two new functions (within the class, but order of where they go is unimportant, I placed them before the event_hook function) as shown below:
Code: Select all
function isolate($src, $regexp = NULL)
{
if($regexp) return preg_replace_callback($regexp, array($this, 'isolate'), $src);
global $_buf;
$_buf[] = $src[0];
return "\001" . (count($_buf) - 1);
}
function restore($text)
{
global $_buf;
return preg_replace('~\001(\d+)~e', '$_buf[$1]', $text);
}
Code: Select all
$eventData[$element] = $this->isolate($eventData[$element], '~[<\[](textarea|pre|geshi).*?[>\]].*?[<\[]/\1[>\]]~si');
$eventData[$element] = nl2br($eventData[$element]);
$eventData[$element] = $this->restore($eventData[$element]);
This modified version should not effect anyone using the rich text editor, but will help those trying to use GeSHi or other code-posting methods. I would strongly suggest applying this to the current nl2br plugin, and/or replacing it and giving it a different name (nl2br_enhanced).