diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..f299a02 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,4 @@ +/node_modules +/output_* +/var +/vendor diff --git a/app/SculpinKernel.php b/app/SculpinKernel.php index 4be6cc7..a009938 100644 --- a/app/SculpinKernel.php +++ b/app/SculpinKernel.php @@ -12,8 +12,7 @@ class SculpinKernel extends AbstractKernel return [ TwigExtensionBundle::class, SiteMapBundle::class, - // Выключен - некорректно форматирует блоки - // HtmlPrettierBundle::class, + HtmlPrettierBundle::class, ]; } } diff --git a/bundle/HtmlPrettierBundle/HtmlPrettier.php b/bundle/HtmlPrettierBundle/HtmlPrettier.php index 8b48775..315360b 100644 --- a/bundle/HtmlPrettierBundle/HtmlPrettier.php +++ b/bundle/HtmlPrettierBundle/HtmlPrettier.php @@ -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); } diff --git a/composer.json b/composer.json index 54bc01f..b104f9a 100644 --- a/composer.json +++ b/composer.json @@ -11,9 +11,9 @@ "require": { }, "require-dev": { - "gajus/dindent": "^2.0", "friendsofphp/php-cs-fixer": "^2.15", - "sculpin/sculpin": "^3.0" + "sculpin/sculpin": "^3.0", + "ext-tidy": "*" }, "autoload": { "psr-4": { diff --git a/composer.lock b/composer.lock index 5b6c3b1..616916c 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "c6dbaa9f2424b9e4e8e3e4fd3d854e0d", + "content-hash": "337638bb471e99cc8a19f050a703792d", "packages": [], "packages-dev": [ { @@ -769,51 +769,6 @@ "description": "A tool to automatically fix PHP code style", "time": "2019-06-01T10:32:12+00:00" }, - { - "name": "gajus/dindent", - "version": "2.0.2", - "source": { - "type": "git", - "url": "https://github.com/gajus/dindent.git", - "reference": "d81c3a6f78fbe1ab26f5e753098bbbef6b6a9f3c" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/gajus/dindent/zipball/d81c3a6f78fbe1ab26f5e753098bbbef6b6a9f3c", - "reference": "d81c3a6f78fbe1ab26f5e753098bbbef6b6a9f3c", - "shasum": "" - }, - "require": { - "php": ">=5.3" - }, - "require-dev": { - "satooshi/php-coveralls": "dev-master" - }, - "type": "library", - "autoload": { - "psr-4": { - "Gajus\\Dindent\\": "src/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Gajus Kuizinas", - "email": "gk@anuary.com" - } - ], - "description": "HTML indentation library for development and testing.", - "homepage": "https://github.com/gajus/dindent", - "keywords": [ - "format", - "html", - "indent" - ], - "time": "2014-10-08T10:03:04+00:00" - }, { "name": "michelf/php-markdown", "version": "1.8.0", @@ -3411,5 +3366,7 @@ "prefer-stable": false, "prefer-lowest": false, "platform": [], - "platform-dev": [] + "platform-dev": { + "ext-tidy": "*" + } } diff --git a/docker/Dockerfile b/docker/Dockerfile index 037e485..5e243d9 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -2,6 +2,15 @@ ARG PHP_BASE_IMAGE FROM ${PHP_BASE_IMAGE} +RUN apt-get update && apt-get install -y \ + libtidy-dev \ + curl \ + git \ + gzip + +RUN docker-php-ext-install tidy \ + && docker-php-ext-enable tidy + # Project folder RUN mkdir -p /srv/app