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
|
|
|
/** |
21
|
|
|
* @param string $text |
22
|
|
|
* |
23
|
|
|
* @return string |
24
|
|
|
*/ |
25
|
|
|
public static function escape($text) |
26
|
|
|
{ |
27
|
|
|
$text = preg_replace('/(?P<char>(?:_|\*))(.+?)(?P=char)/isu', '\\\$1$2\\\$1', $text); |
28
|
|
|
$text = preg_replace('/\*\*(.+?)\*\*/isu', '*\\*$1*\\*', $text); |
29
|
|
|
$text = preg_replace('/\-\-(\-)+/isu', '\-\-\-', $text); |
30
|
|
|
$text = preg_replace('/\n*^(?!\w\s+)(#)/isu', '\\#', $text); |
31
|
|
|
$text = preg_replace('/\[(.*?)\]\((.*?)\)/isu', '\\[$1\\]\\($2\\)', $text); |
32
|
|
|
|
33
|
|
|
return $text; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function __construct() {} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @brief replace BBCode user. |
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 replace BBCode pre. |
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 replace BBCode header. |
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
|
|
|
} |