fipamo/brain/data/Render.inc.php

216 lines
6.2 KiB
PHP
Raw Normal View History

<?php
2021-04-23 01:04:43 +02:00
use Mni\FrontYAML\Parser;
class Render
{
2021-04-22 03:41:32 +02:00
public $loader;
public $twig;
public $pageInfo;
public $menu;
public $background;
public function __construct()
{
2021-04-22 03:41:32 +02:00
$config = new Settings();
$this->loader = new \Twig\Loader\FilesystemLoader("../content/themes");
$this->twig = new \Twig\Environment($this->loader, []);
$settings = $config->getSettings();
$this->menu = $settings["menu"];
$this->theme = $settings["global"]["theme"];
2021-04-22 03:41:32 +02:00
$this->pageInfo = [
"keywords" => isset($settings["global"]["keywords"])
? $settings["global"]["keywords"]
: "fipamo, blog, jamstack, php, markdown, js",
2021-04-22 03:41:32 +02:00
"description" => $settings["global"]["descriptions"],
"image" =>
$settings["global"]["base_url"] . $settings["global"]["background"],
2021-04-27 22:09:18 +02:00
"baseURL" => $settings["global"]["base_url"],
2021-04-22 03:41:32 +02:00
];
//copy current theme assets to public
if (is_file("../public/assets/css/base.css")) {
unlink("../public/assets/css/base.css");
}
copy(
"../content/themes/" . $this->theme . "/assets/css/base.css",
"../public/assets/css/base.css"
);
if (is_file("../public/assets/scripts/start.min.js")) {
unlink("../public/assets/scripts/start.min.js");
}
copy(
"../content/themes/" . $this->theme . "/assets/scripts/start.min.js",
"../public/assets/scripts/start.min.js"
);
2021-04-22 03:41:32 +02:00
}
2021-04-23 01:04:43 +02:00
public function renderPages()
{
$pages = (new Book("../content/pages"))->getContents();
$recent = [];
$featured = [];
$limit = 4;
foreach ($pages as $page) {
if (!$page["deleted"] && $page["published"]) {
if (count($recent) < $limit) {
array_push($recent, [
"path" => $page["path"],
"slug" => $page["slug"],
"title" => $page["title"],
]);
}
if ($page["featured"] == true) {
if (count($featured) < $limit) {
array_push($featured, [
"path" => $page["path"],
"slug" => $page["slug"],
"title" => $page["title"],
]);
}
}
$taglist = explode(",", $page["tags"]);
$tags = [];
foreach ($taglist as $tag) {
$label = trim($tag);
array_push($tags, [
2021-04-26 22:19:47 +02:00
"label" => $label . " ",
2021-04-23 01:04:43 +02:00
"slug" => StringTools::safeString($label),
]);
}
2021-04-26 22:19:47 +02:00
2021-04-23 01:04:43 +02:00
$meta = [
"who" => $page["author"],
2021-04-26 22:19:47 +02:00
"when" => $page["created"],
2021-04-23 01:04:43 +02:00
"tags" => $tags,
];
//render markdown content and clean it
$parser = new Parser();
$rendered = $parser->parse($page["content"]);
$sanitizer = HtmlSanitizer\Sanitizer::create([
"extensions" => ["basic", "image", "list", "code"],
"tags" => [
"img" => [
"allowed_attributes" => ["src", "alt", "title", "class"],
"allowed_hosts" => null,
],
],
2021-04-23 01:04:43 +02:00
]);
2021-04-23 01:04:43 +02:00
$preclean = $sanitizer->sanitize($rendered->getContent());
//just clean renderd string for now, Sanitize doesn't like relative img urls
//so another option is needed
$cleaned = strip_tags($rendered->getContent(), [
2021-04-23 01:04:43 +02:00
"a",
"br",
"p",
"strong",
"br",
"img",
"iframe",
"ul",
"li",
"i",
"h1",
"h2",
"h3",
"pre",
"code",
2021-04-23 01:04:43 +02:00
]);
//$cleaned = preg_replace('/(?:\r\n|[\r\n]){2,}/', "\n\n", $cleaned);
//$cleaned = html_entity_decode($cleaned, ENT_QUOTES, "UTF-8");
2021-04-27 22:09:18 +02:00
//if page feature isn't empty, replace page info meta image
if ($page["feature"] != "" || $page["feature"] != null) {
$this->pageInfo["image"] =
$this->pageInfo["baseURL"] . $page["feature"];
}
2021-04-23 01:04:43 +02:00
if ($page["layout"] == "index") {
$template = $this->theme . "/index.twig";
2021-04-23 01:04:43 +02:00
$location = "../public/index.html";
$dir = null;
$pageOptions = [
"title" => $page["title"],
"background" => $page["feature"],
"content" => $cleaned,
"meta" => $meta,
"recent" => $recent,
"featured" => $featured,
"info" => $this->pageInfo,
"menu" => $this->menu,
];
} else {
$template = $this->theme . "/page.twig";
2021-04-23 01:04:43 +02:00
$location =
"../public/" . $page["path"] . "/" . $page["slug"] . ".html";
$dir = "../public/" . $page["path"];
$pageOptions = [
"title" => $page["title"],
"background" => $page["feature"],
"content" => $cleaned,
"meta" => $meta,
"info" => $this->pageInfo,
"menu" => $this->menu,
];
}
$html = $this->twig->render($template, $pageOptions);
DocTools::writeHTML($location, $html, $dir);
}
}
}
public function renderArchive()
{
$archive = Sorting::archive();
$template = $this->theme . "/archive.twig";
2021-04-23 01:04:43 +02:00
$pageOptions = [
"title" => "Archive",
"background" => $this->pageInfo["image"],
"archives" => $archive,
"info" => $this->pageInfo,
"menu" => $this->menu,
];
$html = $this->twig->render($template, $pageOptions);
$location = "../public/archives.html";
DocTools::writeHTML($location, $html);
}
2021-04-22 03:41:32 +02:00
public function renderTags()
{
$list = Sorting::tags();
foreach ($list as $item) {
$template = $this->theme . "/tags.twig";
2021-04-22 03:41:32 +02:00
$pageOptions = [
"title" => "Pages Tagged as " . $item["tag_name"],
"background" => $this->pageInfo["image"],
"tag_list" => $item["pages"],
"info" => $this->pageInfo,
"menu" => $this->menu,
];
$html = $this->twig->render($template, $pageOptions);
$location = "../public/tags/" . $item["slug"] . ".html";
//if tags folder doesn't exist, make it
if (!is_dir("../public/tags")) {
mkdir("../public/tags", 0755, true);
} else {
}
2021-04-22 03:41:32 +02:00
if (!is_file($location)) {
file_put_contents($location, $html);
} else {
($new = fopen($location, "w")) or die("Unable to open file!");
fwrite($new, $html);
fclose($new);
}
}
}
}