diff --git a/mkht.php b/mkht.php index 4aa85d1..bc09bdd 100755 --- a/mkht.php +++ b/mkht.php @@ -1,82 +1,80 @@ #!/usr/bin/php CSS if ($config['css'] == true) { // Create CSS output directory if needed - if (!file_exists(SITE . "/css")) - mkdir(SITE . "/css", 0755); + if (!file_exists(SITE . '/css')) + mkdir(SITE . '/css', 0755); - require ROOT . "/less.php/lib/Less/Autoloader.php"; + require ROOT . '/less.php/lib/Less/Autoloader.php'; Less_Autoloader::register(); - $colorScheme = array( - "darkColor" => "black", - "lightColor" => "white", - "mainColor" => "red", - ); + $colorScheme = [ + 'darkColor' => 'black', + 'lightColor' => 'white', + 'mainColor' => 'red', + ]; - $options = array( + $options = [ 'cache_dir' => SITE . '/css', 'compress' => true, - ); + ]; - if (file_exists(SITE . "/style.less")) - $lessFiles = array(ROOT . '/style.less' => '', SITE . '/style.less' => ''); + if (file_exists(SITE . '/style.less')) + $lessFiles = [ROOT . '/style.less' => '', SITE . '/style.less' => '']; else - $lessFiles = array(ROOT . '/style.less' => ''); + $lessFiles = [ROOT . '/style.less' => '']; - define("CSS_FILENAME", Less_Cache::Get($lessFiles, $options, $colorScheme)); + define('CSS_FILENAME', Less_Cache::Get($lessFiles, $options, $colorScheme)); } // Determine whether links need to use Onion or DNS -function clearnetOrOnion($clearnetUrl, $onionUrl) { - if (DESTINATION === "onion") { - return $onionUrl; - } else { - return $clearnetUrl; - } +function clearnetOrOnion($clearnet_url, $onion_url) { + return (DESTINATION === 'onion') ? $onion_url : $clearnet_url; } -exec(FIND . " " . SITE . "/src -name '*.gmi' -o -name '*.md'", $pages); +exec(FIND . ' ' . SITE . "/src -name '*.gmi' -o -name '*.md'", $pages); foreach ($pages as $page) { - $pathParts = pathinfo(str_replace("/src", "", $page)); + $pathParts = pathinfo(str_replace('/src', '', $page)); // Create parent directory if needed if (!file_exists($pathParts['dirname'])) @@ -84,65 +82,61 @@ foreach ($pages as $page) { // Execute PHP code ob_start(); - eval("?>" . file_get_contents($page)); - file_put_contents($pathParts['dirname'] . "/" . $pathParts['basename'], ob_get_contents()); + eval('?>' . file_get_contents($page)); + file_put_contents($pathParts['dirname'] . '/' . $pathParts['basename'], ob_get_contents()); ob_end_clean(); // Convert Gemtext to Markdown - if ($pathParts['extension'] === "gmi") { - $gmilines = explode("\n", file_get_contents($pathParts['dirname'] . "/" . $pathParts['basename'])); + if ($pathParts['extension'] === 'gmi') { + $gmilines = explode(LF, file_get_contents($pathParts['dirname'] . '/' . $pathParts['basename'])); foreach ($gmilines as $key => $line) { - if (substr($line, 0, 2) === "=>") { - preg_match("/=> +(.[^ ]+)/", $line, $lnUrl); - preg_match("/=> +.[^ ]+ +(.+)/", $line, $lnTitle); + if (substr($line, 0, 2) === '=>') { + preg_match('/=> +(.[^ ]+)/', $line, $lnUrl); + preg_match('/=> +.[^ ]+ +(.+)/', $line, $lnTitle); $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 (!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])) { - $gmilines[$key] = "[" . $lnTitle[1] . "](" . $lnUrl[1] . ")"; + $gmilines[$key] = '[' . $lnTitle[1] . '](' . $lnUrl[1] . ')'; } else { - $gmilines[$key] = "[" . $lnUrl[1] . "](" . $lnUrl[1] . ")"; + $gmilines[$key] = '[' . $lnUrl[1] . '](' . $lnUrl[1] . ')'; } } } - $code = ""; + $code = ''; foreach ($gmilines as $line) { - $code = $code . "\n" . $line; + $code = $code . LF . $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 - $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 $title = $matches[1]; // Then it will be the HTML page else $title = NULL; - require_once ROOT . "/parsedown/Parsedown.php"; - require_once ROOT . "/parsedown-extra/ParsedownExtra.php"; + require_once ROOT . '/parsedown/Parsedown.php'; + require_once ROOT . '/parsedown-extra/ParsedownExtra.php'; $Parsedown = new ParsedownExtra; $Parsedown = $Parsedown->setUrlsLinked(false); $Parsedown = $Parsedown->setMarkupEscaped(false); $Parsedown = $Parsedown->setBreaksEnabled(true); $pageContent = $Parsedown->text($markdown); - // .md > .html for local links $pageContent = preg_replace('#<a href="(?!.*:)(.*)\.md">#', '<a href="$1.html">', $pageContent); - // Add header and footer to HTML - - $urlPath = str_replace(SITE, "", $pathParts['dirname']); - $relativePathToRoot = ""; - for ($i = substr_count($urlPath, "/") ; $i > 0 ; $i--) - $relativePathToRoot .= "../"; + $urlPath = str_replace(SITE, '', $pathParts['dirname']); + $relativePathToRoot = ''; + for ($i = substr_count($urlPath, '/') ; $i > 0 ; $i--) + $relativePathToRoot .= '../'; ob_start(); @@ -150,11 +144,11 @@ foreach ($pages as $page) { <!DOCTYPE html> <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])) { echo $lang[1]; } else { - preg_match("#/([a-z]{2})(/|$)#", $pathParts['dirname'], $lang); + preg_match('#/([a-z]{2})(/|$)#', $pathParts['dirname'], $lang); if (isset($lang[1])) echo $lang[1]; else @@ -163,25 +157,22 @@ foreach ($pages as $page) { ?>"> <head> - <meta charset="UTF-8"> + <meta charset="utf-8"> <?php if (isset($title) AND !is_null($title) AND isset($config['siteTitle'])) - echo "<title>" . $title . " · " . $config['siteTitle'] . ""; + echo '' . $title . ' · ' . $config['siteTitle'] . ''; else if (isset($title) AND !is_null($title)) - echo "" . $title . ""; + echo '' . $title . ''; else if (isset($config['siteTitle'])) - echo "" . $config['siteTitle'] . ""; + echo '' . $config['siteTitle'] . ''; ?> - -' . LF; - if (file_exists(SITE . "/head.inc.html")) - echo file_get_contents(SITE . "/head.inc.html"); + if (file_exists(SITE . '/head.inc.html')) + echo file_get_contents(SITE . '/head.inc.html'); ?> @@ -190,10 +181,10 @@ foreach ($pages as $page) { if ($config['header']) { ?>
- + '; + if (file_exists(SITE . '/img/logo.webp')) + echo '' . $config['siteTitle'] . ''; else echo $config['siteTitle']; ?> @@ -202,37 +193,37 @@ foreach ($pages as $page) { ' . $pageContent . ""; + if ($config['centerIndex'] AND $pathParts['filename'] === 'index') + echo '
' . $pageContent . '
'; else - echo "
" . $pageContent . "
"; - if (file_exists(SITE . "/end.inc.html")) - require SITE . "/end.inc.html"; - echo ""; + echo '
' . $pageContent . '
'; + if (file_exists(SITE . '/end.inc.html')) + require SITE . '/end.inc.html'; + echo ''; $pageContent = ob_get_clean(); - if (extension_loaded("tidy")) { + if (extension_loaded('tidy')) { $tidy = new tidy; $tidy->parseString( $pageContent, - array( + [ 'indent' => true, 'keep-tabs' => true, 'wrap' => 0 - ) + ] ); $tidy->cleanRepair(); $pageContent = tidy_get_output($tidy); } else { - echo "tidy extension unavailable\n"; + echo 'tidy extension unavailable' . PHP_EOL; } - file_put_contents($pathParts['dirname'] . "/" . $pathParts['filename'] . ".html", $pageContent); + file_put_contents($pathParts['dirname'] . '/' . $pathParts['filename'] . '.html', $pageContent); // Gzip compression - exec(GZIP . " --keep --fast --force " . $pathParts['dirname'] . "/" . $pathParts['filename'] . ".html"); + exec(GZIP . ' --keep --fast --force ' . $pathParts['dirname'] . '/' . $pathParts['filename'] . '.html'); } if ($config['css'] == true) - exec(GZIP . " --keep --fast --force " . SITE . "/css/" . CSS_FILENAME); + exec(GZIP . ' --keep --fast --force ' . SITE . '/css/' . CSS_FILENAME);