|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Class Markdown |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://www.icy2003.com/ |
|
6
|
|
|
* @author icy2003 <[email protected]> |
|
7
|
|
|
* @copyright Copyright (c) 2017, icy2003 |
|
8
|
|
|
*/ |
|
9
|
|
|
namespace icy2003\php\ihelpers; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Markdown 类 |
|
13
|
|
|
*/ |
|
14
|
|
|
class Markdown |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* 预处理文本内容(html 转义、去除两边空白) |
|
19
|
|
|
* |
|
20
|
|
|
* @param string $text |
|
21
|
|
|
* |
|
22
|
|
|
* @return string |
|
23
|
|
|
*/ |
|
24
|
|
|
private static function __text($text) |
|
25
|
|
|
{ |
|
26
|
|
|
return Html::encode(trim($text)); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* 标题 |
|
31
|
|
|
* |
|
32
|
|
|
* @param string $text 标题内容 |
|
33
|
|
|
* @param int $level 标题层级,默认1,一级标题 |
|
34
|
|
|
* |
|
35
|
|
|
* @return string |
|
36
|
|
|
*/ |
|
37
|
|
|
public static function title($text, $level = 1) |
|
38
|
|
|
{ |
|
39
|
|
|
return str_repeat('#', $level) . ' ' . self::__text($text); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* 字体加粗 |
|
44
|
|
|
* |
|
45
|
|
|
* @param string $text 文本内容 |
|
46
|
|
|
* |
|
47
|
|
|
* @return string |
|
48
|
|
|
*/ |
|
49
|
|
|
public static function bold($text) |
|
50
|
|
|
{ |
|
51
|
|
|
return '**' . self::__text($text) . '**'; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* 斜体 |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $text 文本内容 |
|
58
|
|
|
* |
|
59
|
|
|
* @return string |
|
60
|
|
|
*/ |
|
61
|
|
|
public static function italics($text) |
|
62
|
|
|
{ |
|
63
|
|
|
return '*' . self::__text($text) . '*'; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* a 链接 |
|
68
|
|
|
* |
|
69
|
|
|
* @todo 兼容标准语法 |
|
70
|
|
|
* |
|
71
|
|
|
* @param string $text 链接文字 |
|
72
|
|
|
* @param string $url 链接地址 |
|
73
|
|
|
* @param string $title 链接标题 |
|
74
|
|
|
* @param bool $openInNew 是否在新窗口打开 |
|
75
|
|
|
* |
|
76
|
|
|
* @return string |
|
77
|
|
|
*/ |
|
78
|
|
|
public static function a($text, $url, $title = null, $openInNew = false) |
|
79
|
|
|
{ |
|
80
|
|
|
return '[' . self::__text($text) . '](' . $url . ' "' . (null === $title ? $text : $title) . '"' . ')' . (true === $openInNew ? '{:target="_blank"}' : ''); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* 删除线 |
|
85
|
|
|
* |
|
86
|
|
|
* @param string $text 文本内容 |
|
87
|
|
|
* |
|
88
|
|
|
* @return string |
|
89
|
|
|
*/ |
|
90
|
|
|
public static function strikethrough($text) |
|
91
|
|
|
{ |
|
92
|
|
|
return '~~' . self::__text($text) . '~~'; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* 引用 |
|
97
|
|
|
* |
|
98
|
|
|
* @param string $text 文本内容 |
|
99
|
|
|
* |
|
100
|
|
|
* @return string |
|
101
|
|
|
*/ |
|
102
|
|
|
public static function quote($text) |
|
103
|
|
|
{ |
|
104
|
|
|
return '> ' . self::__text($text); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* 分隔符 |
|
109
|
|
|
* |
|
110
|
|
|
* @return string |
|
111
|
|
|
*/ |
|
112
|
|
|
public static function separator() |
|
113
|
|
|
{ |
|
114
|
|
|
return '---'; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* 图片 |
|
119
|
|
|
* |
|
120
|
|
|
* @param string $url 图片链接 |
|
121
|
|
|
* @param string $title 图片标题 |
|
122
|
|
|
* @param string $alt alt |
|
123
|
|
|
* |
|
124
|
|
|
* @return string |
|
125
|
|
|
*/ |
|
126
|
|
|
public static function image($url, $title = null, $alt = null) |
|
127
|
|
|
{ |
|
128
|
|
|
return ''; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* 无序列表 |
|
133
|
|
|
* |
|
134
|
|
|
* @todo 支持多维数组 |
|
135
|
|
|
* |
|
136
|
|
|
* @param array $array 列表数组 |
|
137
|
|
|
* |
|
138
|
|
|
* @return string |
|
139
|
|
|
*/ |
|
140
|
|
|
public static function ul($array) |
|
141
|
|
|
{ |
|
142
|
|
|
return implode(PHP_EOL, array_map(function($row) { |
|
143
|
|
|
return '- ' . self::__text($row); |
|
144
|
|
|
}, $array)); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* 有序列表 |
|
149
|
|
|
* |
|
150
|
|
|
* @todo 支持多维数组 |
|
151
|
|
|
* |
|
152
|
|
|
* @param array $array 列表数组 |
|
153
|
|
|
* |
|
154
|
|
|
* @return string |
|
155
|
|
|
*/ |
|
156
|
|
|
public static function ol($array) |
|
157
|
|
|
{ |
|
158
|
|
|
return implode(PHP_EOL, array_map(function($row) { |
|
159
|
|
|
static $i = 1; |
|
160
|
|
|
return ($i++) . '. ' . self::__text($row); |
|
161
|
|
|
}, $array)); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* 代码 |
|
166
|
|
|
* |
|
167
|
|
|
* @param string $text 代码内容 |
|
168
|
|
|
* |
|
169
|
|
|
* @return string |
|
170
|
|
|
*/ |
|
171
|
|
|
public static function code($text) |
|
172
|
|
|
{ |
|
173
|
|
|
return '`' . self::__text($text) . '`'; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* 多行代码 |
|
178
|
|
|
* |
|
179
|
|
|
* @param string $text 多行代码内容 |
|
180
|
|
|
* @param string $type 代码类型 |
|
181
|
|
|
* |
|
182
|
|
|
* @return string |
|
183
|
|
|
*/ |
|
184
|
|
|
public static function codes($text, $type = null) |
|
185
|
|
|
{ |
|
186
|
|
|
return '```' . $type . PHP_EOL . self::__text($text) . PHP_EOL . '```'; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* 表格 |
|
191
|
|
|
* |
|
192
|
|
|
* @todo 样式和反转 |
|
193
|
|
|
* |
|
194
|
|
|
* @param array $array 表格数组,支持索引和关联数组,索引时要求为二维数组,第一行被认为是表头,关联时键被当成表头 |
|
195
|
|
|
* @param array $styleArray 样式数组 |
|
196
|
|
|
* @param bool $transpose 是否反转 |
|
197
|
|
|
* |
|
198
|
|
|
* @return string |
|
199
|
|
|
*/ |
|
200
|
|
|
public static function table($array, /** @scrutinizer ignore-unused */$styleArray = [], /** @scrutinizer ignore-unused */$transpose = false) |
|
201
|
|
|
{ |
|
202
|
|
|
if (empty($array)) { |
|
203
|
|
|
return ''; |
|
204
|
|
|
} |
|
205
|
|
|
$isAssoc = Arrays::isAssoc($array); |
|
206
|
|
|
if (true === $isAssoc) { |
|
207
|
|
|
$title = array_keys($array); |
|
208
|
|
|
$rows = array_values($array); |
|
209
|
|
|
} else { |
|
210
|
|
|
$title = array_shift($array); |
|
211
|
|
|
$rows = $array; |
|
212
|
|
|
} |
|
213
|
|
|
$lineFunc = function($arr) { |
|
214
|
|
|
return '|' . implode('|', array_map(function($line) { |
|
215
|
|
|
return ' ' . self::__text($line) . ' '; |
|
216
|
|
|
}, $arr)) . '|'; |
|
217
|
|
|
}; |
|
218
|
|
|
$string = ''; |
|
219
|
|
|
$string .= $lineFunc($title) . PHP_EOL; |
|
220
|
|
|
$string .= $lineFunc(Arrays::fill(0, count($title), ':-:')) . PHP_EOL; |
|
221
|
|
|
foreach ($rows as $row) { |
|
222
|
|
|
$string .= $lineFunc($row) . PHP_EOL; |
|
223
|
|
|
} |
|
224
|
|
|
return rtrim($string); |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* 待办事项行 |
|
229
|
|
|
* |
|
230
|
|
|
* @param string $line 任务行 |
|
231
|
|
|
* @param bool $isDone 是否完成 |
|
232
|
|
|
* @param int $level 层级,默认1 |
|
233
|
|
|
* |
|
234
|
|
|
* @return string |
|
235
|
|
|
*/ |
|
236
|
|
|
public static function todo($line, $isDone, $level = 1) |
|
237
|
|
|
{ |
|
238
|
|
|
return str_repeat(' ', $level - 1) . '- [' . (true === $isDone ? 'x' : ' ') . '] ' . $line; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* 生成目录 |
|
243
|
|
|
* |
|
244
|
|
|
* @return string |
|
245
|
|
|
*/ |
|
246
|
|
|
public static function toc() |
|
247
|
|
|
{ |
|
248
|
|
|
return '[TOC]'; |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
/** |
|
252
|
|
|
* markdown 文档开始 |
|
253
|
|
|
* |
|
254
|
|
|
* @return void |
|
255
|
|
|
*/ |
|
256
|
|
|
public static function start() |
|
257
|
|
|
{ |
|
258
|
|
|
ob_start(); |
|
259
|
|
|
ob_implicit_flush(0); |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
/** |
|
263
|
|
|
* markdown 文档结束并转成字符串 |
|
264
|
|
|
* |
|
265
|
|
|
* @return string |
|
266
|
|
|
*/ |
|
267
|
|
|
public static function toString() |
|
268
|
|
|
{ |
|
269
|
|
|
return ob_get_clean(); |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
/** |
|
273
|
|
|
* markdown 文档结束并导出到文件 |
|
274
|
|
|
* |
|
275
|
|
|
* @param string $file 文件路径 |
|
276
|
|
|
* |
|
277
|
|
|
* @return void |
|
278
|
|
|
*/ |
|
279
|
|
|
public static function toFile($file = null) |
|
280
|
|
|
{ |
|
281
|
|
|
$content = self::toString(); |
|
282
|
|
|
null === $file && $file = './' . date('YmdHis') . '.md'; |
|
283
|
|
|
file_put_contents($file, $content); |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
} |
|
287
|
|
|
|