1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Copyright (c) 2015 Nobuo Kihara |
4
|
|
|
* @license https://github.com/softark/creole/blob/master/LICENSE |
5
|
|
|
* @link https://github.com/softark/creole#readme |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace softark\creole\inline; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Adds inline emphasizes and strong elements |
12
|
|
|
*/ |
13
|
|
|
trait EmphStrongTrait |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Parses strong element: ** ... ** |
17
|
|
|
* @marker ** |
18
|
|
|
*/ |
19
|
7 |
View Code Duplication |
protected function parseStrong($text) |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
$pattern = <<< REGEXP |
22
|
7 |
|
/^\*\*( |
23
|
|
|
(.*?{{{.*?}}}.*?)+?| # including inline code span |
24
|
|
|
(?!.*{{{.*?}}}).*? # without inline code span |
25
|
|
|
)\*\*/sx |
26
|
|
|
REGEXP; |
27
|
7 |
|
if (preg_match($pattern, $text, $matches)) { |
28
|
|
|
return [ |
29
|
|
|
[ |
30
|
7 |
|
'strong', |
31
|
7 |
|
$this->parseInline($matches[1]) |
32
|
|
|
], |
33
|
7 |
|
strlen($matches[0]) |
34
|
|
|
]; |
35
|
|
|
} else { |
36
|
|
|
// no ending ** ... should be treated as strong |
37
|
|
|
return [ |
38
|
|
|
[ |
39
|
2 |
|
'strong', |
40
|
2 |
|
$this->parseInline(substr($text, 2)) |
41
|
|
|
], |
42
|
2 |
|
strlen($text) |
43
|
|
|
]; |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Parses strong element: // ... // |
49
|
|
|
* @marker // |
50
|
|
|
*/ |
51
|
8 |
View Code Duplication |
protected function parseEmph($text) |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
// must take care of code elements, 'http://', 'https://', and 'ftp://' |
54
|
|
|
$pattern = <<< REGEXP |
55
|
8 |
|
/^\/\/( |
56
|
|
|
(.*?{{{.*?}}}.*?)+?| # including inline code span |
57
|
|
|
((?!.*{{{.*?}}}).*?) # without inline code span |
58
|
|
|
)(?<!http:|(?<=h)ttps:|(?<=f)tp:)\/\/(?!\/)/sx |
59
|
|
|
REGEXP; |
60
|
|
|
|
61
|
8 |
|
if (preg_match($pattern, $text, $matches)) { |
62
|
|
|
return [ |
63
|
|
|
[ |
64
|
8 |
|
'emph', |
65
|
8 |
|
$this->parseInline($matches[1]) |
66
|
|
|
], |
67
|
8 |
|
strlen($matches[0]) |
68
|
|
|
]; |
69
|
|
|
} else { |
70
|
|
|
// no ending // ... should be treated as em |
71
|
|
|
return [ |
72
|
|
|
[ |
73
|
2 |
|
'emph', |
74
|
2 |
|
$this->parseInline(substr($text, 2)) |
75
|
|
|
], |
76
|
2 |
|
strlen($text) |
77
|
|
|
]; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
7 |
|
protected function renderStrong($block) |
82
|
|
|
{ |
83
|
7 |
|
return '<strong>' . $this->renderAbsy($block[1]) . '</strong>'; |
84
|
|
|
} |
85
|
|
|
|
86
|
8 |
|
protected function renderEmph($block) |
87
|
|
|
{ |
88
|
8 |
|
return '<em>' . $this->renderAbsy($block[1]) . '</em>'; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
abstract protected function parseInline($text); |
92
|
|
|
|
93
|
|
|
abstract protected function renderAbsy($absy); |
94
|
|
|
} |
95
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.