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; |
13
|
|
|
|
14
|
|
|
use Michelf\MarkdownExtra; |
15
|
|
|
use Symplify\PHP7_Sculpin\Converter\SourceConverterContext; |
16
|
|
|
use Symplify\PHP7_Sculpin\Event\ConvertEvent; |
17
|
|
|
use Symplify\PHP7_Sculpin\Event\SculpinEvents; |
18
|
|
|
use Symplify\PHP7_Sculpin\Source\SourceInterface; |
19
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
20
|
|
|
use Symplify\PHP7_Sculpin\TwigBundle\SculpinTwigBundle; |
21
|
|
|
|
22
|
|
|
final class MarkdownConverter |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
const NAME = 'markdown'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var EventDispatcherInterface |
31
|
|
|
*/ |
32
|
|
|
private $eventDispatcher; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var MarkdownExtra |
36
|
|
|
*/ |
37
|
|
|
private $markdown; |
38
|
|
|
|
39
|
|
|
public function __construct(EventDispatcherInterface $eventDispatcher, MarkdownExtra $markdown) |
40
|
|
|
{ |
41
|
|
|
$this->eventDispatcher = $eventDispatcher; |
42
|
|
|
$this->markdown = $markdown; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function convert(SourceConverterContext $converterContext) |
46
|
|
|
{ |
47
|
|
|
$converterContext->setContent( |
48
|
|
|
$this->markdown->transform( |
49
|
|
|
$converterContext->content() |
50
|
|
|
) |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function convertSource(SourceInterface $source) |
55
|
|
|
{ |
56
|
|
|
if (!fnmatch("*.md", $source->filename())) { |
57
|
|
|
return; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$this->eventDispatcher->dispatch( |
61
|
|
|
SculpinEvents::BEFORE_CONVERT, |
62
|
|
|
new ConvertEvent($source, MarkdownConverter::NAME, SculpinTwigBundle::FORMATTER_NAME) |
63
|
|
|
); |
64
|
|
|
|
65
|
|
|
$this->convert(new SourceConverterContext($source)); |
66
|
|
|
|
67
|
|
|
$this->eventDispatcher->dispatch( |
68
|
|
|
SculpinEvents::AFTER_CONVERT, |
69
|
|
|
new ConvertEvent($source, MarkdownConverter::NAME, SculpinTwigBundle::FORMATTER_NAME) |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|