1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Highlighter |
4
|
|
|
*1 |
5
|
|
|
* Copyright (C) 2016, Some right reserved. |
6
|
|
|
* @author Kacper "Kadet" Donat <[email protected]> |
7
|
|
|
* |
8
|
|
|
* Contact with author: |
9
|
|
|
* Xmpp: [email protected] |
10
|
|
|
* E-mail: [email protected] |
11
|
|
|
* |
12
|
|
|
* From Kadet with love. |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Kadet\Highlighter\Language; |
16
|
|
|
|
17
|
|
|
use Kadet\Highlighter\Matcher\CommentMatcher; |
18
|
|
|
use Kadet\Highlighter\Matcher\RegexMatcher; |
19
|
|
|
use Kadet\Highlighter\Matcher\SubStringMatcher; |
20
|
|
|
use Kadet\Highlighter\Parser\CloseRule; |
21
|
|
|
use Kadet\Highlighter\Parser\OpenRule; |
22
|
|
|
use Kadet\Highlighter\Parser\Rule; |
23
|
|
|
use Kadet\Highlighter\Parser\Token\Token; |
24
|
|
|
|
25
|
|
|
class Xml extends GreedyLanguage |
26
|
|
|
{ |
27
|
|
|
const IDENTIFIER = '(?P<namespace>[\w\.-]+:)?(?P<name>[\w\.-]+)'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Tokenization rules |
31
|
|
|
*/ |
32
|
1 |
|
public function setupRules() |
33
|
|
|
{ |
34
|
1 |
|
$this->rules->addMany([ |
35
|
|
|
'tag.open' => [ |
36
|
1 |
|
new OpenRule(new RegexMatcher('/(<[\w\.-]+)[:\/>:\s]/')), |
37
|
1 |
|
new CloseRule(new SubStringMatcher('>'), ['context' => ['!string', '!comment']]) |
38
|
1 |
|
], |
39
|
1 |
|
'tag.close' => new Rule(new RegexMatcher('/(<\/' . self::IDENTIFIER . '>)/')), |
40
|
|
|
|
41
|
1 |
|
'symbol.tag' => new Rule(new RegexMatcher('/<\\/?' . self::IDENTIFIER . '/', [ |
42
|
1 |
|
'name' => Token::NAME, |
43
|
|
|
'namespace' => '$.namespace' |
44
|
1 |
|
]), ['context' => ['tag', '!string']]), |
45
|
|
|
|
46
|
1 |
|
'symbol.attribute' => new Rule(new RegexMatcher('/' . self::IDENTIFIER . '=/', [ |
47
|
1 |
|
'name' => Token::NAME, |
48
|
|
|
'namespace' => '$.namespace' |
49
|
1 |
|
]), ['context' => ['tag', '!string']]), |
50
|
|
|
|
51
|
1 |
|
'constant.entity' => new Rule(new RegexMatcher('/(&(?:\#\d+|[a-z])+;)/si')), |
52
|
|
|
|
53
|
1 |
|
'comment' => new Rule(new CommentMatcher(null, [['<!--', '-->']])), |
54
|
1 |
|
'string' => CommonFeatures::strings(['single' => '\'', 'double' => '"'], ['context' => ['tag']]), |
55
|
1 |
|
]); |
56
|
1 |
|
} |
57
|
|
|
|
58
|
|
|
/** {@inheritdoc} */ |
59
|
|
|
public function getIdentifier() |
60
|
|
|
{ |
61
|
|
|
return 'xml'; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public static function getMetadata() |
65
|
|
|
{ |
66
|
|
|
return [ |
67
|
|
|
'name' => ['xml'], |
68
|
|
|
'mime' => ['application/xml', 'text/xml'], |
69
|
|
|
'extension' => ['*.xml'] |
70
|
|
|
]; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|