diff options
-rw-r--r-- | src/BBCodeParser.php | 17 | ||||
-rw-r--r-- | test/BBCodeParserTest.php | 3 |
2 files changed, 15 insertions, 5 deletions
diff --git a/src/BBCodeParser.php b/src/BBCodeParser.php index af2d129..1a19d1f 100644 --- a/src/BBCodeParser.php +++ b/src/BBCodeParser.php @@ -15,6 +15,9 @@ class BBCodeParser { # convert smilies, which aren't in BBCode (ack!) $in = preg_replace('/<!-- s(.*?) --><img src="\{SMILIES_PATH\}\/.*? \/><!-- s\1 -->/', '\1', $in); + # convert non-BBCode links to BBCode (ack!) + $in = preg_replace('/<!-- (m|w) --><a class="postlink" href="(.*?)">(.*?)<\/a><!-- \1 -->/', "[url:$uid=\\2]\\3[/url:$uid]", $in); + $text_stack = array(); $arg_stack = array(); @@ -41,9 +44,15 @@ class BBCodeParser { $state = self::DONE; } else { - # locate next tag + $ulen = strlen($uid) + 1; + + # locate the start and end of next tag $tstart = strrpos($in, '[', $ustart-$len); - $tag = substr($in, $tstart+1, $ustart-$tstart-1); + $tend = strpos($in, ']', $ustart+$ulen); + + # slice out the uid + $tag = substr($in, $tstart+1, $ustart-$tstart-1) . + substr($in, $ustart+$ulen, $tend-$ustart-$ulen); # determine whether it's an open or close tag if ($tag[0] == '/') { @@ -57,8 +66,8 @@ class BBCodeParser { # copy leading text to output $out .= substr($in, $i, $tstart-$i); - # advance past ":$uid]" to next unparsed character - $i = $ustart + strlen($uid) + 2; + # advance past the closing ']' to next unparsed character + $i = $tend + 1; } break; diff --git a/test/BBCodeParserTest.php b/test/BBCodeParserTest.php index 8e4062a..f2341aa 100644 --- a/test/BBCodeParserTest.php +++ b/test/BBCodeParserTest.php @@ -21,7 +21,8 @@ class BBCodeParserTest extends PHPUnit_Framework_TestCase { 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', "\n> Here's some quoty stuff.\n> > Followed by an even deeper quote.\n> Followed by more stuff.\n", null), array('This is a rather long line. This is a rather long line. This is a rather long line. This is a rather long line. This is a rather long line. This is a rather long line.[quote:2nnqpmcp]This is a rather long line. This is a rather long line. This is a rather long line. This is a rather long line. This is a rather long line. This is a rather long line.[quote:2nnqpmcp]This is a rather long line. This is a rather long line. This is a rather long line. This is a rather long line. This is a rather long line. This is a rather long line.[/quote:2nnqpmcp][/quote:2nnqpmcp]', '2nnqpmcp', "This is a rather long line. This is a rather long line. This is a rather\nlong line. This is a rather long line. This is a rather long line. This\nis a rather long line.\n> This is a rather long line. This is a rather long line. This is a\n> rather long line. This is a rather long line. This is a rather long\n> line. This is a rather long line.\n> > This is a rather long line. This is a rather long line. This is a\n> > rather long line. This is a rather long line. This is a rather long\n> > line. This is a rather long line.\n> \n", null), array("Here is a list:\n[list:wjve1skj]\n[*:wjve1skj] first item[/*:m:wjve1skj]\n[*:wjve1skj] second item[/*:m:wjve1skj]\n[*:wjve1skj] third item\n[list=1:wjve1skj]\n[*:wjve1skj] a subsidiary item[/*:m:wjve1skj]\n[*:wjve1skj] another subsidiary item[/*:m:wjve1skj][/list:o:wjve1skj][/*:m:wjve1skj]\n[*:wjve1skj] the last item[/*:m:wjve1skj][/list:u:wjve1skj]\nAnd some more text.", 'wjve1skj', "Here is a list:\n\n * first item\n * second item\n * third item\n 1. a subsidiary item\n 2. another subsidiary item\n * the last item\n\nAnd some more text.", null), - + array("Here's a BBCode URL: [url:3i2cqt66=http://www.vassalengine.org]http://www.vassalengine.org[/url:3i2cqt66]. Will it be converted?", '3i2cqt66', "Here's a BBCode URL: http://www.vassalengine.org[1]. Will it be\nconverted?\n\n[1] http://www.vassalengine.org\n", null), + array("Here's a non-BBCode URL: <!-- m --><a class=\"postlink\" href=\"http://www.vassalengine.org\">http://www.vassalengine.org</a><!-- m -->. Will it be converted?", '3i2cqt66', "Here's a non-BBCode URL: http://www.vassalengine.org[1]. Will it be\nconverted?\n\n[1] http://www.vassalengine.org\n", 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) ); } |