-
Notifications
You must be signed in to change notification settings - Fork 2
/
Latex.php
112 lines (95 loc) · 3.75 KB
/
Latex.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
<?php
declare(strict_types=1);
/**
* Highlighter
*
* Copyright (C) 2016, Some right reserved.
*
* @author Kacper "Kadet" Donat <kacper@kadet.net>
*
* Contact with author:
* Xmpp: me@kadet.net
* E-mail: contact@kadet.net
*
* From Kadet with love.
*/
namespace Kadet\Highlighter\Language;
use Kadet\Highlighter\Matcher\CommentMatcher;
use Kadet\Highlighter\Matcher\RegexMatcher;
use Kadet\Highlighter\Matcher\WordMatcher;
use Kadet\Highlighter\Parser\Rule;
use Kadet\Highlighter\Parser\Token\Token;
use Kadet\Highlighter\Parser\Validator\Validator;
class Latex extends GreedyLanguage
{
protected static $mathEnvironments = ['align', 'equation', 'math'];
/**
* Tokenization rules
*/
public function setupRules()
{
$this->rules->addMany([
'call.symbol' => new Rule(new RegexMatcher('/(\\\[a-z@]+)/si'), [
'context' => ['!delimiter.environment'],
'priority' => -1
]),
'expression.math' => [
new Rule(new RegexMatcher('/((\${1,2}).*?\2)/s')),
new Rule(new RegexMatcher('/(\\\\\(.*?\\\\\))/s')),
new Rule(new RegexMatcher('/(\\\\\[.*?\\\\\])/s')),
new Rule(
new RegexMatcher(
'/\\\begin{((?:' . implode('|', self::$mathEnvironments) . ')\*?)}(.*?)\\\end{\1}/s',
[2 => Token::NAME]
)
)
],
'delimiter.math' => new Rule(new WordMatcher(['$', '\[', '\]', '\(', '\)'], ['separated' => false]), [
'context' => ['expression.math']
]),
'symbol.annotation' => new Rule(new RegexMatcher('/\[(.*?)\]/')), // make argument parsing?
'symbol.environment' => new Rule(new RegexMatcher('/(\\\(?:begin|end)){(.*?)}/', [
1 => 'delimiter.environment',
2 => Token::NAME
]), ['context' => Validator::everywhere()]),
'symbol.label' => new Rule(new RegexMatcher('/\\\(?:label|ref){(.*?)}/')),
'operator' => [
new Rule(new WordMatcher(['*', '&', '@', '\\\\'], ['separated' => false]), ['context' => Validator::everywhere()]),
new Rule(new WordMatcher(['=', '-', '+', '/', '^', '_'], ['separated' => false]), [
'context' => ['expression.math'],
'priority' => -1
]),
'punctuation.brackets' => new Rule(new WordMatcher(['{', '}', '[', ']', '(', ')'], ['separated' => false]), [
'context' => ['expression.math']
]),
'escape' => new Rule(new RegexMatcher('/(\\\[%@&])/si'), ['context' => Validator::everywhere(), 'priority' => -1])
],
'comment' => new Rule(new CommentMatcher(['%'], [])),
'format.bold' => new Rule(new RegexMatcher('/\\\textbf({((?>[^{}]+|(?1))+)})/si', [
2 => Token::NAME
])),
'format.italics' => new Rule(new RegexMatcher('/\\\textit({((?>[^{}]+|(?1))+)})/si', [
2 => Token::NAME
])),
'variable' => new Rule(new RegexMatcher('/(#\d+)/')),
# math mode
'number' => new Rule(new RegexMatcher('/(-?(?:0[0-7]+|0[xX][0-9a-fA-F]+|0b[01]+|[\d,.]+))/'), [
'context' => ['expression.math'],
'priority' => -2
]),
]);
}
/** {@inheritdoc} */
public function getIdentifier()
{
return 'latex';
}
public static function getMetadata()
{
return [
'name' => ['tex', 'latex'],
'mime' => ['application/x-tex', 'application/x-latex'],
'extension' => ['*.tex', '*.aux', '*.toc']
];
}
}