forked from laysense/laysite
60 lines
2.1 KiB
PHP
60 lines
2.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Content;
|
|
|
|
use RuntimeException;
|
|
|
|
final class PageTemplates
|
|
{
|
|
public static function groups(): array
|
|
{
|
|
static $groups;
|
|
|
|
return $groups ??= require dirname(__DIR__, 2) . '/resources/page-templates.php';
|
|
}
|
|
|
|
public static function palette(string $theme): array
|
|
{
|
|
static $css;
|
|
$css ??= file_get_contents(dirname(__DIR__, 2) . '/public/assets/page-themes.css');
|
|
// The opening rule for each theme is the same palette used in the live template preview.
|
|
if ($css === false || !preg_match('/^\.theme-' . preg_quote($theme, '/') . '\s*\{([^{}]*)\}/m', $css, $rule)) {
|
|
throw new RuntimeException('找不到整页模板的配色。');
|
|
}
|
|
preg_match_all('/--([a-z-]+):\s*(#[a-f0-9]{6})\s*;/i', $rule[1], $matches);
|
|
$colors = array_intersect_key(array_combine($matches[1], $matches[2]), DesignLibrary::COLORS);
|
|
if (count($colors) !== count(DesignLibrary::COLORS)) {
|
|
throw new RuntimeException('整页模板的配色不完整。');
|
|
}
|
|
|
|
return $colors;
|
|
}
|
|
|
|
public static function find(string $key): ?array
|
|
{
|
|
foreach (self::groups() as $theme => $group) {
|
|
if (!isset($group['pages'][$key])) {
|
|
continue;
|
|
}
|
|
$template = $group['pages'][$key];
|
|
$sections = [];
|
|
$quicklinks = [];
|
|
foreach ($template['sections'] as [$anchor, $title, $style, $body, $feed]) {
|
|
$sections[] = ['key' => $anchor, 'title' => $title, 'style' => $style, 'body' => Html::sanitize($body, $anchor),
|
|
'lead' => '', 'feed' => $feed, 'alignments' => '{}', 'version' => 0];
|
|
$quicklinks[] = ['#' . $anchor, $title];
|
|
}
|
|
|
|
return array_replace($template, [
|
|
'slug' => $key, 'theme' => $theme, 'title' => $template['name'], 'accent' => '',
|
|
'lead' => $template['description'], 'quicklinks' => $quicklinks, 'status' => 'draft',
|
|
'alignments' => [], 'image_scale' => 0, 'sections' => $sections,
|
|
]);
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|