mkht.php/mkht.php

245 lines
6.6 KiB
PHP
Raw Normal View History

2021-10-09 20:13:55 +02:00
#!/usr/bin/php
<?php
2023-05-23 16:49:39 +02:00
if (php_sapi_name() !== 'cli')
exit('Must be run from CLI');
2021-10-09 20:13:55 +02:00
// Initialization
2023-05-23 16:49:39 +02:00
const LF = "\n";
2021-10-09 20:13:55 +02:00
2023-05-23 16:49:39 +02:00
define('ROOT', dirname($_SERVER['SCRIPT_FILENAME']));
$use_pandoc = true;
if (isset($argv[1]))
2023-05-23 16:49:39 +02:00
define('SITE', $argv[1]);
else
2023-05-23 16:49:39 +02:00
define('SITE', getcwd());
2021-10-09 20:13:55 +02:00
if (isset($argv[2]))
2023-05-23 16:49:39 +02:00
define('DESTINATION', $argv[2]);
2021-10-09 20:13:55 +02:00
else
2023-05-23 16:49:39 +02:00
define('DESTINATION', 'dns');
2021-10-09 20:13:55 +02:00
2023-05-23 16:49:39 +02:00
if (file_exists(SITE . '/config.ini'))
$config = parse_ini_file(SITE . '/config.ini');
2021-10-09 20:13:55 +02:00
if (!isset($config['css']))
2022-08-30 16:40:07 +02:00
$config['css'] = 1;
2021-10-09 20:13:55 +02:00
if (!isset($config['header']))
2023-05-23 16:49:39 +02:00
$config['header'] = false;
2021-10-09 20:13:55 +02:00
if (!isset($config['centerIndex']))
2022-06-01 17:33:32 +02:00
$config['centerIndex'] = false;
2021-10-09 20:13:55 +02:00
2021-10-12 00:03:18 +02:00
if (!isset($config['defaultLang']))
2023-05-23 16:49:39 +02:00
$config['defaultLang'] = '';
2021-10-12 00:03:18 +02:00
2021-10-09 20:13:55 +02:00
// Less > CSS
2022-08-30 16:40:07 +02:00
if ($config['css'] == true) {
// Create CSS output directory if needed
2023-05-23 16:49:39 +02:00
if (!file_exists(SITE . '/css'))
mkdir(SITE . '/css', 0755);
2022-08-30 16:40:07 +02:00
2023-05-23 16:49:39 +02:00
require ROOT . '/less.php/lib/Less/Autoloader.php';
2022-08-30 16:40:07 +02:00
Less_Autoloader::register();
2023-05-23 16:49:39 +02:00
$colorScheme = [
'darkColor' => 'black',
'lightColor' => 'white',
'mainColor' => 'red',
];
2022-08-30 16:40:07 +02:00
2023-05-23 16:49:39 +02:00
$options = [
2022-08-30 16:40:07 +02:00
'cache_dir' => SITE . '/css',
'compress' => true,
2023-05-23 16:49:39 +02:00
];
2022-08-30 16:40:07 +02:00
2023-05-23 16:49:39 +02:00
if (file_exists(SITE . '/style.less'))
$lessFiles = [ROOT . '/style.less' => '', SITE . '/style.less' => ''];
2022-08-30 16:40:07 +02:00
else
2023-05-23 16:49:39 +02:00
$lessFiles = [ROOT . '/style.less' => ''];
2021-10-09 20:13:55 +02:00
2023-05-23 16:49:39 +02:00
define('CSS_FILENAME', Less_Cache::Get($lessFiles, $options, $colorScheme));
2022-08-30 16:40:07 +02:00
}
2021-10-09 20:13:55 +02:00
// Determine whether links need to use Onion or DNS
2023-05-23 16:49:39 +02:00
function clearnetOrOnion($clearnet_url, $onion_url) {
return (DESTINATION === 'onion') ? $onion_url : $clearnet_url;
2021-10-09 20:13:55 +02:00
}
2023-05-24 18:57:21 +02:00
exec('find ' . SITE . "/src -name '*.gmi' -o -name '*.md'", $pages);
2021-10-09 20:13:55 +02:00
foreach ($pages as $page) {
2023-05-23 16:49:39 +02:00
$pathParts = pathinfo(str_replace('/src', '', $page));
2022-06-01 17:33:32 +02:00
// Create parent directory if needed
if (!file_exists($pathParts['dirname']))
mkdir($pathParts['dirname'], 0755, true);
// Execute PHP code
ob_start();
2023-05-23 16:49:39 +02:00
eval('?>' . file_get_contents($page));
file_put_contents($pathParts['dirname'] . '/' . $pathParts['basename'], ob_get_contents());
2022-06-01 17:33:32 +02:00
ob_end_clean();
// Convert Gemtext to Markdown
2023-05-23 16:49:39 +02:00
if ($pathParts['extension'] === 'gmi') {
$gmilines = explode(LF, file_get_contents($pathParts['dirname'] . '/' . $pathParts['basename']));
2022-06-01 17:33:32 +02:00
foreach ($gmilines as $key => $line) {
2023-05-23 16:49:39 +02:00
if (substr($line, 0, 2) === '=>') {
preg_match('/=> +(.[^ ]+)/', $line, $lnUrl);
preg_match('/=> +.[^ ]+ +(.+)/', $line, $lnTitle);
2022-06-01 17:33:32 +02:00
$urlPathParts = pathinfo(parse_url($lnUrl[1], PHP_URL_PATH));
// .gmi > .md for local links
2023-05-23 16:49:39 +02:00
if (!str_contains($lnUrl[1], ':') AND $urlPathParts['extension'] === 'gmi') // If it's a local link
$lnUrl[1] = $urlPathParts['dirname'] . '/' . $urlPathParts['filename'] . '.md';
2022-06-01 17:33:32 +02:00
if (isset($lnTitle[1])) {
2023-05-23 16:49:39 +02:00
$gmilines[$key] = '[' . $lnTitle[1] . '](' . $lnUrl[1] . ')';
2022-06-01 17:33:32 +02:00
} else {
2023-05-23 16:49:39 +02:00
$gmilines[$key] = '[' . $lnUrl[1] . '](' . $lnUrl[1] . ')';
2022-06-01 17:33:32 +02:00
}
}
}
2023-05-23 16:49:39 +02:00
$code = '';
2022-06-01 17:33:32 +02:00
foreach ($gmilines as $line) {
2023-05-23 16:49:39 +02:00
$code = $code . LF . $line;
2022-06-01 17:33:32 +02:00
}
2023-05-23 16:49:39 +02:00
file_put_contents($pathParts['dirname'] . '/' . $pathParts['filename'] . '.md', $code);
2022-06-01 17:33:32 +02:00
}
// Compile Markdown to HTML
2023-05-23 16:49:39 +02:00
$markdown = file_get_contents($pathParts['dirname'] . '/' . $pathParts['filename'] . '.md');
2022-06-01 17:33:32 +02:00
if (preg_match("/# (.*)\\n/", $markdown, $matches)) // If a main heading is found
$title = $matches[1]; // Then it will be the HTML page <title>
else
$title = NULL;
if ($use_pandoc) {
$process = proc_open('pandoc --fail-if-warnings -f markdown -t html', [
0 => ['pipe', 'r'],
1 => ['pipe', 'w'],
], $pipes);
if (is_resource($process) !== true)
exit('Can\'t spawn pandoc.');
fwrite($pipes[0], $markdown);
fclose($pipes[0]);
$pageContent = fread($pipes[1], 1000);
fclose($pipes[1]);
if (proc_close($process) !== 0)
exit('pandoc failed.');
} else {
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);
}
2022-06-01 17:33:32 +02:00
// .md > .html for local links
$pageContent = preg_replace('#<a href="(?!.*:)(.*)\.md">#', '<a href="$1.html">', $pageContent);
// Add header and footer to HTML
2023-05-23 16:49:39 +02:00
$urlPath = str_replace(SITE, '', $pathParts['dirname']);
$relativePathToRoot = '';
for ($i = substr_count($urlPath, '/') ; $i > 0 ; $i--)
$relativePathToRoot .= '../';
2022-06-01 17:33:32 +02:00
ob_start();
?>
<!DOCTYPE html>
<html lang="<?php
2023-05-23 16:49:39 +02:00
preg_match('#\.([a-zA-Z-]{2,5})\.#', $pathParts['basename'], $lang);
2022-06-01 17:33:32 +02:00
if (isset($lang[1])) {
echo $lang[1];
} else {
2023-05-23 16:49:39 +02:00
preg_match('#/([a-z]{2})(/|$)#', $pathParts['dirname'], $lang);
2022-06-01 17:33:32 +02:00
if (isset($lang[1]))
echo $lang[1];
else
echo $config['defaultLang'];
}
?>">
<head>
2023-05-23 16:49:39 +02:00
<meta charset="utf-8">
2022-06-01 17:33:32 +02:00
<?php
if (isset($title) AND !is_null($title) AND isset($config['siteTitle']))
2023-05-23 16:49:39 +02:00
echo '<title>' . $title . ' · ' . $config['siteTitle'] . '</title>';
2022-06-01 17:33:32 +02:00
else if (isset($title) AND !is_null($title))
2023-05-23 16:49:39 +02:00
echo '<title>' . $title . '</title>';
2022-06-01 17:33:32 +02:00
else if (isset($config['siteTitle']))
2023-05-23 16:49:39 +02:00
echo '<title>' . $config['siteTitle'] . '</title>';
2022-06-01 17:33:32 +02:00
?>
<meta name="viewport" content="width=device-width, initial-scale=1">
<?php
2023-05-23 16:49:39 +02:00
if ($config['css'] == true)
echo '<link rel="stylesheet" media="screen" href="' . $relativePathToRoot . 'css/' . CSS_FILENAME . '">' . LF;
2022-06-01 17:33:32 +02:00
2023-05-23 16:49:39 +02:00
if (file_exists(SITE . '/head.inc.html'))
echo file_get_contents(SITE . '/head.inc.html');
2022-06-01 17:33:32 +02:00
?>
</head>
<body>
<?php
if ($config['header']) {
?>
<header>
2023-05-23 16:49:39 +02:00
<a href="./<?= $relativePathToRoot ?>">
2022-06-01 17:33:32 +02:00
<?php
2023-05-23 16:49:39 +02:00
if (file_exists(SITE . '/img/logo.webp'))
echo '<img src="img/logo.webp" ' . getimagesize(SITE . '/img/logo.webp')[3] . ' alt="' . $config['siteTitle'] . '" />';
2022-06-01 17:33:32 +02:00
else
echo $config['siteTitle'];
?>
</a>
</header>
<?php
}
2023-05-23 16:49:39 +02:00
if ($config['centerIndex'] AND $pathParts['filename'] === 'index')
echo '<div class="centered">' . $pageContent . '</div>';
2022-06-01 17:33:32 +02:00
else
2023-05-23 16:49:39 +02:00
echo '<main>' . $pageContent . '</main>';
if (file_exists(SITE . '/end.inc.html'))
require SITE . '/end.inc.html';
echo '</body></html>';
2022-06-01 17:33:32 +02:00
2022-06-01 19:36:07 +02:00
$pageContent = ob_get_clean();
2023-05-23 16:49:39 +02:00
if (extension_loaded('tidy')) {
2022-06-01 19:36:07 +02:00
$tidy = new tidy;
2023-05-24 18:57:21 +02:00
$tidy->parseString($pageContent, [
2022-06-01 19:36:07 +02:00
'indent' => true,
'keep-tabs' => true,
'wrap' => 0
2023-05-23 16:49:39 +02:00
]
2022-06-01 19:36:07 +02:00
);
$tidy->cleanRepair();
$pageContent = tidy_get_output($tidy);
} else {
2023-05-23 16:49:39 +02:00
echo 'tidy extension unavailable' . PHP_EOL;
2022-06-01 19:36:07 +02:00
}
2023-05-23 16:49:39 +02:00
file_put_contents($pathParts['dirname'] . '/' . $pathParts['filename'] . '.html', $pageContent);
2022-06-01 17:33:32 +02:00
// Gzip compression
2023-05-24 18:57:21 +02:00
exec('gzip --keep --fast --force ' . $pathParts['dirname'] . '/' . $pathParts['filename'] . '.html');
}
ob_start();
2021-10-09 20:13:55 +02:00
}
2022-08-30 16:40:07 +02:00
if ($config['css'] == true)
2023-05-24 18:57:21 +02:00
exec('gzip --keep --fast --force ' . SITE . '/css/' . CSS_FILENAME);