1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pilipinews\Common\Converters; |
4
|
|
|
|
5
|
|
|
use League\HTMLToMarkdown\Configuration; |
6
|
|
|
use League\HTMLToMarkdown\ConfigurationAwareInterface; |
7
|
|
|
use League\HTMLToMarkdown\Converter\ConverterInterface; |
8
|
|
|
use League\HTMLToMarkdown\ElementInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Emphasis Converter |
12
|
|
|
* |
13
|
|
|
* @package Pilipinews |
14
|
|
|
* @author Rougin Royce Gutib <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class EmphasisConverter implements ConverterInterface, ConfigurationAwareInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var \League\HTMLToMarkdown\Configuration |
20
|
|
|
*/ |
21
|
|
|
protected $config; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Sets the configuration instance. |
25
|
|
|
* |
26
|
|
|
* @param \League\HTMLToMarkdown\Configuration $config |
27
|
|
|
*/ |
28
|
12 |
|
public function setConfig(Configuration $config) |
29
|
|
|
{ |
30
|
12 |
|
$this->config = $config; |
31
|
12 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Converts the specified element into a parsed string. |
35
|
|
|
* |
36
|
|
|
* @param \League\HTMLToMarkdown\ElementInterface $element |
37
|
|
|
* @return string |
38
|
|
|
*/ |
39
|
9 |
|
public function convert(ElementInterface $element) |
40
|
|
|
{ |
41
|
9 |
|
$tag = $element->getTagName(); |
42
|
|
|
|
43
|
9 |
|
$value = $element->getValue(); |
44
|
|
|
|
45
|
9 |
|
if ($tag === 'i' || $tag === 'em') { |
46
|
3 |
|
$style = $this->config->getOption('italic_style'); |
47
|
2 |
|
} else { |
48
|
9 |
|
$style = $this->config->getOption('bold_style'); |
49
|
|
|
|
50
|
9 |
|
if (! $this->hasLinks($value)) { |
51
|
9 |
|
$value = mb_convert_case($value, MB_CASE_UPPER, 'UTF-8'); |
52
|
6 |
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
9 |
|
list($prefix, $suffix) = $this->prefixSuffix($value); |
56
|
|
|
|
57
|
9 |
|
return $prefix . $style . trim($value) . $style . $suffix; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Returns the supported HTML tags. |
62
|
|
|
* |
63
|
|
|
* @return string[] |
64
|
|
|
*/ |
65
|
12 |
|
public function getSupportedTags() |
66
|
|
|
{ |
67
|
12 |
|
return array('em', 'i', 'strong', 'b'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Checks if the specified string contains a URL. |
72
|
|
|
* |
73
|
|
|
* @param string $string |
74
|
|
|
* @return boolean |
75
|
|
|
*/ |
76
|
9 |
|
protected function hasLinks($string) |
77
|
|
|
{ |
78
|
9 |
|
$regex = '(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9]\.[^\s]{2,})'; |
79
|
|
|
|
80
|
9 |
|
preg_match($regex, $string, $matches); |
81
|
|
|
|
82
|
9 |
|
return isset($matches[0]) === true; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Returns prefix and suffix of a specified string. |
87
|
|
|
* |
88
|
|
|
* @param string $string |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
9 |
|
protected function prefixSuffix($string) |
92
|
|
|
{ |
93
|
9 |
|
$prefix = ltrim($string) !== $string ? ' ' : ''; |
94
|
|
|
|
95
|
9 |
|
$suffix = rtrim($string) !== $string ? ' ' : ''; |
96
|
|
|
|
97
|
9 |
|
return (array) array($prefix, $suffix); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|