forked from sculpin/sculpin
/
TextileConverter.php
85 lines (75 loc) · 2.18 KB
/
TextileConverter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
declare(strict_types=1);
/*
* This file is a part of Sculpin.
*
* (c) Dragonfly Development Inc.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sculpin\Bundle\TextileBundle;
use Netcarver\Textile\Parser;
use Sculpin\Core\Converter\ConverterContextInterface;
use Sculpin\Core\Converter\ConverterInterface;
use Sculpin\Core\Event\SourceSetEvent;
use Sculpin\Core\Sculpin;
use Sculpin\Core\Source\SourceInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* @author Beau Simensen <beau@dflydev.com>
*/
final class TextileConverter implements ConverterInterface, EventSubscriberInterface
{
/**
* @var Parser
*/
private Parser $textileParser;
/**
* File name extensions that are handled as textile.
*
* @var string[]
*/
private array $extensions = [];
/**
* @param string[] $extensions file name extensions that are handled as markdown
*/
public function __construct(Parser $parser, array $extensions = [])
{
$this->textileParser = $parser;
$this->extensions = $extensions;
}
/**
* {@inheritdoc}
*/
public function convert(ConverterContextInterface $converterContext): void
{
$converterContext->setContent($this->textileParser->parse($converterContext->content()));
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents(): array
{
return [
Sculpin::EVENT_BEFORE_RUN => 'beforeRun',
];
}
/**
* Event hook to register this converter for all sources that have markdown file extensions.
*
* @internal
*/
public function beforeRun(SourceSetEvent $sourceSetEvent): void
{
/** @var SourceInterface $source */
foreach ($sourceSetEvent->updatedSources() as $source) {
foreach ($this->extensions as $extension) {
if (fnmatch("*.{$extension}", $source->filename())) {
$source->data()->append('converters', SculpinTextileBundle::CONVERTER_NAME);
break;
}
}
}
}
}