Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions src/Collection/CollectionPaginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,39 @@ public function __construct($outputPathResolver)
$this->outputPathResolver = $outputPathResolver;
}

public function paginate($file, $items, $perPage, $prefix)
public function paginate(string $relativePath, string $filename, $items, $perPage, string $prefix = '')
{
$chunked = collect($items)->chunk($perPage);
$totalPages = $chunked->count();
$this->prefix = $prefix;
$numberedPageLinks = $chunked->map(function ($_, $i) use ($file) {
$numberedPageLinks = $chunked->map(function ($_, $i) use ($relativePath, $filename) {
$page = $i + 1;

return ['number' => $page, 'path' => $this->getPageLink($file, $page)];
return ['number' => $page, 'path' => $this->getPageLink($relativePath, $filename, $page)];
})->pluck('path', 'number');

return $chunked->map(function ($items, $i) use ($file, $totalPages, $numberedPageLinks) {
return $chunked->map(function ($items, $i) use ($relativePath, $filename, $totalPages, $numberedPageLinks) {
$currentPage = $i + 1;

return new IterableObject([
'items' => $items,
'previous' => $currentPage > 1 ? $this->getPageLink($file, $currentPage - 1) : null,
'current' => $this->getPageLink($file, $currentPage),
'next' => $currentPage < $totalPages ? $this->getPageLink($file, $currentPage + 1) : null,
'first' => $this->getPageLink($file, 1),
'last' => $this->getPageLink($file, $totalPages),
'previous' => $currentPage > 1 ? $this->getPageLink($relativePath, $filename, $currentPage - 1) : null,
'current' => $this->getPageLink($relativePath, $filename, $currentPage),
'next' => $currentPage < $totalPages ? $this->getPageLink($relativePath, $filename, $currentPage + 1) : null,
'first' => $this->getPageLink($relativePath, $filename, 1),
'last' => $this->getPageLink($relativePath, $filename, $totalPages),
'currentPage' => $currentPage,
'totalPages' => $totalPages,
'pages' => $numberedPageLinks,
]);
});
}

private function getPageLink($file, $pageNumber)
private function getPageLink(string $relativePath, string $filename, int $pageNumber): string
{
$link = $this->outputPathResolver->link(
$file->getRelativePath(),
$file->getFilenameWithoutExtension(),
$relativePath,
$filename,
'html',
$pageNumber,
$this->prefix,
Expand Down
6 changes: 4 additions & 2 deletions src/File/OutputFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ class OutputFile

private $prefix;

public function __construct(InputFile $inputFile, $path, $name, $extension, $contents, $data, $page = 1, $prefix = '')
public function __construct(?InputFile $inputFile, $path, $name, $extension, $contents, $data, $page = 1, $prefix = '')
{
$this->setInputFile($inputFile, $data);
if ($inputFile !== null) {
$this->setInputFile($inputFile, $data);
}
$this->path = $path;
$this->name = $name;
$this->extension = $extension;
Expand Down
66 changes: 50 additions & 16 deletions src/Handlers/PaginatedPageHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace TightenCo\Jigsaw\Handlers;

use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use TightenCo\Jigsaw\Collection\CollectionPaginator;
use TightenCo\Jigsaw\File\InputFile;
use TightenCo\Jigsaw\File\OutputFile;
use TightenCo\Jigsaw\File\TemporaryFilesystem;
use TightenCo\Jigsaw\PageData;
Expand Down Expand Up @@ -42,35 +44,67 @@ public function shouldHandle($file)
return isset($content->frontMatter['pagination']);
}

public function handle($file, PageData $pageData)
public function handle($file, PageData $pageData): Collection
{
$page = $pageData->page;
$page->addVariables($this->getPageVariables($file));
$collection = $page->pagination->collection;
$prefix = $page->pagination->prefix
?: $page->collections->{$collection}->prefix
?: $page->collections->{$collection}?->prefix
?: $page->prefix
?: '';
$perPage = $page->pagination->perPage
?: $page->collections->{$collection}?->perPage
?: $page->perPage
?: 10;
$extension = strtolower($file->getExtension());
$outputExtension = ($extension == 'php' || $extension == 'md') ? 'html' : $extension;

return $this->paginator->paginate(
$file,
return $this->buildOutputFiles(
$file->getRelativePath(),
$file->getFilenameWithoutExtension(),
$outputExtension,
$pageData->get($collection),
$page->pagination->perPage
?: $page->collections->{$collection}->perPage
?: $page->perPage
?: 10,
$perPage,
$prefix,
$pageData,
fn ($pageData) => $this->renderFile($file, $pageData),
$file,
);
}

public function handleDefinition(string $relativePath, string $filename, string $template, $items, int $perPage, PageData $pageData): Collection
{
return $this->buildOutputFiles(
$relativePath,
$filename,
'html',
$items,
$perPage,
'',
$pageData,
fn ($pageData) => $this->view->renderView($template, $pageData),
);
}

private function buildOutputFiles(string $relativePath, string $filename, string $extension, $items, int $perPage, string $prefix, PageData $pageData, callable $renderer, ?InputFile $inputFile = null): Collection
{
return $this->paginator->paginate(
$relativePath,
$filename,
$items,
$perPage,
$prefix,
)->map(function ($page) use ($file, $pageData, $prefix) {
)->map(function ($page) use ($relativePath, $filename, $extension, $pageData, $prefix, $renderer, $inputFile) {
$pageData->setPagePath($page->current);
$pageData->put('pagination', $page);
$extension = strtolower($file->getExtension());

return new OutputFile(
$file,
$file->getRelativePath(),
$file->getFilenameWithoutExtension(),
($extension == 'php' || $extension == 'md') ? 'html' : $extension,
$this->render($file, $pageData),
$inputFile,
$relativePath,
$filename,
$extension,
$renderer($pageData),
$pageData,
$page->currentPage,
$prefix,
Expand All @@ -83,7 +117,7 @@ private function getPageVariables($file)
return $this->parser->getFrontMatter($file->getContents());
}

private function render($file, $pageData)
private function renderFile($file, $pageData)
{
$bladeContent = $this->parser->getBladeContent($file->getContents());
$bladeFilePath = $this->temporaryFilesystem->put(
Expand Down
12 changes: 12 additions & 0 deletions src/Jigsaw.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class Jigsaw

protected $verbose;

protected $pendingPages = [];

protected static $commands = [];

public function __construct(
Expand Down Expand Up @@ -64,6 +66,15 @@ public static function addUserCommands($app, $container)
$app->addCommands(array_map(fn ($command) => new $command($container), self::$commands));
}

public function paginateCollection(string $path, string $collection, string $template, int $perPage = 10, array $variables = []): self
{
$this->setConfig("collections.{$collection}", []);

$this->pendingPages[] = compact('path', 'collection', 'template', 'perPage', 'variables');

return $this;
}

protected function buildCollections()
{
$this->remoteItemLoader->write($this->siteData->collections, $this->getSourcePath());
Expand All @@ -81,6 +92,7 @@ protected function buildSite($useCache)
$this->getSourcePath(),
$this->getDestinationPath(),
$this->siteData,
$this->pendingPages,
);
$this->outputPaths = $this->pageInfo->keys();

Expand Down
46 changes: 43 additions & 3 deletions src/SiteBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use TightenCo\Jigsaw\Console\ConsoleOutput;
use TightenCo\Jigsaw\File\Filesystem;
use TightenCo\Jigsaw\File\InputFile;
use TightenCo\Jigsaw\Handlers\PaginatedPageHandler;

class SiteBuilder
{
Expand Down Expand Up @@ -42,10 +43,11 @@ public function setUseCache($useCache)
return $this;
}

public function build($source, $destination, $siteData)
public function build($source, $destination, $siteData, $pendingPages = [])
{
$this->prepareDirectory($this->cachePath, ! $this->useCache);
$generatedFiles = $this->generateFiles($source, $siteData);
$generatedFiles = $this->generateFiles($source, $siteData)
->merge($this->generatePaginatedPages($pendingPages, $siteData));
$this->prepareDirectory($destination, true);
$outputFiles = $this->writeFiles($generatedFiles, $destination);
$this->cleanup();
Expand Down Expand Up @@ -99,14 +101,42 @@ private function generateFiles($source, $siteData)
return $files;
}

private function generatePaginatedPages($pendingPages, $siteData)
{
if (empty($pendingPages)) {
return collect();
}

$handler = collect($this->handlers)->first(fn ($h) => $h instanceof PaginatedPageHandler);

return collect($pendingPages)->flatMap(function ($def) use ($handler, $siteData) {
$relativePath = ltrim(dirname($def['path']), '.');
$filename = basename($def['path']);
$meta = $this->getMetaDataForPath($relativePath, $filename, $siteData->page->baseUrl);

$pageData = PageData::withPageMetaData($siteData, $meta);
Container::getInstance()->instance('pageData', $pageData);
$pageData->page->addVariables($def['variables']);

return $handler->handleDefinition(
$relativePath,
$filename,
$def['template'],
$siteData->get($def['collection']),
$def['perPage'],
$pageData,
);
});
}

private function writeFiles($files, $destination)
{
$this->consoleOutput->writeWritingFiles();

return $files->mapWithKeys(function ($file) use ($destination) {
$outputLink = $this->writeFile($file, $destination);

return [$outputLink => $file->inputFile()->getPageData()];
return [$outputLink => $file->inputFile()?->getPageData()];
});
}

Expand Down Expand Up @@ -136,6 +166,16 @@ private function getHandler($file)
});
}

private function getMetaDataForPath(string $relativePath, string $filename, ?string $baseUrl): array
{
$path = rightTrimPath($this->outputPathResolver->link($relativePath, $filename, 'html'));
$url = rightTrimPath($baseUrl ?? '') . '/' . trimPath($path);
$extension = 'html';
$modifiedTime = time();

return compact('filename', 'baseUrl', 'path', 'relativePath', 'extension', 'url', 'modifiedTime');
}

private function getMetaData($file, $baseUrl)
{
$filename = $file->getFilenameWithoutExtension();
Expand Down
5 changes: 5 additions & 0 deletions src/View/ViewRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ public function render($path, $data)
return app('view')->file($path, $data->all())->render();
}

public function renderView(string $view, $data): string
{
return app('view')->make($view, $data->all())->render();
}

public function renderString($string)
{
return app('blade.compiler')->compileString($string);
Expand Down
Loading
Loading