1 | <?php |
||
9 | class LinkConverter implements ConverterInterface, ConfigurationAwareInterface |
||
10 | { |
||
11 | /** |
||
12 | * @var Configuration |
||
13 | */ |
||
14 | protected $config; |
||
15 | |||
16 | /** |
||
17 | * @param Configuration $config |
||
18 | */ |
||
19 | 93 | public function setConfig(Configuration $config) { |
|
22 | |||
23 | /** |
||
24 | * @param ElementInterface $element |
||
25 | * |
||
26 | * @return string |
||
27 | */ |
||
28 | 9 | public function convert(ElementInterface $element) |
|
29 | { |
||
30 | 9 | $href = $element->getAttribute('href'); |
|
31 | 9 | $title = $element->getAttribute('title'); |
|
32 | 9 | $text = trim($element->getValue(), "\t\n\r\0\x0B"); |
|
33 | |||
34 | 9 | if ($title !== '') { |
|
35 | 3 | $markdown = '[' . $text . '](' . $href . ' "' . $title . '")'; |
|
36 | 9 | } elseif ($href === $text && $this->isValidAutolink($href)) { |
|
37 | 3 | $markdown = '<' . $href . '>'; |
|
38 | 9 | } elseif ($href === 'mailto:' . $text && $this->isValidEmail($text)) { |
|
39 | 3 | $markdown = '<' . $text . '>'; |
|
40 | } else { |
||
41 | 9 | if (stristr($href, ' ')) { |
|
42 | $href = '<'.$href.'>'; |
||
43 | } |
||
44 | 9 | $markdown = '[' . $text . '](' . $href . ')'; |
|
45 | } |
||
46 | |||
47 | 9 | if (!$href) { |
|
48 | 3 | $markdown = html_entity_decode($element->getChildrenAsString()); |
|
49 | } |
||
50 | |||
51 | 9 | return $markdown; |
|
52 | } |
||
53 | |||
54 | /** |
||
55 | * @return string[] |
||
56 | */ |
||
57 | 93 | public function getSupportedTags() |
|
61 | |||
62 | /** |
||
63 | * @param string $href |
||
64 | * |
||
65 | * @return bool |
||
66 | */ |
||
67 | 6 | private function isValidAutolink($href) |
|
72 | |||
73 | /** |
||
74 | * @param string $email |
||
75 | * |
||
76 | * @return bool |
||
77 | */ |
||
78 | 3 | private function isValidEmail($email) |
|
83 | } |
||
84 |