From b09e1c716463aa7a37781200f1fab65ed26b8328 Mon Sep 17 00:00:00 2001 From: Ro Date: Thu, 22 Apr 2021 16:04:43 -0700 Subject: [PATCH] pages rendering to html --- brain/api/v1/SettingsAPI.inc.php | 6 +- brain/data/Render.inc.php | 120 ++++++++++++++++++++ brain/utility/DocTools.inc.php | 17 +++ composer.json | 3 +- composer.lock | 188 ++++++++++++++++++++++++++++++- 5 files changed, 330 insertions(+), 4 deletions(-) diff --git a/brain/api/v1/SettingsAPI.inc.php b/brain/api/v1/SettingsAPI.inc.php index 9155a80..26a5b61 100644 --- a/brain/api/v1/SettingsAPI.inc.php +++ b/brain/api/v1/SettingsAPI.inc.php @@ -14,10 +14,12 @@ class SettingsAPI switch ($task) { case "publish": $render = new Render(); - $render->renderTags(); + //$render->renderTags(); + //$render->renderArchive(); + $render->renderPages(); $result = [ - "message" => "Items sorted. GOOD EFFORT", + "message" => "Site Rendered. GOOD EFFORT", "type" => "TASK_NONE", ]; diff --git a/brain/data/Render.inc.php b/brain/data/Render.inc.php index 7dac5cb..591aeca 100644 --- a/brain/data/Render.inc.php +++ b/brain/data/Render.inc.php @@ -1,4 +1,5 @@ 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, [ + "tag_name" => $label, + "slug" => StringTools::safeString($label), + ]); + } + $meta = [ + "who" => $page["author"], + "when" => $page["updated"], + "tags" => $tags, + ]; + + //render markdown content and clean it + $parser = new Parser(); + $rendered = $parser->parse($page["content"]); + $sanitizer = \HtmlSanitizer\Sanitizer::create([ + "extensions" => ["basic", "image", "list"], + ]); + $preclean = $sanitizer->sanitize($rendered->getContent()); + $cleaned = strip_tags($preclean, [ + "a", + "br", + "p", + "strong", + "br", + "img", + "iframe", + "ul", + "li", + "i", + "h1", + "h2", + "h3", + ]); + //$cleaned = preg_replace('/(?:\r\n|[\r\n]){2,}/', "\n\n", $cleaned); + //$cleaned = html_entity_decode($cleaned, ENT_QUOTES, "UTF-8"); + + if ($page["layout"] == "index") { + $template = "fipamo-default/index.twig"; + $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 = "fipamo-default/page.twig"; + $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 = "fipamo-default/archive.twig"; + $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); + } + public function renderTags() { $list = Sorting::tags(); diff --git a/brain/utility/DocTools.inc.php b/brain/utility/DocTools.inc.php index cdf98ba..cc52766 100644 --- a/brain/utility/DocTools.inc.php +++ b/brain/utility/DocTools.inc.php @@ -34,6 +34,23 @@ class DocTools fclose($new); } + public static function writeHTML($location, $html, $path = null) + { + if ($path != null) { + if (!is_dir($path)) { + //Directory does not exist, so lets create it. + mkdir($path, 0755, true); + } + } + 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); + } + } + public static function objectToMD($object) { $markdown = diff --git a/composer.json b/composer.json index 8eb6166..2147fb3 100644 --- a/composer.json +++ b/composer.json @@ -7,6 +7,7 @@ "mnapoli/front-yaml": "^1.8", "lodash-php/lodash-php": "^0.0.7", "rbdwllr/reallysimplejwt": "^4.0", - "fightbulc/moment": "^1.33" + "fightbulc/moment": "^1.33", + "tgalopin/html-sanitizer": "^1.4" } } diff --git a/composer.lock b/composer.lock index 030001b..6e0098f 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "d5ef6d43f774049d093af6375d20ac11", + "content-hash": "f829b5451dc7ceba4625d240a185b162", "packages": [ { "name": "erusev/parsedown", @@ -169,6 +169,75 @@ }, "time": "2021-03-27T13:10:08+00:00" }, + { + "name": "league/uri-parser", + "version": "1.4.1", + "source": { + "type": "git", + "url": "https://github.com/thephpleague/uri-parser.git", + "reference": "671548427e4c932352d9b9279fdfa345bf63fa00" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/thephpleague/uri-parser/zipball/671548427e4c932352d9b9279fdfa345bf63fa00", + "reference": "671548427e4c932352d9b9279fdfa345bf63fa00", + "shasum": "" + }, + "require": { + "php": ">=7.0.0" + }, + "require-dev": { + "friendsofphp/php-cs-fixer": "^2.0", + "phpstan/phpstan": "^0.9.2", + "phpstan/phpstan-phpunit": "^0.9.4", + "phpstan/phpstan-strict-rules": "^0.9.0", + "phpunit/phpunit": "^6.0" + }, + "suggest": { + "ext-intl": "Allow parsing RFC3987 compliant hosts", + "league/uri-schemes": "Allow validating and normalizing URI parsing results" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.x-dev" + } + }, + "autoload": { + "psr-4": { + "League\\Uri\\": "src" + }, + "files": [ + "src/functions_include.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Ignace Nyamagana Butera", + "email": "nyamsprod@gmail.com", + "homepage": "https://nyamsprod.com" + } + ], + "description": "userland URI parser RFC 3986 compliant", + "homepage": "https://github.com/thephpleague/uri-parser", + "keywords": [ + "parse_url", + "parser", + "rfc3986", + "rfc3987", + "uri", + "url" + ], + "support": { + "issues": "https://github.com/thephpleague/uri-parser/issues", + "source": "https://github.com/thephpleague/uri-parser/tree/master" + }, + "time": "2018-11-22T07:55:51+00:00" + }, { "name": "lodash-php/lodash-php", "version": "0.0.7", @@ -224,6 +293,75 @@ }, "time": "2020-09-21T11:55:26+00:00" }, + { + "name": "masterminds/html5", + "version": "2.7.4", + "source": { + "type": "git", + "url": "https://github.com/Masterminds/html5-php.git", + "reference": "9227822783c75406cfe400984b2f095cdf03d417" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/Masterminds/html5-php/zipball/9227822783c75406cfe400984b2f095cdf03d417", + "reference": "9227822783c75406cfe400984b2f095cdf03d417", + "shasum": "" + }, + "require": { + "ext-ctype": "*", + "ext-dom": "*", + "ext-libxml": "*", + "php": ">=5.3.0" + }, + "require-dev": { + "phpunit/phpunit": "^4.8.35" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.7-dev" + } + }, + "autoload": { + "psr-4": { + "Masterminds\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Matt Butcher", + "email": "technosophos@gmail.com" + }, + { + "name": "Matt Farina", + "email": "matt@mattfarina.com" + }, + { + "name": "Asmir Mustafic", + "email": "goetas@gmail.com" + } + ], + "description": "An HTML5 parser and serializer.", + "homepage": "http://masterminds.github.io/html5-php", + "keywords": [ + "HTML5", + "dom", + "html", + "parser", + "querypath", + "serializer", + "xml" + ], + "support": { + "issues": "https://github.com/Masterminds/html5-php/issues", + "source": "https://github.com/Masterminds/html5-php/tree/2.7.4" + }, + "time": "2020-10-01T13:52:52+00:00" + }, { "name": "mnapoli/front-yaml", "version": "1.8.0", @@ -2077,6 +2215,54 @@ ], "time": "2021-03-06T07:59:01+00:00" }, + { + "name": "tgalopin/html-sanitizer", + "version": "1.4.0", + "source": { + "type": "git", + "url": "https://github.com/tgalopin/html-sanitizer.git", + "reference": "56cca6b48de4e50d16a4f549e3e677ae0d561e91" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/tgalopin/html-sanitizer/zipball/56cca6b48de4e50d16a4f549e3e677ae0d561e91", + "reference": "56cca6b48de4e50d16a4f549e3e677ae0d561e91", + "shasum": "" + }, + "require": { + "ext-dom": "*", + "league/uri-parser": "^1.4.1", + "masterminds/html5": "^2.4", + "php": ">=7.1", + "psr/log": "^1.0" + }, + "require-dev": { + "phpunit/phpunit": "^7.4", + "symfony/var-dumper": "^4.1" + }, + "type": "library", + "autoload": { + "psr-4": { + "HtmlSanitizer\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Titouan Galopin", + "email": "galopintitouan@gmail.com" + } + ], + "description": "Sanitize untrustworthy HTML user input", + "support": { + "issues": "https://github.com/tgalopin/html-sanitizer/issues", + "source": "https://github.com/tgalopin/html-sanitizer/tree/master" + }, + "time": "2020-02-03T16:51:08+00:00" + }, { "name": "twig/twig", "version": "v3.3.0",