Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
uses: actions/cache@v2
with:
path: vendor
key: ${{ runner.os }}-composer-dev-${{ hashFiles('**/composer.lock') }}
key: ${{ runner.os }}-composer-dev-${{ hashFiles('**/composer.lock', '**/composer.json') }}
restore-keys: |
${{ runner.os }}-composer-dev-

Expand Down
86 changes: 43 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,74 +15,74 @@ composer require csrdelft/bb
Either use the `DefaultParser` or implement your own parser.

```php
$parser = new \CsrDelft\bb\DefaultParser();
$parser = new \CsrDelft\Lib\Bb\DefaultParser();

echo $parser->getHtml('[h=1]Hello World[/h]');
```

### Tags
The default tags available are:

|Tag name|Description|
|---|---|
|`[b]`| Bold text |
|`[i]`|Italic|
|`[u]`|Underline|
|`[s]`|Strikethrough|
|`[sub]`|Subscript|
|`[sup]`|Superscript|
|`[clear]`|Clear|
|`[code]`|Code block|
|`[commentaar]`|Comment|
|`[div class=? w=? h=? float=left/right clear?]`|Div|
|`[email]`|Email|
|`[h]`|Header|
|`[hr]`|Horizontal rule|
|`[1337]`|Leet speak|
|`[lishort]`, `[*]`| List item|
|`[list]`, `[ulist]`| List|
|`[li]`|List item|
|`[me]`| /me|
|`[rn]`| New line|
|`[nobold]`|Disable `[b]`|
|`[quote]`|Blockquote|
|`[table]`|Table element|
|`[td]`| Table cell|
|`[th]`| Table header|
|`[tr]`|Table Row|
| Tag name | Description |
|-------------------------------------------------|-----------------|
| `[b]` | Bold text |
| `[i]` | Italic |
| `[u]` | Underline |
| `[s]` | Strikethrough |
| `[sub]` | Subscript |
| `[sup]` | Superscript |
| `[clear]` | Clear |
| `[code]` | Code block |
| `[commentaar]` | Comment |
| `[div class=? w=? h=? float=left/right clear?]` | Div |
| `[email]` | Email |
| `[h]` | Header |
| `[hr]` | Horizontal rule |
| `[1337]` | Leet speak |
| `[lishort]`, `[*]` | List item |
| `[list]`, `[ulist]` | List |
| `[li]` | List item |
| `[me]` | /me |
| `[rn]` | New line |
| `[nobold]` | Disable `[b]` |
| `[quote]` | Blockquote |
| `[table]` | Table element |
| `[td]` | Table cell |
| `[th]` | Table header |
| `[tr]` | Table Row |

### Custom tags

Tags must extend the `\CsrDelft\bb\BbTag` class. Tags must implement the `parse($arguments)` and `getTagName()` methods.
Tags must extend the `\CsrDelft\Lib\Bb\BbTag` class. Tags must implement the `parse($arguments)` and `getTagName()` methods.

The `getTagName` method retuns a string or list of strings with the name(s) of this tag.
The `getTagName` method returns a string or list of strings with the name(s) of this tag.

The `parse` method receives a map of arguments. The `readContent` method can be used to retrieve the contents of
the tag, this content is parsed by the parser when it is received. The parser reads the input until an end tag is
found. `readContent` has an optional parameter for tags which are forbidden to be in this tag. For instance a `[sup]`
tag cannot contain another sup tag or a sub tag.

A tag has access to an environment, which is by default of type `CsrDelft\bb\BBenv` (can be overridden).
A tag has access to an environment, which is by default of type `CsrDelft\Lib\Bb\BBenv` (can be overridden).

### Custom tag example

```php
class BbSuperscript extends BbTag {
public static function getTagName() {
return 'sup';
}

public function render() {
return '<sup class="bb-tag-sup">' . $this->getContent() . '</sup>';
}
public function parse($arguments = []) {
$this->readContent(['sub', 'sup']);
}
public static function getTagName(): string {
return 'sup';
}

public function render(): string {
return '<sup class="bb-tag-sup">' . $this->getContent() . '</sup>';
}

public function parse($arguments = []): void {
$this->readContent(['sub', 'sup']);
}
}
```

### Custom parser

A custom parser must extend `Parser` and contains a list of tags in the `$tags` field. See `DefaultParser` for the
A custom parser must extend `Parser` and contains a list of tags in the `getTags` method. See `DefaultParser` for the
list of default tags.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
},
"autoload": {
"psr-4": {
"CsrDelft\\bb\\": "src/",
"CsrDelft\\bb\\test\\": "tests/lib/"
"CsrDelft\\Lib\\Bb\\": "src/",
"CsrDelft\\Lib\\Bb\\Test\\": "tests/lib/"
}
},
"scripts": {
Expand Down
5 changes: 3 additions & 2 deletions src/BbEnv.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?php

namespace CsrDelft\bb;
namespace CsrDelft\Lib\Bb;

/**
* @author G.J.W. Oolbekkink <g.j.w.oolbekkink@gmail.com>
* @since 27/03/2019
*/
class BbEnv {
class BbEnv
{
/**
* @var string One of default, light, preview, plain
*/
Expand Down
5 changes: 3 additions & 2 deletions src/BbException.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<?php

namespace CsrDelft\bb;
namespace CsrDelft\Lib\Bb;

use Exception;

/**
* @author G.J.W. Oolbekkink <g.j.w.oolbekkink@gmail.com>
* @since 06/07/2019
*/
class BbException extends Exception {
class BbException extends Exception
{

}
48 changes: 27 additions & 21 deletions src/BbTag.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace CsrDelft\bb;
namespace CsrDelft\Lib\Bb;

use CsrDelft\bb\tag\BbNode;
use CsrDelft\Lib\Bb\Tag\BbNode;
use Error;
use stdClass;

Expand Down Expand Up @@ -44,7 +44,7 @@ public function setEnv($env)
$this->env = $env;
}

public function isAllowed()
public function isAllowed(): bool
{
return true;
}
Expand All @@ -54,12 +54,12 @@ public function isAllowed()
* @return mixed
* @throws BbException
*/
abstract public function parse($arguments = []);
abstract public function parse(array $arguments = []): void;

/**
* @return BbNode[]|null
*/
public function getChildren()
public function getChildren(): ?array
{
return $this->children;
}
Expand All @@ -76,7 +76,7 @@ public function setChildren($children)
* @return string
* @throws BbException
*/
public function getContent()
public function getContent(): string
{
if ($this->content === null) {
throw new BbException("Cannot read content during parsing");
Expand All @@ -85,30 +85,29 @@ public function getContent()
return $this->content;
}

public function setContent($content)
public function setContent(string $content): void
{
$this->content = $content;
}

/**
* ParseLight defaults to parse
*
* @return mixed
* @throws BbException
*/
public function renderLight()
public function renderLight(): string
{
return $this->render();
}

abstract public function render();
abstract public function render(): string;

/**
* render preview will strip html tags by default.
*
* @return string
*/
public function renderPreview()
public function renderPreview(): string
{
return strip_tags($this->render());
}
Expand All @@ -118,7 +117,7 @@ public function renderPreview()
*
* @return string
*/
public function renderPlain()
public function renderPlain(): string
{
return strip_tags($this->render());
}
Expand All @@ -128,10 +127,10 @@ public function renderPlain()
*
* [tag=123] or [tag]123[/tag]
*
* @param $arguments
* @param string[] $arguments
* @return string
*/
protected function readMainArgument($arguments)
protected function readMainArgument(array $arguments): string
{
if (is_array($this->getTagName())) {
foreach ($this->getTagName() as $tagName) {
Expand All @@ -152,6 +151,9 @@ protected function readMainArgument($arguments)
}
}

/**
* @return string|string[]
*/
abstract public static function getTagName();

/**
Expand All @@ -161,21 +163,25 @@ abstract public static function getTagName();
*
* @param string[] $forbidden Tag names that cannot exist in this tag.
*/
protected function readContent($forbidden = [], $parse_bb = true)
protected function readContent(array $forbidden = [], bool $parse_bb = true): void
{
if ($this->content != NULL)
if ($this->content != null) {
throw new Error("Can not call readContent twice on the same tag");
}
$stoppers = $this->getStoppers();
$parse_bb_state_before = $this->parser->bb_mode;
$this->parser->bb_mode &= $parse_bb;
$parse_bb_state_before = $this->parser->bbMode;
$this->parser->bbMode &= $parse_bb;

$result = $this->parser->parseArray($stoppers, $forbidden);

$this->parser->bb_mode = $parse_bb_state_before;
$this->parser->bbMode = $parse_bb_state_before;
$this->children = $result;
}

protected function getStoppers()
/**
* @return string[]
*/
protected function getStoppers(): array
{
$stoppers = [];

Expand All @@ -190,7 +196,7 @@ protected function getStoppers()
return $stoppers;
}

private function createStopper($tagName)
private function createStopper($tagName): string
{
return "[/$tagName]";
}
Expand Down
Loading