summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/BBCodeParser.php22
-rw-r--r--test/BBCodeParserTest.php3
2 files changed, 18 insertions, 7 deletions
diff --git a/src/BBCodeParser.php b/src/BBCodeParser.php
index 85f4cc7..ddec8f7 100644
--- a/src/BBCodeParser.php
+++ b/src/BBCodeParser.php
@@ -11,8 +11,9 @@ class BBCodeParser {
function parse($in, $uid) {
+ $quote_stack = array();
+
$arg_stack = array();
- $contents_stack = array();
$fn_number = 1;
$fn = array();
@@ -20,8 +21,7 @@ class BBCodeParser {
$i = 0;
$len = strlen($in);
- $indent = '';
- $list_conter_stack = array();
+ $list_counter_stack = array();
$out = '';
@@ -83,12 +83,14 @@ class BBCodeParser {
# nothing to do on opening
break;
case 'quote':
+ $quote_stack[] = $out;
+ $out = '';
break;
case 'code':
+ $out .= "\n";
break;
case 'list':
$out .= "\n";
- $indent .= ' ';
switch ($arg) {
case '1': $list_counter_stack[] = 1; break;
@@ -98,7 +100,7 @@ class BBCodeParser {
break;
case '*':
- $out .= "\n" . $indent;
+ $out .= "\n" . str_repeat(' ', count($list_counter_stack));
$c = array_pop($list_counter_stack);
if ($c == '*') {
@@ -151,15 +153,23 @@ class BBCodeParser {
}
break;
case 'quote':
+ $level = count($quote_stack);
+ $out = wordwrap($out, 72 - 2*$level);
+ $out = str_replace("\n", "\n> ", $out);
+ $out = '> ' . $out;
+ $out = array_pop($quote_stack) . $out;
break;
case 'code':
+ $out .= "\n";
break;
case 'list':
$out .= "\n";
- $indent = substr($indent, -1);
array_pop($list_counter_stack);
break;
case '*':
+ if ($in[$i] != "\n") {
+ $out .= "\n";
+ }
break;
case 'img':
break;
diff --git a/test/BBCodeParserTest.php b/test/BBCodeParserTest.php
index f1047f9..d3be5cb 100644
--- a/test/BBCodeParserTest.php
+++ b/test/BBCodeParserTest.php
@@ -18,7 +18,8 @@ class BBCodeParserTest extends PHPUnit_Framework_TestCase {
return array(
array('', '3i2cqt66', '', null),
array('[b:3i2cqt66]This is a[/b:3i2cqt66] test of [i:3i2cqt66]the BBCode[/i:3i2cqt66] parser. Will [u:3i2cqt66][i:3i2cqt66]it[/i:3i2cqt66][/u:3i2cqt66] parse? Also, throw in some difficult characters: 1 < 2 < 4 > 3.', '3i2cqt66', '__This is a__ test of _the BBCode_ parser. Will __it__ parse? Also, throw in some difficult characters: 1 < 2 < 4 > 3.', null),
- array("Foo\n[list=2:11cx3qbi]\n[*:11cx3qbi] first[/*:m:11cx3qbi]\n[*:11cx3qbi] second[/*:m:11cx3qbi][/list:o:11cx3qbi]\nBar", '11cx3qbi', "Foo\n\n * first\n * second\n\nBar", null)
+ array("[quote:2nnqpmcp]Here's some quoty stuff.[quote:2nnqpmcp]Followed by an even deeper quote.[/quote:2nnqpmcp]Followed by more stuff.[/quote:2nnqpmcp]", '2nnqpmcp', "> Here's some quoty stuff.\n> > Followed by an even deeper quote.\n> Followed by more stuff.", null),
+# array("Foo\n[list:11cx3qbi]\n[*:11cx3qbi] first[/*:11cx3qbi]\n[*:11cx3qbi] second[/*:11cx3qbi][/list:o:11cx3qbi]\nBar", '11cx3qbi', "Foo\n\n * first\n * second\n\nBar", null)
);
}
}