2022-03-19 00:00:51 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace brain\data;
|
|
|
|
|
|
|
|
use brain\utility\Sorting;
|
|
|
|
use brain\utility\DocTools;
|
2022-05-17 04:14:38 +02:00
|
|
|
|
2022-03-19 00:00:51 +01:00
|
|
|
use function _\find;
|
|
|
|
|
|
|
|
class Render
|
|
|
|
{
|
|
|
|
public $loader;
|
2022-05-17 04:14:38 +02:00
|
|
|
|
2022-03-19 00:00:51 +01:00
|
|
|
public $twig;
|
2022-05-17 04:14:38 +02:00
|
|
|
|
2022-03-19 00:00:51 +01:00
|
|
|
public $pageInfo;
|
2022-05-17 04:14:38 +02:00
|
|
|
|
2022-03-19 00:00:51 +01:00
|
|
|
public $menu;
|
2022-05-17 04:14:38 +02:00
|
|
|
|
2022-03-19 00:00:51 +01:00
|
|
|
public $background;
|
2022-05-17 02:41:15 +02:00
|
|
|
|
2022-03-19 00:00:51 +01:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$config = new Settings();
|
|
|
|
//TODO: Add theme folder to loader
|
2022-05-17 02:41:15 +02:00
|
|
|
$settings = $config->getSettings();
|
|
|
|
$this->menu = $settings['menu'];
|
|
|
|
$this->theme = $settings['global']['theme'];
|
|
|
|
$this->loader = new \Twig\Loader\FilesystemLoader('../content/themes/' . $this->theme);
|
|
|
|
$this->twig = new \Twig\Environment($this->loader, []);
|
2022-03-19 00:00:51 +01:00
|
|
|
$this->pageInfo = [
|
2022-05-17 02:41:15 +02:00
|
|
|
'keywords' => isset($settings['global']['keywords'])
|
|
|
|
? $settings['global']['keywords']
|
|
|
|
: 'fipamo, blog, jamstack, php, markdown, js',
|
|
|
|
'description' => $settings['global']['descriptions'],
|
|
|
|
'image' => $settings['global']['base_url'] . $settings['global']['background'],
|
|
|
|
'baseURL' => $settings['global']['base_url'],
|
2022-03-19 00:00:51 +01:00
|
|
|
];
|
|
|
|
//move global theme image assets to public folder
|
2022-05-17 02:41:15 +02:00
|
|
|
foreach (new \DirectoryIterator('../content/themes/' . $this->theme . '/assets/images/global/') as $file
|
2022-03-19 00:00:51 +01:00
|
|
|
) {
|
|
|
|
if ($file->isDot()) {
|
|
|
|
continue;
|
|
|
|
}
|
2022-05-17 02:41:15 +02:00
|
|
|
if (!is_file('../public/assets/images/global/' . $file->getFileName())) {
|
2022-03-19 00:00:51 +01:00
|
|
|
copy(
|
2022-05-17 02:41:15 +02:00
|
|
|
'../content/themes/' .
|
2022-03-19 00:00:51 +01:00
|
|
|
$this->theme .
|
2022-05-17 02:41:15 +02:00
|
|
|
'/assets/images/global/' .
|
2022-03-19 00:00:51 +01:00
|
|
|
$file->getFileName(),
|
2022-05-17 02:41:15 +02:00
|
|
|
'../public/assets/images/global/' . $file->getFileName()
|
2022-03-19 00:00:51 +01:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
//image is already there, so chill
|
|
|
|
}
|
|
|
|
//print $file->getFilename() . "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
//copy current theme assets to public
|
|
|
|
|
|
|
|
//clear files in css and scripts folder
|
|
|
|
$styles = glob('../public/assets/css/*'); // get all file names
|
|
|
|
foreach ($styles as $file) { // iterate files
|
|
|
|
if (is_file($file)) {
|
|
|
|
//don't erase dashboard css
|
|
|
|
if (!$file == '../public/assets/css/dash.css') {
|
|
|
|
unlink($file); // delete file
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$scripts = glob('../public/assets/scripts/*'); // get all file names
|
|
|
|
foreach ($scripts as $file) { // iterate files
|
|
|
|
if (is_file($file)) {
|
2022-05-17 02:41:15 +02:00
|
|
|
if (!$file == '../public/assets/scripts/Start.js') {
|
2022-03-19 00:00:51 +01:00
|
|
|
unlink($file); // delete file
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//copy theme assets to public
|
2022-05-17 02:41:15 +02:00
|
|
|
$newcss = glob('../content/themes/' . $this->theme . '/assets/css/*');
|
2022-03-19 00:00:51 +01:00
|
|
|
foreach ($newcss as $file) { // iterate files
|
|
|
|
if (is_file($file)) {
|
2022-05-17 02:41:15 +02:00
|
|
|
$path = explode('/', $file);
|
|
|
|
copy($file, '../public/assets/css/' . $path[6]);
|
2022-03-19 00:00:51 +01:00
|
|
|
}
|
|
|
|
}
|
2022-05-17 02:41:15 +02:00
|
|
|
$newjs = glob('../content/themes/' . $this->theme . '/assets/scripts/*');
|
2022-03-19 00:00:51 +01:00
|
|
|
foreach ($newjs as $file) { // iterate files
|
|
|
|
if (is_file($file)) {
|
2022-05-17 02:41:15 +02:00
|
|
|
$path = explode('/', $file);
|
|
|
|
copy($file, '../public/assets/scripts/' . $path[6]);
|
2022-03-19 00:00:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-05-17 04:14:38 +02:00
|
|
|
|
2022-03-19 00:00:51 +01:00
|
|
|
public function renderPages()
|
|
|
|
{
|
2022-05-17 02:41:15 +02:00
|
|
|
$pages = (new Book())->getContents();
|
|
|
|
$recent = [];
|
2022-03-19 00:00:51 +01:00
|
|
|
$featured = [];
|
2022-05-17 02:41:15 +02:00
|
|
|
$limit = 4;
|
2022-03-19 00:00:51 +01:00
|
|
|
foreach ($pages as $page) {
|
|
|
|
$pageOptions = Sorting::page($page);
|
|
|
|
|
2022-05-17 02:41:15 +02:00
|
|
|
$layout = $page['layout'];
|
2022-03-19 00:00:51 +01:00
|
|
|
//new pages have no layout, so defautl for now
|
2022-05-17 02:41:15 +02:00
|
|
|
if ($layout == '' || $layout == null) {
|
|
|
|
$layout = 'page';
|
2022-03-19 00:00:51 +01:00
|
|
|
}
|
|
|
|
|
2022-05-17 02:41:15 +02:00
|
|
|
$template = $layout . '.twig';
|
|
|
|
if (str_contains($page['layout'], 'index')) {
|
|
|
|
$location = '../public/index.html';
|
|
|
|
$dir = null;
|
2022-03-19 00:00:51 +01:00
|
|
|
} else {
|
|
|
|
// if page is a menu item, render the page on public root
|
2022-05-17 02:41:15 +02:00
|
|
|
if ($page['menu'] == 'true') {
|
|
|
|
$location = '../public/' . $page['slug'] . '.html';
|
|
|
|
$dir = '../public/';
|
2022-03-19 00:00:51 +01:00
|
|
|
} else {
|
2022-05-17 02:41:15 +02:00
|
|
|
$location = '../public/' . $page['path'] . '/' . $page['slug'] . '.html';
|
|
|
|
$dir = '../public/' . $page['path'];
|
2022-03-19 00:00:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$html = $this->twig->render($template, $pageOptions);
|
|
|
|
DocTools::writeHTML($location, $html, $dir);
|
|
|
|
}
|
|
|
|
}
|
2022-05-17 04:14:38 +02:00
|
|
|
|
2022-03-19 00:00:51 +01:00
|
|
|
public function renderArchive()
|
|
|
|
{
|
2022-05-17 02:41:15 +02:00
|
|
|
$archive = Sorting::archive();
|
|
|
|
$template = 'archive.twig';
|
2022-03-19 00:00:51 +01:00
|
|
|
$pageOptions = [
|
2022-05-17 02:41:15 +02:00
|
|
|
'title' => 'Archive',
|
|
|
|
'background' => $this->pageInfo['image'],
|
|
|
|
'archives' => $archive,
|
|
|
|
'info' => $this->pageInfo,
|
|
|
|
'menu' => $this->menu,
|
2022-03-19 00:00:51 +01:00
|
|
|
];
|
|
|
|
|
2022-05-17 02:41:15 +02:00
|
|
|
$html = $this->twig->render($template, $pageOptions);
|
|
|
|
$location = '../public/archives.html';
|
2022-03-19 00:00:51 +01:00
|
|
|
DocTools::writeHTML($location, $html);
|
|
|
|
}
|
2022-05-17 04:14:38 +02:00
|
|
|
|
2022-03-19 00:00:51 +01:00
|
|
|
public function renderTags()
|
|
|
|
{
|
|
|
|
$list = Sorting::tags();
|
|
|
|
foreach ($list as $item) {
|
2022-05-17 02:41:15 +02:00
|
|
|
$template = 'tags.twig';
|
2022-03-19 00:00:51 +01:00
|
|
|
$pageOptions = [
|
2022-05-17 02:41:15 +02:00
|
|
|
'title' => 'Pages Tagged as ' . $item['tag_name'],
|
|
|
|
'background' => $this->pageInfo['image'],
|
|
|
|
'tag_list' => $item['pages'],
|
|
|
|
'info' => $this->pageInfo,
|
|
|
|
'menu' => $this->menu,
|
2022-03-19 00:00:51 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
$html = $this->twig->render($template, $pageOptions);
|
|
|
|
|
2022-05-17 02:41:15 +02:00
|
|
|
$location = '../public/tags/' . $item['slug'] . '.html';
|
2022-03-19 00:00:51 +01:00
|
|
|
|
|
|
|
//if tags folder doesn't exist, make it
|
2022-05-17 02:41:15 +02:00
|
|
|
if (!is_dir('../public/tags')) {
|
|
|
|
mkdir('../public/tags', 0755, true);
|
2022-03-19 00:00:51 +01:00
|
|
|
} else {
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_file($location)) {
|
|
|
|
file_put_contents($location, $html);
|
|
|
|
} else {
|
2022-05-17 02:41:15 +02:00
|
|
|
($new = fopen($location, 'w')) or die('Unable to open file!');
|
2022-03-19 00:00:51 +01:00
|
|
|
fwrite($new, $html);
|
|
|
|
fclose($new);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-05-17 04:14:38 +02:00
|
|
|
|
2022-03-19 00:00:51 +01:00
|
|
|
public function renderIndex()
|
|
|
|
{
|
|
|
|
//TODO: Need to fix this to account for new index templating system
|
2022-05-17 02:41:15 +02:00
|
|
|
$pages = (new Book())->getContents();
|
|
|
|
$index = find($pages, ['layout' => 'index']);
|
|
|
|
$template = 'index.twig';
|
|
|
|
$location = '../public/index.html';
|
|
|
|
$dir = null;
|
2022-03-19 00:00:51 +01:00
|
|
|
|
|
|
|
$meta = [
|
2022-05-17 02:41:15 +02:00
|
|
|
'who' => $index['author'],
|
|
|
|
'when' => $index['created'],
|
2022-03-19 00:00:51 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
$pageOptions = [
|
2022-05-17 02:41:15 +02:00
|
|
|
'title' => $index['title'],
|
|
|
|
'background' => $index['feature'],
|
|
|
|
'meta' => $meta,
|
2022-03-19 00:00:51 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
$html = $this->twig->render($template, $pageOptions);
|
|
|
|
DocTools::writeHTML($location, $html, $dir);
|
|
|
|
}
|
|
|
|
}
|