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

88 lines
2.6 KiB
PHP
Raw Normal View History

2020-08-02 14:15:06 +02:00
<?php
if (php_sapi_name() === "cli") {
define("SITE", $argv[1]);
define("DESTINATION", $argv[2]);
2020-08-02 14:15:06 +02:00
require "bibli/parsedown/Parsedown.php";
2021-04-05 18:32:22 +02:00
require "bibli/parsedown-extra/ParsedownExtra.php";
2021-09-21 19:19:05 +02:00
exec("/usr/bin/find " . SITE . " -name '*.gmi' -o -name '*.md'", $pages);
2021-04-05 18:32:22 +02:00
require "inc/url.php";
2021-04-05 18:32:22 +02:00
2021-09-21 19:19:05 +02:00
foreach ($pages as $page) {
2021-04-05 18:32:22 +02:00
2021-09-21 19:19:05 +02:00
$pathParts = pathinfo($page);
2020-08-02 14:15:06 +02:00
2021-09-18 19:09:11 +02:00
// Convert Gemtext to Markdown
2021-09-21 19:19:05 +02:00
$gmilines = explode("\n", file_get_contents($page));
if (substr($gmilines[0], 0, 2) === "# ")
$title = substr($gmilines[0], 2);
else
$title = NULL;
2021-09-18 19:09:11 +02:00
foreach ($gmilines as $key => $line) {
if (substr($line, 0, 2) === "=>") {
2021-09-21 19:19:05 +02:00
preg_match("/=> +(.[^ ]+)/", $line, $lnUrl);
preg_match("/=> +.[^ ]+ +(.+)/", $line, $lnTitle);
2021-09-18 19:09:11 +02:00
$mdSpecial = array("[", "]", "(", ")");
$htmlEntities = array("&#91;", "&#93;", "&#40;", "&#41;");
2021-09-21 19:19:05 +02:00
$lnUrl[1] = str_replace($mdSpecial, $htmlEntities, $lnUrl[1]);
if (isset($lnTitle[1])) {
$lnTitle[1] = str_replace($mdSpecial, $htmlEntities, $lnTitle[1]);
$gmilines[$key] = "[" . $lnTitle[1] . "](" . $lnUrl[1] . ")";
2021-09-18 19:09:11 +02:00
} else {
2021-09-21 19:19:05 +02:00
$gmilines[$key] = "[" . $lnUrl[1] . "](" . $lnUrl[1] . ")";
2021-09-18 19:09:11 +02:00
}
}
}
$code = "";
foreach ($gmilines as $line) {
$code = $code . "\n" . $line;
}
2020-08-02 14:15:06 +02:00
// pages/exemple.md > temp/exemple.temp
2021-04-05 18:32:22 +02:00
// Exécute le PHP vers du HTML
2020-08-02 14:15:06 +02:00
ob_start();
2021-09-18 19:09:11 +02:00
eval("?>" . $code);
2021-09-21 19:19:05 +02:00
$contenuPage = ob_get_contents();
2020-08-02 14:15:06 +02:00
ob_end_clean();
// temp/exemple.temp > temp2/exemple.temp2
2021-04-05 18:32:22 +02:00
// Compile le Markdown vers du HTML
2021-09-18 19:08:26 +02:00
$Parsedown = new ParsedownExtra;
2021-04-05 18:32:22 +02:00
$Parsedown = $Parsedown->setUrlsLinked(false);
$Parsedown = $Parsedown->setMarkupEscaped(false);
$Parsedown = $Parsedown->setBreaksEnabled(true);
$contenuPage = $Parsedown->text($contenuPage);
2020-08-02 14:15:06 +02:00
// temp2/exemple.temp2 > exemple.html
2021-04-05 18:32:22 +02:00
// Ajoute header et footer au HTML
2020-08-02 14:15:06 +02:00
ob_start();
2021-04-05 18:32:22 +02:00
2020-08-02 14:15:06 +02:00
require "inc/debut.php";
2021-09-21 19:19:05 +02:00
if ($pathParts['filename'] === "index") {
2020-08-02 14:15:06 +02:00
echo "<div class='centre'>";
2021-09-21 19:19:05 +02:00
} else {
echo "<article>";
2020-08-02 14:15:06 +02:00
}
2021-09-21 19:19:05 +02:00
echo $contenuPage;
if ($pathParts['filename'] === "index") {
2020-08-02 14:15:06 +02:00
echo "</div>";
2021-09-21 19:19:05 +02:00
} else {
echo "</article>";
2020-08-02 14:15:06 +02:00
}
require "inc/footer.php";
2021-09-21 19:19:05 +02:00
file_put_contents($pathParts['dirname'] . "/" . $pathParts['filename'] . ".html", ob_get_contents());
2020-08-02 14:15:06 +02:00
ob_end_clean();
}
// exemple.html > exemple.html.gz
2021-04-05 18:32:22 +02:00
// Compresse les fichiers textes statiques HTML & CSS
exec('/usr/local/bin/static-compress -c zopfli "' . SITE . '/*.html" "' . SITE . '/*.js" "' . SITE . '/css/*.css"');
2020-08-02 14:15:06 +02:00
} else {
exit("Must be run from CLI");
2020-08-02 14:15:06 +02:00
}