|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Highlighter |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (C) 2016, Some right reserved. |
|
7
|
|
|
* |
|
8
|
|
|
* @author Kacper "Kadet" Donat <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* Contact with author: |
|
11
|
|
|
* Xmpp: [email protected] |
|
12
|
|
|
* E-mail: [email protected] |
|
13
|
|
|
* |
|
14
|
|
|
* From Kadet with love. |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
namespace Kadet\Highlighter\Language; |
|
18
|
|
|
|
|
19
|
|
|
use Kadet\Highlighter\Matcher\CommentMatcher; |
|
20
|
|
|
use Kadet\Highlighter\Matcher\RegexMatcher; |
|
21
|
|
|
use Kadet\Highlighter\Matcher\WordMatcher; |
|
22
|
|
|
use Kadet\Highlighter\Parser\Rule; |
|
23
|
|
|
use Kadet\Highlighter\Parser\Token\Token; |
|
24
|
|
|
use Kadet\Highlighter\Parser\Validator\Validator; |
|
25
|
|
|
|
|
26
|
|
|
class Latex extends GreedyLanguage |
|
27
|
|
|
{ |
|
28
|
|
|
protected static $mathEnvironments = ['align', 'equation', 'math']; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Tokenization rules |
|
32
|
|
|
*/ |
|
33
|
|
|
public function setupRules() |
|
34
|
|
|
{ |
|
35
|
|
|
$this->rules->addMany([ |
|
36
|
|
|
'call.symbol' => new Rule(new RegexMatcher('/(\\\[a-z@]+)/si'), [ |
|
37
|
|
|
'context' => ['!delimiter.environment'], |
|
38
|
|
|
'priority' => -1 |
|
39
|
|
|
]), |
|
40
|
|
|
|
|
41
|
|
|
'expression.math' => [ |
|
42
|
|
|
new Rule(new RegexMatcher('/((\${1,2}).*?\2)/s')), |
|
43
|
|
|
new Rule(new RegexMatcher('/(\\\\\(.*?\\\\\))/s')), |
|
44
|
|
|
new Rule(new RegexMatcher('/(\\\\\[.*?\\\\\])/s')), |
|
45
|
|
|
new Rule( |
|
46
|
|
|
new RegexMatcher( |
|
47
|
|
|
'/\\\begin{((?:' . implode('|', self::$mathEnvironments) . ')\*?)}(.*?)\\\end{\1}/s', |
|
48
|
|
|
[2 => Token::NAME] |
|
49
|
|
|
) |
|
50
|
|
|
) |
|
51
|
|
|
], |
|
52
|
|
|
|
|
53
|
|
|
'delimiter.math' => new Rule(new WordMatcher(['$', '\[', '\]', '\(', '\)'], ['separated' => false]), [ |
|
54
|
|
|
'context' => ['expression.math'] |
|
55
|
|
|
]), |
|
56
|
|
|
|
|
57
|
|
|
'symbol.annotation' => new Rule(new RegexMatcher('/\[(.*?)\]/')), // make argument parsing? |
|
58
|
|
|
'symbol.environment' => new Rule(new RegexMatcher('/(\\\(?:begin|end)){(.*?)}/', [ |
|
59
|
|
|
1 => 'delimiter.environment', |
|
60
|
|
|
2 => Token::NAME |
|
61
|
|
|
]), ['context' => Validator::everywhere()]), |
|
62
|
|
|
|
|
63
|
|
|
'symbol.label' => new Rule(new RegexMatcher('/\\\(?:label|ref){(.*?)}/')), |
|
64
|
|
|
|
|
65
|
|
|
'operator' => [ |
|
66
|
|
|
new Rule(new WordMatcher(['*', '&', '@', '\\\\'], ['separated' => false]), ['context' => Validator::everywhere()]), |
|
67
|
|
|
new Rule(new WordMatcher(['=', '-', '+', '/', '^', '_'], ['separated' => false]), [ |
|
68
|
|
|
'context' => ['expression.math'], |
|
69
|
|
|
'priority' => -1 |
|
70
|
|
|
]), |
|
71
|
|
|
'punctuation.brackets' => new Rule(new WordMatcher(['{', '}', '[', ']', '(', ')'], ['separated' => false]), [ |
|
72
|
|
|
'context' => ['expression.math'] |
|
73
|
|
|
]), |
|
74
|
|
|
'escape' => new Rule(new RegexMatcher('/(\\\[%@&])/si'), ['context' => Validator::everywhere(), 'priority' => -1]) |
|
75
|
|
|
|
|
76
|
|
|
], |
|
77
|
|
|
'comment' => new Rule(new CommentMatcher(['%'], [])), |
|
78
|
|
|
|
|
79
|
|
|
'format.bold' => new Rule(new RegexMatcher('/\\\textbf({((?>[^{}]+|(?1))+)})/si', [ |
|
80
|
|
|
2 => Token::NAME |
|
81
|
|
|
])), |
|
82
|
|
|
'format.italics' => new Rule(new RegexMatcher('/\\\textit({((?>[^{}]+|(?1))+)})/si', [ |
|
83
|
|
|
2 => Token::NAME |
|
84
|
|
|
])), |
|
85
|
|
|
|
|
86
|
|
|
'variable' => new Rule(new RegexMatcher('/(#\d+)/')), |
|
87
|
|
|
|
|
88
|
|
|
# math mode |
|
89
|
|
|
'number' => new Rule(new RegexMatcher('/(-?(?:0[0-7]+|0[xX][0-9a-fA-F]+|0b[01]+|[\d,.]+))/'), [ |
|
90
|
|
|
'context' => ['expression.math'], |
|
91
|
|
|
'priority' => -2 |
|
92
|
|
|
]), |
|
93
|
|
|
]); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** {@inheritdoc} */ |
|
97
|
1 |
|
public function getIdentifier() |
|
98
|
|
|
{ |
|
99
|
1 |
|
return 'latex'; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public static function getMetadata() |
|
103
|
|
|
{ |
|
104
|
|
|
return [ |
|
105
|
|
|
'name' => ['tex', 'latex'], |
|
106
|
|
|
'mime' => ['application/x-tex', 'application/x-latex'], |
|
107
|
|
|
'extension' => ['*.tex', '*.aux', '*.toc'] |
|
108
|
|
|
]; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|