-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
1,958 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
namespace Coyote\Services\Parser\Extensions; | ||
|
||
use League\CommonMark\Node\Block\AbstractBlock; | ||
|
||
class Emoji extends AbstractBlock | ||
{ | ||
private static ?array $resourceFile = null; | ||
|
||
public string $code; | ||
public string $unified; | ||
public string $name; | ||
|
||
public function __construct(string $code) | ||
{ | ||
parent::__construct(); | ||
$this->code = $code; | ||
|
||
if (self::$resourceFile === null) { | ||
self::$resourceFile = \json_decode(\file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'emoji.json'), true); | ||
} | ||
$resourceFile = self::$resourceFile; | ||
|
||
$emoji = $resourceFile['emoticons'][$code]; | ||
|
||
$this->unified = $emoji['unified']; | ||
$this->name = $emoji['name']; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
namespace Coyote\Services\Parser\Extensions; | ||
|
||
use League\CommonMark\Parser\Cursor; | ||
use League\CommonMark\Parser\Inline\InlineParserInterface; | ||
use League\CommonMark\Parser\Inline\InlineParserMatch; | ||
use League\CommonMark\Parser\InlineParserContext; | ||
|
||
class EmojiParser implements InlineParserInterface | ||
{ | ||
public function getMatchDefinition(): InlineParserMatch | ||
{ | ||
return InlineParserMatch::join( | ||
InlineParserMatch::string(':'), | ||
InlineParserMatch::regex('\w+'), | ||
InlineParserMatch::string(':')); | ||
} | ||
|
||
public function parse(InlineParserContext $inlineContext): bool | ||
{ | ||
$cursor = $inlineContext->getCursor(); | ||
$previous = $cursor->peek(-1); | ||
if ($previous === null || $previous === ' ') { | ||
return $this->appendEmoji($cursor, $inlineContext); | ||
} | ||
return false; | ||
} | ||
|
||
private function appendEmoji(Cursor $cursor, InlineParserContext $context): bool | ||
{ | ||
$previousState = $cursor->saveState(); | ||
$emoji = $cursor->match('/^:[\w+-]+:/i'); | ||
if ($emoji === null) { | ||
$cursor->restoreState($previousState); | ||
return false; | ||
} | ||
$emoji = \subStr($emoji, 1, -1); | ||
if (\array_key_exists($emoji, Emojis::$codes)) { | ||
$container = $context->getContainer(); | ||
$container->appendChild(new Emoji($emoji)); | ||
return true; | ||
} | ||
$cursor->restoreState($previousState); | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
namespace Coyote\Services\Parser\Extensions; | ||
|
||
use League\CommonMark\Node\Node; | ||
use League\CommonMark\Renderer\ChildNodeRendererInterface; | ||
use League\CommonMark\Renderer\NodeRendererInterface; | ||
use League\CommonMark\Util\HtmlElement; | ||
|
||
class EmojiRenderer implements NodeRendererInterface | ||
{ | ||
public function render(Node $node, ChildNodeRendererInterface $childRenderer): HtmlElement | ||
{ | ||
if ($node instanceof Emoji) { | ||
return $this->renderEmoji($node); | ||
} | ||
throw new \LogicException('Failed to parse markdown emoticons'); | ||
} | ||
|
||
private function renderEmoji(Emoji $emoji): HtmlElement | ||
{ | ||
return new HtmlElement('img', [ | ||
'class' => 'img-smile', | ||
'src' => $this->cdnUrl($emoji), | ||
'alt' => ":$emoji->code:, $emoji->name", | ||
'title' => $emoji->name, | ||
'style' => 'width:16px', | ||
]); | ||
} | ||
|
||
private function cdnUrl(Emoji $emoji): string | ||
{ | ||
return "https://cdn.jsdelivr.net/gh/twitter/[email protected]/assets/svg/$emoji->unified.svg"; | ||
} | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,62 @@ | ||
<?php | ||
namespace Coyote\Services\Parser\Parsers; | ||
|
||
use Coyote\Services\Parser\Extensions\Emoji; | ||
use TRegx\CleanRegex\Match\Details\Detail; | ||
use TRegx\CleanRegex\Pattern; | ||
|
||
class Smilies extends HashParser | ||
{ | ||
private array $smilies = [ | ||
':)' => 'smile.gif', | ||
':-)' => 'smile.gif', | ||
';)' => 'wink.gif', | ||
';-)' => 'wink.gif', | ||
':-|' => 'neutral.gif', | ||
':D' => 'laugh.gif', | ||
':-D' => 'laugh.gif', | ||
':(' => 'sad.gif', | ||
':-(' => 'sad.gif', | ||
':P' => 'tongue1.gif', | ||
':p' => 'tongue1.gif', | ||
':-P' => 'tongue1.gif', | ||
':-/' => 'confused.gif', | ||
':/' => 'damn.gif', | ||
':[' => 'mad.gif', | ||
':-[' => 'mad.gif', | ||
':|' => 'zonk.gif', | ||
':]' => 'squared.gif', | ||
':d' => 'teeth.gif', | ||
':)' => 'smile', | ||
':-)' => 'twinkle', | ||
|
||
';)' => 'wink', | ||
';-)' => 'wink', | ||
|
||
':|' => 'neutral', | ||
':-|' => 'neutral', | ||
|
||
':D' => 'laugh', | ||
':-D' => 'laugh', | ||
|
||
':(' => 'frown', | ||
':-(' => 'frown', | ||
|
||
':P' => 'tongue', | ||
':p' => 'tongue', | ||
':-P' => 'tongue', | ||
':-p' => 'tongue', | ||
|
||
';P' => 'tongue_wink', | ||
';p' => 'tongue_wink', | ||
';-P' => 'tongue_wink', | ||
';-p' => 'tongue_wink', | ||
|
||
':/' => 'confused', | ||
':-/' => 'confused', | ||
|
||
':[' => 'angry', | ||
':-[' => 'angry', | ||
|
||
':]' => 'squared.gif', | ||
|
||
':d' => 'grimacing', | ||
]; | ||
|
||
public function hashedParse(string $text): string | ||
{ | ||
return Pattern::template('(?<=^|[\n \>]|\.)(@)') | ||
->alteration(array_keys($this->smilies)) | ||
->replace($text) | ||
->callback(function (Detail $match) { | ||
->callback(function (Detail $match): string { | ||
$smiley = $match->get(1); | ||
$link = $this->smilies[$smiley]; | ||
return '<img class="img-smile" alt="' . $smiley . '" title="' . $smiley . '" src="/img/smilies/' . $link . '">'; | ||
$emojiCode = $this->smilies[$smiley]; | ||
|
||
$emo = new Emoji($emojiCode); | ||
|
||
$src = "https://cdn.jsdelivr.net/gh/twitter/[email protected]/assets/svg/$emo->unified.svg"; | ||
return "<img class=\"img-smile\" alt=\"{$emo->name}\" title=\"{$emo->name}\" src=\"{$src}\" style=\"width:16px\">"; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,6 +50,7 @@ | |
.img-smile { | ||
display: inline; | ||
width: 16px; | ||
} | ||
h1, h2, h3, h4, h5, h6 { | ||
|