Skip to content

Commit

Permalink
Do some refactorin'.
Browse files Browse the repository at this point in the history
  • Loading branch information
damiendart committed Jul 28, 2024
1 parent 0e9cc22 commit a74a837
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 128 deletions.
25 changes: 10 additions & 15 deletions src/ssg/StaticSiteGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
use StaticSiteGenerator\Steps\ProcessMarkdownStep;
use StaticSiteGenerator\Steps\ProcessTwigStep;
use StaticSiteGenerator\Steps\WriteFilesStep;
use StaticSiteGenerator\Support\MarkdownConverterFactory;
use StaticSiteGenerator\Support\TwigEnvironmentFactory;
use StaticSiteGenerator\ValueObjects\SiteMetadata;

final readonly class StaticSiteGenerator
Expand All @@ -28,27 +30,20 @@ public function __construct(
private string $inputDirectory,
private string $outputDirectory,
) {
$releaseTimestamp = getenv('RELEASE_TIMESTAMP') ?: (new \DateTimeImmutable())->format('YmdHis');

$siteMetadata = new SiteMetadata(
[
'author' => 'Damien Dart',
'authorEmail' => '[email protected]',
'metaTwitterAuthor' => '@damiendart',
'metaTwitterSite' => '@damiendart',
'metaOpengraphImage' => "https://www.robotinaponcho.net/assets/opengraph.{$releaseTimestamp}.png",
'releaseTimestamp' => $releaseTimestamp,
'urlBase' => 'https://www.robotinaponcho.net/',
],
);
$markdownConverterFactory = new MarkdownConverterFactory();
$siteMetadata = new SiteMetadata();

$this->pipeline = new Pipeline(
new ProcessFrontMatterStep(),
new GenerateSlugsStep(),
new GenerateCollectionsStep($siteMetadata),
new GenerateSitemapEntriesStep($siteMetadata),
new ProcessMarkdownStep(),
new ProcessTwigStep($this->inputDirectory, $siteMetadata),
new ProcessMarkdownStep($markdownConverterFactory),
new ProcessTwigStep(
$this->inputDirectory,
$siteMetadata,
new TwigEnvironmentFactory($markdownConverterFactory),
),
new MinifyHtmlStep(),
new WriteFilesStep($this->outputDirectory),
);
Expand Down
19 changes: 3 additions & 16 deletions src/ssg/Steps/ProcessMarkdownStep.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,16 @@
namespace StaticSiteGenerator\Steps;

use League\CommonMark\ConverterInterface;
use League\CommonMark\Environment\Environment;
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
use League\CommonMark\Extension\DescriptionList\DescriptionListExtension;
use League\CommonMark\Extension\SmartPunct\SmartPunctExtension;
use League\CommonMark\Extension\Table\TableExtension;
use League\CommonMark\MarkdownConverter;
use StaticSiteGenerator\Inputfile;
use StaticSiteGenerator\Support\MarkdownConverterFactory;

final readonly class ProcessMarkdownStep implements StepInterface
{
private ConverterInterface $converter;

public function __construct()
public function __construct(MarkdownConverterFactory $factory)
{
$environment = new Environment();

$environment
->addExtension(new CommonMarkCoreExtension())
->addExtension(new DescriptionListExtension())
->addExtension(new SmartPunctExtension())
->addExtension(new TableExtension());

$this->converter = new MarkdownConverter($environment);
$this->converter = $factory->make();
}

public function run(Inputfile ...$inputFiles): array
Expand Down
97 changes: 3 additions & 94 deletions src/ssg/Steps/ProcessTwigStep.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,13 @@

namespace StaticSiteGenerator\Steps;

use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
use League\CommonMark\Extension\DescriptionList\DescriptionListExtension;
use League\CommonMark\Extension\SmartPunct\SmartPunctExtension;
use League\CommonMark\Extension\Table\TableExtension;
use League\CommonMark\MarkdownConverter;
use Michelf\SmartyPantsTypographer;

use function StaticSiteGenerator\dedent;

use StaticSiteGenerator\Inputfile;
use StaticSiteGenerator\Support\TwigEnvironmentFactory;
use StaticSiteGenerator\ValueObjects\SiteMetadata;
use Twig\Environment;
use Twig\Loader\ArrayLoader;
use Twig\Loader\ChainLoader;
use Twig\Loader\FilesystemLoader;
use Twig\Loader\LoaderInterface;
use Twig\TwigFilter;

final readonly class ProcessTwigStep implements StepInterface
{
Expand All @@ -35,6 +25,7 @@
public function __construct(
string $inputDirectory,
private SiteMetadata $globalMetadata,
private TwigEnvironmentFactory $twigEnvironmentFactory,
) {
$this->filesystemLoader = new FilesystemLoader();

Expand All @@ -60,7 +51,7 @@ public function run(Inputfile ...$inputFiles): array
);
$chainLoader->addLoader($this->filesystemLoader);

$environment = $this->createEnvironment($chainLoader);
$environment = $this->twigEnvironmentFactory->make($chainLoader);
$data = array_merge(
$this->globalMetadata->metadata,
$inputFile->metadata,
Expand Down Expand Up @@ -93,86 +84,4 @@ private function processRelativePathname(string $pathname): string

return $pathname;
}

private function createEnvironment(LoaderInterface $loader): Environment
{
$environment = new Environment(
$loader,
['strict_variables' => true],
);

$environment->addFilter(
new TwigFilter(
'dedent',
static function (string $string): string {
return dedent($string);
},
),
);

$environment->addFilter(
new TwigFilter(
'markdown',
static function (string $string): string {
$environment = new \League\CommonMark\Environment\Environment();
$environment
->addExtension(new CommonMarkCoreExtension())
->addExtension(new DescriptionListExtension())
->addExtension(new SmartPunctExtension())
->addExtension(new TableExtension());

$converter = new MarkdownConverter($environment);

return $converter
->convert($string)
->getContent();
},
['is_safe' => ['html']],
),
);

$environment->addFilter(
new TwigFilter(
'smartypants',
[SmartyPantsTypographer::class, 'defaultTransform'],
['is_safe' => ['html']],
),
);

$environment->addFilter(
new TwigFilter(
'widont',
static function (?string $string): string {
$string ??= '';

if (str_word_count($string) < 4) {
return $string;
}

if (
mb_strlen(
join(
' ',
\array_slice(
explode(' ', $string),
-2,
),
),
) > 14
) {
return $string;
}

return preg_replace(
'/\s+(\S+)$/',
'&#160;$1',
rtrim($string),
);
},
['is_safe' => ['html']],
),
);

return $environment;
}
}
35 changes: 35 additions & 0 deletions src/ssg/Support/MarkdownConverterFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/*
* Copyright (C) Damien Dart, <[email protected]>.
* This file is distributed under the MIT licence. For more information,
* please refer to the accompanying "LICENCE" file.
*/

declare(strict_types=1);

namespace StaticSiteGenerator\Support;

use League\CommonMark\ConverterInterface;
use League\CommonMark\Environment\Environment;
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
use League\CommonMark\Extension\DescriptionList\DescriptionListExtension;
use League\CommonMark\Extension\SmartPunct\SmartPunctExtension;
use League\CommonMark\Extension\Table\TableExtension;
use League\CommonMark\MarkdownConverter;

final class MarkdownConverterFactory
{
public function make(): ConverterInterface
{
$environment = new Environment();

$environment
->addExtension(new CommonMarkCoreExtension())
->addExtension(new DescriptionListExtension())
->addExtension(new SmartPunctExtension())
->addExtension(new TableExtension());

return new MarkdownConverter($environment);
}
}
91 changes: 91 additions & 0 deletions src/ssg/Support/TwigEnvironmentFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

/*
* Copyright (C) Damien Dart, <[email protected]>.
* This file is distributed under the MIT licence. For more information,
* please refer to the accompanying "LICENCE" file.
*/

declare(strict_types=1);

namespace StaticSiteGenerator\Support;

use Michelf\SmartyPantsTypographer;
use Twig\Environment;
use Twig\Loader\LoaderInterface;
use Twig\TwigFilter;

final readonly class TwigEnvironmentFactory
{
public function __construct(
private MarkdownConverterFactory $markdownConverterFactory,
) {}

public function make(LoaderInterface $loader): Environment
{
$environment = new Environment(
$loader,
['strict_variables' => true],
);

$environment->addFilter(new TwigFilter('dedent', 'StaticSiteGenerator\dedent'));

$environment->addFilter(
new TwigFilter(
'markdown',
function (string $string): string {
$converter = $this->markdownConverterFactory->make();

return $converter
->convert($string)
->getContent();
},
['is_safe' => ['html']],
),
);

$environment->addFilter(
new TwigFilter(
'smartypants',
[SmartyPantsTypographer::class, 'defaultTransform'],
['is_safe' => ['html']],
),
);

$environment->addFilter(
new TwigFilter(
'widont',
static function (?string $string): string {
$string ??= '';

if (str_word_count($string) < 4) {
return $string;
}

if (
mb_strlen(
join(
' ',
\array_slice(
explode(' ', $string),
-2,
),
),
) > 14
) {
return $string;
}

return preg_replace(
'/\s+(\S+)$/',
'&#160;$1',
rtrim($string),
);
},
['is_safe' => ['html']],
),
);

return $environment;
}
}
19 changes: 16 additions & 3 deletions src/ssg/ValueObjects/SiteMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,20 @@

final class SiteMetadata
{
public function __construct(
public array $metadata,
) {}
public array $metadata;

public function __construct()
{
$releaseTimestamp = getenv('RELEASE_TIMESTAMP') ?: (new \DateTimeImmutable())->format('YmdHis');

$this->metadata = [
'author' => 'Damien Dart',
'authorEmail' => '[email protected]',
'metaTwitterAuthor' => '@damiendart',
'metaTwitterSite' => '@damiendart',
'metaOpengraphImage' => "https://www.robotinaponcho.net/assets/opengraph.{$releaseTimestamp}.png",
'releaseTimestamp' => $releaseTimestamp,
'urlBase' => 'https://www.robotinaponcho.net/',
];
}
}

0 comments on commit a74a837

Please sign in to comment.