Skip to content

Commit

Permalink
Do some refactorin'.
Browse files Browse the repository at this point in the history
  • Loading branch information
damiendart committed Aug 18, 2024
1 parent 2fc2a39 commit e9807c1
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/ssg/Steps/GenerateSlugsStep.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ private function slugify(string $input): string
{
foreach (self::TEMPLATE_EXTENSIONS as $extension) {
if (str_ends_with($input, ".{$extension}")) {
$input = mb_strcut($input, 0, -5);
$input = mb_strcut($input, 0, -\strlen($extension) - 1);

break;
}
Expand Down
31 changes: 18 additions & 13 deletions src/ssg/Steps/ProcessFrontMatterStep.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,10 @@ protected function process(Inputfile $inputFile): Inputfile
$content = str_replace($matches[0], '', $inputFile->getContent());

/** @var array{array-key, mixed} $metadata */
$metadata = $this->yamlParser->parse($matches[1]);
$metadata = $this->yamlParser->parse($matches[1]) ?? [];

if (\array_key_exists('git', $metadata)) {
$git = explode(' ', trim($metadata['git'], '$'));

if (5 !== \count($git)) {
$metadata['git'] = new GitMetadata();
} else {
$metadata['git'] = new GitMetadata(
\DateTimeImmutable::createFromFormat('U', $git[2]),
\DateTimeImmutable::createFromFormat('U', $git[4]),
$git[1],
$git[3],
);
}
$metadata['git'] = $this->parseGitMetadataKeyword($metadata['git']);
}

return $inputFile
Expand All @@ -60,4 +49,20 @@ protected function process(Inputfile $inputFile): Inputfile

return $inputFile;
}

private function parseGitMetadataKeyword(string $keyword): GitMetadata
{
$parts = explode(' ', trim($keyword, '$'));

if (5 !== \count($parts)) {
throw new \RuntimeException('Unable to parse expanded "Metadata" keyword');
}

return new GitMetadata(
\DateTimeImmutable::createFromFormat('U', $parts[2]),
\DateTimeImmutable::createFromFormat('U', $parts[4]),
$parts[1],
$parts[3],
);
}
}
4 changes: 2 additions & 2 deletions src/ssg/Steps/ProcessMarkdownStep.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected function process(InputFile $inputFile): InputFile

return $inputFile
->withContent($this->processContent($inputFile->getContent()))
->withOutputPath($this->processRelativePathname($inputFile->outputPath));
->withOutputPath($this->processOutputPath($inputFile->outputPath));
}

private function extractTitle(InputFile $inputFile): InputFile
Expand Down Expand Up @@ -61,7 +61,7 @@ private function processContent(string $content): string
return $this->converter->convert($content)->getContent();
}

private function processRelativePathname(string $pathname): string
private function processOutputPath(string $pathname): string
{
error_clear_last();

Expand Down
6 changes: 3 additions & 3 deletions src/ssg/Steps/ProcessTwigStep.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

final class ProcessTwigStep extends AbstractStep
{
private InputFileCollection $collection;
private InputFileCollection $inputFiles;
private LoaderInterface $filesystemLoader;

public function __construct(
Expand All @@ -34,7 +34,7 @@ public function __construct(

public function run(InputFile ...$inputFiles): array
{
$this->collection = new InputFileCollection(...$inputFiles);
$this->inputFiles = new InputFileCollection(...$inputFiles);

return parent::run(...$inputFiles);
}
Expand Down Expand Up @@ -67,7 +67,7 @@ protected function process(InputFile $inputFile): InputFile
$template = $inputFile->outputPath;
}

$context['inputFiles'] = $this->collection;
$context['inputFiles'] = $this->inputFiles;

return $inputFile
->withContent($environment->render($template, $context))
Expand Down

0 comments on commit e9807c1

Please sign in to comment.