SpanElementParser   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 47
c 1
b 0
f 1
dl 0
loc 86
rs 10
wmc 14

6 Methods

Rating   Name   Duplication   Size   Complexity  
A anchors() 0 9 2
A emails() 0 6 1
A spans() 0 26 6
A images() 0 8 3
A links() 0 8 1
A parse() 0 5 1
1
<?php
2
3
/*
4
 * This file is part of the HTMLUP package.
5
 *
6
 * (c) Jitendra Adhikari <[email protected]>
7
 *     <https://github.com/adhocore>
8
 *
9
 * Licensed under MIT license.
10
 */
11
12
namespace Ahc;
13
14
class SpanElementParser
15
{
16
    use HtmlHelper;
17
18
    const RE_URL       = '~<(https?:[\/]{2}[^\s]+?)>~';
19
    const RE_EMAIL     = '~<(\S+?@\S+?)>~';
20
    const RE_MD_IMG    = '~!\[(.+?)\]\s*\((.+?)\s*(".+?")?\)~';
21
    const RE_MD_URL    = '~\[(.+?)\]\s*\((.+?)\s*(".+?")?\)~';
22
    const RE_MD_FONT   = '!(\*{1,2}|_{1,2}|`|~~)(.+?)\\1!';
23
24
    public function parse($markup)
25
    {
26
        return $this->spans(
27
            $this->anchors(
28
                $this->links($markup)
29
            )
30
        );
31
    }
32
33
    protected function links($markup)
34
    {
35
        $markup = $this->emails($markup);
36
37
        return \preg_replace(
38
            static::RE_URL,
39
            '<a href="$1">$1</a>',
40
            $markup
41
        );
42
    }
43
44
    protected function emails($markup)
45
    {
46
        return \preg_replace(
47
            static::RE_EMAIL,
48
            '<a href="mailto:$1">$1</a>',
49
            $markup
50
        );
51
    }
52
53
    protected function anchors($markup)
54
    {
55
        $markup = $this->images($markup);
56
57
        return \preg_replace_callback(static::RE_MD_URL, function ($a) {
58
            $title = isset($a[3]) ? " title={$a[3]} " : '';
59
60
            return "<a href=\"{$a[2]}\"{$title}>{$a[1]}</a>";
61
        }, $markup);
62
    }
63
64
    protected function images($markup)
65
    {
66
        return \preg_replace_callback(static::RE_MD_IMG, function ($img) {
67
            $title = isset($img[3]) ? " title={$img[3]} " : '';
68
            $alt   = $img[1] ? " alt=\"{$img[1]}\" " : '';
69
70
            return "<img src=\"{$img[2]}\"{$title}{$alt}/>";
71
        }, $markup);
72
    }
73
74
    protected function spans($markup)
75
    {
76
        // em/code/strong/del
77
        return \preg_replace_callback(static::RE_MD_FONT, function ($em) {
78
            switch (\substr($em[1], 0, 2)) {
79
                case '**':
80
                case '__':
81
                    $tag = 'strong';
82
                    break;
83
84
                case '~~':
85
                    $tag = 'del';
86
                    break;
87
88
                case $em[1] === '*':
89
                case $em[1] === '_':
90
                    $tag = 'em';
91
                    break;
92
93
                default:
94
                    $tag   = 'code';
95
                    $em[2] = $this->escape($em[2]);
96
            }
97
98
            return "<$tag>{$em[2]}</$tag>";
99
        }, $markup);
100
    }
101
}
102