Total Complexity | 60 |
Total Lines | 408 |
Duplicated Lines | 0 % |
Changes | 11 | ||
Bugs | 6 | Features | 0 |
Complex classes like Parsedown often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Parsedown, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
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) |
||
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) |
||
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) |
||
353 | } |
||
354 | |||
355 | protected function blockNoteComplete($block) |
||
361 | } |
||
362 | |||
363 | /** |
||
364 | * Apply Highlight to code blocks. |
||
365 | */ |
||
366 | protected function blockFencedCodeComplete($block) |
||
388 | } |
||
389 | |||
390 | /** |
||
391 | * {@inheritdoc} |
||
392 | */ |
||
393 | protected function parseAttributeData($attributeString) |
||
423 | } |
||
424 | |||
425 | /** |
||
426 | * Returns URL without query string. |
||
427 | */ |
||
428 | private function cleanUrl(string $path): string |
||
433 |