Passed
Branch symfony-console (1f7bcb)
by Kacper
03:39
created

Latex::getAliases()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 8
rs 9.4285
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
use Kadet\Highlighter\Matcher\CommentMatcher;
19
use Kadet\Highlighter\Matcher\RegexMatcher;
20
use Kadet\Highlighter\Matcher\WordMatcher;
21
use Kadet\Highlighter\Parser\Rule;
22
use Kadet\Highlighter\Parser\Token\Token;
23
use Kadet\Highlighter\Parser\Validator\Validator;
24
25
class Latex extends GreedyLanguage
26
{
27
    protected static $mathEnvironments = ['align', 'equation', 'math'];
28
29
    /**
30
     * Tokenization rules
31
     */
32
    public function setupRules()
33
    {
34
        $this->rules->addMany([
35
            'call.symbol' => new Rule(new RegexMatcher('/(\\\[a-z]+)/si'), ['context' => Validator::everywhere(), 'priority' => -1]),
36
37
            'string.math' => [
38
                new Rule(new RegexMatcher('/((\${1,2}).*?\2)/s')),
39
                new Rule(new RegexMatcher('/(\\\\\(.*?\\\\\))/s')),
40
                new Rule(new RegexMatcher('/(\\\\\[.*?\\\\\])/s')),
41
                new Rule(
42
                    new RegexMatcher(
43
                        '/\\\begin{((?:' . implode('|', self::$mathEnvironments) . ')\*?)}(.*?)\\\end{\1}/s',
44
                        [2 => Token::NAME]
45
                    )
46
                ),
47
            ],
48
49
            'symbol.argument'    => new Rule(new RegexMatcher('/\[(.*?)\]/')),
50
            'symbol.environment' => new Rule(new RegexMatcher('/\\\(?:begin|end){(.*?)}/')),
51
52
            'symbol.label' => new Rule(new RegexMatcher('/\\\(?:label|ref){(.*?)}/')),
53
54
            'operator' => [
55
                new Rule(new WordMatcher(['*', '&', '\\\\'], ['separated' => false]), ['context' => Validator::everywhere(
56
                )
57
                ]),
58
                new Rule(new WordMatcher(['=', '-', '+', '/', '^', '_'], ['separated' => false]), [
59
                    'context'  => ['string.math'],
60
                    'priority' => -1
61
                ]),
62
            ],
63
            'comment' => new Rule(new CommentMatcher(['%'], [])),
64
65
            'format.bold' => new Rule(new RegexMatcher('/\\\textbf({((?>[^{}]+|(?1))+)})/si', [
66
                2 => Token::NAME
67
            ])),
68
            'format.italics' => new Rule(new RegexMatcher('/\\\textit({((?>[^{}]+|(?1))+)})/si', [
69
                2 => Token::NAME
70
            ])),
71
72
            # math mode
73
            'number' => new Rule(new RegexMatcher('/(-?(?:0[0-7]+|0[xX][0-9a-fA-F]+|0b[01]+|[\d,]+))/'), [
74
                'context'  => ['string.math'],
75
                'priority' => -2
76
            ]),
77
        ]);
78
    }
79
80
    /** {@inheritdoc} */
81
    public function getIdentifier()
82
    {
83
        return 'latex';
84
    }
85
86
    public static function getAliases()
87
    {
88
        return [
89
            'name'      => ['tex', 'latex'],
90
            'mime'      => ['application/x-tex', 'application/x-latex'],
91
            'extension' => ['*.tex', '*.aux', '*.toc']
92
        ];
93
    }
94
}
95