-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for PLAIN BB-Code and CODE=rich (#36)
* had to reset my branch * remove unused files in parser-directory after reset * Fix typos/mistakes in code comments * Properly set up phpunit testsuite and extract helper methods to trait * Add phpunit to phan github action * simplify the parser and make sure text-nodes are buffered * stop parsing after unclosed bbcode there is no reason the check the text-buffer after a unclosed bbcode, because the unclosed bbcode consumes the rest of the input anyway. * fix wording in test * fix wording in test #2 * add comment about nested bbcodes in test * added parser-helpers trait again * used more descriptive variable-names in parser * used more descriptive variable-names in parser * hide phan progress-bar, because github cannot render it * (wip) added support for nested code-blocks * some fixes and adjusted node-export logic * tests added * fix code=rich export issue * small adjustments in architecture + saner api * test if pr is broken * yep, pr is broken * fix IteratorAggregate usage * make tests type-safe * added more tests and fixed some associated issues * fix typo in file-path * fixed some typos and added more tests * use expectException in tests Co-authored-by: JR Cologne <[email protected]>
- Loading branch information
1 parent
8a1a862
commit 38ef1fe
Showing
17 changed files
with
2,908 additions
and
448 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
namespace DevCommunityDE\CodeFormatter\Parser; | ||
|
||
/** | ||
* this class represents element attributes in a | ||
* raw (match) and a tokenized (pairs) form. | ||
* | ||
* @internal | ||
*/ | ||
final class ElemAttrs | ||
{ | ||
/** @var string */ | ||
private $match; | ||
|
||
/** @var array */ | ||
private $pairs; | ||
|
||
/** | ||
* constructor. | ||
* | ||
* @param string $match | ||
* @param array<string,string> $pairs | ||
*/ | ||
public function __construct(string $match, array $pairs) | ||
{ | ||
$this->match = $match; | ||
$this->pairs = $pairs; | ||
} | ||
|
||
/** | ||
* returns a value. | ||
* | ||
* @param string $name | ||
* | ||
* @return string|null | ||
*/ | ||
public function getValue(string $name): ?string | ||
{ | ||
return $this->pairs[$name] ?? null; | ||
} | ||
|
||
/** | ||
* returns the matched attribute string. | ||
* | ||
* @return string | ||
*/ | ||
public function getMatch(): string | ||
{ | ||
return $this->match; | ||
} | ||
|
||
/** | ||
* checks if some attributes are set. | ||
* | ||
* @return bool | ||
*/ | ||
public function hasMatch(): bool | ||
{ | ||
return !empty($this->match); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
namespace DevCommunityDE\CodeFormatter\Parser; | ||
|
||
/** | ||
* this class represents a bbcode. | ||
*/ | ||
final class ElemNode extends Node | ||
{ | ||
/** @var string */ | ||
private $name; | ||
|
||
/** @var ElemAttrs */ | ||
private $attrs; | ||
|
||
/** @var NodeList|string */ | ||
private $body; | ||
|
||
/** | ||
* constructs a new code-token. | ||
* | ||
* @param string $name | ||
* @param ElemAttrs $attrs | ||
* @param NodeList|string $body | ||
*/ | ||
public function __construct(string $name, ElemAttrs $attrs, $body) | ||
{ | ||
\assert(\is_string($body) || $body instanceof NodeList); | ||
parent::__construct(Node::KIND_ELEM); | ||
$this->name = $name; | ||
$this->attrs = $attrs; | ||
$this->body = $body; | ||
} | ||
|
||
/** | ||
* checks if this node represents a [CODE] bbcode. | ||
* | ||
* @return bool | ||
*/ | ||
public function isCode(): bool | ||
{ | ||
return 0 === strcasecmp($this->name, 'code'); | ||
} | ||
|
||
/** | ||
* checks if this node represents a [CODE=rich] bbcode. | ||
* | ||
* @return bool | ||
*/ | ||
public function isRich(): bool | ||
{ | ||
if (0 !== strcasecmp($this->name, 'code')) { | ||
return false; | ||
} | ||
|
||
$langAttr = $this->getLang(); | ||
|
||
if (empty($langAttr)) { | ||
/* defaults to "text" */ | ||
return false; | ||
} | ||
|
||
return 0 === strcasecmp($langAttr, 'rich'); | ||
} | ||
|
||
/** | ||
* utility method to retrieve a lang-attribute. | ||
* | ||
* @return string|null | ||
*/ | ||
public function getLang(): ?string | ||
{ | ||
$langAttr = $this->getAttr('lang'); | ||
|
||
if (null === $langAttr && $this->isCode()) { | ||
$langAttr = $this->getAttr('@value'); | ||
} | ||
|
||
return $langAttr; | ||
} | ||
|
||
/** | ||
* returns the element name. | ||
* | ||
* @return string | ||
*/ | ||
public function getName(): string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
/** | ||
* returns a single node attribute. | ||
* | ||
* @param string $name | ||
* | ||
* @return string|null | ||
*/ | ||
public function getAttr(string $name): ?string | ||
{ | ||
return $this->attrs->getValue($name); | ||
} | ||
|
||
/** | ||
* returns the node attributes. | ||
* | ||
* @return string | ||
*/ | ||
public function getAttrMatch(): string | ||
{ | ||
return $this->attrs->getMatch(); | ||
} | ||
|
||
/** | ||
* returns the node-body. | ||
* | ||
* @return NodeList|string | ||
*/ | ||
public function getBody() | ||
{ | ||
return $this->body; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DevCommunityDE\CodeFormatter\Parser; | ||
|
||
abstract class Node | ||
{ | ||
/** text node */ | ||
public const KIND_TEXT = 1; | ||
|
||
/** element node (bbcode) */ | ||
public const KIND_ELEM = 2; | ||
|
||
/** @var int */ | ||
private $kind; | ||
|
||
/** | ||
* constructs a new token. | ||
* | ||
* @param int $kind | ||
*/ | ||
public function __construct(int $kind) | ||
{ | ||
$this->kind = $kind; | ||
} | ||
|
||
/** | ||
* returns the node kind. | ||
* | ||
* @return int | ||
*/ | ||
public function getKind(): int | ||
{ | ||
return $this->kind; | ||
} | ||
} |
Oops, something went wrong.