Skip to content

Introduce CommonMarkParser #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 8 commits into
base: v2.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"cache/void-adapter": "^1.1",
"jakeasmith/http_build_url": "^1",
"jbbcode/jbbcode": "^1.4",
"league/commonmark": "^2.0",
"youthweb/urllinker": "^1.4"
},
"require-dev": {
Expand Down
51 changes: 51 additions & 0 deletions src/Extension/BBCode/BBCodeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

/*
* A BBCode-to-HTML parser for youthweb.net
* Copyright (C) 2016-2021 Youthweb e.V. <[email protected]>

* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

namespace Youthweb\BBCodeParser\Extension\BBCode;

use League\CommonMark\Environment\EnvironmentBuilderInterface;
use League\CommonMark\Extension\ExtensionInterface;
use League\CommonMark\Node as CoreNode;
use League\CommonMark\Parser as CoreParser;
use League\CommonMark\Renderer as CoreRenderer;
use Youthweb\BBCodeParser\Extension\BBCode\Delimiter\Processor\EmphasisDelimiterProcessor;

/**
* BBCodeExtension
*/
final class BBCodeExtension implements ExtensionInterface
{
public function register(EnvironmentBuilderInterface $environment): void
{
$environment
->addBlockStartParser(new Parser\Block\BoldBlockStartParser(), 40)

->addInlineParser(new CoreParser\Inline\NewlineParser(), 200)

->addRenderer(CoreNode\Block\Document::class, new CoreRenderer\Block\DocumentRenderer(), 0)
->addRenderer(Node\Block\BoldBlock::class, new Renderer\Block\BoldBlockRenderer(), 0)
->addRenderer(CoreNode\Block\Paragraph::class, new CoreRenderer\Block\ParagraphRenderer(), 0)

->addRenderer(CoreNode\Inline\Text::class, new CoreRenderer\Inline\TextRenderer(), 0)
;
}
}
42 changes: 42 additions & 0 deletions src/Extension/BBCode/Node/Block/BoldBlock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

/*
* A BBCode-to-HTML parser for youthweb.net
* Copyright (C) 2016-2021 Youthweb e.V. <[email protected]>

* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

namespace Youthweb\BBCodeParser\Extension\BBCode\Node\Block;

use League\CommonMark\Node\Block\AbstractBlock;
use League\CommonMark\Node\RawMarkupContainerInterface;
use League\CommonMark\Node\StringContainerInterface;

final class BoldBlock extends AbstractBlock implements RawMarkupContainerInterface, StringContainerInterface
{
private string $literal = '';

public function getLiteral(): string
{
return $this->literal;
}

public function setLiteral(string $literal): void
{
$this->literal = $literal;
}
}
93 changes: 93 additions & 0 deletions src/Extension/BBCode/Parser/Block/BoldBlockParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

declare(strict_types=1);

/*
* A BBCode-to-HTML parser for youthweb.net
* Copyright (C) 2016-2021 Youthweb e.V. <[email protected]>

* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

namespace Youthweb\BBCodeParser\Extension\BBCode\Parser\Block;

use League\CommonMark\Parser\Block\AbstractBlockContinueParser;
use League\CommonMark\Parser\Block\BlockContinue;
use League\CommonMark\Parser\Block\BlockContinueParserInterface;
use League\CommonMark\Parser\Block\BlockContinueParserWithInlinesInterface;
use League\CommonMark\Parser\Cursor;
use League\CommonMark\Parser\InlineParserEngineInterface;
use League\CommonMark\Util\RegexHelper;
use Youthweb\BBCodeParser\Extension\BBCode\Node\Block\BoldBlock;

final class BoldBlockParser extends AbstractBlockContinueParser implements BlockContinueParserWithInlinesInterface
{
private BoldBlock $block;

private string $content = '';

private bool $finished = false;

public function __construct()
{
$this->block = new BoldBlock();
}

public function getBlock(): BoldBlock
{
return $this->block;
}

public function tryContinue(Cursor $cursor, BlockContinueParserInterface $activeBlockParser): ?BlockContinue
{
if ($cursor->getNextNonSpaceCharacter() !== '[') {
return BlockContinue::at($cursor);
}

$tmpCursor = clone $cursor;
$tmpCursor->advanceToNextNonSpaceOrTab();
$line = $tmpCursor->getSubstring(0, 4);

if ($line === '[/b]') {
return BlockStart::none();
}

return BlockContinue::at($cursor);
}

public function addLine(string $line): void
{
if ($this->content !== '') {
$this->content .= "\n";
}

$line = substr($line, 0, -4);

$this->content .= $line;
}

public function closeBlock(): void
{
$this->block->setLiteral($this->content);
$this->content = '';
}

/**
* Parse any inlines inside of the current block
*/
public function parseInlines(InlineParserEngineInterface $inlineParser): void
{
// #TODO
}
}
51 changes: 51 additions & 0 deletions src/Extension/BBCode/Parser/Block/BoldBlockStartParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

/*
* A BBCode-to-HTML parser for youthweb.net
* Copyright (C) 2016-2021 Youthweb e.V. <[email protected]>

* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

namespace Youthweb\BBCodeParser\Extension\BBCode\Parser\Block;

use League\CommonMark\Parser\Block\BlockStart;
use League\CommonMark\Parser\Block\BlockStartParserInterface;
use League\CommonMark\Parser\Cursor;
use League\CommonMark\Parser\MarkdownParserStateInterface;
use League\CommonMark\Util\RegexHelper;

final class BoldBlockStartParser implements BlockStartParserInterface
{
public function tryStart(Cursor $cursor, MarkdownParserStateInterface $parserState): ?BlockStart
{
if ($cursor->isIndented() || $cursor->getNextNonSpaceCharacter() !== '[') {
return BlockStart::none();
}

$tmpCursor = clone $cursor;
$tmpCursor->advanceToNextNonSpaceOrTab();
$line = $tmpCursor->getSubstring(0, 3);

if ($line !== '[b]') {
return BlockStart::none();
}

$cursor->advanceBy(3);

return BlockStart::of(new BoldBlockParser())->at($cursor);
}
}
73 changes: 73 additions & 0 deletions src/Extension/BBCode/Renderer/Block/BoldBlockRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

declare(strict_types=1);

/*
* A BBCode-to-HTML parser for youthweb.net
* Copyright (C) 2016-2021 Youthweb e.V. <[email protected]>

* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

namespace Youthweb\BBCodeParser\Extension\BBCode\Renderer\Block;

use League\CommonMark\Node\Node;
use League\CommonMark\Renderer\ChildNodeRendererInterface;
use League\CommonMark\Renderer\NodeRendererInterface;
use League\CommonMark\Util\HtmlElement;
use League\CommonMark\Xml\XmlNodeRendererInterface;
use League\Config\ConfigurationAwareInterface;
use League\Config\ConfigurationInterface;
use Youthweb\BBCodeParser\Extension\BBCode\Node\Block\BoldBlock;

final class BoldBlockRenderer implements NodeRendererInterface, XmlNodeRendererInterface, ConfigurationAwareInterface
{
private ConfigurationInterface $config;

/**
* @param BoldBlock $node
*
* {@inheritDoc}
*/
public function render(Node $node, ChildNodeRendererInterface $childRenderer): HtmlElement
{
BoldBlock::assertInstanceOf($node);

$innerHtml = $childRenderer->renderNodes($node->children());

if ($innerHtml === '') {
$innerHtml = $node->getLiteral();
}

return new HtmlElement('b', [], $innerHtml);
}

public function setConfiguration(ConfigurationInterface $configuration): void
{
$this->config = $configuration;
}

public function getXmlTagName(Node $node): string
{
return 'strong';
}

/**
* {@inheritDoc}
*/
public function getXmlAttributes(Node $node): array
{
return [];
}
}
Loading