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

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

4
.dockerignore Normal file
View File

@ -0,0 +1,4 @@
/node_modules
/output_*
/var
/vendor

View File

@ -12,8 +12,7 @@ class SculpinKernel extends AbstractKernel
return [ return [
TwigExtensionBundle::class, TwigExtensionBundle::class,
SiteMapBundle::class, SiteMapBundle::class,
// Выключен - некорректно форматирует блоки <code> HtmlPrettierBundle::class,
// HtmlPrettierBundle::class,
]; ];
} }
} }

View File

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

View File

@ -11,9 +11,9 @@
"require": { "require": {
}, },
"require-dev": { "require-dev": {
"gajus/dindent": "^2.0",
"friendsofphp/php-cs-fixer": "^2.15", "friendsofphp/php-cs-fixer": "^2.15",
"sculpin/sculpin": "^3.0" "sculpin/sculpin": "^3.0",
"ext-tidy": "*"
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {

51
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "c6dbaa9f2424b9e4e8e3e4fd3d854e0d", "content-hash": "337638bb471e99cc8a19f050a703792d",
"packages": [], "packages": [],
"packages-dev": [ "packages-dev": [
{ {
@ -769,51 +769,6 @@
"description": "A tool to automatically fix PHP code style", "description": "A tool to automatically fix PHP code style",
"time": "2019-06-01T10:32:12+00:00" "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", "name": "michelf/php-markdown",
"version": "1.8.0", "version": "1.8.0",
@ -3411,5 +3366,7 @@
"prefer-stable": false, "prefer-stable": false,
"prefer-lowest": false, "prefer-lowest": false,
"platform": [], "platform": [],
"platform-dev": [] "platform-dev": {
"ext-tidy": "*"
}
} }

View File

@ -2,6 +2,15 @@ ARG PHP_BASE_IMAGE
FROM ${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 # Project folder
RUN mkdir -p /srv/app RUN mkdir -p /srv/app