Cancelled
Push — figcaption ( 2b9cc6...0b8c0c )
by Arnaud
14:24 queued 11:21
created

Parsedown::inlineImage()   D

Complexity

Conditions 18
Paths 103

Size

Total Lines 72
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
cc 18
eloc 36
c 3
b 1
f 0
nc 103
nop 1
dl 0
loc 72
rs 4.8416

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Cecil.
7
 *
8
 * Copyright (c) Arnaud Ligny <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Cecil\Converter;
15
16
use Cecil\Assets\Asset;
17
use Cecil\Assets\Image;
18
use Cecil\Builder;
19
use Cecil\Exception\RuntimeException;
20
use Cecil\Util;
21
use Highlight\Highlighter;
22
23
class Parsedown extends \ParsedownToC
24
{
25
    /** @var Builder */
26
    protected $builder;
27
28
    /** {@inheritdoc} */
29
    protected $regexAttribute = '(?:[#.][-\w:\\\]+[ ]*|[-\w:\\\]+(?:=(?:["\'][^\n]*?["\']|[^\s]+)?)?[ ]*)';
30
31
    /** Valid a media block (image, audio or video) */
32
    protected $MarkdownMediaRegex = "~^!\[.*?\]\(.*?\)~";
33
34
    /** @var Highlighter */
35
    protected $highlighter;
36
37
    public function __construct(Builder $builder, ?array $options = null)
38
    {
39
        $this->builder = $builder;
40
41
        // "insert" line block: ++text++ -> <ins>text</ins>
42
        $this->InlineTypes['+'][] = 'Insert';
43
        $this->inlineMarkerList = implode('', array_keys($this->InlineTypes));
44
        $this->specialCharacters[] = '+';
45
46
        // Media (image, audio or video) block
47
        $this->BlockTypes['!'][] = 'Media';
48
49
        // "notes" block
50
        $this->BlockTypes[':'][] = 'Note';
51
52
        // code highlight
53
        $this->highlighter = new Highlighter();
54
55
        // options
56
        $options = array_merge(['selectors' => $this->builder->getConfig()->get('body.toc')], $options ?? []);
57
58
        parent::__construct($options);
59
    }
60
61
    /**
62
     * Insert inline.
63
     * e.g.: ++text++ -> <ins>text</ins>.
64
     */
65
    protected function inlineInsert($Excerpt)
66
    {
67
        if (!isset($Excerpt['text'][1])) {
68
            return;
69
        }
70
71
        if ($Excerpt['text'][1] === '+' && preg_match('/^\+\+(?=\S)(.+?)(?<=\S)\+\+/', $Excerpt['text'], $matches)) {
72
            return [
73
                'extent'  => strlen($matches[0]),
74
                'element' => [
75
                    'name'    => 'ins',
76
                    'text'    => $matches[1],
77
                    'handler' => 'line',
78
                ],
79
            ];
80
        }
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    protected function inlineImage($Excerpt)
87
    {
88
        $image = parent::inlineImage($Excerpt);
89
        if (!isset($image)) {
90
            return null;
91
        }
92
93
        // clean source path / URL
94
        $image['element']['attributes']['src'] = $this->cleanUrl($image['element']['attributes']['src']);
95
96
        // should be lazy loaded?
97
        if ($this->builder->getConfig()->get('body.images.lazy.enabled') && !isset($image['element']['attributes']['loading'])) {
98
            $image['element']['attributes']['loading'] = 'lazy';
99
        }
100
101
        // disable remote image handling?
102
        if (Util\Url::isUrl($image['element']['attributes']['src']) && !$this->builder->getConfig()->get('body.images.remote.enabled') ?? true) {
103
            return $image;
104
        }
105
106
        // create asset
107
        $asset = new Asset($this->builder, $image['element']['attributes']['src'], ['force_slash' => false]);
108
        $image['element']['attributes']['src'] = $asset;
109
        $width = $asset->getWidth();
110
111
        /**
112
         * Should be resized?
113
         */
114
        $assetResized = null;
115
        if (isset($image['element']['attributes']['width'])
116
            && (int) $image['element']['attributes']['width'] < $width
117
            && $this->builder->getConfig()->get('body.images.resize.enabled')
118
        ) {
119
            $width = (int) $image['element']['attributes']['width'];
120
121
            try {
122
                $assetResized = $asset->resize($width);
123
                $image['element']['attributes']['src'] = $assetResized;
124
            } catch (\Exception $e) {
125
                $this->builder->getLogger()->debug($e->getMessage());
126
127
                return $image;
128
            }
129
        }
130
131
        // set width
132
        if (!isset($image['element']['attributes']['width']) && $asset['type'] == 'image') {
133
            $image['element']['attributes']['width'] = $width;
134
        }
135
        // set height
136
        if (!isset($image['element']['attributes']['height']) && $asset['type'] == 'image') {
137
            $image['element']['attributes']['height'] = ($assetResized ?? $asset)->getHeight();
138
        }
139
140
        /**
141
         * Should be responsive?
142
         */
143
        if ($asset['type'] == 'image' && $this->builder->getConfig()->get('body.images.responsive.enabled')) {
144
            try {
145
                if ($srcset = Image::buildSrcset(
146
                    $assetResized ?? $asset,
147
                    $this->builder->getConfig()->get('assets.images.responsive.widths') ?? [480, 640, 768, 1024, 1366, 1600, 1920]
148
                )) {
149
                    $image['element']['attributes']['srcset'] = $srcset;
150
                    $image['element']['attributes']['sizes'] = $this->builder->getConfig()->get('assets.images.responsive.sizes.default');
151
                }
152
            } catch (\Exception $e) {
153
                $this->builder->getLogger()->debug($e->getMessage());
154
            }
155
        }
156
157
        return $image;
158
    }
159
160
    /**
161
     * Media block support:
162
     * 1. <picture>/<source> for WebP images
163
     * 2. <audio> and <video> elements
164
     * 3. <figure>/<figcaption> for element with a title.
165
     */
166
    protected function blockMedia($Excerpt)
167
    {
168
        if (1 !== preg_match($this->MarkdownMediaRegex, $Excerpt['text'])) {
169
            return;
170
        }
171
172
        $InlineImage = $this->inlineImage($Excerpt);
173
        if (!isset($InlineImage)) {
174
            return;
175
        }
176
        $block = $InlineImage;
177
178
        switch ($block['element']['attributes']['alt']) {
179
            case 'audio':
180
                $audio = [];
181
                $audio['element'] = [
182
                    'name'    => 'audio',
183
                    'handler' => 'element',
184
                ];
185
                $audio['element']['attributes'] = $block['element']['attributes'];
186
                unset($audio['element']['attributes']['loading']);
187
                $block = $audio;
188
                unset($block['element']['attributes']['alt']);
189
                break;
190
            case 'video':
191
                $video = [];
192
                $video['element'] = [
193
                    'name'       => 'video',
194
                    'handler'    => 'element',
195
                ];
196
                $video['element']['attributes'] = $block['element']['attributes'];
197
                unset($video['element']['attributes']['loading']);
198
                if (isset($block['element']['attributes']['poster'])) {
199
                    $video['element']['attributes']['poster'] = new Asset($this->builder, $block['element']['attributes']['poster'], ['force_slash' => false]);
200
                }
201
                $block = $video;
202
                unset($block['element']['attributes']['alt']);
203
        }
204
205
        /*
206
        <!-- if image has a title: a <figure> is required for <figcaption> -->
207
        <figure>
208
            <!-- if WebP: a <picture> is required for <source> -->
209
            <picture>
210
                <source type="image/webp"
211
                    srcset="..."
212
                    sizes="..."
213
                >
214
                <img src="..."
215
                    srcset="..."
216
                    sizes="..."
217
                >
218
            </picture>
219
            <!-- title -->
220
            <figcaption>...</figcaption>
221
        </figure>
222
        */
223
224
        // creates a <picture> used to add WebP <source> in addition to the image <img> element
225
        if ($this->builder->getConfig()->get('body.images.webp.enabled') ?? false
226
            && (($InlineImage['element']['attributes']['src'])['type'] == 'image'
227
                && ($InlineImage['element']['attributes']['src'])['subtype'] != 'image/webp')
228
        ) {
229
            try {
230
                // Image src must be an Asset instance
231
                if (is_string($InlineImage['element']['attributes']['src'])) {
232
                    throw new RuntimeException(\sprintf('Asset "%s" can\'t be converted to WebP', $InlineImage['element']['attributes']['src']));
233
                }
234
                // Image asset is an animated GIF
235
                if (Image::isAnimatedGif($InlineImage['element']['attributes']['src'])) {
236
                    throw new RuntimeException(\sprintf('Asset "%s" is an animated GIF and can\'t be converted to WebP', $InlineImage['element']['attributes']['src']));
237
                }
238
                $assetWebp = Image::convertTopWebp($InlineImage['element']['attributes']['src'], $this->builder->getConfig()->get('assets.images.quality') ?? 75);
239
                $srcset = '';
240
                // build responsives WebP?
241
                if ($this->builder->getConfig()->get('body.images.responsive.enabled')) {
242
                    try {
243
                        $srcset = Image::buildSrcset(
244
                            $assetWebp,
245
                            $this->builder->getConfig()->get('assets.images.responsive.widths') ?? [480, 640, 768, 1024, 1366, 1600, 1920]
246
                        );
247
                    } catch (\Exception $e) {
248
                        $this->builder->getLogger()->debug($e->getMessage());
249
                    }
250
                }
251
                // if not, default image as srcset
252
                if (empty($srcset)) {
253
                    $srcset = (string) $assetWebp;
254
                }
255
                $PictureBlock = [
256
                    'element' => [
257
                        'name'    => 'picture',
258
                        'handler' => 'elements',
259
                    ],
260
                ];
261
                $source = [
262
                    'element' => [
263
                        'name'       => 'source',
264
                        'attributes' => [
265
                            'type'   => 'image/webp',
266
                            'srcset' => $srcset,
267
                            'sizes'  => $this->builder->getConfig()->get('assets.images.responsive.sizes.default'),
268
                        ],
269
                    ],
270
                ];
271
                $PictureBlock['element']['text'][] = $source['element'];
272
                // clean title (and preserve raw HTML)
273
                if (isset($InlineImage['element']['attributes']['title'])) {
274
                    $titleRawHtml = $this->line($InlineImage['element']['attributes']['title']);
275
                    $InlineImage['element']['attributes']['title'] = strip_tags($this->line($InlineImage['element']['attributes']['title']));
276
                }
277
                $PictureBlock['element']['text'][] = $InlineImage['element'];
278
                $block = $PictureBlock;
279
            } catch (\Exception $e) {
280
                $this->builder->getLogger()->debug($e->getMessage());
281
            }
282
        }
283
284
        // if there is a title: put the <img> (or <picture>) in a <figure> element to use the <figcaption>
285
        if ($this->builder->getConfig()->get('body.images.caption.enabled') && !empty($InlineImage['element']['attributes']['title'])) {
286
            $FigureBlock = [
287
                'element' => [
288
                    'name'    => 'figure',
289
                    'handler' => 'elements',
290
                    'text'    => [
291
                        $block['element'],
292
                    ],
293
                ],
294
            ];
295
            $InlineFigcaption = [
296
                'element' => [
297
                    'name'                   => 'figcaption',
298
                    'allowRawHtmlInSafeMode' => true,
299
                    'rawHtml'                => $this->line($titleRawHtml),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $titleRawHtml does not seem to be defined for all execution paths leading up to this point.
Loading history...
300
                ],
301
            ];
302
            $FigureBlock['element']['text'][] = $InlineFigcaption['element'];
303
304
            return $FigureBlock;
305
        }
306
307
        return $block;
308
    }
309
310
    /**
311
     * Note block-level markup.
312
     *
313
     * :::tip
314
     * **Tip:** This is an advice.
315
     * :::
316
     *
317
     * Code inspired by https://github.com/sixlive/parsedown-alert from TJ Miller (@sixlive).
318
     */
319
    protected function blockNote($block)
320
    {
321
        if (preg_match('/:::(.*)/', $block['text'], $matches)) {
322
            $block = [
323
                'char'    => ':',
324
                'element' => [
325
                    'name'       => 'aside',
326
                    'text'       => '',
327
                    'attributes' => [
328
                        'class' => 'note',
329
                    ],
330
                ],
331
            ];
332
            if (!empty($matches[1])) {
333
                $block['element']['attributes']['class'] .= " note-{$matches[1]}";
334
            }
335
336
            return $block;
337
        }
338
    }
339
340
    protected function blockNoteContinue($line, $block)
341
    {
342
        if (isset($block['complete'])) {
343
            return;
344
        }
345
        if (preg_match('/:::/', $line['text'])) {
346
            $block['complete'] = true;
347
348
            return $block;
349
        }
350
        $block['element']['text'] .= $line['text']."\n";
351
352
        return $block;
353
    }
354
355
    protected function blockNoteComplete($block)
356
    {
357
        $block['element']['rawHtml'] = $this->text($block['element']['text']);
358
        unset($block['element']['text']);
359
360
        return $block;
361
    }
362
363
    /**
364
     * Apply Highlight to code blocks.
365
     */
366
    protected function blockFencedCodeComplete($block)
367
    {
368
        if (!$this->builder->getConfig()->get('body.highlight.enabled')) {
369
            return $block;
370
        }
371
        if (!isset($block['element']['text']['attributes'])) {
372
            return $block;
373
        }
374
375
        $code = $block['element']['text']['text'];
376
        unset($block['element']['text']['text']);
377
        $languageClass = $block['element']['text']['attributes']['class'];
378
        $language = explode('-', $languageClass);
379
        $highlighted = $this->highlighter->highlight($language[1], $code);
380
        $block['element']['text']['attributes']['class'] = vsprintf('%s hljs %s', [
381
            $languageClass,
382
            $highlighted->language,
383
        ]);
384
        $block['element']['text']['rawHtml'] = $highlighted->value;
385
        $block['element']['text']['allowRawHtmlInSafeMode'] = true;
386
387
        return $block;
388
    }
389
390
    /**
391
     * {@inheritdoc}
392
     */
393
    protected function parseAttributeData($attributeString)
394
    {
395
        $attributes = preg_split('/[ ]+/', $attributeString, -1, PREG_SPLIT_NO_EMPTY);
396
        $Data = [];
397
        $HtmlAtt = [];
398
399
        foreach ($attributes as $attribute) {
400
            switch ($attribute[0]) {
401
                case '#': // ID
402
                    $Data['id'] = substr($attribute, 1);
403
                    break;
404
                case '.': // Classes
405
                    $classes[] = substr($attribute, 1);
406
                    break;
407
                default:  // Attributes
408
                    parse_str($attribute, $parsed);
409
                    $HtmlAtt = array_merge($HtmlAtt, $parsed);
410
            }
411
        }
412
413
        if (isset($classes)) {
414
            $Data['class'] = implode(' ', $classes);
415
        }
416
        if (!empty($HtmlAtt)) {
417
            foreach ($HtmlAtt as $a => $v) {
418
                $Data[$a] = trim($v, '"');
419
            }
420
        }
421
422
        return $Data;
423
    }
424
425
    /**
426
     * Returns URL without query string.
427
     */
428
    private function cleanUrl(string $path): string
429
    {
430
        return strtok(trim($path), '?') ?: trim($path);
431
    }
432
}
433