Better indentation

This commit is contained in:
Miraty 2022-06-01 17:33:32 +02:00
parent 3ece3cbb71
commit afe82852f5
3 changed files with 234 additions and 239 deletions

View file

@ -4,7 +4,7 @@ mkht.php is a PHP script for building Gemini, Markdown and HTML/CSS sites from s
Place your pages tree in `/src/*/*.(gmi|md)`. Place your pages tree in `/src/*/*.(gmi|md)`.
Optionnal files: Optional files:
* `/config.ini` * `/config.ini`
* `/style.less` * `/style.less`
* `/logo.png` * `/logo.png`

297
mkht.php
View file

@ -1,7 +1,7 @@
#!/usr/bin/php #!/usr/bin/php
<?php <?php
if (php_sapi_name() !== "cli") if (php_sapi_name() !== "cli")
exit("Must be run from CLI"); exit("Must be run from CLI");
// Initialization // Initialization
@ -11,224 +11,219 @@ define("GZIP", "/usr/bin/gzip");
define("ROOT", dirname($_SERVER['SCRIPT_FILENAME'])); define("ROOT", dirname($_SERVER['SCRIPT_FILENAME']));
if (isset($argv[1])) if (isset($argv[1]))
define("SITE", $argv[1]); define("SITE", $argv[1]);
else else
define("SITE", getcwd()); define("SITE", getcwd());
if (isset($argv[2])) if (isset($argv[2]))
define("DESTINATION", $argv[2]); define("DESTINATION", $argv[2]);
else else
define("DESTINATION", "local"); define("DESTINATION", "local");
if (file_exists(SITE . "/config.ini")) if (file_exists(SITE . "/config.ini"))
$config = parse_ini_file(SITE . "/config.ini"); $config = parse_ini_file(SITE . "/config.ini");
if (!isset($config['css'])) if (!isset($config['css']))
$config['css'] = true; $config['css'] = true;
if (!isset($config['header'])) if (!isset($config['header']))
$config['header'] = true; $config['header'] = true;
if (!isset($config['centerIndex'])) if (!isset($config['centerIndex']))
$config['centerIndex'] = false; $config['centerIndex'] = false;
if (!isset($config['defaultLang'])) if (!isset($config['defaultLang']))
$config['defaultLang'] = ""; $config['defaultLang'] = "";
// Less > CSS // Less > CSS
// Create CSS output directory if needed // Create CSS output directory if needed
if (!file_exists(SITE . "/css")) if (!file_exists(SITE . "/css"))
mkdir(SITE . "/css", 0755); mkdir(SITE . "/css", 0755);
require ROOT . "/less.php/lib/Less/Autoloader.php"; require ROOT . "/less.php/lib/Less/Autoloader.php";
Less_Autoloader::register(); Less_Autoloader::register();
$colorScheme = array( $colorScheme = array(
"darkColor" => "black", "darkColor" => "black",
"lightColor" => "white", "lightColor" => "white",
"mainColor" => "red", "mainColor" => "red",
); );
$options = array( $options = array(
'cache_dir' => SITE . '/css', 'cache_dir' => SITE . '/css',
'compress' => true, 'compress' => true,
); );
if (file_exists(SITE . "/style.less")) if (file_exists(SITE . "/style.less"))
$lessFiles = array(ROOT . '/style.less' => '', SITE . '/style.less' => ''); $lessFiles = array(ROOT . '/style.less' => '', SITE . '/style.less' => '');
else else
$lessFiles = array(ROOT . '/style.less' => ''); $lessFiles = array(ROOT . '/style.less' => '');
define("CSS_FILENAME", Less_Cache::Get($lessFiles, $options, $colorScheme)); define("CSS_FILENAME", Less_Cache::Get($lessFiles, $options, $colorScheme));
// URLs don't end with .html on the Antopie website // URLs don't end with .html on the Antopie website
function formerUrlLocale($page) { function formerUrlLocale($page) {
if (DESTINATION === "dns" OR DESTINATION === "onion") { if (DESTINATION === "dns" OR DESTINATION === "onion") {
echo $page; echo $page;
} else { } else {
echo $page . ".html"; echo $page . ".html";
} }
} }
// Determine whether links need to use Onion or DNS // Determine whether links need to use Onion or DNS
function clearnetOrOnion($clearnetUrl, $onionUrl) { function clearnetOrOnion($clearnetUrl, $onionUrl) {
if (DESTINATION === "onion") { if (DESTINATION === "onion") {
return $onionUrl; return $onionUrl;
} else { } else {
return $clearnetUrl; return $clearnetUrl;
} }
} }
exec(FIND . " " . SITE . "/src -name '*.gmi' -o -name '*.md'", $pages); exec(FIND . " " . SITE . "/src -name '*.gmi' -o -name '*.md'", $pages);
foreach ($pages as $page) { foreach ($pages as $page) {
$pathParts = pathinfo(str_replace("/src", "", $page)); $pathParts = pathinfo(str_replace("/src", "", $page));
// Create parent directory if needed // Create parent directory if needed
if (!file_exists($pathParts['dirname'])) if (!file_exists($pathParts['dirname']))
mkdir($pathParts['dirname'], 0755, true); mkdir($pathParts['dirname'], 0755, true);
// Execute PHP code // Execute PHP code
ob_start(); ob_start();
eval("?>" . file_get_contents($page)); eval("?>" . file_get_contents($page));
file_put_contents($pathParts['dirname'] . "/" . $pathParts['basename'], ob_get_contents()); file_put_contents($pathParts['dirname'] . "/" . $pathParts['basename'], ob_get_contents());
ob_end_clean(); ob_end_clean();
// Convert Gemtext to Markdown // Convert Gemtext to Markdown
if ($pathParts['extension'] === "gmi") { if ($pathParts['extension'] === "gmi") {
$gmilines = explode("\n", file_get_contents($pathParts['dirname'] . "/" . $pathParts['basename'])); $gmilines = explode("\n", file_get_contents($pathParts['dirname'] . "/" . $pathParts['basename']));
foreach ($gmilines as $key => $line) { foreach ($gmilines as $key => $line) {
if (substr($line, 0, 2) === "=>") { if (substr($line, 0, 2) === "=>") {
preg_match("/=> +(.[^ ]+)/", $line, $lnUrl); preg_match("/=> +(.[^ ]+)/", $line, $lnUrl);
preg_match("/=> +.[^ ]+ +(.+)/", $line, $lnTitle); preg_match("/=> +.[^ ]+ +(.+)/", $line, $lnTitle);
$urlPathParts = pathinfo(parse_url($lnUrl[1], PHP_URL_PATH)); $urlPathParts = pathinfo(parse_url($lnUrl[1], PHP_URL_PATH));
// .gmi > .md for local links // .gmi > .md for local links
if (!str_contains($lnUrl[1], ":") AND $urlPathParts['extension'] === "gmi") // If it's a local link if (!str_contains($lnUrl[1], ":") AND $urlPathParts['extension'] === "gmi") // If it's a local link
$lnUrl[1] = $urlPathParts['dirname'] . "/" . $urlPathParts['filename'] . ".md"; $lnUrl[1] = $urlPathParts['dirname'] . "/" . $urlPathParts['filename'] . ".md";
if (isset($lnTitle[1])) { if (isset($lnTitle[1])) {
$gmilines[$key] = "[" . $lnTitle[1] . "](" . $lnUrl[1] . ")"; $gmilines[$key] = "[" . $lnTitle[1] . "](" . $lnUrl[1] . ")";
} else { } else {
$gmilines[$key] = "[" . $lnUrl[1] . "](" . $lnUrl[1] . ")"; $gmilines[$key] = "[" . $lnUrl[1] . "](" . $lnUrl[1] . ")";
} }
} }
} }
$code = ""; $code = "";
foreach ($gmilines as $line) { foreach ($gmilines as $line) {
$code = $code . "\n" . $line; $code = $code . "\n" . $line;
} }
file_put_contents($pathParts['dirname'] . "/" . $pathParts['filename'] . ".md", $code); file_put_contents($pathParts['dirname'] . "/" . $pathParts['filename'] . ".md", $code);
} }
// Compile Markdown to HTML with Parsedown // Compile Markdown to HTML with Parsedown
$markdown = file_get_contents($pathParts['dirname'] . "/" . $pathParts['filename'] . ".md"); $markdown = file_get_contents($pathParts['dirname'] . "/" . $pathParts['filename'] . ".md");
if (preg_match("/# (.*)\\n/", $markdown, $matches)) // If a main heading is found if (preg_match("/# (.*)\\n/", $markdown, $matches)) // If a main heading is found
$title = $matches[1]; // Then it will be the HTML page <title> $title = $matches[1]; // Then it will be the HTML page <title>
else else
$title = NULL; $title = NULL;
require_once ROOT . "/parsedown/Parsedown.php"; require_once ROOT . "/parsedown/Parsedown.php";
require_once ROOT . "/parsedown-extra/ParsedownExtra.php"; require_once ROOT . "/parsedown-extra/ParsedownExtra.php";
$Parsedown = new ParsedownExtra; $Parsedown = new ParsedownExtra;
$Parsedown = $Parsedown->setUrlsLinked(false); $Parsedown = $Parsedown->setUrlsLinked(false);
$Parsedown = $Parsedown->setMarkupEscaped(false); $Parsedown = $Parsedown->setMarkupEscaped(false);
$Parsedown = $Parsedown->setBreaksEnabled(true); $Parsedown = $Parsedown->setBreaksEnabled(true);
$pageContent = $Parsedown->text($markdown); $pageContent = $Parsedown->text($markdown);
// .md > .html for local links // .md > .html for local links
$pageContent = preg_replace('#<a href="(?!.*:)(.*)\.md">#', '<a href="$1.html">', $pageContent); $pageContent = preg_replace('#<a href="(?!.*:)(.*)\.md">#', '<a href="$1.html">', $pageContent);
// Add header and footer to HTML // Add header and footer to HTML
$urlPath = str_replace(SITE, "", $pathParts['dirname']); $urlPath = str_replace(SITE, "", $pathParts['dirname']);
$relativePathToRoot = ""; $relativePathToRoot = "";
for ($i = substr_count($urlPath, "/") ; $i > 0 ; $i--) for ($i = substr_count($urlPath, "/") ; $i > 0 ; $i--)
$relativePathToRoot .= "../"; $relativePathToRoot .= "../";
ob_start(); ob_start();
?> ?>
<!DOCTYPE html> <!DOCTYPE html>
<html lang="<?php <html lang="<?php
preg_match("#\.([a-zA-Z-]{2,5})\.#", $pathParts['basename'], $lang); preg_match("#\.([a-zA-Z-]{2,5})\.#", $pathParts['basename'], $lang);
if (isset($lang[1])) { if (isset($lang[1])) {
echo $lang[1]; echo $lang[1];
} else { } else {
preg_match("#/([a-z]{2})(/|$)#", $pathParts['dirname'], $lang); preg_match("#/([a-z]{2})(/|$)#", $pathParts['dirname'], $lang);
if (isset($lang[1])) if (isset($lang[1]))
echo $lang[1]; echo $lang[1];
else else
echo $config['defaultLang']; echo $config['defaultLang'];
} }
?>"> ?>">
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<?php <?php
if (isset($title) AND !is_null($title) AND isset($config['siteTitle'])) if (isset($title) AND !is_null($title) AND isset($config['siteTitle']))
echo "<title>" . $title . " · " . $config['siteTitle'] . "</title>"; echo "<title>" . $title . " · " . $config['siteTitle'] . "</title>";
else if (isset($title) AND !is_null($title)) else if (isset($title) AND !is_null($title))
echo "<title>" . $title . "</title>"; echo "<title>" . $title . "</title>";
else if (isset($config['siteTitle'])) else if (isset($config['siteTitle']))
echo "<title>" . $config['siteTitle'] . "</title>"; echo "<title>" . $config['siteTitle'] . "</title>";
?> ?>
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<?php if ($config['css']) { ?> <?php
<link rel="stylesheet" media="screen" href="<?= $relativePathToRoot ?>css/<?= CSS_FILENAME ?>"> if ($config['css']) {
<?php } ?> ?>
<?php <link rel="stylesheet" media="screen" href="<?= $relativePathToRoot ?>css/<?= CSS_FILENAME ?>">
if (file_exists(SITE . "/head.inc.html")) <?php
echo file_get_contents(SITE . "/head.inc.html"); }
?>
</head>
<body> if (file_exists(SITE . "/head.inc.html"))
<?php echo file_get_contents(SITE . "/head.inc.html");
if ($config['header']) { ?>
?> </head>
<header>
<a id="lienHeader" href="./<?= $relativePathToRoot ?>">
<?php
if (file_exists(SITE . "/img/logo.webp"))
echo '<img src="img/logo.webp" ' . getimagesize(SITE . "/img/logo.webp")[3] . ' alt="' . $config['siteTitle'] . '" />';
else
echo $config['siteTitle'];
?>
</a>
</header>
<?php
}
?>
<?php <body>
if ($config['centerIndex'] AND $pathParts['filename'] === "index") { <?php
echo '<div class="centered">'; if ($config['header']) {
} else { ?>
echo "<main>"; <header>
} <a id="lienHeader" href="./<?= $relativePathToRoot ?>">
echo $pageContent; <?php
if ($config['centerIndex'] AND $pathParts['filename'] === "index") { if (file_exists(SITE . "/img/logo.webp"))
echo "</div>"; echo '<img src="img/logo.webp" ' . getimagesize(SITE . "/img/logo.webp")[3] . ' alt="' . $config['siteTitle'] . '" />';
} else { else
echo "</main>"; echo $config['siteTitle'];
} ?>
if (file_exists(SITE . "/end.inc.html")) </a>
require SITE . "/end.inc.html"; </header>
echo "</body></html>"; <?php
file_put_contents($pathParts['dirname'] . "/" . $pathParts['filename'] . ".html", ob_get_contents()); }
ob_end_clean();
// Gzip compression if ($config['centerIndex'] AND $pathParts['filename'] === "index")
exec(GZIP . " --keep --fast --force " . $pathParts['dirname'] . "/" . $pathParts['filename'] . ".html"); echo '<div class="centered">' . $pageContent . "</div>";
else
echo "<main>" . $pageContent . "</main>";
if (file_exists(SITE . "/end.inc.html"))
require SITE . "/end.inc.html";
echo "</body></html>";
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); exec(GZIP . " --keep --fast --force " . SITE . "/css/" . CSS_FILENAME);

View file

@ -3,85 +3,85 @@
@smallWidthScreen: ~"(max-width: 420px)"; @smallWidthScreen: ~"(max-width: 420px)";
::selection { ::selection {
@media @light { @media @light {
color: @lightColor; color: @lightColor;
background-color: @darkColor; background-color: @darkColor;
} }
@media @dark { @media @dark {
color: @darkColor; color: @darkColor;
background-color: @lightColor; background-color: @lightColor;
} }
} }
* { * {
padding: 0px; padding: 0px;
margin: 0px; margin: 0px;
@media @light { @media @light {
scrollbar-color: @darkColor @lightColor; scrollbar-color: @darkColor @lightColor;
} }
@media @dark { @media @dark {
scrollbar-color: @lightColor @darkColor; scrollbar-color: @lightColor @darkColor;
} }
} }
body { body {
font-family: system-ui, sans-serif; font-family: system-ui, sans-serif;
font-size: 30px; font-size: 30px;
line-height: 42px; line-height: 42px;
margin: 20px; margin: 20px;
@media @light { @media @light {
background-color: @lightColor; background-color: @lightColor;
color: @darkColor; color: @darkColor;
} }
@media @dark { @media @dark {
background-color: @darkColor; background-color: @darkColor;
color: @lightColor; color: @lightColor;
} }
@media @smallWidthScreen { @media @smallWidthScreen {
font-size: 26px; font-size: 26px;
line-height: 38px; line-height: 38px;
} }
} }
main { main {
margin-left: 20%; margin-left: 20%;
margin-right: 20%; margin-right: 20%;
@media (max-width: 1400px) { @media (max-width: 1400px) {
margin-left: 10%; margin-left: 10%;
margin-right: 10%; margin-right: 10%;
} }
@media (max-width: 800px) { @media (max-width: 800px) {
margin-left: 0px; margin-left: 0px;
margin-right: 0px; margin-right: 0px;
padding-left: 0px; padding-left: 0px;
padding-right: 0px; padding-right: 0px;
} }
} }
a, a:visited { a, a:visited {
text-decoration: underline; text-decoration: underline;
transition-property: color, border-color; transition-property: color, border-color;
transition-duration: 0.05s; transition-duration: 0.05s;
transition-timing-function: linear; transition-timing-function: linear;
@media @light { @media @light {
background-color: @lightColor; background-color: @lightColor;
color: @darkColor; color: @darkColor;
} }
@media @dark { @media @dark {
background-color: @darkColor; background-color: @darkColor;
color: @lightColor; color: @lightColor;
} }
&:hover, &:focus { &:hover, &:focus {
text-decoration: none; text-decoration: none;
color: @mainColor; color: @mainColor;
} }
@ -89,19 +89,19 @@ a, a:visited {
.button { .button {
border-width: 2px; border-width: 2px;
border-style: solid; border-style: solid;
padding: 8px 16px 8px 16px; padding: 8px 16px 8px 16px;
margin: 5px; margin: 5px;
display: inline-block; display: inline-block;
border-radius: 14px; border-radius: 14px;
text-decoration: none; text-decoration: none;
@media @light { @media @light {
border-color: @darkColor; border-color: @darkColor;
} }
@media @dark { @media @dark {
border-color: @lightColor; border-color: @lightColor;
} }
&:hover, &:focus { &:hover, &:focus {
color: @mainColor; color: @mainColor;
@ -111,9 +111,9 @@ a, a:visited {
} }
.smallButton { .smallButton {
.button(); .button();
font-size: 80%; font-size: 80%;
padding: 0px 16px 0px 16px; padding: 0px 16px 0px 16px;
} }
img { img {
@ -125,21 +125,21 @@ img {
display: block; display: block;
margin: auto; margin: auto;
margin-top: 20px; margin-top: 20px;
margin-bottom: 20px; margin-bottom: 20px;
} }
.border { .border {
border-width: 1px; border-width: 1px;
border-style: solid; border-style: solid;
border-radius: 5px; border-radius: 5px;
@media @light { @media @light {
border-color: @darkColor; border-color: @darkColor;
} }
@media @dark { @media @dark {
border-color: @lightColor; border-color: @lightColor;
} }
} }
strong { strong {
@ -148,14 +148,14 @@ strong {
pre, code, var, samp { pre, code, var, samp {
font-family: monospace; font-family: monospace;
overflow: auto; overflow: auto;
font-style: normal; font-style: normal;
border-radius: 15px; border-radius: 15px;
word-break: break-all; word-break: break-all;
} }
abbr[title] { abbr[title] {
text-decoration: dotted underline; text-decoration: dotted underline;
} }
address { address {
@ -181,8 +181,8 @@ p {
} }
header, footer, .centered { header, footer, .centered {
text-align: center; text-align: center;
justify-content: center; justify-content: center;
} }
footer { footer {
@ -201,21 +201,21 @@ h1 {
font-size: 70px; font-size: 70px;
text-align: center; text-align: center;
padding-top: 15px; padding-top: 15px;
margin-bottom: 20px; margin-bottom: 20px;
@media @smallWidthScreen { @media @smallWidthScreen {
font-size: 45px; font-size: 45px;
} }
} }
h2 { h2 {
font-size: 60px; font-size: 60px;
margin-top: 30px; margin-top: 30px;
margin-bottom: 15px; margin-bottom: 15px;
@media @smallWidthScreen { @media @smallWidthScreen {
font-size: 40px; font-size: 40px;
} }
} }
h3 { h3 {
@ -223,7 +223,7 @@ h3 {
margin-top: 30px; margin-top: 30px;
margin-bottom: 15px; margin-bottom: 15px;
@media @smallWidthScreen { @media @smallWidthScreen {
font-size: 35px; font-size: 35px;
} }
} }