Skip to content

Commit

Permalink
Add preview mode
Browse files Browse the repository at this point in the history
  • Loading branch information
cbasje committed Mar 9, 2023
1 parent f2eb928 commit 7e53278
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/BbEnv.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/
class BbEnv {
/**
* @var string One of default, light, plain
* @var string One of default, light, preview, plain
*/
public $mode = "default";
public $quote_level = 0;
Expand Down
10 changes: 10 additions & 0 deletions src/BbTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,16 @@ public function renderLight()

abstract public function render();

/**
* render preview will strip html tags by default.
*
* @return string
*/
public function renderPreview()
{
return strip_tags($this->render());
}

/**
* render plain will strip html tags by default.
*
Expand Down
4 changes: 3 additions & 1 deletion src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public function getHtml($bbcode) {
* @return BbNode[]|null
*/
public function parseString($bbcode) {
if ($this->env->mode !== "plain") {
if ($this->env->mode !== "preview" && $this->env->mode !== "plain") {
$bbcode = str_replace(array("\r\n", "\n"), self::BR_TAG, $bbcode);
}

Expand Down Expand Up @@ -201,6 +201,8 @@ public function render($blocks, $mode)

if ($mode == "light") {
$text .= $block->renderLight();
} elseif ($mode == "preview") {
$text .= $block->renderPreview();
} elseif ($mode == "plain") {
$text .= $block->renderPlain();
} else {
Expand Down
5 changes: 5 additions & 0 deletions src/internal/BbError.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ public function renderPlain()
return $this->render();
}

public function renderPreview()
{
return $this->render();
}

public function renderLight()
{
return $this->render();
Expand Down
5 changes: 5 additions & 0 deletions src/internal/BbString.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ public function renderPlain()
return $this->render();
}

public function renderPreview()
{
return $this->render();
}

public function renderLight()
{
return $this->render();
Expand Down
4 changes: 4 additions & 0 deletions src/tag/BbHorizontalRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public function renderPlain() {
return "---";
}

public function renderPreview() {
return "---";
}

public static function isParagraphLess() {
return true;
}
Expand Down
4 changes: 4 additions & 0 deletions src/tag/BbLishort.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public function renderPlain() {
return " * " . $this->getContent();
}

public function renderPreview() {
return " - " . $this->getContent();
}

public function parse($arguments = [])
{
$this->setChildren($this->parser->parseArray(['[br]']));
Expand Down
4 changes: 4 additions & 0 deletions src/tag/BbListItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public function renderPlain() {
return " * " . $this->getContent();
}

public function renderPreview() {
return " - " . $this->getContent();
}

public function parse($arguments = [])
{
$this->readContent();
Expand Down
4 changes: 4 additions & 0 deletions src/tag/BbNewline.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ public function renderPlain() {
return "\n";
}

public function renderPreview() {
return " ";
}

public function parse($arguments = [])
{
// No arguments
Expand Down
6 changes: 6 additions & 0 deletions src/tag/BbNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ interface BbNode
*/
public function renderPlain();

/**
* @return string
* @throws BbException
*/
public function renderPreview();

/**
* @return string
* @throws BbException
Expand Down
4 changes: 4 additions & 0 deletions src/tag/BbQuote.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public function renderPlain() {
return "> " . str_replace("\n", "\n> ", $this->getContent());
}

public function renderPreview() {
return "\"" . str_replace("\n", "\n> ", $this->getContent()) . "\"";
}

public function render($arguments = []) {
return '<div class="citaatContainer bb-tag-quote"><strong>Citaat</strong>' .
'<div class="citaat">' . $this->getContent() . '</div></div>';
Expand Down

0 comments on commit 7e53278

Please sign in to comment.