1
|
|
|
<?php |
2
|
|
|
namespace Xetaravel\Markdown\Emoji; |
3
|
|
|
|
4
|
|
|
use League\CommonMark\Inline\Element\Image; |
5
|
|
|
use League\CommonMark\Inline\Parser\AbstractInlineParser; |
6
|
|
|
use League\CommonMark\InlineParserContext; |
7
|
|
|
|
8
|
|
|
class EmojiParser extends AbstractInlineParser |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The emoji mappings. |
12
|
|
|
* |
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
protected $map; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The emoji extension. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $ext = '.png'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The emoji path directory. |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $path; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Create a new emoji parser instance. |
33
|
|
|
* |
34
|
|
|
* @return void |
|
|
|
|
35
|
|
|
*/ |
36
|
|
|
public function __construct() |
37
|
|
|
{ |
38
|
|
|
$this->map = include __DIR__ . "/EmojiSheet.php"; |
39
|
|
|
$this->path = config('app.url') . '/images/emojis/'; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Get the characters that must be matched. |
44
|
|
|
* |
45
|
|
|
* @return array |
46
|
|
|
*/ |
47
|
|
|
public function getCharacters() |
48
|
|
|
{ |
49
|
|
|
return [':']; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Parse a line and determine if it contains an emoji. If it does, |
54
|
|
|
* then we do the necessary. |
55
|
|
|
* |
56
|
|
|
* @param \League\CommonMark\InlineParserContext $inlineContext |
57
|
|
|
* |
58
|
|
|
* @return bool |
59
|
|
|
*/ |
60
|
|
|
public function parse(InlineParserContext $inlineContext) |
61
|
|
|
{ |
62
|
|
|
$cursor = $inlineContext->getCursor(); |
63
|
|
|
|
64
|
|
|
$previous = $cursor->peek(-1); |
65
|
|
|
if ($previous !== null && $previous !== ' ') { |
66
|
|
|
return false; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$saved = $cursor->saveState(); |
70
|
|
|
|
71
|
|
|
$cursor->advance(); |
72
|
|
|
|
73
|
|
|
$handle = $cursor->match('/^[a-z0-9\+\-_]+:/'); |
74
|
|
|
|
75
|
|
|
if (!$handle) { |
76
|
|
|
$cursor->restoreState($saved); |
77
|
|
|
|
78
|
|
|
return false; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$next = $cursor->peek(0); |
82
|
|
|
|
83
|
|
|
if ($next !== null && $next !== ' ') { |
84
|
|
|
$cursor->restoreState($saved); |
85
|
|
|
|
86
|
|
|
return false; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$key = substr($handle, 0, -1); |
90
|
|
|
|
91
|
|
|
if (!in_array($key, $this->map)) { |
92
|
|
|
$cursor->restoreState($saved); |
93
|
|
|
|
94
|
|
|
return false; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$fileName = $this->path . $key . $this->ext; |
98
|
|
|
|
99
|
|
|
$inline = new Image($fileName, $key); |
100
|
|
|
$inline->data['attributes'] = [ |
101
|
|
|
'class' => 'emoji', |
102
|
|
|
'data-emoji' => $key, |
103
|
|
|
'width' => '24', |
104
|
|
|
'height' => '24', |
105
|
|
|
'title' => ':' . $key . ':' |
106
|
|
|
]; |
107
|
|
|
$inlineContext->getContainer()->appendChild($inline); |
108
|
|
|
|
109
|
|
|
return true; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.