Passed
Branch master (f3f280)
by Maciej
03:18
created

Markdown::setupRules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 75
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1.485

Importance

Changes 4
Bugs 2 Features 0
Metric Value
cc 1
eloc 48
nc 1
nop 0
dl 0
loc 75
ccs 9
cts 42
cp 0.2143
crap 1.485
rs 9.1344
c 4
b 2
f 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
/**
4
 * Highlighter
5
 *
6
 * Copyright (C) 2016, Some right reserved.
7
 *
8
 * @author Kacper "Kadet" Donat <[email protected]>
9
 *
10
 * Contact with author:
11
 * Xmpp: [email protected]
12
 * E-mail: [email protected]
13
 *
14
 * From Kadet with love.
15
 */
16
17
namespace Kadet\Highlighter\Language;
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 2
    public function setupRules()
38
    {
39
        parent::setupRules();
40
41
        $this->rules->validator = new Validator(
42
            ['!format.block.code', '!format.monospace', '!operator.escape', '!operator', '!tag']
43
        );
44
45
        $this->rules->addMany([
46
            'format.header' => [
47
                new Rule(new RegexMatcher('/^\s{0,3}(#+.+?)\r?$/m')),
48
                new Rule(new RegexMatcher('/^([^\r\n]+?)^(?:-+|=+)\R?$/m'))
49
            ],
50
            'format.italics'   => new Rule(
51
                new RegexMatcher('/(?:^|[^*_])(?P<italics>(?P<i>[*_])(?>[^*_\r\n]|(?:(?P<b>[*_]{2})(?>[^*_\r\n]|(?&italics))*?\g{b}))+\g{i})/'),
52
                ['italics' => Token::NAME]
53
            ),
54
            'format.bold'  => new Rule(
55
                new RegexMatcher('/(?P<bold>(?P<b>\*\*|__)(?>[^*_\r\n]|(?:(?P<i>[*_]{2})(?>[^*_\r\n]|(?&bold))*?\g{i}))+\g{b})/', [
56
                    'bold' => Token::NAME
57
                ])
58
            ),
59
            'format.strike'    => new Rule(new RegexMatcher('/(~~.+?~~)/')),
60
            'format.monospace' => [
61
                new Rule(new RegexMatcher('/(?:[^`]|^)(`.*?`)/')),
62
                new Rule(new RegexMatcher('/(``.*?``)/')),
63
                new Rule(new RegexMatcher('/^((?:(?: {4,}|\t).*?(?>\R|$)+?)+)/m')),
64
            ],
65
66
            'operator.list.ordered'   => new Rule(new RegexMatcher('/^\s*(\d+[.)])/m')),
67
            'operator.list.unordered' => new Rule(new RegexMatcher('/^\s*([-+*])/m'), [
68
                'context' => ['none'],
69
                'priority' => 1
70
            ]),
71
72
            'string.quote'       => new Rule(new RegexMatcher('/((?:^>.*?\R)+)/m')),
73
            'format.block.code'  => [
74
                new Rule(
75
                    new DelegateRegexMatcher(
76
                        '/^```(.*?)\R(.*?)\R^```/ms',
77
                        function ($match, TokenFactoryInterface $factory) {
78 2
                            $lang = KeyLighter::get()->getLanguage($match[1][0]);
79 2
                            yield $factory->create(Token::NAME, ['pos' => $match[0][1], 'length' => strlen($match[0][0])]);
80 2
                            yield $factory->create(
81 2
                                "language.{$lang->getIdentifier()}",
82
                                [
83 2
                                    'pos'    => $match[2][1],
84 2
                                    'length' => strlen($match[2][0]),
85 2
                                    'inject' => $lang,
86
                                    'class'  => LanguageToken::class,
87
                                ]
88
                            );
89 2
                        }
90
                    ),
91
                    [
92
                        'context'     => Validator::everywhere(),
93
                        'postProcess' => true,
94
                        'priority'    => 1000
95
                    ]
96
                ),
97
            ],
98
99
            'operator.escape' => new Rule(new RegexMatcher('/(\\\.)/')),
100
101
            'operator.horizontal' => new Rule(new RegexMatcher('/^\s{,3}(([-*_])( ?\2)+)\R/m'), [
102
                'priority' => 2
103
            ]),
104
105
            'symbol.link'  => new Rule(new RegexMatcher('/[^!](\[(?:[^\[\]]|!\[.*?\]\(.*?\))*\])/i')),
106
            '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'), [
107
                'priority' => 0,
108
            ]),
109
            'symbol.image' => new Rule(new RegexMatcher('/(!)(\[.*?\])\(.*?\)/i', [
110
                1 => 'operator.image',
111
                2 => '$.title',
112
            ])),
113
        ]);
114
    }
115
116
    /**
117
     * Unique language identifier, for example 'php'
118
     *
119
     * @return string
120
     */
121 2
    public function getIdentifier()
122
    {
123 2
        return 'markdown';
124
    }
125
126
    public static function getMetadata()
127
    {
128
        return [
129
            'name'      => ['markdown', 'md'],
130
            'mime'      => ['text/markdown'],
131
            'extension' => ['*.markdown', '*.md']
132
        ];
133
    }
134
}
135