Skip to content

Commit ec10ad6

Browse files
wouterjlinawolf
authored andcommitted
Add custom Twig loader to remove last new line of a file
1 parent 4673d3a commit ec10ad6

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

packages/guides/resources/config/guides.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
use phpDocumentor\Guides\Twig\EnvironmentBuilder;
5858
use phpDocumentor\Guides\Twig\GlobalMenuExtension;
5959
use phpDocumentor\Guides\Twig\Theme\ThemeManager;
60+
use phpDocumentor\Guides\Twig\TrimFilesystemLoader;
6061
use phpDocumentor\Guides\Twig\TwigTemplateRenderer;
6162
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
6263
use Symfony\Component\DependencyInjection\Reference;
@@ -223,11 +224,12 @@
223224
param('phpdoc.guides.base_template_paths'),
224225
)
225226

226-
->set(FilesystemLoader::class)
227+
->set(TrimFilesystemLoader::class)
227228
->arg(
228229
'$paths',
229230
param('phpdoc.guides.base_template_paths'),
230231
)
232+
->alias(FilesystemLoader::class, TrimFilesystemLoader::class)
231233

232234
->set(LoadSettingsFromComposer::class)
233235
->tag('event_listener', ['event' => PostProjectNodeCreated::class])
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of phpDocumentor.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*
11+
* @link https://phpdoc.org
12+
*/
13+
14+
namespace phpDocumentor\Guides\Twig;
15+
16+
use Twig\Loader\FilesystemLoader;
17+
use Twig\Source;
18+
19+
use function preg_replace;
20+
21+
/**
22+
* A file system loader that trims the last line ending from the template
23+
* content.
24+
*/
25+
class TrimFilesystemLoader extends FilesystemLoader
26+
{
27+
// phpcs:disable SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
28+
29+
/** @param string $name */
30+
public function getSourceContext($name): Source
31+
{
32+
// phpcs:enable
33+
$source = parent::getSourceContext($name);
34+
35+
return new Source(
36+
preg_replace('/\R$/', '', $source->getCode()) ?? $source->getCode(),
37+
$source->getName(),
38+
$source->getPath(),
39+
);
40+
}
41+
}

0 commit comments

Comments
 (0)