85 lines
2.4 KiB
PHP
85 lines
2.4 KiB
PHP
<?php
|
|
|
|
/**
|
|
* This class handles anything to do with reading files
|
|
* from the "site" directory and building out the
|
|
* collections and page array, and populating
|
|
* page objects with their relevant data.
|
|
*
|
|
* It also handles folders that need to be ignored
|
|
* such as "assets".
|
|
*/
|
|
|
|
namespace App\Build;
|
|
|
|
use Illuminate\Contracts\Filesystem\Filesystem;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class SiteParser
|
|
{
|
|
const IGNORED_DIRECTORIES = ["assets", "_"];
|
|
protected Filesystem $sourceDisk;
|
|
protected Filesystem $targetDisk;
|
|
protected array $paths;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->sourceDisk = Storage::disk('source');
|
|
$this->targetDisk = Storage::disk('build_local');
|
|
}
|
|
|
|
protected function readSite()
|
|
{
|
|
$files = $this->sourceDisk->files(recursive: true);
|
|
foreach (self::IGNORED_DIRECTORIES as $ignore) {
|
|
$files = array_filter($files, fn($path) => !str_starts_with($path, $ignore));
|
|
}
|
|
$pages = [];
|
|
|
|
// Set up a structure
|
|
foreach ($files as $f) {
|
|
$pages[] = [
|
|
'path' => $f,
|
|
'name' => basename($f),
|
|
'source' => Storage::disk('source')->path($f),
|
|
'output_path' => $this->getOutputPath($f),
|
|
'extension' => $this->getFileExtension($f)
|
|
];
|
|
}
|
|
}
|
|
|
|
public function getOutputPath(string $filename): string
|
|
{
|
|
$indexFiles = ['index.md', 'index.blade.php'];
|
|
|
|
$baseDirectory = Str::of($filename)->remove(basename($filename))->trim('/')->toString();
|
|
if (in_array(basename($filename), $indexFiles)) {
|
|
return join_paths($baseDirectory, "index.html");
|
|
}
|
|
|
|
return join_paths($baseDirectory, "{$this->slugFromFilename($filename)}", "index.html");
|
|
}
|
|
|
|
public function getFileExtension($filename): string
|
|
{
|
|
// Check for .blade.php first
|
|
if (str_ends_with($filename, '.blade.php')) {
|
|
return 'blade.php';
|
|
}
|
|
|
|
// Fall back to regular extension
|
|
return pathinfo($filename, PATHINFO_EXTENSION);
|
|
}
|
|
|
|
/**
|
|
* Parse all content collections and sub collections.
|
|
* Return a collection of ContentCollection objects
|
|
* with the correct data populated based on front
|
|
* matter and config.
|
|
*
|
|
* @return Collection
|
|
*/
|
|
public function collections(): Collection {}
|
|
}
|