Добавлен tidy для форматирования

This commit is contained in:
2019-06-14 12:53:23 +03:00
parent 8210141951
commit 2116dd9dcd
6 changed files with 35 additions and 61 deletions

View File

@ -2,10 +2,12 @@
namespace Homepage\HtmlPrettierBundle;
use Gajus\Dindent\Indenter;
use Generator;
use Sculpin\Core\Event\SourceSetEvent;
use Sculpin\Core\Sculpin;
use Sculpin\Core\Source\SourceInterface;
use Sculpin\Core\Source\SourceSet;
use function strlen;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class HtmlPrettier implements EventSubscriberInterface
@ -22,23 +24,26 @@ class HtmlPrettier implements EventSubscriberInterface
public function formatHtml(SourceSetEvent $event): void
{
$indenter = new Indenter([
'indentation_character' => ' ',
]);
$config = [
'indent' => true,
'wrap' => 120,
];
$sources = $this->filterSource($event->sourceSet());
/** @var \Sculpin\Core\Source\SourceInterface $source */
/** @var SourceInterface $source */
foreach ($sources as $source) {
$html = $source->formattedContent();
$formatted = $indenter->indent($html);
$source->setFormattedContent($formatted);
$tidy = new \tidy();
$tidy->parseString($html, $config, 'utf8');
$tidy->cleanRepair();
$source->setFormattedContent((string) $tidy);
}
}
private function filterSource(SourceSet $sourceSet): \Generator
private function filterSource(SourceSet $sourceSet): Generator
{
/** @var \Sculpin\Core\Source\SourceInterface $source */
/** @var SourceInterface $source */
foreach ($sourceSet->allSources() as $source) {
$filename = $source->filename();
@ -55,7 +60,7 @@ class HtmlPrettier implements EventSubscriberInterface
private function endsWith($haystack, $needle): bool
{
$length = \strlen($needle);
$length = strlen($needle);
return $length === 0 || (substr($haystack, -$length) === $needle);
}