forked from cebe/markdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GithubMarkdown.php
162 lines (144 loc) · 3.49 KB
/
GithubMarkdown.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
<?php
/**
* @copyright Copyright (c) 2014 Carsten Brandt
* @license https://github.com/cebe/markdown/blob/master/LICENSE
* @link https://github.com/cebe/markdown#readme
*/
namespace cebe\markdown;
use cebe\markdown\block\TableTrait;
/**
* Markdown parser for github flavored markdown.
*
* @author Carsten Brandt <[email protected]>
*/
class GithubMarkdown extends Markdown
{
use TableTrait;
/**
* @var boolean whether to interpret newlines as `<br />`-tags.
* This feature is useful for comments where newlines are often meant to be real new lines.
*/
public $enableNewlines = false;
/**
* @inheritDoc
*/
protected $escapeCharacters = [
// from Markdown
'\\', // backslash
'`', // backtick
'*', // asterisk
'_', // underscore
'{', '}', // curly braces
'[', ']', // square brackets
'(', ')', // parentheses
'#', // hash mark
'+', // plus sign
'-', // minus sign (hyphen)
'.', // dot
'!', // exclamation mark
'<', '>',
// added by GithubMarkdown
':', // colon
'|', // pipe
];
/**
* @inheritDoc
*/
protected function inlineMarkers()
{
return parent::inlineMarkers() + [
'http' => 'parseUrl',
'ftp' => 'parseUrl',
'~~' => 'parseStrike',
'|' => 'parseTd',
];
}
// block parsing
/**
* @inheritDoc
*/
protected function identifyLine($lines, $current)
{
if (isset($lines[$current]) && (strncmp($lines[$current], '```', 3) === 0 || strncmp($lines[$current], '~~~', 3) === 0)) {
return 'fencedCode';
}
if ($this->identifyTable($lines, $current)) {
return 'table';
}
return parent::identifyLine($lines, $current);
}
/**
* Consume lines for a fenced code block
*/
protected function consumeFencedCode($lines, $current)
{
// consume until ```
$block = [
'type' => 'code',
'content' => [],
];
$line = rtrim($lines[$current]);
$fence = substr($line, 0, $pos = strrpos($line, $line[0]) + 1);
$language = substr($line, $pos);
if (!empty($language)) {
$block['language'] = $language;
}
for ($i = $current + 1, $count = count($lines); $i < $count; $i++) {
if (rtrim($line = $lines[$i]) !== $fence) {
$block['content'][] = $line;
} else {
break;
}
}
return [$block, $i];
}
// inline parsing
/**
* Parses the strikethrough feature.
*/
protected function parseStrike($markdown)
{
if (preg_match('/^~~(.+?)~~/', $markdown, $matches)) {
return [
'<del>' . $this->parseInline($matches[1]) . '</del>',
strlen($matches[0])
];
}
return [$markdown[0] . $markdown[1], 2];
}
/**
* Parses urls and adds auto linking feature.
*/
protected function parseUrl($markdown)
{
$pattern = <<<REGEXP
/(?(R) # in case of recursion match parentheses
\(((?>[^\s()]+)|(?R))*\)
| # else match a link with title
^(https?|ftp):\/\/(([^\s()]+)|(?R))+(?<![\.,:;\'"!\?\s])
)/x
REGEXP;
if (!in_array('parseLink', $this->context) && preg_match($pattern, $markdown, $matches)) {
$href = htmlspecialchars($matches[0], ENT_COMPAT | ENT_HTML401, 'UTF-8');
$text = htmlspecialchars(urldecode($matches[0]), ENT_NOQUOTES, 'UTF-8');
return [
"<a href=\"$href\">$text</a>",
strlen($matches[0])
];
}
return [substr($markdown, 0, 4), 4];
}
/**
* @inheritdocs
*
* Parses a newline indicated by two spaces on the end of a markdown line.
*/
protected function parsePlainText($text)
{
if ($this->enableNewlines) {
return preg_replace("/( \n|\n)/", $this->html5 ? "<br>\n" : "<br />\n", $text);
} else {
return parent::parsePlainText($text);
}
}
}