L'ancien dépot du générateur de sites d'Antopie https://antopie.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
 
 
 

129 lines
3.8 KiB

#!/usr/bin/php
<?php
if (php_sapi_name() !== "cli")
exit("Must be run from CLI");
define("FIND", "/usr/bin/find");
define("GZIP", "/usr/bin/gzip");
define("ROOT", dirname($_SERVER['SCRIPT_FILENAME']));
define("SITE", $argv[1]);
if (isset($argv[2]))
define("DESTINATION", $argv[2]);
else
define("DESTINATION", "local");
if (isset($argv[3]))
define("SITE_TITLE", $argv[3]);
else
define("SITE_TITLE", SITE);
require ROOT . "/bibli/parsedown/Parsedown.php";
require ROOT . "/bibli/parsedown-extra/ParsedownExtra.php";
// Less > CSS
require ROOT . "/bibli/less.php/lib/Less/Autoloader.php";
Less_Autoloader::register();
$options = array('cache_dir' => SITE . '/css', 'compress' => true);
if (file_exists(SITE . "/style.less"))
$lessFiles = array(ROOT . '/style.less' => '', SITE . '/style.less' => '');
else
$lessFiles = array(ROOT . '/style.less' => '');
define("CSS_FILENAME", Less_Cache::Get($lessFiles, $options));
exec(FIND . " " . SITE . " -name '*.gmi' -o -name '*.md'", $pages);
require "inc/url.php";
foreach ($pages as $page) {
$pathParts = pathinfo($page);
// Convert Gemtext to Markdown
if ($pathParts['extension'] === "gmi") {
$gmilines = explode("\n", file_get_contents($page));
if (substr($gmilines[0], 0, 2) === "# ")
$title = substr($gmilines[0], 2);
else
$title = NULL;
foreach ($gmilines as $key => $line) {
if (substr($line, 0, 2) === "=>") {
preg_match("/=> +(.[^ ]+)/", $line, $lnUrl);
preg_match("/=> +.[^ ]+ +(.+)/", $line, $lnTitle);
// Escape Markdown special characters
$mdSpecial = array("[", "]", "(", ")");
$htmlEntities = array("&#91;", "&#93;", "&#40;", "&#41;");
$lnUrl[1] = str_replace($mdSpecial, $htmlEntities, $lnUrl[1]);
$urlPathParts = pathinfo(parse_url($lnUrl[1], PHP_URL_PATH));
// .gmi > .md for local links
if (!str_contains($lnUrl[1], ":") AND $urlPathParts['extension'] === "gmi") // If it's a local link
$lnUrl[1] = $urlPathParts['dirname'] . "/" . $urlPathParts['filename'] . ".md";
if (isset($lnTitle[1])) {
$lnTitle[1] = str_replace($mdSpecial, $htmlEntities, $lnTitle[1]);
$gmilines[$key] = "[" . $lnTitle[1] . "](" . $lnUrl[1] . ")";
} else {
$gmilines[$key] = "[" . $lnUrl[1] . "](" . $lnUrl[1] . ")";
}
}
}
$code = "";
foreach ($gmilines as $line) {
$code = $code . "\n" . $line;
}
file_put_contents($pathParts['dirname'] . "/" . $pathParts['filename'] . ".md", $code);
}
// Execute PHP code
ob_start();
eval("?>" . file_get_contents($pathParts['dirname'] . "/" . $pathParts['filename'] . ".md"));
$pageContent = ob_get_contents();
ob_end_clean();
// Compile Markdown to HTML with Parsedown
$Parsedown = new ParsedownExtra;
$Parsedown = $Parsedown->setUrlsLinked(false);
$Parsedown = $Parsedown->setMarkupEscaped(false);
$Parsedown = $Parsedown->setBreaksEnabled(true);
$pageContent = $Parsedown->text($pageContent);
// .md > .html for local links
$pageContent = preg_replace('#<a href="(?!.*:)(.*)\.md">#', '<a href="$1.html">', $pageContent);
// Add header and footer to HTML
ob_start();
require "inc/debut.php";
if ($pathParts['filename'] === "index") {
echo "<div class='centre'>";
} else {
echo "<article>";
}
echo $pageContent;
if ($pathParts['filename'] === "index") {
echo "</div>";
} else {
echo "</article>";
}
require "inc/footer.php";
file_put_contents($pathParts['dirname'] . "/" . $pathParts['filename'] . ".html", ob_get_contents());
ob_end_clean();
// Gzip compression
exec(GZIP . " --keep --fast --force " . $pathParts['dirname'] . "/" . $pathParts['filename'] . ".html");
}
exec(GZIP . " --keep --fast --force " . SITE . "/css/" . CSS_FILENAME);