Skip to content

Commit

Permalink
feat: table-parser & delimiter fix
Browse files Browse the repository at this point in the history
  • Loading branch information
YellowPhoenix18 committed Apr 13, 2021
1 parent c332019 commit e526e91
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 11 deletions.
20 changes: 11 additions & 9 deletions src/Parser/EditorjsParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@
use Exception;
use DOMDocument;
use Masterminds\HTML5;
use SyntaxPhoenix\EJSParserBundle\Parser\EditorjsParserExtension;
use SyntaxPhoenix\EJSParserBundle\Parser\Extension\RawParser;
use SyntaxPhoenix\EJSParserBundle\Parser\Extension\CodeParser;
use SyntaxPhoenix\EJSParserBundle\Parser\Extension\DelimeterParser;
use SyntaxPhoenix\EJSParserBundle\Parser\Extension\EmbedParser;
use SyntaxPhoenix\EJSParserBundle\Parser\Extension\HeaderParser;
use SyntaxPhoenix\EJSParserBundle\Parser\Extension\ImageParser;
use SyntaxPhoenix\EJSParserBundle\Parser\Extension\LinkParser;
use SyntaxPhoenix\EJSParserBundle\Parser\Extension\ListParser;
use SyntaxPhoenix\EJSParserBundle\Parser\Extension\ParagraphParser;
use SyntaxPhoenix\EJSParserBundle\Parser\Extension\RawParser;
use SyntaxPhoenix\EJSParserBundle\Parser\Extension\EmbedParser;
use SyntaxPhoenix\EJSParserBundle\Parser\Extension\ImageParser;
use SyntaxPhoenix\EJSParserBundle\Parser\Extension\HeaderParser;
use SyntaxPhoenix\EJSParserBundle\Parser\EditorjsParserExtension;
use SyntaxPhoenix\EJSParserBundle\Parser\Extension\WarningParser;
use SyntaxPhoenix\EJSParserBundle\Parser\Extension\DelimiterParser;
use SyntaxPhoenix\EJSParserBundle\Parser\Extension\ParagraphParser;
use SyntaxPhoenix\EJSParserBundle\Parser\Extension\TableParser;

class EditorjsParser
{
Expand All @@ -40,15 +41,16 @@ public function __construct(object $data)

$this->parser = [
'header' => new HeaderParser(),
'delimeter' => new DelimeterParser(),
'delimiter' => new DelimiterParser(),
'code' => new CodeParser(),
'paragraph' => new ParagraphParser(),
'link' => new LinkParser(),
'embed' => new EmbedParser(),
'raw' => new RawParser(),
'list' => new ListParser(),
'warning' => new WarningParser(),
'simpleImage' => new ImageParser()
'simpleImage' => new ImageParser(),
'table' => new TableParser()
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Masterminds\HTML5;
use SyntaxPhoenix\EJSParserBundle\Parser\EditorjsParserExtension;

class DelimeterParser implements EditorjsParserExtension
class DelimiterParser implements EditorjsParserExtension
{

public function parseBlock(HTML5 $html5, DOMDocument $document, object $block, string $prefix): DOMElement
Expand Down
38 changes: 38 additions & 0 deletions src/Parser/Extension/TableParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace SyntaxPhoenix\EJSParserBundle\Parser\Extension;

use DOMElement;
use DOMDocument;
use Masterminds\HTML5;
use SyntaxPhoenix\EJSParserBundle\Parser\EditorjsParserExtension;

class TableParser implements EditorjsParserExtension
{

public function parseBlock(HTML5 $html5, DOMDocument $document, object $block, string $prefix): DOMElement
{
$wrapper = $document->createElement('div');

$wrapper->setAttribute('class', "{$prefix}-table");

$table = $document->createElement('table');

$tableBody = $document->createElement('tbody');

foreach ($block->data->content as $row) {
$tableRow = $document->createElement('tr');
foreach ($row as $item) {
$tableDefinition = $document->createElement('td', $item);
$tableRow->appendChild($tableDefinition);
}
$tableBody->appendChild($tableRow);
}

$table->appendChild($tableBody);

$wrapper->appendChild($table);

return $wrapper;
}
}
2 changes: 1 addition & 1 deletion src/Twig/EditorjsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function getFunctions(): array
/**
* @param array $data
*/
public function editorjsParse(array $data = []): string
public function editorjsParse(object $data): string
{
$parser = new EditorjsParser($data);
return $parser->toHtml();
Expand Down

0 comments on commit e526e91

Please sign in to comment.