1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is a part of Sculpin. |
5
|
|
|
* |
6
|
|
|
* (c) Dragonfly Development Inc. |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Symplify\PHP7_Sculpin\Markdown\EventSubscriber; |
13
|
|
|
|
14
|
|
|
use Symplify\PHP7_Sculpin\Markdown\MarkdownConverter; |
15
|
|
|
use Symplify\PHP7_Sculpin\TwigBundle\SculpinTwigBundle; |
16
|
|
|
use Symplify\PHP7_Sculpin\Event\ConvertEvent; |
17
|
|
|
use Symplify\PHP7_Sculpin\Event\SculpinEvents; |
18
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
19
|
|
|
|
20
|
|
|
final class ConvertEventSubscriber implements EventSubscriberInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var string[] |
24
|
|
|
*/ |
25
|
|
|
private $addPlaceholderRe = [ |
26
|
|
|
'/^({%\s+block\s+(\w+).+?%})$/m', // {% %} style code |
27
|
|
|
'/^({%\s+endblock\s+%})$/m', // {% %} style code |
28
|
|
|
'/^({{.+?}})$/m', // {{ }} style code |
|
|
|
|
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Placeholder text. |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
private $placeholder = "\n<div><!-- sculpin-hidden -->$1<!-- /sculpin-hidden --></div>\n"; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Regex used to remove placeholder. |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
private $removePlaceholderRe = "/(\n?<div><!-- sculpin-hidden -->|<!-- \/sculpin-hidden --><\/div>\n|\n?<div><!-- sculpin-hidden -->|<!-- \/sculpin-hidden --><\/div>\n)/m"; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
|
|
public static function getSubscribedEvents() : array |
49
|
|
|
{ |
50
|
|
|
return [ |
51
|
|
|
SculpinEvents::BEFORE_CONVERT => 'beforeConvert', |
52
|
|
|
SculpinEvents::AFTER_CONVERT => 'afterConvert', |
53
|
|
|
]; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
View Code Duplication |
public function beforeConvert(ConvertEvent $convertEvent) |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
if ($convertEvent->isHandledBy(MarkdownConverter::NAME, SculpinTwigBundle::FORMATTER_NAME)) { |
59
|
|
|
$content = $convertEvent->source()->content(); |
60
|
|
|
foreach ($this->addPlaceholderRe as $re) { |
61
|
|
|
$content = preg_replace($re, $this->placeholder, $content); |
62
|
|
|
} |
63
|
|
|
$content = preg_replace('/{%\s+(\w+)\s+(\w+\++\w+)\s+%}(.*){%\s+end\1\s+%}/Us', '<div data-$1="$2">$3</div>', $content); |
64
|
|
|
$convertEvent->source()->setContent($content); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
View Code Duplication |
public function afterConvert(ConvertEvent $convertEvent) |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
if ($convertEvent->isHandledBy(MarkdownConverter::NAME, SculpinTwigBundle::FORMATTER_NAME)) { |
71
|
|
|
$content = $convertEvent->source()->content(); |
72
|
|
|
$content = preg_replace($this->removePlaceholderRe, '', $content); |
73
|
|
|
$content = preg_replace('/<div data-(\w+)="(\w+\++\w+)">(.*?)<\/div>/Us', '{% $1 \'$2\' %}$3{% end$1 %}', $content); |
74
|
|
|
$convertEvent->source()->setContent($content); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.