Skip to content

Commit

Permalink
release: 1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Genert Org committed Mar 4, 2018
1 parent 66c4f47 commit 77d8a5b
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 11 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ All notable changes to this project will be documented in this file.

The project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [1.1.0] - 2018-03-04
### Added
- `addLinebreakParser` adds linebreak parser to support changing newlines to `<br />`.

### Changed
- Removed linebreak parser from BBCode default parsers list.

## [1.0.3] - 2017-07-18
### Added
- Newline parser
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ $bbCode->addParser(
$bbCode->convertToHtml('[link target=www.yourlinkhere.com]Text to be displayed[/link].');
```

### `addLinebreakParser()`

Adds linebreak parser to BBCode parsers list to convert newlines to `<br />` in HTML.

## Laravel installation

Expand Down
8 changes: 7 additions & 1 deletion src/BBCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
use Genert\BBCode\Parser\BBCodeParser;
use Genert\BBCode\Parser\HTMLParser;

final class BBCode {
final class BBCode
{
private $htmlParser;
private $bbCodeParser;

Expand Down Expand Up @@ -60,4 +61,9 @@ public function addParser(string $name, string $pattern, string $replace, string

return $this;
}

public function addLinebreakParser()
{
return $this->addParser('linebreak', '/[\r\n]/', '<br />', '');
}
}
8 changes: 2 additions & 6 deletions src/Parser/BBCodeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

namespace Genert\BBCode\Parser;

final class BBCodeParser extends Parser {
final class BBCodeParser extends Parser
{
protected $parsers = [
'bold' => [
'pattern' => '/\[b\](.*?)\[\/b\]/s',
Expand Down Expand Up @@ -110,11 +111,6 @@ final class BBCodeParser extends Parser {
'replace' => '<td>$1</td>',
'content' => '$1',
],
'linebreak' => [
'pattern' => '/\r\n/',
'replace' => '<br />',
'content' => '',
],
];

public function stripTags(string $source): string
Expand Down
3 changes: 2 additions & 1 deletion src/Parser/HTMLParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

namespace Genert\BBCode\Parser;

final class HTMLParser extends Parser {
final class HTMLParser extends Parser
{
protected $parsers = [
'bold' => [
'pattern' => '/<strong>(.*?)<\/strong>|<b>(.*?)<\/b>/s',
Expand Down
3 changes: 2 additions & 1 deletion src/Parser/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

namespace Genert\BBCode\Parser;

class Parser {
class Parser
{
/**
* Static case insensitive flag to enable
* case insensitivity when parsing BBCode.
Expand Down
60 changes: 58 additions & 2 deletions tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
use PHPUnit\Framework\TestCase;
use Genert\BBCode\BBCode;

class ParserTest extends TestCase {
public function testParser() {
class ParserTest extends TestCase
{
public function testParser()
{
$bbCode = new BBCode();

$this->assertNotNull($bbCode);
Expand Down Expand Up @@ -160,6 +162,60 @@ public function testBBCodeReturnsCorrectHtml() {
$this->assertEquals($output, $bbCode->convertToHtml($input));
}

// https://github.com/Genert/bbcode/issues/2
public function testCustomBulletsListBBCodeConversion()
{
$bbCode = new BBCode();

$input = '
[ul]
[li]List item 1[/li]
[li]List item 2[/li]
[li]List item 3[/li]
[/ul]
';

$bbCode->addParser(
'bullet_list',
'/\[ul\](.*?)\[\/ul\]/s',
'<ul>$1</ul>',
'$1'
);

$bbCode->addParser(
'list_item',
'/\[li\](.*?)\[\/li\]/',
'<li>$1</li>',
'$1'
);

$output = '
<ul>
<li>List item 1</li>
<li>List item 2</li>
<li>List item 3</li>
</ul>
';

$this->assertEquals($output, $bbCode->convertToHtml($input));
}

public function testAddNewLineBBCodeConversion()
{
$bbCode = new BBCode();

$bbCode->addNewlineParser();

$input = '[b]bold[/b]
[i]italic[/i]
[u]underline[/u]
[s]line through[/s]';

$output = trim('<b>bold</b><br /> <i>italic</i><br /> <u>underline</u><br /> <s>line through</s>');

$this->assertEquals($output, $bbCode->convertToHtml($input));
}

public function testCaseSensitivity()
{
$bbCode = new BBCode();
Expand Down

0 comments on commit 77d8a5b

Please sign in to comment.