integrate spoiler into bbcode

git-svn-id: file:///home/shish/svn/shimmie2/trunk@766 7f39781d-f577-437e-ae19-be835c7a54ca
This commit is contained in:
shish 2008-04-06 18:41:36 +00:00
parent 7b1eed5ce0
commit 73ba66fbc8
2 changed files with 28 additions and 44 deletions

View file

@ -1,44 +0,0 @@
<?php
/**
* Name: Spoiler Filter
* Author: Shish <webmaster@shishnet.org>
* Link: http://trac.shishnet.org/shimmie2/
* License: GPLv2
* Description: Adds a [spoiler] tag to rot13 text inside it
*/
class Spoiler extends Extension {
public function receive_event($event) {
if(is_a($event, 'TextFormattingEvent')) {
$event->formatted = $this->filter($event->formatted);
$event->stripped = $this->strip($event->stripped);
}
}
private function filter($text) {
return str_replace(
array("[spoiler]","[/spoiler]"),
array("<span style=\"background-color:#000; color:#000;\">","</span>"),
$text);
}
private function strip($text) {
$l1 = strlen("[spoiler]");
$l2 = strlen("[/spoiler]");
while(true) {
$start = strpos($text, "[spoiler]");
if($start === false) break;
$end = strpos($text, "[/spoiler]");
if($end === false) break;
$beginning = substr($text, 0, $start);
$middle = str_rot13(substr($text, $start+$l1, ($end-$start-$l1)));
$ending = substr($text, $end + $l2, (strlen($text)-$end+$l2));
$text = $beginning . $middle . $ending;
}
return $text;
}
}
add_event_listener(new Spoiler(), 45); // before bbcode, so before <br>s are inserted
?>

View file

@ -30,6 +30,7 @@ class BBCode extends Extension {
$text = preg_replace("/\[li\](.*?)\[\/li\]/s", "<li>\\1</li>", $text);
$text = preg_replace("#\[\*\]#s", "<li>", $text);
$text = preg_replace("#<br><(li|ul|ol|/ul|/ol)>#s", "<\\1>", $text);
$text = $this->filter_spoiler($text);
return $text;
}
@ -51,6 +52,33 @@ class BBCode extends Extension {
$text = preg_replace("/\[ol\](.*?)\[\/ol\]/s", "\\1", $text);
$text = preg_replace("/\[li\](.*?)\[\/li\]/s", "\\1", $text);
$text = preg_replace("/\[\*\](.*?)/s", "\\1", $text);
$text = $this->strip_spoiler($text);
return $text;
}
private function filter_spoiler($text) {
return str_replace(
array("[spoiler]","[/spoiler]"),
array("<span style=\"background-color:#000; color:#000;\">","</span>"),
$text);
}
private function strip_spoiler($text) {
$l1 = strlen("[spoiler]");
$l2 = strlen("[/spoiler]");
while(true) {
$start = strpos($text, "[spoiler]");
if($start === false) break;
$end = strpos($text, "[/spoiler]");
if($end === false) break;
$beginning = substr($text, 0, $start);
$middle = str_rot13(substr($text, $start+$l1, ($end-$start-$l1)));
$ending = substr($text, $end + $l2, (strlen($text)-$end+$l2));
$text = $beginning . $middle . $ending;
}
return $text;
}
}