diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 8b16273..d0529ef 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -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-
diff --git a/README.md b/README.md
index 2dd9ebc..26d45d9 100644
--- a/README.md
+++ b/README.md
@@ -15,7 +15,7 @@ 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]');
```
@@ -23,66 +23,66 @@ 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 '' . $this->getContent() . '';
- }
-
- public function parse($arguments = []) {
- $this->readContent(['sub', 'sup']);
- }
+ public static function getTagName(): string {
+ return 'sup';
+ }
+
+ public function render(): string {
+ return '' . $this->getContent() . '';
+ }
+
+ 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.
diff --git a/composer.json b/composer.json
index e5dafbc..c0f3ead 100644
--- a/composer.json
+++ b/composer.json
@@ -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": {
diff --git a/src/BbEnv.php b/src/BbEnv.php
index 47597e7..317ace3 100644
--- a/src/BbEnv.php
+++ b/src/BbEnv.php
@@ -1,12 +1,13 @@
* @since 27/03/2019
*/
-class BbEnv {
+class BbEnv
+{
/**
* @var string One of default, light, preview, plain
*/
diff --git a/src/BbException.php b/src/BbException.php
index a7b60bd..b8f3261 100644
--- a/src/BbException.php
+++ b/src/BbException.php
@@ -1,6 +1,6 @@
* @since 06/07/2019
*/
-class BbException extends Exception {
+class BbException extends Exception
+{
}
diff --git a/src/BbTag.php b/src/BbTag.php
index 8e8916f..eadb599 100644
--- a/src/BbTag.php
+++ b/src/BbTag.php
@@ -1,8 +1,8 @@
env = $env;
}
- public function isAllowed()
+ public function isAllowed(): bool
{
return true;
}
@@ -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;
}
@@ -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");
@@ -85,7 +85,7 @@ public function getContent()
return $this->content;
}
- public function setContent($content)
+ public function setContent(string $content): void
{
$this->content = $content;
}
@@ -93,22 +93,21 @@ public function setContent($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());
}
@@ -118,7 +117,7 @@ public function renderPreview()
*
* @return string
*/
- public function renderPlain()
+ public function renderPlain(): string
{
return strip_tags($this->render());
}
@@ -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) {
@@ -152,6 +151,9 @@ protected function readMainArgument($arguments)
}
}
+ /**
+ * @return string|string[]
+ */
abstract public static function getTagName();
/**
@@ -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 = [];
@@ -190,7 +196,7 @@ protected function getStoppers()
return $stoppers;
}
- private function createStopper($tagName)
+ private function createStopper($tagName): string
{
return "[/$tagName]";
}
diff --git a/src/DefaultParser.php b/src/DefaultParser.php
index 82ad794..473a066 100644
--- a/src/DefaultParser.php
+++ b/src/DefaultParser.php
@@ -1,62 +1,71 @@
* @since 06/07/2019
*/
-final class DefaultParser extends Parser {
- protected $tags = [
- BbBold::class,
- BbClear::class,
- BbCode::class,
- BbCommentaar::class,
- BbDiv::class,
- BbEmail::class,
- BbHeading::class,
- BbHorizontalRule::class,
- BbItalic::class,
- BbLeet::class,
- BbLishort::class,
- BbListItem::class,
- BbMe::class,
- BbNewline::class,
- BbNobold::class,
- BbQuote::class,
- BbStrikethrough::class,
- BbSubscript::class,
- BbSuperscript::class,
- BbTable::class,
- BbTableCell::class,
- BbTableHeader::class,
- BbTableRow::class,
- BbList::class,
- BbUnderline::class,
- ];
+final class DefaultParser extends Parser
+{
+ /**
+ * @return BbNode[]
+ */
+ public function getTags(): array
+ {
+ return [
+ new BbBold(),
+ new BbClear(),
+ new BbCode(),
+ new BbCommentaar(),
+ new BbDiv(),
+ new BbEmail(),
+ new BbHeading(),
+ new BbHorizontalRule(),
+ new BbItalic(),
+ new BbLeet(),
+ new BbLishort(),
+ new BbListItem(),
+ new BbMe(),
+ new BbNewline(),
+ new BbNobold(),
+ new BbQuote(),
+ new BbStrikethrough(),
+ new BbSubscript(),
+ new BbSuperscript(),
+ new BbTable(),
+ new BbTableCell(),
+ new BbTableHeader(),
+ new BbTableRow(),
+ new BbList(),
+ new BbUnderline(),
+ ];
+ }
}
diff --git a/src/Internal/BbError.php b/src/Internal/BbError.php
new file mode 100644
index 0000000..1544e61
--- /dev/null
+++ b/src/Internal/BbError.php
@@ -0,0 +1,64 @@
+error = $error;
+ }
+
+ public function isAllowed(): bool
+ {
+ return true;
+ }
+
+ public function render(): string
+ {
+ return $this->error;
+ }
+
+ public function getChildren(): array
+ {
+ return [];
+ }
+
+ public function setContent($content): void
+ {
+ // Nop
+ }
+
+ /**
+ * @throws BbException
+ */
+ public function getContent(): string
+ {
+ throw new BbException("Error heeft geen content");
+ }
+
+ public function renderPlain(): string
+ {
+ return $this->render();
+ }
+
+ public function renderPreview(): string
+ {
+ return $this->render();
+ }
+
+ public function renderLight(): string
+ {
+ return $this->render();
+ }
+}
diff --git a/src/internal/BbString.php b/src/Internal/BbString.php
similarity index 55%
rename from src/internal/BbString.php
rename to src/Internal/BbString.php
index b1cb5d0..07dbdf5 100644
--- a/src/internal/BbString.php
+++ b/src/Internal/BbString.php
@@ -1,9 +1,9 @@
string = $string;
}
- public function isAllowed()
+ public function isAllowed(): bool
{
return true;
}
- public function render()
+ public function render(): string
{
return $this->string;
}
- public function getChildren()
+ public function getChildren(): array
{
return [];
}
- public function setContent($content)
+ public function setContent($content): void
{
// Nop
}
- public function getContent()
+ public function getContent(): string
{
return $this->string;
}
- public function renderPlain()
+ public function renderPlain(): string
{
return $this->render();
}
- public function renderPreview()
+ public function renderPreview(): string
{
return $this->render();
}
- public function renderLight()
+ public function renderLight(): string
{
return $this->render();
}
-}
\ No newline at end of file
+}
diff --git a/src/Parser.php b/src/Parser.php
index e71214a..97fafc5 100644
--- a/src/Parser.php
+++ b/src/Parser.php
@@ -1,17 +1,18 @@
This is an experimental feature! Please let me know if you find strange behaviour!
+ * When set to true, the parser will try to use <p> tags around text, and remove unnecessary br's.
+ * This is an experimental feature! Please let me know if you find strange behaviour!
* @var boolean
*/
- protected $paragraph_mode = false;
- /**
- * List of possible tags.
- *
- * @var array
- */
- protected $tags = [];
- /**
- * Storage for BB code
- */
- private $bbcode;
+ protected $paragraphMode = false;
/**
* Storage for outgoing HTML
*/
- private $HTML;
+ private $html;
/**
* How deep are we; e.g. How many open tags?
*/
@@ -91,40 +85,42 @@ abstract class Parser {
/**
* Amount of tags already parsed
*/
- private $tags_counted = 0;
+ private $tagsCounted = 0;
/**
* Keep track of open paragraphs
*/
- private $paragraph_open = false;
+ private $paragraphOpen = false;
/**
* Tags that do not need to be encapsulated in paragraphs, filled in constructor.
*/
- private $paragraphless_tags = ['br'];
+ private $paragraphlessTags = ['br'];
/**
* Keep track of current paragraph-required-status
*
- * When we're in a tag that does not need to be encapsulate in a paragraph, this var will be false, otherwise true. Works only when in paragraph mode.
+ * When we're in a tag that does not need to be encapsulate in a paragraph, this var will be false, otherwise true.
+ * Works only when in paragraph mode.
*/
- private $paragraph_required = true;
+ private $paragraphRequired = true;
/**
* Environment
*
* @var BbEnv
*/
- private $env = [];
+ private $env = null;
/**
- * @var BbTag[]
+ * @var BbTag[]|string[]
*/
private $registry = [];
- public function __construct($env = null) {
+ public function __construct($env = null)
+ {
if (is_null($env)) {
$env = new BbEnv();
}
$this->env = $env;
- foreach ($this->tags as $tag) {
+ foreach ($this->getTags() as $tag) {
if (is_array($tag::getTagName())) {
foreach ($tag::getTagName() as $tagName) {
$this->registry[$tagName] = $tag;
@@ -134,11 +130,21 @@ public function __construct($env = null) {
}
if ($tag::isParagraphLess()) {
- $this->paragraphless_tags[] = $tag::getTagName();
+ $this->paragraphlessTags[] = $tag::getTagName();
}
}
}
+ /**
+ * @param BbEnv|mixed|null $env
+ */
+ public function setEnv($env): void
+ {
+ $this->env = $env;
+ }
+
+ abstract public function getTags();
+
/**
* Transform BB code to HTML code.
*
@@ -146,7 +152,8 @@ public function __construct($env = null) {
* @param string $bbcode BB code to be transformed
* @return string HTML
*/
- public function getHtml($bbcode) {
+ public function getHtml($bbcode)
+ {
if (strlen($bbcode) == 0) {
return null;
}
@@ -155,22 +162,23 @@ public function getHtml($bbcode) {
$html = $this->render($blocks, $this->env->mode);
- $this->HTML = str_replace(self::BR_TAG, "
\n", $html);
+ $this->html = str_replace(self::BR_TAG, "
\n", $html);
- return $this->HTML;
+ return $this->html;
}
/**
* @param string $bbcode
* @return BbNode[]|null
*/
- public function parseString($bbcode) {
+ public function parseString($bbcode)
+ {
if ($this->env->mode !== "preview" && $this->env->mode !== "plain") {
$bbcode = str_replace(array("\r\n", "\n"), self::BR_TAG, $bbcode);
}
// Create the parsearray with the buildarray function, pretty nice ;)
- $this->tags_counted = 0;
+ $this->tagsCounted = 0;
$this->parseArray = $this->tokenize($bbcode);
// Fix html rights
@@ -219,7 +227,8 @@ public function render($blocks, $mode)
return $text;
}
- private function tokenize($str) {
+ private function tokenize($str)
+ {
$tokens = "[]";
$index = 0;
@@ -246,7 +255,7 @@ private function tokenize($str) {
$prevIndex = $index;
- if ($numTags > $this->max_tags) {
+ if ($numTags > $this->maxTags) {
return ['[max # of tags reached, quitting splitting procedure]' . $str];
}
}
@@ -263,11 +272,12 @@ private function tokenize($str) {
/**
* Set [html] and [nohtml] tags according to settings
*/
- private function htmlFix() {
+ private function htmlFix()
+ {
// First, check if html is allowed
- if (!$this->allow_html) {
+ if (!$this->allowHtml) {
$html = false;
- } elseif ($this->standard_html) {
+ } elseif ($this->standardHtml) {
$html = true;
} else {
$html = false;
@@ -282,7 +292,7 @@ private function htmlFix() {
break;
case self::TAG_HTML_OPEN:
case self::TAG_NOHTML_CLOSE:
- if ($this->allow_html) {
+ if ($this->allowHtml) {
$html = true;
}
break;
@@ -310,64 +320,58 @@ private function htmlFix() {
* @param array $forbidden
* @return BbNode[]
*/
- public function parseArray($stoppers = [], $forbidden = []) {
-
+ public function parseArray($stoppers = [], $forbidden = [])
+ {
if (!is_array($this->parseArray)) { // Well, nothing to parse
return null;
}
$blocks = [];
- $forbidden_aantal_open = 0;
+ $forbiddenAantalOpen = 0;
while ($entry = array_shift($this->parseArray)) {
-
if (in_array($entry, $stoppers)) {
-
- if ($forbidden_aantal_open == 0) {
-
+ if ($forbiddenAantalOpen == 0) {
$this->level--;
return $blocks;
} else {
- $forbidden_aantal_open--;
+ $forbiddenAantalOpen--;
$blocks[] = new BbString($entry);
}
} else {
-
$tag = $this->getTag($entry);
if ($tag && in_array($tag, $forbidden)) {
if ($tag != 'br') {
- $forbidden_aantal_open++;
+ $forbiddenAantalOpen++;
} else {
$entry = "\n";
}
}
- $isOpenTag = substr($entry, 0, 1) == '[' && substr($entry, strlen($entry) - 1, 1) == ']' && substr($entry, 1, 1) != '/';
+ $isOpenTag = $this->isOpenTag($entry);
$isAllowed = !in_array($tag, $forbidden) && !isset($forbidden['all']);
$exists = isset($this->registry[$tag]);
- if ($this->bb_mode && $isOpenTag && $exists && $isAllowed) {
+ if ($this->bbMode && $isOpenTag && $exists && $isAllowed) {
$tagInstance = $this->createTagInstance($this->registry[$tag], $this, $this->env);
$arguments = $this->getArguments($entry);
- if ($this->paragraph_mode) {
+ if ($this->paragraphMode) {
// Add paragraphs if necessary
- $paragraph_setting_modified = false;
- if (!$this->paragraph_open && !in_array($tag, $this->paragraphless_tags) && $this->level == 0) { // Only encaps when level = 0, we don't want paragraphs inside lists or stuff
+ $paragraphSettingModified = false;
+ if (!$this->paragraphOpen && !in_array($tag, $this->paragraphlessTags) && $this->level == 0) { // Only encaps when level = 0, we don't want paragraphs inside lists or stuff
$text .= '
'; - $this->paragraph_open = true; - } elseif (in_array($tag, $this->paragraphless_tags)) { + $this->paragraphOpen = true; + } elseif (in_array($tag, $this->paragraphlessTags)) { // We're in some tag that doesn't need to be
enclosed, like a heading or a table. - if ($this->paragraph_required) { - $paragraph_setting_modified = true; - $this->paragraph_required = false; + if ($this->paragraphRequired) { + $paragraphSettingModified = true; + $this->paragraphRequired = false; } - if ($this->paragraph_open && $this->level == 0) { - - $text .= "
\n\n"; - $this->paragraph_open = false; + if ($this->paragraphOpen && $this->level == 0) { + $this->paragraphOpen = false; } } } @@ -380,15 +384,14 @@ public function parseArray($stoppers = [], $forbidden = []) { } // Reset paragraph_required. - if ($this->paragraph_mode && $paragraph_setting_modified) { - $this->paragraph_required = true; + if ($this->paragraphMode && $paragraphSettingModified) { + $this->paragraphRequired = true; } $blocks[] = $tagInstance; } else { - if ($this->paragraph_mode && $entry == self::BR_TAG) { - + if ($this->paragraphMode && $entry == self::BR_TAG) { $shift = array_shift($this->parseArray); if ($shift == self::BR_TAG) { // Two brs, looks like a new paragraph! @@ -397,24 +400,23 @@ public function parseArray($stoppers = [], $forbidden = []) { $secondshift = array_shift($this->parseArray); while ($secondshift == self::BR_TAG) { $secondshift = array_shift($this->parseArray); - $text .= ""; - $this->paragraph_open = true; + $this->paragraphOpen = true; } } else { if ($this->level == 0) { /** @noinspection HtmlUnknownAttribute */ $entry = "
"; - $this->paragraph_open = true; + $this->paragraphOpen = true; } } } else { @@ -424,7 +426,7 @@ public function parseArray($stoppers = [], $forbidden = []) { // We have found 1 [br], so normally we'd put it back // But if next thing is a paragraphless tag (say, table) or end of document, // We can skip the [br], since there will be a
anyway. - } elseif (in_array($this->getTag($shift), $this->paragraphless_tags)) { + } elseif (in_array($this->getTag($shift), $this->paragraphlessTags)) { $entry = null; } @@ -432,9 +434,8 @@ public function parseArray($stoppers = [], $forbidden = []) { } // Add paragraphs if necessary - if ($this->paragraph_mode && !$this->paragraph_open && $this->paragraph_required && $this->level == 0) { - $text .= ''; - $this->paragraph_open = true; + if ($this->paragraphMode && !$this->paragraphOpen && $this->paragraphRequired && $this->level == 0) { + $this->paragraphOpen = true; } $blocks[] = new BbString($entry); @@ -442,8 +443,8 @@ public function parseArray($stoppers = [], $forbidden = []) { } } // End of BIG while! - if ($this->paragraph_open) { // No need for a level check, should be zero anyway. - $this->paragraph_open = false; + if ($this->paragraphOpen) { // No need for a level check, should be zero anyway. + $this->paragraphOpen = false; $blocks[] = new BbString("
"); } @@ -454,10 +455,11 @@ public function parseArray($stoppers = [], $forbidden = []) { * return name of a tag * * When supplied with a full tag ([b] or [img w=5 h=10]), return tag name - * @return string * @param string $fullTag The full tag to get the tagname from + * @return string */ - private function getTag($fullTag) { + private function getTag($fullTag) + { if (substr($fullTag, 0, 1) == '[' && substr($fullTag, strlen($fullTag) - 1, 1) == ']') { return strtok($fullTag, '[ =]'); } else { @@ -469,12 +471,12 @@ private function getTag($fullTag) { * return arguments of a tag in array-form * * When supplied with a full tag ([h=5] or [img=blah.gif w=5 h=10]), return array with argument/value as key/value pairs - * @return array * @param string $fullTag The full tag to get the arguments from + * @return string[] */ - private function getArguments($fullTag) { - - $argument_array = Array(); + private function getArguments(string $fullTag): array + { + $arguments = []; $tag = substr($fullTag, 1, strlen($fullTag) - 2); $argList = explode(' ', $tag); $i = 0; @@ -490,21 +492,18 @@ private function getArguments($fullTag) { } } if (isset($value) && isset($key)) { - // FIXME: stupid javascript filtering detected if (strstr(strtolower($value), 'javascript:')) { $value = 'disabled'; } - // if(strstr(strtolower($value), '(')){ - // $value = 'disabled'; - // } - $argument_array[$key] = $value; + $arguments[$key] = $value; } } - return $argument_array; + return $arguments; } - protected function createTagInstance(string $tag, Parser $parser, $env) { + protected function createTagInstance(string $tag, Parser $parser, $env): BbTag + { /** @var BbTag $tagInstance */ $tagInstance = new $tag($parser, $env); $tagInstance->setParser($parser); @@ -513,4 +512,14 @@ protected function createTagInstance(string $tag, Parser $parser, $env) { return $tagInstance; } + /** + * @param string $entry + * @return bool + */ + private function isOpenTag(string $entry): bool + { + return substr($entry, 0, 1) == '[' && substr($entry, strlen($entry) - 1, 1) == ']' + && substr($entry, 1, 1) != '/'; + } + } diff --git a/src/Tag/BbBold.php b/src/Tag/BbBold.php new file mode 100644 index 0000000..6b18bbe --- /dev/null +++ b/src/Tag/BbBold.php @@ -0,0 +1,45 @@ + + * @since 27/03/2019 + */ +class BbBold extends BbTag +{ + private $disabled = false; + + public static function getTagName(): string + { + return 'b'; + } + + public function parse($arguments = []): void + { + if ($this->env->nobold === true && $this->env->quote_level == 0) { + $this->disabled = true; + } + $this->readContent(); + } + + public function renderPlain(): string + { + if ($this->disabled) { + return $this->getContent(); + } else { + return '*' . $this->getContent() . '*'; + } + } + + public function render(): string + { + if ($this->disabled) { + return $this->getContent(); + } else { + return '' . $this->getContent() . ''; + } + } +} diff --git a/src/tag/BbClear.php b/src/Tag/BbClear.php similarity index 53% rename from src/tag/BbClear.php rename to src/Tag/BbClear.php index a57ba02..3a13977 100644 --- a/src/tag/BbClear.php +++ b/src/Tag/BbClear.php @@ -1,32 +1,36 @@ * @since 27/03/2019 */ -class BbClear extends BbTag { +class BbClear extends BbTag +{ /** * @var string */ private $clearClass; - public static function getTagName() { - return 'clear'; - } + public static function getTagName(): string + { + return 'clear'; + } - public function parse($arguments = []) { + public function parse($arguments = []): void + { $this->clearClass = 'clear'; if (isset($arguments['clear']) && ($arguments['clear'] === 'left' || $arguments['clear'] === 'right')) { $this->clearClass .= '-' . $arguments['clear']; } } - public function render() { - return ''; - } + public function render(): string + { + return ''; + } } diff --git a/src/Tag/BbCode.php b/src/Tag/BbCode.php new file mode 100644 index 0000000..ccd369d --- /dev/null +++ b/src/Tag/BbCode.php @@ -0,0 +1,50 @@ + + * @example [code=PHP]phpinfo();[/code] + */ +class BbCode extends BbTag +{ + + /** + * @var string + */ + private $code; + + public static function getTagName(): string + { + return 'code'; + } + + public function parse($arguments = []): void + { + $this->readContent(['br'], false); + $this->code = isset($arguments['code']) ? $arguments['code'] . ' ' : ''; + } + + public function renderPlain(): string + { + return "$this->code\n\t" . str_replace("\n", "\n\t", $this->getContent()); + } + + public function render(): string + { + return vsprintf( + "%s
' . $this->getContent() . '