Skip to content
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

WIP: Emoji extension #546

Closed
wants to merge 1 commit into from
Closed
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 @@ -37,6 +37,7 @@
"scrutinizer/ocular": "^1.5",
"symfony/finder": "^5.1",
"symfony/yaml": "^2.3 | ^3.0 | ^4.0 | ^5.0",
"unicorn-fail/emoji": "1.0.x-dev",
"unleashedtech/php-coding-standard": "^2.5",
"vimeo/psalm": "^3.14"
},
Expand Down
48 changes: 48 additions & 0 deletions src/Extension/Emoji/EmojiExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

/*
* This file is part of the league/commonmark package.
*
* (c) Colin O'Dell <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace League\CommonMark\Extension\Emoji;

use League\CommonMark\Environment\ConfigurableEnvironmentInterface;
use League\CommonMark\Event\DocumentParsedEvent;
use League\CommonMark\Extension\Emoji\Listener\EmojiProcessorListener;
use League\CommonMark\Extension\Emoji\Node\Emoji;
use League\CommonMark\Extension\Emoji\Parser\EmojiParserInterface;
use League\CommonMark\Extension\Emoji\Parser\UnicornFailEmojiParser;
use League\CommonMark\Extension\ExtensionInterface;

final class EmojiExtension implements ExtensionInterface
{
/**
* @var EmojiParserInterface
*
* @psalm-readonly
*/
private $parser;

public function __construct(?EmojiParserInterface $parser = null)
{
$this->parser = $parser ?? new UnicornFailEmojiParser();
}

public function getEmojiParser(): EmojiParserInterface
{
return $this->parser;
}

public function register(ConfigurableEnvironmentInterface $environment): void
{
$environment->addEventListener(DocumentParsedEvent::class, new EmojiProcessorListener($this->parser), -100);
$environment->addRenderer(Emoji::class, new EmojiRenderer());
}
}
34 changes: 34 additions & 0 deletions src/Extension/Emoji/EmojiRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

/*
* This file is part of the league/commonmark package.
*
* (c) Colin O'Dell <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace League\CommonMark\Extension\Emoji;

use League\CommonMark\Extension\Emoji\Node\Emoji;
use League\CommonMark\Node\Node;
use League\CommonMark\Renderer\ChildNodeRendererInterface;
use League\CommonMark\Renderer\NodeRendererInterface;

final class EmojiRenderer implements NodeRendererInterface
{
/**
* {@inheritdoc}
*/
public function render(Node $node, ChildNodeRendererInterface $childRenderer)
{
if (! ($node instanceof Emoji)) {
throw new \InvalidArgumentException('Incompatible node type: ' . \get_class($node));
}

return (string) $node->getToken();
}
}
22 changes: 22 additions & 0 deletions src/Extension/Emoji/Exception/InvalidEmojiException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

/*
* This file is part of the league/commonmark package.
*
* (c) Colin O'Dell <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace League\CommonMark\Extension\Emoji\Exception;

final class InvalidEmojiException extends \RuntimeException
{
public static function wrap(\Throwable $throwable): self
{
return new InvalidEmojiException('Failed to parse emojis: ' . $throwable->getMessage(), 0, $throwable);
}
}
69 changes: 69 additions & 0 deletions src/Extension/Emoji/Listener/EmojiProcessorListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

declare(strict_types=1);

/*
* This file is part of the league/commonmark package.
*
* (c) Colin O'Dell <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace League\CommonMark\Extension\Emoji\Listener;

use League\CommonMark\Configuration\ConfigurationAwareInterface;
use League\CommonMark\Configuration\ConfigurationInterface;
use League\CommonMark\Event\DocumentParsedEvent;
use League\CommonMark\Extension\Emoji\Parser\EmojiParserInterface;
use League\CommonMark\Node\Inline\Text;

/**
* Searches the Document for Text elements and parses it into distinct Emoji nodes.
*/
final class EmojiProcessorListener implements ConfigurationAwareInterface
{
/**
* @var EmojiParserInterface
*
* @psalm-readonly
*/
private $parser;

public function __construct(EmojiParserInterface $parser)
{
$this->parser = $parser;
}

public function __invoke(DocumentParsedEvent $e): void
{
$walker = $e->getDocument()->walker();
while ($event = $walker->next()) {
if (! $event->isEntering()) {
continue;
}

$text = $event->getNode();
if (! ($text instanceof Text)) {
continue;
}

$nodes = $this->parser->parse($text->getLiteral());
if (! $nodes) {
continue;
}

foreach ($nodes as $node) {
$text->insertBefore($node);
}

$text->detach();
}
}

public function setConfiguration(ConfigurationInterface $configuration): void
{
$this->parser->setConfiguration($configuration);
}
}
33 changes: 33 additions & 0 deletions src/Extension/Emoji/Node/Emoji.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

/*
* This file is part of the league/commonmark package.
*
* (c) Colin O'Dell <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace League\CommonMark\Extension\Emoji\Node;

use League\CommonMark\Node\Inline\AbstractInline;
use UnicornFail\Emoji\Token\AbstractEmojiToken;

final class Emoji extends AbstractInline
{
/** @var AbstractEmojiToken */
protected $token;

public function __construct(AbstractEmojiToken $token)
{
$this->token = $token;
}

public function getToken(): ?AbstractEmojiToken
{
return $this->token;
}
}
29 changes: 29 additions & 0 deletions src/Extension/Emoji/Parser/EmojiParserInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

/*
* This file is part of the league/commonmark package.
*
* (c) Colin O'Dell <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace League\CommonMark\Extension\Emoji\Parser;

use League\CommonMark\Configuration\ConfigurationAwareInterface;
use League\CommonMark\Extension\Emoji\Exception\InvalidEmojiException;
use League\CommonMark\Node\Inline\AbstractInline;

interface EmojiParserInterface extends ConfigurationAwareInterface
{
/**
* @return AbstractInline[]
*
* @throws InvalidEmojiException if parsing fails
* @throws \RuntimeException if other errors occur
*/
public function parse(string $string): array;
}
61 changes: 61 additions & 0 deletions src/Extension/Emoji/Parser/UnicornFailEmojiParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

/*
* This file is part of the league/commonmark package.
*
* (c) Colin O'Dell <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace League\CommonMark\Extension\Emoji\Parser;

use League\CommonMark\Configuration\ConfigurationInterface;
use League\CommonMark\Extension\Emoji\Node\Emoji;
use League\CommonMark\Node\Inline\Text;
use UnicornFail\Emoji\Parser;
use UnicornFail\Emoji\Token\AbstractEmojiToken;

final class UnicornFailEmojiParser implements EmojiParserInterface
{
/** @var ConfigurationInterface */
private $config;

/** @var Parser */
private $parser;

public function getParser(): Parser
{
if (! isset($this->parser)) {
if (! \class_exists(Parser::class)) {
throw new \RuntimeException('Failed to parse emojis: "unicorn-fail/emoji" library is missing');
}

$this->parser = new Parser($this->config->get('emoji/configuration', []));
}

return $this->parser;
}

/**
* {@inheritDoc}
*/
public function parse(string $string): array
{
$nodes = [];
$tokens = $this->getParser()->parse($string);
foreach ($tokens as $token) {
$nodes[] = $token instanceof AbstractEmojiToken ? new Emoji($token) : new Text((string) $token);
}

return $nodes;
}

public function setConfiguration(ConfigurationInterface $configuration): void
{
$this->config = $configuration;
}
}
33 changes: 33 additions & 0 deletions tests/functional/Extension/Emoji/EmojiExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace League\CommonMark\Tests\Functional\Extension\Emoji;

use League\CommonMark\CommonMarkConverter;
use League\CommonMark\Environment\Environment;
use League\CommonMark\Extension\Emoji\EmojiExtension;
use PHPUnit\Framework\TestCase;

final class EmojiExtensionTest extends TestCase
{
/** @var Environment */
private $environment;

protected function setUp(): void
{
$this->environment = Environment::createCommonMarkEnvironment();
$this->environment->addExtension(new EmojiExtension());
}

public function testWithSampleData(): void
{
$markdown = '🙍🏿‍♂️ is leaving on a &#x2708;️. Going to 🇦🇺. Might see some :kangaroo:! <3 Remember to 📱 :D';
$expected = "<p>🙍🏿‍♂️ is leaving on a ✈️. Going to 🇦🇺. Might see some 🦘! ❤️ Remember to 📱 😀</p>\n";

$converter = new CommonMarkConverter([], $this->environment);
$result = $converter->convertToHtml($markdown);

$this->assertSame($expected, (string) $result);
}
}