-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathParser.php
268 lines (233 loc) · 8.22 KB
/
Parser.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<?php
/**
* Parser.php
* 内容解析器
*/
Class BracketDown_Parser {
/**
* 解析注音
*/
static public function ruby($text)
{
$reg = '/\{\{(.*?):(.*?)\}\}/s';
$rp = '<ruby>${1}<rp>(</rp><rt>${2}</rt><rp>)</rp></ruby>';
$text = preg_replace($reg,$rp,$text);
return $text;
}
/**
* 解析键盘按键
*/
static public function kbd($text)
{
$text = preg_replace('/\[\[(.*?)\]\]/s','<kbd>${1}</kbd>',$text);
return $text;
}
/**
* 解析 <details>
*/
static public function details($text)
{
$text = preg_replace(
'/\[details sum="(.*?)"\](.*?)\[\/details\]/s',
'<details class="bracketdown"><summary>${1}</summary><div class="bracketdown-details-content">${2}</div></details>',
$text);
$text = preg_replace('/\[details\](.*?)\[summary\](.*?)\[\/summary\](.*?)\[\/details\]/s','<details class="bracketdown"><summary>${2}</summary><div class="bracketdown-details-content">${3}</div></details>',$text);
return $text;
}
/**
* 解析文字块
*/
static public function block($text)
{
$text = preg_replace(
'/\[block\](.*?)\[\/block\]/s',
'<div class="bracketdown-block">${1}</div>',
$text);
$text = preg_replace(
'/\[notice\](.*?)\[\/notice\]/s',
'<div class="bracketdown-block bracketdown-notice">${1}</div>',
$text);
return $text;
}
/**
* 解析按钮
*/
static public function btn($text)
{
$text = preg_replace(
'/\[btn link="\<a(.*?)href="(.*?)"(.*?)\>(.*?)\<\/a\>"\](.*?)\[\/btn\]/s',
'<a href="${2}" class="bracketdown-button">${5}</a>',
$text);
$text = preg_replace(
'/\[btn link="(.*?)"\](.*?)\[\/btn\]/s',
'<a href="${1}" class="bracketdown-button">${2}</a>',
$text);
return $text;
}
/**
* 解析 Text-Color
*/
static public function textColor($text)
{
$text = preg_replace(
'/\&\{(.*?)\|(.*?)\|(.*?)\}/s',
'<span style="color:${2};background:${3}">${1}</span>'
,$text);
$text = preg_replace(
'/\&\{(.*?)\|(.*?)\}/s',
'<span style="color:${2}">${1}</span>'
,$text);
$text = preg_replace(
'/\%\{(.*?)\|(.*?)\}/s',
'<span style="background:${2}">${1}</span>'
,$text);
return $text;
}
/**
* 解析下划线
*/
static public function underline($text)
{
$text = preg_replace(
'/\?\?(.*?)\?\?/s',
'<span class="bracketdown-underline">${1}</span>'
,$text);
return $text;
}
/**
* 解析站内文章卡片
* credit youranreus/G
* edited
*/
static public function postCard($text)
{
if (preg_match_all("/\[art\](.*?)\[\/art\]/s", $text, $matches)){
$i = 0;
foreach($matches[1] as $id) {
$db = Typecho_Db::get();
$result = $db->fetchAll($db->select()->from('table.contents')
->where('status = ?', 'publish')
->where('type = ?', 'post')
->where('cid = ?', $id)
);
if($result){
$val = Typecho_Widget::widget('Widget_Abstract_Contents')->push($result[0]);
$excerpt = mb_substr($val['text'], 0, 100, 'utf-8');
$text = str_replace(
$matches[0][$i],
'<div class="bracketdown-post">
<h4 class="bracketdown-post-title"><a href="'.$val['permalink'].'">'.$result[0]['title'].'</a></h4>
<p class="bracketdown-post-excerpt">'.$excerpt.'...</p>
<p class="bracketdown-post-meta">
<span>'.date('Y-m-d', $val['created']).'</span>
<a href="'.$val['permalink'].'">阅读全文</a>
</p>
</div>',
$text);
}else{
$text = str_replace(
$matches[0][$i],
'<div class="bracketdown-post"><p>文章 cid 错误,获取不到信息。</p></div>'
);
}
$i++;
}
return $text;
}else{
return $text;
}
}
/**
* 解析文章内直接写入的链接
* 将其变为更容易阅读的形式
*
* github()、bilibili() 等方法的入口
*
* Credit https://github.com/ShangJixin/Typecho-Plugin-superLink/blob/main/JixinParser.php
*/
static public function linkToContent($text) {
if(Typecho_Widget::widget('Widget_Options')->plugin('BracketDown')->ifParseLink=='0') {
//若文章中有直接写入的链接
if(preg_match_all('/<a(.*?)href="(.*?)"(.*?)>(.*?)<\/a>/is',$text,$matches)){
$i=0;
foreach ($matches[0] as $child){
if ($matches[2][$i] != $matches[4][$i]){
$i+=1;
continue;
}
$strip_child = strip_tags($child);
$text = BracketDown_Parser::bilibili(
BracketDown_Parser::github(
$text,
preg_quote($child,'/'),
$strip_child
),
preg_quote($child,'/'),
$strip_child
);
$i+=1;
}
}
}
return $text;
}
/**
* 解析 github 链接
*/
static public function github($text, $replace, $url)
{
if (preg_match("/https?:\/\/github.com\/(.*?)\/(.*?)/is",$url,$matches)){
if (preg_match("/https?:\/\/github.com\/blog\/(.*?)/is",$url,$matches)){
return $text;
}
if (preg_match("/https?:\/\/github.com\/(.*?)\/(.*?)\/(.*?)\//is",$url,$matches)){
return $text;
}
//链接可以被解析
$data = str_replace('https://github.com/','', $url);
$text = preg_replace(
'/'.$replace.'/i',
'<div class="github-card" data-github="'.$data.'">Loading...</div>',
$text
);
} else {
return $text;
}
return $text;
}
/**
* 创建 bilibili 嵌入代码
*/
static public function bilibili($text, $replace, $url)
{
if (preg_match("/https?:\/\/(m.|www.|)bilibili.(com|tv)\/video\/(a|b)v([A-Za-z0-9]+)(\/?.*?&p=|\/?\?p=)?(\d+)?/i", $url, $matches) || preg_match("/https?:\/\/(www.|)(acg|b23).tv\/(a|b)v([A-Za-z0-9]+)(\/?.*?&p=|\/?\?p=)?(\d+)?/i", $url, $matches)){
$text = preg_replace(
'/'.$replace.'/i',
'<iframe src="'.BracketDown_Parser::bilibiliURL($url).'" class="bilibili-video-player" scrolling="no" border="0" frameborder="no" framespacing="0" allowfullscreen="true"> </iframe>',
$text
);
return $text;
} else {
return $text;
}
return $text;
}
/**
* 解析 bilibili 链接
*/
static public function bilibiliURL($url)
{
$quality_request = "&as_wide=1&high_quality=1";
if(preg_match("/https?:\/\/(m.|www.|)bilibili.(com|tv)\/video\/(a|b)v([A-Za-z0-9]+)(\/?.*?&p=|\/?\?p=)?(\d+)?/i", $url, $matches)) {
$vid = (is_numeric($matches[4]) ? 'aid='.$matches[4] : 'bvid='.$matches[4]) . (empty($matches[6]) ? '' : '&page='.intval($matches[6]));
$iframe = 'https://player.bilibili.com/player.html?'.$vid.$quality_request;
return $iframe;
} else if(preg_match("/https?:\/\/(www.|)(acg|b23).tv\/(a|b)v([A-Za-z0-9]+)(\/?.*?&p=|\/?\?p=)?(\d+)?/i", $url, $matches)) {
$vid = (is_numeric($matches[4]) ? 'aid='.$matches[4] : 'bvid='.$matches[4]) . (empty($matches[6]) ? '' : '&page='.intval($matches[6]));
$iframe = 'https://player.bilibili.com/player.html?'.$vid.$quality_request;
return $iframe;
} else {
return 0;
}
}
}