Passed
Branch 0.8-dev (82421f)
by Kacper
02:45
created

Markdown::setupRules()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 77
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 2 Features 0
Metric Value
c 7
b 2
f 0
dl 0
loc 77
rs 8.9342
cc 1
eloc 49
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 Html
29
{
30
    protected $_options = [
31
        'variables' => false,
32
    ];
33
34
    /**
35
     * Tokenization rules setup
36
     */
37
    public function setupRules()
38
    {
39
        parent::setupRules();
40
41
        $this->rules->validator = new Validator(
42
            ['!format.block.code', '!format.monospace', '!keyword.escape', '!operator', '!tag']
43
        );
44
45
        $this->rules->addMany([
46
            'format.header' => [
47
                new Rule(new RegexMatcher('/^\s{0,3}(#+.+?)$/m')),
48
                new Rule(new RegexMatcher('/^(.+?)^(?:-+|=+)$/m'))
49
            ],
50
            'format.italics'   => new Rule(
51
                new RegexMatcher('/(?:^|[^*_])(?P<italics>(?P<i>[*_])(?>[^*_\n]|(?:(?P<b>[*_]{2})(?>[^*_\n]|(?&italics))*?\g{b}))+\g{i})/'), [
52
                    'italics' => Token::NAME
53
                ]
54
            ),
55
            'format.bold'  => new Rule(
56
                new RegexMatcher('/(?P<bold>(?P<b>\*\*|__)(?>[^*_\n]|(?:(?P<i>[*_]{2})(?>[^*_\n]|(?&bold))*?\g{i}))+\g{b})/', [
57
                    'bold' => Token::NAME
58
                ])
59
            ),
60
            'format.strike'    => new Rule(new RegexMatcher('/(~~.+?~~)/')),
61
            'format.monospace' => [
62
                new Rule(new RegexMatcher('/(?:[^`]|^)(`.*?`)/')),
63
                new Rule(new RegexMatcher('/(``.*?``)/')),
64
                new Rule(new RegexMatcher('/^((?:(?: {4,}|\t).*?(?>\n|$)+?)+)/m')),
65
            ],
66
67
            'operator.list.ordered'   => new Rule(new RegexMatcher('/^\s*(\d+[.)])/m')),
68
            'operator.list.unordered' => new Rule(new RegexMatcher('/^\s*([-+*])/m'), [
69
                'context' => ['none'],
70
                'priority' => 1
71
            ]),
72
73
            'string.quote'       => new Rule(new RegexMatcher('/((?:^>.*?\n)+)/m')),
74
            'format.block.code'  => [
75
                new Rule(
76
                    new DelegateRegexMatcher(
77
                        '/^```(.*?)\r?\n(.*?)\r?\n^```/ms',
78
                        function($match, TokenFactoryInterface $factory) {
79
                            $lang = KeyLighter::get()->getLanguage($match[1][0]);
80
                            yield $factory->create(Token::NAME, ['pos' => $match[0][1], 'length' => strlen($match[0][0])]);
81
                            yield $factory->create(
82
                                "language.{$lang->getIdentifier()}", [
83
                                    'pos'           => $match[2][1],
84
                                    'length'        => strlen($match[2][0]),
85
                                    'inject'        => $lang,
86
                                    'class'         => LanguageToken::class,
87
                                ]
88
                            );
89
                        }
90
                    ), [
91
                        'context'     => Validator::everywhere(),
92
                        'postProcess' => true,
93
                        'priority'    => 1000
94
                    ]
95
                ),
96
            ],
97
98
            'keyword.escape' => new Rule(new RegexMatcher('/(\\\.)/')),
99
100
            'operator.horizontal' => new Rule(new RegexMatcher('/^\s{,3}(([-*_])( ?\2)+)$/m'), [
101
                'priority' => 2
102
            ]),
103
104
            'symbol.link'  => new Rule(new RegexMatcher('/[^!](\[(?:[^\[\]]|!\[.*?\]\(.*?\))*\])/i')),
105
            'symbol.url'   => new Rule(new RegexMatcher('#(<(https?://|[^/])[-A-Za-z0-9+&@\#/%?=~_|!:,.;]+[-A-Za-z0-9+&@\#/%=~_|]>|https?://[-A-Za-z0-9+&@\#/%?=~_|!:,.;]+[-A-Za-z0-9+&@\#/%=~_|])#i'), [
106
                'priority' => 0,
107
            ]),
108
            'symbol.image' => new Rule(new RegexMatcher('/(!)(\[.*?\])\(.*?\)/i', [
109
                1 => 'operator.image',
110
                2 => '$.title',
111
            ])),
112
        ]);
113
    }
114
115
    /**
116
     * Unique language identifier, for example 'php'
117
     *
118
     * @return string
119
     */
120
    public function getIdentifier()
121
    {
122
        return 'markdown';
123
    }
124
}
125