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 Language |
29
|
|
|
{ |
30
|
|
|
protected $_options = [ |
31
|
|
|
'variables' => false, |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Tokenization rules setup |
36
|
|
|
*/ |
37
|
|
|
public function setupRules() |
38
|
|
|
{ |
39
|
|
|
$this->rules->validator = new Validator(['!format.block.code', '!format.monospace', '!keyword.escape', '!operator']); |
40
|
|
|
$this->rules->addMany([ |
41
|
|
|
'format.header' => [ |
42
|
|
|
new Rule(new RegexMatcher('/^(#+.+?)$/m')), |
43
|
|
|
new Rule(new RegexMatcher('/^(.+?)^(?:-+|=+)$/m')) |
44
|
|
|
], |
45
|
|
|
'format.italics' => new Rule( |
46
|
|
|
new RegexMatcher('/(?:^|[^*_])(?P<italics>(?P<i>[*_])(?>[^*_\n]|(?:(?P<b>[*_]{2})(?>[^*_\n]|(?&italics))*?\g{b}))+\g{i})/'), [ |
47
|
|
|
'italics' => Token::NAME |
48
|
|
|
] |
49
|
|
|
), |
50
|
|
|
'format.emphasis' => new Rule( |
51
|
|
|
new RegexMatcher('/(?P<bold>(?P<b>\*\*|__)(?>[^*_\n]|(?:(?P<i>[*_]{2})(?>[^*_\n]|(?&bold))*?\g{i}))+\g{b})/', [ |
52
|
|
|
'bold' => Token::NAME |
53
|
|
|
]) |
54
|
|
|
), |
55
|
|
|
'format.strike' => new Rule(new RegexMatcher('/(~~.+?~~)/')), |
56
|
|
|
'format.monospace' => new Rule(new RegexMatcher('/[^`](`[^`]+?`)/')), |
57
|
|
|
|
58
|
|
|
'operator.list.ordered' => new Rule(new RegexMatcher('/^\s*(\d+[.)])/m')), |
59
|
|
|
'operator.list.unordered' => new Rule(new RegexMatcher('/^\s*([-+*])/m'), [ |
60
|
|
|
'context' => ['none'], |
61
|
|
|
'priority' => 1 |
62
|
|
|
]), |
63
|
|
|
|
64
|
|
|
'string.quote' => new Rule(new RegexMatcher('/((?:^>.*?\n)+)/m')), |
65
|
|
|
'format.block.code' => new Rule( |
66
|
|
|
new DelegateRegexMatcher( |
67
|
|
|
'/^```(.*?)\r?\n(.*?)\r?\n^```/ms', |
68
|
|
|
function($match, TokenFactoryInterface $factory) { |
69
|
|
|
$lang = KeyLighter::get()->getLanguage($match[1][0]); |
70
|
|
|
yield $factory->create(null, ['pos' => $match[0][1], 'length' => strlen($match[0][0])]); |
71
|
|
|
yield $factory->create( |
72
|
|
|
"language.{$lang->getIdentifier()}", [ |
73
|
|
|
'pos' => $match[2][1], |
74
|
|
|
'length' => strlen($match[2][0]), |
75
|
|
|
'postProcessed' => true, |
76
|
|
|
'inject' => $lang, |
77
|
|
|
'class' => LanguageToken::class, |
78
|
|
|
'language' => $this |
79
|
|
|
] |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
), [ |
83
|
|
|
'context' => Validator::everywhere(), |
84
|
|
|
'postProcess' => true, |
85
|
|
|
'priority' => 1000 |
86
|
|
|
] |
87
|
|
|
), |
88
|
|
|
|
89
|
|
|
'keyword.escape' => new Rule(new RegexMatcher('/(\\\.)/')), |
90
|
|
|
|
91
|
|
|
'operator.horizontal' => new Rule(new RegexMatcher('/^(-+)\s*$/m')), |
92
|
|
|
]); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Unique language identifier, for example 'php' |
97
|
|
|
* |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
|
|
public function getIdentifier() |
101
|
|
|
{ |
102
|
|
|
return 'markdown'; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|