|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of GitterBot package. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Serafim <[email protected]> |
|
6
|
|
|
* @date 09.10.2015 16:56 |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Interfaces\Gitter; |
|
13
|
|
|
|
|
14
|
|
|
use Converter\BBCodeConverter; |
|
15
|
|
|
use Domains\Bot\TextParserInterface; |
|
16
|
|
|
|
|
17
|
|
|
class TextParser extends BBCodeConverter implements TextParserInterface |
|
18
|
|
|
{ |
|
19
|
|
|
|
|
20
|
|
|
public function __construct() {} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @brief Replaces BBCode italic. |
|
24
|
|
|
*/ |
|
25
|
|
|
protected function replaceItalic() { |
|
26
|
|
|
|
|
27
|
|
|
$this->text = preg_replace_callback('%\[i\]([\W\D\w\s]*?)\[/i\]%iu', |
|
28
|
|
|
|
|
29
|
|
|
function ($matches) { |
|
30
|
|
|
return "_".trim($matches[1], " ")."_"; |
|
31
|
|
|
}, |
|
32
|
|
|
|
|
33
|
|
|
$this->text |
|
34
|
|
|
); |
|
35
|
|
|
|
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @brief Removes BBCode center. |
|
40
|
|
|
*/ |
|
41
|
|
|
protected function removeUser() { |
|
42
|
|
|
|
|
43
|
|
|
$this->text = preg_replace_callback('%\[user\]([\W\D\w\s]*?)\[/user\]%iu', |
|
44
|
|
|
|
|
45
|
|
|
function ($matches) { |
|
46
|
|
|
$username = trim($matches[1]); |
|
47
|
|
|
|
|
48
|
|
|
return empty($username) ? "" : "@{$username}"; |
|
49
|
|
|
}, |
|
50
|
|
|
|
|
51
|
|
|
$this->text); |
|
52
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @brief Removes BBCode center. |
|
57
|
|
|
*/ |
|
58
|
|
|
protected function removePre() { |
|
59
|
|
|
|
|
60
|
|
|
$this->text = preg_replace_callback('%\[pre\]([\W\D\w\s]*?)\[/pre\]%iu', |
|
61
|
|
|
|
|
62
|
|
|
function ($matches) { |
|
63
|
|
|
return "`{$matches[1]}``"; |
|
64
|
|
|
}, |
|
65
|
|
|
|
|
66
|
|
|
$this->text); |
|
67
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @brief Removes BBCode center. |
|
72
|
|
|
*/ |
|
73
|
|
|
protected function removeHeader() { |
|
74
|
|
|
|
|
75
|
|
|
$this->text = preg_replace_callback('%\[h([0-6]{1})\]([\W\D\w\s]*?)\[/h[0-6]?\]%iu', |
|
76
|
|
|
|
|
77
|
|
|
function ($matches) { |
|
78
|
|
|
$size = $matches[1]; |
|
79
|
|
|
|
|
80
|
|
|
return str_repeat('#', $size).' '.$matches[2].PHP_EOL; |
|
81
|
|
|
}, |
|
82
|
|
|
|
|
83
|
|
|
$this->text); |
|
84
|
|
|
|
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function toMarkdown() |
|
88
|
|
|
{ |
|
89
|
|
|
parent::toMarkdown(); |
|
90
|
|
|
$this->removeHeader(); |
|
91
|
|
|
$this->removePre(); |
|
92
|
|
|
$this->removeUser(); |
|
93
|
|
|
|
|
94
|
|
|
return $this->text; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param string $message |
|
99
|
|
|
* |
|
100
|
|
|
* @return string |
|
101
|
|
|
*/ |
|
102
|
|
|
public function parse(string $message) |
|
103
|
|
|
{ |
|
104
|
|
|
$this->text = $message; |
|
105
|
|
|
|
|
106
|
|
|
return $this->toMarkdown(); |
|
107
|
|
|
} |
|
108
|
|
|
} |