@@ -0,0 +1,68 @@ | |||
<?php | |||
namespace App\BbCode; | |||
use JBBCode\CodeDefinitionSet; | |||
use JBBCode\CodeDefinitionBuilder; | |||
class KnockoutDefinitionSet implements CodeDefinitionSet | |||
{ | |||
/** @var CodeDefinition[] The default code definitions in this set. */ | |||
protected $definitions = array(); | |||
/** | |||
* Constructs the default code definitions. | |||
*/ | |||
public function __construct() | |||
{ | |||
/* [b] bold tag */ | |||
$builder = new CodeDefinitionBuilder('b', '<strong>{param}</strong>'); | |||
$this->definitions[] = $builder->build(); | |||
/* [i] italics tag */ | |||
$builder = new CodeDefinitionBuilder('i', '<em>{param}</em>'); | |||
$this->definitions[] = $builder->build(); | |||
/* [u] underline tag */ | |||
$builder = new CodeDefinitionBuilder('u', '<u>{param}</u>'); | |||
$this->definitions[] = $builder->build(); | |||
$urlValidator = new \JBBCode\validators\UrlValidator(); | |||
/* [url] link tag */ | |||
$builder = new CodeDefinitionBuilder('url', '<a href="{param}">{param}</a>'); | |||
$builder->setParseContent(false)->setBodyValidator($urlValidator); | |||
$this->definitions[] = $builder->build(); | |||
/* [url=http://example.com] link tag */ | |||
$builder = new CodeDefinitionBuilder('url', '<a href="{option}">{param}</a>'); | |||
$builder->setUseOption(true)->setParseContent(true)->setOptionValidator($urlValidator); | |||
$this->definitions[] = $builder->build(); | |||
/* [img] image tag */ | |||
$builder = new CodeDefinitionBuilder('img', '<img src="{param}" />'); | |||
$builder->setUseOption(false)->setParseContent(false)->setBodyValidator($urlValidator); | |||
$this->definitions[] = $builder->build(); | |||
/* [img=alt text] image tag */ | |||
$builder = new CodeDefinitionBuilder('img', '<img src="{param}" alt="{option}" />'); | |||
$builder->setUseOption(true)->setParseContent(false)->setBodyValidator($urlValidator); | |||
$this->definitions[] = $builder->build(); | |||
/* [color] color tag */ | |||
$builder = new CodeDefinitionBuilder('color', '<span style="color: {option}">{param}</span>'); | |||
$builder->setUseOption(true)->setOptionValidator(new \JBBCode\validators\CssColorValidator()); | |||
$this->definitions[] = $builder->build(); | |||
} | |||
/** | |||
* Returns an array of the default code definitions. | |||
* | |||
* @return CodeDefinition[] | |||
*/ | |||
public function getCodeDefinitions() | |||
{ | |||
return $this->definitions; | |||
} | |||
} |
@@ -13,7 +13,7 @@ class Unknown implements TagInterface | |||
$formattedString = "[{$tokenTag->getType()}"; | |||
if ($tokenTag->getArgument()) { | |||
$formattedString .= "={$tokenTag->getArgument()}"; | |||
$formattedString .= " {$tokenTag->getArgument()}"; | |||
} | |||
$formattedString .= "]{$tokenTag->getText()}[/{$tokenTag->getType()}]"; | |||
@@ -2,117 +2,24 @@ | |||
namespace App\Helper; | |||
// formatting | |||
use App\BbCode\Tag\Unknown; | |||
use App\BbCode\Tag\Text; | |||
use App\BbCode\Tag\Bold; | |||
use App\BbCode\Tag\Italic; | |||
use App\BbCode\Tag\Underline; | |||
use App\BbCode\Tag\Strikethrough; | |||
use App\BbCode\Tag\Spoiler; | |||
use App\BbCode\Tag\HeadingOne; | |||
use App\BbCode\Tag\HeadingTwo; | |||
use App\BbCode\Tag\Link; | |||
use App\BbCode\Tag\Code; | |||
// quotes | |||
use App\BbCode\Tag\Quote; | |||
use App\BbCode\Tag\Blockquote; | |||
// embeds | |||
use App\BbCode\Tag\Image; | |||
use App\BbCode\Tag\Twitter; | |||
use App\BbCode\Tag\Youtube; | |||
use App\BbCode\Tag\Video; | |||
use App\BbCode\KnockoutDefinitionSet; | |||
use Knockout\BbCode\BbCode as Parser; | |||
use Knockout\BbCode\Tokenizer\Tokenizer; | |||
use JBBCode\Parser; | |||
class BBCode { | |||
private $tokens; | |||
private $parser; | |||
public function __construct($content) | |||
{ | |||
$this->tokens = (new Tokenizer())->tokenize($content); | |||
$this->parser = new Parser(); | |||
$this->parser->addCodeDefinitionSet(new KnockoutDefinitionSet()); | |||
$this->parser->parse($content); | |||
} | |||
public function render() | |||
{ | |||
$parser = new Parser(); | |||
$unknown = new Unknown(); | |||
$parser->addTag($unknown); | |||
$text = new Text(); | |||
$parser->addTag($text); | |||
$bold = new Bold(); | |||
$parser->addTag($bold); | |||
$italic = new Italic(); | |||
$parser->addTag($italic); | |||
$underline = new Underline(); | |||
$parser->addTag($underline); | |||
$strikethrough = new Strikethrough(); | |||
$parser->addTag($strikethrough); | |||
$image = new Image(); | |||
$parser->addTag($image); | |||
$link = new Link(); | |||
$parser->addTag($link); | |||
$code = new Code(); | |||
$parser->addTag($code); | |||
$quote = new Quote(); | |||
$parser->addTag($quote); | |||
$blockquote = new Blockquote(); | |||
$parser->addTag($blockquote); | |||
$twitter = new Twitter(); | |||
$parser->addTag($twitter); | |||
$youtube = new Youtube(); | |||
$parser->addTag($youtube); | |||
$video = new Video(); | |||
$parser->addTag($video); | |||
$tags = [ | |||
$unknown, | |||
$text, | |||
$bold, | |||
$italic, | |||
$underline, | |||
$strikethrough, | |||
$image, | |||
$link, | |||
$code, | |||
$quote, | |||
$blockquote, | |||
$twitter, | |||
$youtube, | |||
$video | |||
]; | |||
$anyChild = [ | |||
$bold, | |||
$quote, | |||
$blockquote, | |||
$text, | |||
$unknown | |||
]; | |||
foreach ($anyChild as $parent) { | |||
foreach ($tags as $tag) { | |||
$parser->addAllowedChildTag($parent, $tag); | |||
} | |||
} | |||
return $parser->format($this->tokens); | |||
return $this->parser->getAsHtml(); | |||
} | |||
} |
@@ -12,9 +12,9 @@ | |||
], | |||
"require": { | |||
"php": ">=7.4", | |||
"jbbcode/jbbcode": "^1.4", | |||
"laravel/lumen-framework": "^8.0", | |||
"nesbot/carbon": "^2.43", | |||
"knockout/bbcode": "dev-master" | |||
"nesbot/carbon": "^2.43" | |||
}, | |||
"require-dev": { | |||
"barryvdh/laravel-debugbar": "^3.5", | |||
@@ -4,7 +4,7 @@ | |||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", | |||
"This file is @generated automatically" | |||
], | |||
"content-hash": "a350fd1e86adcc528a94e674954ba0fa", | |||
"content-hash": "65ffb6735fb8aab2c27031564c2b60dd", | |||
"packages": [ | |||
{ | |||
"name": "brick/math", | |||
@@ -1862,31 +1862,56 @@ | |||
"time": "2020-11-02T14:01:41+00:00" | |||
}, | |||
{ | |||
"name": "knockout/bbcode", | |||
"version": "dev-master", | |||
"name": "jbbcode/jbbcode", | |||
"version": "v1.4.2", | |||
"source": { | |||
"type": "git", | |||
"url": "https://git.mos6581.com/Knockout/BBCode.git", | |||
"reference": "636800e8507616e38df94c60706cdaad577d523d" | |||
"url": "https://github.com/jbowens/jBBCode.git", | |||
"reference": "d9a132e7886a11cf997e3ec025a41bdf97899704" | |||
}, | |||
"dist": { | |||
"type": "zip", | |||
"url": "https://api.github.com/repos/jbowens/jBBCode/zipball/d9a132e7886a11cf997e3ec025a41bdf97899704", | |||
"reference": "d9a132e7886a11cf997e3ec025a41bdf97899704", | |||
"shasum": "" | |||
}, | |||
"require": { | |||
"php": ">=7.0" | |||
"php": ">=5.3.0" | |||
}, | |||
"require-dev": { | |||
"phpunit/phpunit": "5.6.*" | |||
"friendsofphp/php-cs-fixer": "^2.1", | |||
"phpunit/phpunit": "4.5.*", | |||
"satooshi/php-coveralls": "0.6.*" | |||
}, | |||
"default-branch": true, | |||
"type": "library", | |||
"autoload": { | |||
"psr-0": { | |||
"Knockout": "src/" | |||
"JBBCode": "." | |||
} | |||
}, | |||
"notification-url": "https://packagist.org/downloads/", | |||
"license": [ | |||
"MIT" | |||
], | |||
"description": "php parser for bb code", | |||
"time": "2021-01-03T22:57:20+00:00" | |||
"authors": [ | |||
{ | |||
"name": "Jackson Owens", | |||
"email": "jackson_owens@alumni.brown.edu", | |||
"homepage": "http://jbowens.org/", | |||
"role": "Developer" | |||
} | |||
], | |||
"description": "A lightweight but extensible BBCode parser written in PHP 5.3.", | |||
"homepage": "http://jbbcode.com/", | |||
"keywords": [ | |||
"BB", | |||
"bbcode" | |||
], | |||
"support": { | |||
"issues": "https://github.com/jbowens/jBBCode/issues", | |||
"source": "https://github.com/jbowens/jBBCode/tree/v1.4.2" | |||
}, | |||
"time": "2020-06-19T16:09:00+00:00" | |||
}, | |||
{ | |||
"name": "laravel/lumen-framework", | |||
@@ -7625,9 +7650,7 @@ | |||
], | |||
"aliases": [], | |||
"minimum-stability": "dev", | |||
"stability-flags": { | |||
"knockout/bbcode": 20 | |||
}, | |||
"stability-flags": [], | |||
"prefer-stable": true, | |||
"prefer-lowest": false, | |||
"platform": { | |||