|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the Cecil/Cecil package. |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (c) Arnaud Ligny <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Cecil\Converter; |
|
12
|
|
|
|
|
13
|
|
|
use Cecil\Assets\Asset; |
|
14
|
|
|
use Cecil\Builder; |
|
15
|
|
|
|
|
16
|
|
|
class Parsedown extends \ParsedownToC |
|
17
|
|
|
{ |
|
18
|
|
|
/** @var Builder */ |
|
19
|
|
|
protected $builder; |
|
20
|
|
|
|
|
21
|
|
|
/** {@inheritdoc} */ |
|
22
|
|
|
protected $regexAttribute = '(?:[#.][-\w:\\\]+[ ]*|[-\w:\\\]+(?:=(?:["\'][^\n]*?["\']|[^\s]+)?)?[ ]*)'; |
|
23
|
|
|
|
|
24
|
|
|
public function __construct(Builder $builder) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->builder = $builder; |
|
27
|
|
|
parent::__construct(['selectors' => $this->builder->getConfig()->get('body.toc')]); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* {@inheritdoc} |
|
32
|
|
|
*/ |
|
33
|
|
|
protected function inlineImage($excerpt) |
|
34
|
|
|
{ |
|
35
|
|
|
$image = parent::inlineImage($excerpt); |
|
36
|
|
|
if (!isset($image)) { |
|
37
|
|
|
return null; |
|
38
|
|
|
} |
|
39
|
|
|
$image['element']['attributes']['src'] = $imageSource = trim($this->removeQuery($image['element']['attributes']['src'])); |
|
40
|
|
|
$asset = new Asset($this->builder, $image['element']['attributes']['src']); |
|
41
|
|
|
$width = $asset->getWidth(); |
|
42
|
|
|
/** |
|
43
|
|
|
* Should be lazy loaded? |
|
44
|
|
|
*/ |
|
45
|
|
|
if ($this->builder->getConfig()->get('content.images.lazy.enabled')) { |
|
46
|
|
|
$image['element']['attributes']['loading'] = 'lazy'; |
|
47
|
|
|
} |
|
48
|
|
|
/** |
|
49
|
|
|
* Should be resized? |
|
50
|
|
|
*/ |
|
51
|
|
|
if ($image['element']['attributes']['width'] !== null |
|
52
|
|
|
&& (int) $image['element']['attributes']['width'] < $width |
|
53
|
|
|
&& $this->builder->getConfig()->get('content.images.resize.enabled') |
|
54
|
|
|
) { |
|
55
|
|
|
$width = (int) $image['element']['attributes']['width']; |
|
56
|
|
|
$imageResized = $asset->resize($width); |
|
57
|
|
|
$image['element']['attributes']['src'] = $imageResized; |
|
58
|
|
|
} |
|
59
|
|
|
if ($image['element']['attributes']['width'] === null) { |
|
60
|
|
|
$image['element']['attributes']['width'] = $width; |
|
61
|
|
|
} |
|
62
|
|
|
/** |
|
63
|
|
|
* Should be responsive? |
|
64
|
|
|
*/ |
|
65
|
|
|
if ($this->builder->getConfig()->get('content.images.responsive.enabled')) { |
|
66
|
|
|
$steps = $this->builder->getConfig()->get('content.images.responsive.width.steps'); |
|
67
|
|
|
$wMin = $this->builder->getConfig()->get('content.images.responsive.width.min'); |
|
68
|
|
|
$wMax = $this->builder->getConfig()->get('content.images.responsive.width.max'); |
|
69
|
|
|
$srcset = ''; |
|
70
|
|
|
for ($i = 1; $i <= $steps; $i++) { |
|
71
|
|
|
$w = ceil($wMin * $i); |
|
72
|
|
|
if ($w > $width || $w > $wMax) { |
|
73
|
|
|
break; |
|
74
|
|
|
} |
|
75
|
|
|
$a = new Asset($this->builder, $imageSource); |
|
76
|
|
|
$img = $a->resize($w); |
|
|
|
|
|
|
77
|
|
|
$srcset .= sprintf('%s %sw', $img, $w); |
|
78
|
|
|
if ($i < $steps) { |
|
79
|
|
|
$srcset .= ', '; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
if ($imageResized) { |
|
|
|
|
|
|
83
|
|
|
$srcset .= sprintf(',%s %sw', $imageResized, $width); |
|
|
|
|
|
|
84
|
|
|
} else { |
|
85
|
|
|
$srcset .= sprintf(',%s %sw', $imageSource, $width); |
|
86
|
|
|
} |
|
87
|
|
|
// ie: srcset="/img-480.jpg 480w, /img-800.jpg 800w" |
|
88
|
|
|
$image['element']['attributes']['srcset'] = $srcset; |
|
89
|
|
|
$image['element']['attributes']['sizes'] = $this->builder->getConfig()->get('content.images.responsive.sizes.default'); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return $image; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* {@inheritdoc} |
|
97
|
|
|
*/ |
|
98
|
|
|
protected function parseAttributeData($attributeString) |
|
99
|
|
|
{ |
|
100
|
|
|
$Data = []; |
|
101
|
|
|
|
|
102
|
|
|
$attributes = preg_split('/[ ]+/', $attributeString, -1, PREG_SPLIT_NO_EMPTY); |
|
103
|
|
|
|
|
104
|
|
|
$HtmlAtt = []; |
|
105
|
|
|
|
|
106
|
|
|
foreach ($attributes as $attribute) { |
|
107
|
|
|
if ($attribute[0] === '#') { |
|
108
|
|
|
$Data['id'] = substr($attribute, 1); |
|
109
|
|
|
} elseif ($attribute[0] === '.') { |
|
110
|
|
|
$classes[] = substr($attribute, 1); |
|
111
|
|
|
} else { |
|
112
|
|
|
parse_str($attribute, $parsed); |
|
113
|
|
|
$HtmlAtt = array_merge($HtmlAtt, $parsed); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
if (isset($classes)) { |
|
118
|
|
|
$Data['class'] = implode(' ', $classes); |
|
119
|
|
|
} |
|
120
|
|
|
if (isset($HtmlAtt)) { |
|
121
|
|
|
foreach ($HtmlAtt as $a => $v) { |
|
122
|
|
|
$Data[$a] = trim($v, '"'); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
return $Data; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Removes query string from URL. |
|
131
|
|
|
*/ |
|
132
|
|
|
private function removeQuery(string $path): string |
|
133
|
|
|
{ |
|
134
|
|
|
return strtok($path, '?'); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|