1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Highlighter |
4
|
|
|
* |
5
|
|
|
* Copyright (C) 2016, Some right reserved. |
6
|
|
|
* |
7
|
|
|
* @author Kacper "Kadet" Donat <[email protected]> |
8
|
|
|
* |
9
|
|
|
* Contact with author: |
10
|
|
|
* Xmpp: [email protected] |
11
|
|
|
* E-mail: [email protected] |
12
|
|
|
* |
13
|
|
|
* From Kadet with love. |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace Kadet\Highlighter\Language; |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
use Kadet\Highlighter\KeyLighter; |
20
|
|
|
use Kadet\Highlighter\Matcher\DelegateRegexMatcher; |
21
|
|
|
use Kadet\Highlighter\Matcher\RegexMatcher; |
22
|
|
|
use Kadet\Highlighter\Parser\Rule; |
23
|
|
|
use Kadet\Highlighter\Parser\Token\LanguageToken; |
24
|
|
|
use Kadet\Highlighter\Parser\Token\Token; |
25
|
|
|
use Kadet\Highlighter\Parser\TokenFactoryInterface; |
26
|
|
|
use Kadet\Highlighter\Parser\Validator\Validator; |
27
|
|
|
|
28
|
|
|
class Markdown extends Html |
29
|
|
|
{ |
30
|
|
|
protected $_options = [ |
31
|
|
|
'variables' => false, |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Tokenization rules setup |
36
|
|
|
*/ |
37
|
|
|
public function setupRules() |
38
|
|
|
{ |
39
|
|
|
parent::setupRules(); |
40
|
|
|
|
41
|
|
|
$this->rules->validator = new Validator( |
42
|
|
|
['!format.block.code', '!format.monospace', '!keyword.escape', '!operator', '!tag'] |
43
|
|
|
); |
44
|
|
|
|
45
|
|
|
$this->rules->addMany([ |
46
|
|
|
'format.header' => [ |
47
|
|
|
new Rule(new RegexMatcher('/^\s{0,3}(#+.+?)$/m')), |
48
|
|
|
new Rule(new RegexMatcher('/^(.+?)^(?:-+|=+)$/m')) |
49
|
|
|
], |
50
|
|
|
'format.italics' => new Rule( |
51
|
|
|
new RegexMatcher('/(?:^|[^*_])(?P<italics>(?P<i>[*_])(?>[^*_\n]|(?:(?P<b>[*_]{2})(?>[^*_\n]|(?&italics))*?\g{b}))+\g{i})/'), [ |
52
|
|
|
'italics' => Token::NAME |
53
|
|
|
] |
54
|
|
|
), |
55
|
|
|
'format.bold' => new Rule( |
56
|
|
|
new RegexMatcher('/(?P<bold>(?P<b>\*\*|__)(?>[^*_\n]|(?:(?P<i>[*_]{2})(?>[^*_\n]|(?&bold))*?\g{i}))+\g{b})/', [ |
57
|
|
|
'bold' => Token::NAME |
58
|
|
|
]) |
59
|
|
|
), |
60
|
|
|
'format.strike' => new Rule(new RegexMatcher('/(~~.+?~~)/')), |
61
|
|
|
'format.monospace' => [ |
62
|
|
|
new Rule(new RegexMatcher('/(?:[^`]|^)(`.*?`)/')), |
63
|
|
|
new Rule(new RegexMatcher('/(``.*?``)/')), |
64
|
|
|
new Rule(new RegexMatcher('/^((?:(?: {4,}|\t).*?(?>\n|$)+?)+)/m')), |
65
|
|
|
], |
66
|
|
|
|
67
|
|
|
'operator.list.ordered' => new Rule(new RegexMatcher('/^\s*(\d+[.)])/m')), |
68
|
|
|
'operator.list.unordered' => new Rule(new RegexMatcher('/^\s*([-+*])/m'), [ |
69
|
|
|
'context' => ['none'], |
70
|
|
|
'priority' => 1 |
71
|
|
|
]), |
72
|
|
|
|
73
|
|
|
'string.quote' => new Rule(new RegexMatcher('/((?:^>.*?\n)+)/m')), |
74
|
|
|
'format.block.code' => [ |
75
|
|
|
new Rule( |
76
|
|
|
new DelegateRegexMatcher( |
77
|
|
|
'/^```(.*?)\r?\n(.*?)\r?\n^```/ms', |
78
|
|
|
function($match, TokenFactoryInterface $factory) { |
79
|
|
|
$lang = KeyLighter::get()->getLanguage($match[1][0]); |
80
|
|
|
yield $factory->create(Token::NAME, ['pos' => $match[0][1], 'length' => strlen($match[0][0])]); |
81
|
|
|
yield $factory->create( |
82
|
|
|
"language.{$lang->getIdentifier()}", [ |
83
|
|
|
'pos' => $match[2][1], |
84
|
|
|
'length' => strlen($match[2][0]), |
85
|
|
|
'inject' => $lang, |
86
|
|
|
'class' => LanguageToken::class, |
87
|
|
|
] |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
), [ |
91
|
|
|
'context' => Validator::everywhere(), |
92
|
|
|
'postProcess' => true, |
93
|
|
|
'priority' => 1000 |
94
|
|
|
] |
95
|
|
|
), |
96
|
|
|
], |
97
|
|
|
|
98
|
|
|
'keyword.escape' => new Rule(new RegexMatcher('/(\\\.)/')), |
99
|
|
|
|
100
|
|
|
'operator.horizontal' => new Rule(new RegexMatcher('/^\s{,3}(([-*_])( ?\2)+)$/m'), [ |
101
|
|
|
'priority' => 2 |
102
|
|
|
]), |
103
|
|
|
|
104
|
|
|
'symbol.link' => new Rule(new RegexMatcher('/[^!](\[(?:[^\[\]]|!\[.*?\]\(.*?\))*\])/i')), |
105
|
|
|
'symbol.url' => new Rule(new RegexMatcher('#(<(https?://|[^/])[-A-Za-z0-9+&@\#/%?=~_|!:,.;]+[-A-Za-z0-9+&@\#/%=~_|]>|https?://[-A-Za-z0-9+&@\#/%?=~_|!:,.;]+[-A-Za-z0-9+&@\#/%=~_|])#i'), [ |
106
|
|
|
'priority' => 0, |
107
|
|
|
]), |
108
|
|
|
'symbol.image' => new Rule(new RegexMatcher('/(!)(\[.*?\])\(.*?\)/i', [ |
109
|
|
|
1 => 'operator.image', |
110
|
|
|
2 => '$.title', |
111
|
|
|
])), |
112
|
|
|
]); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Unique language identifier, for example 'php' |
117
|
|
|
* |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
|
|
public function getIdentifier() |
121
|
|
|
{ |
122
|
|
|
return 'markdown'; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|