Completed
Branch 0.8-dev (5d9532)
by Kacper
02:49
created

Markdown::setupRules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 57
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 1 Features 0
Metric Value
c 5
b 1
f 0
dl 0
loc 57
rs 9.6818
cc 1
eloc 38
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
19
use Kadet\Highlighter\KeyLighter;
20
use Kadet\Highlighter\Matcher\DelegateRegexMatcher;
21
use Kadet\Highlighter\Matcher\RegexMatcher;
22
use Kadet\Highlighter\Parser\Rule;
23
use Kadet\Highlighter\Parser\Token\LanguageToken;
24
use Kadet\Highlighter\Parser\Token\Token;
25
use Kadet\Highlighter\Parser\TokenFactoryInterface;
26
use Kadet\Highlighter\Parser\Validator\Validator;
27
28
class Markdown extends Language
29
{
30
    protected $_options = [
31
        'variables' => false,
32
    ];
33
34
    /**
35
     * Tokenization rules setup
36
     */
37
    public function setupRules()
38
    {
39
        $this->rules->validator = new Validator(['!format.block.code', '!format.monospace', '!keyword.escape', '!operator']);
40
        $this->rules->addMany([
41
            'format.header' => [
42
                new Rule(new RegexMatcher('/^(#+.+?)$/m')),
43
                new Rule(new RegexMatcher('/^(.+?)^(?:-+|=+)$/m'))
44
            ],
45
            'format.italics'   => new Rule(
46
                new RegexMatcher('/(?:^|[^*_])(?P<italics>(?P<i>[*_])(?>[^*_\n]|(?:(?P<b>[*_]{2})(?>[^*_\n]|(?&italics))*?\g{b}))+\g{i})/'), [
47
                    'italics' => Token::NAME
48
                ]
49
            ),
50
            'format.emphasis'  => new Rule(
51
                new RegexMatcher('/(?P<bold>(?P<b>\*\*|__)(?>[^*_\n]|(?:(?P<i>[*_]{2})(?>[^*_\n]|(?&bold))*?\g{i}))+\g{b})/', [
52
                    'bold' => Token::NAME
53
                ])
54
            ),
55
            'format.strike'    => new Rule(new RegexMatcher('/(~~.+?~~)/')),
56
            'format.monospace' => new Rule(new RegexMatcher('/[^`](`[^`]+?`)/')),
57
58
            'operator.list.ordered'   => new Rule(new RegexMatcher('/^\s*(\d+[.)])/m')),
59
            'operator.list.unordered' => new Rule(new RegexMatcher('/^\s*([-+*])/m'), [
60
                'context' => ['none'],
61
                'priority' => 1
62
            ]),
63
64
            'string.quote'       => new Rule(new RegexMatcher('/((?:^>.*?\n)+)/m')),
65
            'format.block.code'  => new Rule(
66
                new DelegateRegexMatcher(
67
                    '/^```(.*?)\r?\n(.*?)\r?\n^```/ms',
68
                    function($match, TokenFactoryInterface $factory) {
69
                        $lang = KeyLighter::get()->getLanguage($match[1][0]);
70
                        yield $factory->create(null, ['pos' => $match[0][1], 'length' => strlen($match[0][0])]);
71
                        yield $factory->create(
72
                            "language.{$lang->getIdentifier()}", [
73
                                'pos'           => $match[2][1],
74
                                'length'        => strlen($match[2][0]),
75
                                'postProcessed' => true,
76
                                'inject'        => $lang,
77
                                'class'         => LanguageToken::class,
78
                                'language'      => $this
79
                            ]
80
                        );
81
                    }
82
                ), [
83
                    'context'     => Validator::everywhere(),
84
                    'postProcess' => true,
85
                    'priority'    => 1000
86
                ]
87
            ),
88
89
            'keyword.escape' => new Rule(new RegexMatcher('/(\\\.)/')),
90
91
            'operator.horizontal' => new Rule(new RegexMatcher('/^(-+)\s*$/m')),
92
        ]);
93
    }
94
95
    /**
96
     * Unique language identifier, for example 'php'
97
     *
98
     * @return string
99
     */
100
    public function getIdentifier()
101
    {
102
        return 'markdown';
103
    }
104
}
105