|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace UnicornFail\Emoji\Extension\Twemoji; |
|
6
|
|
|
|
|
7
|
|
|
use League\Configuration\ConfigurationAwareInterface; |
|
8
|
|
|
use League\Configuration\ConfigurationInterface; |
|
9
|
|
|
use UnicornFail\Emoji\Event\DocumentParsedEvent; |
|
10
|
|
|
use UnicornFail\Emoji\Extension\EmojiCoreProcessor; |
|
11
|
|
|
use UnicornFail\Emoji\Node\Emoji; |
|
12
|
|
|
use UnicornFail\Emoji\Node\Image; |
|
13
|
|
|
use UnicornFail\Emoji\Util\HtmlElement; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Replaces emojis with Twemoji images. |
|
17
|
|
|
*/ |
|
18
|
|
|
final class TwemojiProcessor implements ConfigurationAwareInterface |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var ConfigurationInterface |
|
22
|
|
|
* |
|
23
|
|
|
* @psalm-readonly-allow-private-mutation |
|
24
|
|
|
* @psalm-suppress PropertyNotSetInConstructor |
|
25
|
|
|
*/ |
|
26
|
|
|
private $config; |
|
27
|
|
|
|
|
28
|
|
|
public function __invoke(DocumentParsedEvent $e): void |
|
29
|
|
|
{ |
|
30
|
|
|
$urlBase = (string) $this->config->get('twemoji.urlBase'); |
|
31
|
|
|
|
|
32
|
|
|
$classPrefix = (string) $this->config->get('twemoji.classPrefix'); |
|
33
|
|
|
|
|
34
|
|
|
/** @var ?int $size */ |
|
35
|
|
|
$size = $this->config->get('twemoji.size'); |
|
36
|
|
|
|
|
37
|
|
|
$relativeSvg = (bool) $this->config->get('twemoji.relativeSvg'); |
|
38
|
|
|
|
|
39
|
|
|
$type = (string) $this->config->get('twemoji.type'); |
|
40
|
|
|
|
|
41
|
|
|
$walker = $e->getDocument()->walker(); |
|
42
|
|
|
while ($event = $walker->next()) { |
|
43
|
|
|
$node = $event->getNode(); |
|
44
|
|
|
if (! ($node instanceof Emoji) || $node->hexcode === null) { |
|
45
|
|
|
continue; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
$parsedType = $node->getParsedType(); |
|
49
|
|
|
$configPath = EmojiCoreProcessor::CONFIG_PATHS[$parsedType] ?? null; |
|
50
|
|
|
$convertTo = null; |
|
51
|
|
|
|
|
52
|
|
|
if ($configPath && $this->config->exists($configPath)) { |
|
53
|
|
|
$convertTo = (string) ($this->config->get($configPath) ?? ''); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
// Only convert types that are twemoji. |
|
57
|
|
|
if ($convertTo !== 'twemoji') { |
|
58
|
|
|
continue; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$url = \sprintf( |
|
62
|
|
|
'%s/%s/%s.%s', |
|
63
|
|
|
$urlBase, |
|
64
|
|
|
$type === 'png' ? '72x72' : 'svg', |
|
65
|
|
|
\strtolower($node->hexcode), |
|
66
|
|
|
$type |
|
67
|
|
|
); |
|
68
|
|
|
|
|
69
|
|
|
$image = new Image($node->getParsedValue(), $node, $url, $node->annotation); |
|
70
|
|
|
|
|
71
|
|
|
/** @var string[] $classes */ |
|
72
|
|
|
$classes = (array) $this->config->get('twemoji.classes'); |
|
73
|
|
|
$image->addClass(...$classes); |
|
74
|
|
|
|
|
75
|
|
|
if ($node->annotation !== null) { |
|
76
|
|
|
$image->addClass(HtmlElement::cleanCssIdentifier($classPrefix |
|
77
|
|
|
? $classPrefix . $node->annotation |
|
78
|
|
|
: $node->annotation)); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
switch ($type) { |
|
82
|
|
|
case 'png': |
|
83
|
|
|
// Set default size for PNG images. |
|
84
|
|
|
if ($size === null) { |
|
85
|
|
|
$size = 72; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
if ($size) { |
|
89
|
|
|
$image->attributes->set('height', (string) $size); |
|
90
|
|
|
$image->attributes->set('width', (string) $size); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
break; |
|
94
|
|
|
|
|
95
|
|
|
case 'svg': |
|
96
|
|
|
// Ensure SVG images aren't massive and relative to its surroundings. |
|
97
|
|
|
if ($relativeSvg) { |
|
98
|
|
|
$image->attributes->set('style', 'width: 1em; height: 1em;'); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
break; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
$node->replaceWith($image); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function setConfiguration(ConfigurationInterface $configuration): void |
|
109
|
|
|
{ |
|
110
|
|
|
$this->config = $configuration; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|