Completed
Push — master ( 6465ca...dc4b6d )
by Kacper
08:36
created

Latex   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 4%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 0
loc 85
ccs 2
cts 50
cp 0.04
rs 10
c 1
b 1
f 0
wmc 3
lcom 1
cbo 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setupRules() 0 62 1
A getMetadata() 0 8 1
A getIdentifier() 0 4 1
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('/(\\\[[email protected]]+)/si'), [
36
                'context' => ['!delimiter.environment'],
37
                'priority' => -1
38
            ]),
39
40
            'expression.math' => [
41
                new Rule(new RegexMatcher('/((\${1,2}).*?\2)/s')),
42
                new Rule(new RegexMatcher('/(\\\\\(.*?\\\\\))/s')),
43
                new Rule(new RegexMatcher('/(\\\\\[.*?\\\\\])/s')),
44
                new Rule(
45
                    new RegexMatcher(
46
                        '/\\\begin{((?:' . implode('|', self::$mathEnvironments) . ')\*?)}(.*?)\\\end{\1}/s',
47
                        [2 => Token::NAME]
48
                    )
49
                )
50
            ],
51
52
            'delimiter.math' => new Rule(new WordMatcher(['$', '\[', '\]', '\(', '\)'], ['separated' => false]), [
53
                'context' => ['expression.math']
54
            ]),
55
56
            'symbol.annotation'    => new Rule(new RegexMatcher('/\[(.*?)\]/')), // make argument parsing?
57
            'symbol.environment' => new Rule(new RegexMatcher('/(\\\(?:begin|end)){(.*?)}/', [
58
                1 => 'delimiter.environment',
59
                2 => Token::NAME
60
            ]), ['context' => Validator::everywhere()]),
61
62
            'symbol.label' => new Rule(new RegexMatcher('/\\\(?:label|ref){(.*?)}/')),
63
64
            'operator' => [
65
                new Rule(new WordMatcher(['*', '&', '@', '\\\\'], ['separated' => false]), ['context' => Validator::everywhere()]),
66
                new Rule(new WordMatcher(['=', '-', '+', '/', '^', '_'], ['separated' => false]), [
67
                    'context'  => ['expression.math'],
68
                    'priority' => -1
69
                ]),
70
                'punctuation.brackets' => new Rule(new WordMatcher(['{', '}', '[', ']', '(', ')'], ['separated' => false]), [
71
                    'context' => ['expression.math']
72
                ]),
73
                'escape' => new Rule(new RegexMatcher('/(\\\[%@&])/si'), ['context' => Validator::everywhere(), 'priority' => -1])
74
75
            ],
76
            'comment' => new Rule(new CommentMatcher(['%'], [])),
77
78
            'format.bold' => new Rule(new RegexMatcher('/\\\textbf({((?>[^{}]+|(?1))+)})/si', [
79
                2 => Token::NAME
80
            ])),
81
            'format.italics' => new Rule(new RegexMatcher('/\\\textit({((?>[^{}]+|(?1))+)})/si', [
82
                2 => Token::NAME
83
            ])),
84
85
            'variable' => new Rule(new RegexMatcher('/(#\d+)/')),
86
87
            # math mode
88
            'number' => new Rule(new RegexMatcher('/(-?(?:0[0-7]+|0[xX][0-9a-fA-F]+|0b[01]+|[\d,.]+))/'), [
89
                'context'  => ['expression.math'],
90
                'priority' => -2
91
            ]),
92
        ]);
93
    }
94
95
    /** {@inheritdoc} */
96 1
    public function getIdentifier()
97
    {
98 1
        return 'latex';
99
    }
100
101
    public static function getMetadata()
102
    {
103
        return [
104
            'name'      => ['tex', 'latex'],
105
            'mime'      => ['application/x-tex', 'application/x-latex'],
106
            'extension' => ['*.tex', '*.aux', '*.toc']
107
        ];
108
    }
109
}
110