This repository has been archived on 2023-05-26. You can view files and clone it, but cannot push or open issues or pull requests.
web/compil.php

92 lines
3.0 KiB
PHP
Executable File

<?php
if (php_sapi_name() === "cli") {
define("SITE", $argv[1]);
define("DESTINATION", $argv[2]);
require "bibli/parsedown/Parsedown.php";
require "bibli/parsedown-extra/ParsedownExtra.php";
$nomsPages = array_diff(scandir(SITE . "/md"), array('..', '.'));
require "inc/url.php";
require SITE . "/pages.inc.php";
foreach ($nomsPages as $pageId) {
$pageId = basename($pageId, ".md");
if (!file_exists("temp/" . SITE))
mkdir("temp/" . SITE, 555);
// Convert Gemtext to Markdown
$gmilines = explode("\n", file_get_contents(SITE . "/md/" . $pageId . ".md"));
foreach ($gmilines as $key => $line) {
if (substr($line, 0, 2) === "=>") {
preg_match("/=> +(.[^ ]+)/", $line, $url);
preg_match("/=> +.[^ ]+ +(.+)/", $line, $title);
$mdSpecial = array("[", "]", "(", ")");
$htmlEntities = array("&#91;", "&#93;", "&#40;", "&#41;");
$url[1] = str_replace($mdSpecial, $htmlEntities, $url[1]);
if (isset($title[1])) {
$title[1] = str_replace($mdSpecial, $htmlEntities, $title[1]);
$gmilines[$key] = "[" . $title[1] . "](" . $url[1] . ")";
} else {
$gmilines[$key] = "[" . $url[1] . "](" . $url[1] . ")";
}
}
}
$code = "";
foreach ($gmilines as $line) {
$code = $code . "\n" . $line;
}
// pages/exemple.md > temp/exemple.temp
// Exécute le PHP vers du HTML
ob_start();
eval("?>" . $code);
//require SITE . "/md/" . $pageId . ".md";
file_put_contents("temp/" . SITE . "/" . $pageId . ".temp", ob_get_contents());
ob_end_clean();
// temp/exemple.temp > temp2/exemple.temp2
// Compile le Markdown vers du HTML
$contenuPage = file_get_contents("temp/" . SITE . "/" . $pageId . ".temp");
$Parsedown = new ParsedownExtra;
$Parsedown = $Parsedown->setUrlsLinked(false);
$Parsedown = $Parsedown->setMarkupEscaped(false);
$Parsedown = $Parsedown->setBreaksEnabled(true);
$contenuPage = $Parsedown->text($contenuPage);
file_put_contents("temp/" . SITE . "/" . $pageId . ".temp2", $contenuPage);
// temp2/exemple.temp2 > exemple.html
// Ajoute header et footer au HTML
ob_start();
$pageMetas = getPageMetas($pageId);
require "inc/debut.php";
if ($pageMetas['type'] == "article") {
echo "<h1>" . $pageMetas['h1'] . "</h1>";
echo "<article>";
} else if ($pageMetas['type'] == "centré") {
echo "<div class='centre'>";
}
require "temp/" . SITE . "/" . $pageId . ".temp2";
if ($pageMetas['type'] == "article") {
echo "</article>";
} else if ($pageMetas['type'] == "centré") {
echo "</div>";
}
require "inc/footer.php";
file_put_contents(SITE . "/" . $pageId . ".html", ob_get_contents());
ob_end_clean();
}
// exemple.html > exemple.html.gz
// Compresse les fichiers textes statiques HTML & CSS
exec('/usr/local/bin/static-compress -c zopfli "' . SITE . '/*.html" "' . SITE . '/*.js" "' . SITE . '/css/*.css"');
} else {
exit("Must be run from CLI");
}