Flow transformations as $content

This commit is contained in:
Miraty 2023-05-31 03:11:36 +02:00
parent d69392cd1a
commit 350b74c24e
1 changed files with 17 additions and 18 deletions

View File

@ -84,9 +84,9 @@ $feed = ob_get_clean();
foreach ($files_dates as $src_page => $last_mod) {
$dest_page = str_replace('/src/', '/', $src_page);
$page_content = file_get_contents($src_page);
$content = file_get_contents($src_page);
preg_match('/^# ?(?<title>.*)$/Dm', $page_content, $matches);
preg_match('/^# ?(?<title>.*)$/Dm', $content, $matches);
$title = $matches['title'] ?? NULL;
$path_parts = pathinfo($dest_page);
@ -102,13 +102,13 @@ foreach ($files_dates as $src_page => $last_mod) {
// Execute PHP code
ob_start();
eval('?>' . $page_content);
file_put_contents($base_filepath . '.gmi', ob_get_contents());
ob_end_clean();
eval('?>' . $content);
$content = ob_get_clean();
file_put_contents($base_filepath . '.gmi', $content);
// Convert Gemtext to Markdown
if ($path_parts['extension'] === 'gmi') {
$gmilines = explode(LF, file_get_contents($base_filepath . '.gmi'));
$gmilines = explode(LF, $content);
foreach ($gmilines as $key => $line) {
if (str_starts_with($line, '=>')) {
@ -124,26 +124,26 @@ foreach ($files_dates as $src_page => $last_mod) {
$gmilines[$key] = '[' . ($lnTitle[1] ?? $lnUrl[1]) . '](' . $lnUrl[1] . ')';
}
}
file_put_contents($base_filepath . '.md', implode(LF, $gmilines));
$content = implode(LF, $gmilines);
file_put_contents($base_filepath . '.md', $content);
}
// Compile Markdown to HTML
$markdown = file_get_contents($base_filepath . '.md');
$process = proc_open('pandoc --fail-if-warnings -f markdown_phpextra-citations-native_divs-native_spans+abbreviations+hard_line_breaks+lists_without_preceding_blankline -t html --wrap none', [
0 => ['pipe', 'r'],
1 => ['pipe', 'w'],
], $pipes);
if (is_resource($process) !== true)
exit('Can\'t spawn pandoc.' . PHP_EOL);
fwrite($pipes[0], $markdown);
fwrite($pipes[0], $content);
fclose($pipes[0]);
$pageContent = stream_get_contents($pipes[1]);
$content = stream_get_contents($pipes[1]);
fclose($pipes[1]);
if (proc_close($process) !== 0)
exit('pandoc failed.' . PHP_EOL);
// .md > .html for local links
$pageContent = preg_replace('#<a href="(?!.*:)(.*)\.md">#', '<a href="$1.html">', $pageContent);
$content = preg_replace('#<a href="(?!.*:)(.*)\.md">#', '<a href="$1.html">', $content);
$relativePathToRoot = '';
for ($i = substr_count(str_replace(SITE, '', $path_parts['dirname']), '/') ; $i > 0 ; $i--)
@ -190,7 +190,6 @@ foreach ($files_dates as $src_page => $last_mod) {
echo '<link rel="stylesheet" media="screen" href="' . $relativePathToRoot . 'mkht-php.css">' . LF;
}
if (file_exists(SITE . '/head.inc.html'))
echo file_get_contents(SITE . '/head.inc.html');
?>
@ -214,26 +213,26 @@ foreach ($files_dates as $src_page => $last_mod) {
}
if ($config['center-index'] AND $path_parts['filename'] === 'index')
echo '<div class="centered">' . $pageContent . '</div>';
echo '<div class="centered">' . $content . '</div>';
else
echo '<main>' . $pageContent . '</main>';
echo '<main>' . $content . '</main>';
if (file_exists(SITE . '/end.inc.html'))
require SITE . '/end.inc.html';
echo '</body></html>';
$pageContent = ob_get_clean();
$content = ob_get_clean();
if (extension_loaded('tidy')) {
$pageContent = tidy_repair_string($pageContent, [
$content = tidy_repair_string($content, [
'indent' => true,
'indent-spaces' => 4,
'output-xhtml' => true,
'wrap' => 0,
]);
$pageContent = str_replace(' ', ' ', $pageContent);
$content = str_replace(' ', ' ', $content);
}
file_put_contents($base_filepath . '.html', $pageContent);
file_put_contents($base_filepath . '.html', $content);
// Gzip compression
exec('gzip --keep --fast --force ' . $base_filepath . '.html');