forked from typecho/typecho
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMarkdown.php
65 lines (56 loc) · 1.45 KB
/
Markdown.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
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
/**
* Markdown解析
*
* @package Markdown
* @copyright Copyright (c) 2014 Typecho team (http://www.typecho.org)
* @license GNU General Public License 2.0
*/
class Markdown
{
/**
* convert
*
* @param string $text
* @return string
*/
public static function convert($text)
{
static $parser;
if (empty($parser)) {
$parser = new HyperDown();
$parser->hook('afterParseCode', function ($html) {
return preg_replace("/<code class=\"([_a-z0-9-]+)\">/i", "<code class=\"lang-\\1\">", $html);
});
$parser->enableHtml(true);
}
return str_replace('<p><!--more--></p>', '<!--more-->', $parser->makeHtml($text));
}
/**
* transerCodeClass
*
* @param string $html
* @return string
*/
public static function transerCodeClass($html)
{
return preg_replace("/<code class=\"([_a-z0-9-]+)\">/i", "<code class=\"lang-\\1\">", $html);
}
/**
* @param $html
* @return mixed
*/
public static function transerComment($html)
{
return preg_replace_callback("/<!\-\-(.+?)\-\->/s", array('Markdown', 'transerCommentCallback'), $html);
}
/**
* @param $matches
* @return string
*/
public static function transerCommentCallback($matches)
{
return self::$parser->makeHolder($matches[0]);
}
}