A bare bones front-end for knockout designed for maximum compatibility with "obsolete" browsers
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
2.3KB

  1. <?php
  2. namespace App\Helper;
  3. // formatting
  4. use App\BbCode\Tag\Unknown;
  5. use App\BbCode\Tag\Text;
  6. use App\BbCode\Tag\Bold;
  7. use App\BbCode\Tag\Italic;
  8. use App\BbCode\Tag\Underline;
  9. use App\BbCode\Tag\Strikethrough;
  10. use App\BbCode\Tag\Spoiler;
  11. use App\BbCode\Tag\HeadingOne;
  12. use App\BbCode\Tag\HeadingTwo;
  13. use App\BbCode\Tag\Link;
  14. // quotes
  15. use App\BbCode\Tag\Quote;
  16. use App\BbCode\Tag\Blockquote;
  17. // embeds
  18. use App\BbCode\Tag\Image;
  19. use App\BbCode\Tag\Twitter;
  20. use App\BbCode\Tag\Youtube;
  21. use App\BbCode\Tag\Video;
  22. use Knockout\BbCode\BbCode as Parser;
  23. use Knockout\BbCode\Tokenizer\Tokenizer;
  24. class BBCode {
  25. private $tokens;
  26. public function __construct($content)
  27. {
  28. $this->tokens = (new Tokenizer())->tokenize($content);
  29. }
  30. public function render()
  31. {
  32. $parser = new Parser();
  33. $unknown = new Unknown();
  34. $parser->addTag($unknown);
  35. $text = new Text();
  36. $parser->addTag($text);
  37. $bold = new Bold();
  38. $parser->addTag($bold);
  39. $italic = new Italic();
  40. $parser->addTag($italic);
  41. $underline = new Underline();
  42. $parser->addTag($underline);
  43. $strikethrough = new Strikethrough();
  44. $parser->addTag($strikethrough);
  45. $image = new Image();
  46. $parser->addTag($image);
  47. $link = new Link();
  48. $parser->addTag($link);
  49. $quote = new Quote();
  50. $parser->addTag($quote);
  51. $blockquote = new Blockquote();
  52. $parser->addTag($blockquote);
  53. $twitter = new Twitter();
  54. $parser->addTag($twitter);
  55. $youtube = new Youtube();
  56. $parser->addTag($youtube);
  57. $video = new Video();
  58. $parser->addTag($video);
  59. $tags = [
  60. $unknown,
  61. $text,
  62. $bold,
  63. $italic,
  64. $underline,
  65. $strikethrough,
  66. $image,
  67. $link,
  68. $quote,
  69. $blockquote,
  70. $twitter,
  71. $youtube,
  72. $video
  73. ];
  74. $anyChild = [
  75. $bold,
  76. $quote,
  77. $blockquote,
  78. $text,
  79. $unknown
  80. ];
  81. foreach ($anyChild as $parent) {
  82. foreach ($tags as $tag) {
  83. $parser->addAllowedChildTag($parent, $tag);
  84. }
  85. }
  86. return $parser->format($this->tokens);
  87. }
  88. }