diff --git a/README.md b/README.md index d23196e..5f6be74 100755 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ If `site_path` is not set, it will default to current directory. Source pages must end in `.md` and can use Markdown, HTML and PHP. -The following files have special meaning +The following files have special meaning: `/config.ini` : some default settings can be changed by this file @@ -38,7 +38,7 @@ The following files have special meaning `/end.inc.html` : added just before `` -Files starting with a dot or not ending in `.gmi`, `.md` or `.html` are ignored. +Files starting with a dot (except for `.htaccess`) are ignored. Files containing `draft` in their name (separated from other characters by `.`) are ignored. diff --git a/mkht.php b/mkht.php index b218913..ee18a6e 100755 --- a/mkht.php +++ b/mkht.php @@ -2,7 +2,7 @@ getType() !== 'file') continue; - if (!file_exists($path_parts['dirname'])) // Create parent directory if needed - mkdir($path_parts['dirname'], 0755, true); + if (in_array('draft', explode('.', $path_parts_target['basename']), true)) + continue; + if (!file_exists($path_parts_target['dirname'])) // Create parent directory if needed + mkdir($path_parts_target['dirname'], 0755, true); copy($src, $target); if ($node_info->getExtension() !== 'md') continue; - $files_dates[$src] = $node_info->getMTime(); - - if (in_array('draft', explode('.', $path_parts['basename']), true)) - continue; - - $base_filepath = $path_parts['dirname'] . '/' . $path_parts['filename']; + $base_filepath_src = $path_parts_src['dirname'] . '/' . $path_parts_src['filename']; + $base_filepath_target = $path_parts_target['dirname'] . '/' . $path_parts_target['filename']; $content = file_get_contents($src); preg_match('/^# (?.*)$/Dm', $content, $matches); $title = $matches['title'] ?? NULL; - if (!file_exists($base_filepath . '.html') OR (filemtime($src) > filemtime($base_filepath . '.html')) OR $opt['force']) { + preg_match('#\.(?<lang>[a-z]{2})\.md#', $path_parts_src['basename'], $matches); + $lang = $matches['lang'] ?? $config['default-lang'] ?? exit('no language found for ' . $src . LF); + + if (!file_exists($base_filepath_target . '.html') OR (filemtime($src) > filemtime($base_filepath_target . '.html')) OR $opt['force']) { echo 'Compiling ' . $src . ' ' . date("Y-m-d H:i:s", $node_info->getMTime()) . LF; // Execute PHP code @@ -91,7 +94,7 @@ foreach($nodes as $node) { $content = ob_get_clean(); // Convert Gemtext to Markdown - if ($path_parts['extension'] === 'gmi' OR $path_parts['extension'] === 'md') { + if ($path_parts_target['extension'] === 'gmi' OR $path_parts_target['extension'] === 'md') { $content = preg_replace_callback( '/^=>\h*(?<addr>\S+)(:?\h+(?<title>\V+))?$/m', function ($matches) { @@ -120,60 +123,49 @@ foreach($nodes as $node) { // .md > .html for local links $content = preg_replace('/ href="([^:"]+)\.md"/', ' href="$1.html"', $content); - $relative_root_path = str_repeat('../', substr_count(str_replace(SITE . '/target', '', $path_parts['dirname']), '/')); + $relative_root_path = str_repeat('../', substr_count(str_replace(SITE . '/target', '', $path_parts_target['dirname']), '/')); ob_start(); - ?> - <!DOCTYPE html> - <html lang="<?php - - preg_match('#\.([a-zA-Z-]{2,5})\.#', $path_parts['basename'], $file_lang); - if (isset($file_lang[1])) { - $lang = $file_lang[1]; - } else { - preg_match('#/([a-z]{2})(/|$)#', $path_parts['dirname'], $dir_lang); - $lang = $dir_lang[1] ?? $config['default-lang']; - } - echo $lang ?? ''; - - ?>"> - <head> - <meta charset="utf-8"> - <?php +?> +<!DOCTYPE html> +<html lang="<?= $lang ?>"> + <head> + <meta charset="utf-8"> +<?php if (isset($title) AND isset($config['title'])) echo '<title>' . $title . ' ยท ' . $config['title'] . ''; else if (isset($title)) echo '' . $title . ''; else if (isset($config['title'])) echo '' . $config['title'] . ''; - ?> - - +?> + + '; + echo ' '; if ($config['announce-feed']) - echo '' . LF; + echo ' ' . LF; if ($config['announce-css']) { if (file_exists(SITE . '/style.css')) - echo '' . LF; - echo '' . LF; + echo ' ' . LF; + echo ' ' . LF; } if (file_exists(SITE . '/head.inc.html')) echo file_get_contents(SITE . '/head.inc.html'); ?> - + - + ' . file_get_contents(SITE . '/header.inc.php')); - if ($config['center-index'] AND $path_parts['filename'] === 'index') + if ($config['center-index'] AND $path_parts_target['filename'] === 'index') echo '
' . $content . '
'; else echo '
' . $content . '
'; @@ -193,26 +185,26 @@ foreach($nodes as $node) { $content = str_replace(' ', ' ', $content); } - file_put_contents($base_filepath . '.html', $content); + file_put_contents($base_filepath_target . '.html', $content); // Gzip compression - exec('gzip --keep --fast --force ' . $base_filepath . '.html'); + exec('gzip --keep --fast --force ' . $base_filepath_target . '.html'); } - $relative_addr = substr_replace($base_filepath . '.html', '', strpos($base_filepath, SITE), strlen(SITE)); + $relative_addr = substr_replace($base_filepath_src . '.html', '', strpos($base_filepath_src, SITE), strlen(SITE)); // As of RFC 3151: A URN Namespace for Public Identifiers $public_id = 'urn:publicid:' . $config['id'] . str_replace('/', '%2F', $relative_addr); - preg_match('#\(?.*)\#s', file_get_contents($base_filepath . '.html'), $match); + preg_match('#\(?.*)\#s', file_get_contents($base_filepath_target . '.html'), $match); $atom_entry_content = $match['content']; // Make relative links absolute $atom_entry_content = preg_replace_callback('# href=\"(?[^:"]+)\"#', function ($matches) { global $config; - global $path_parts; + global $path_parts_target; - return ' href="' . ($config['base-url'][0] ?? '') . substr($path_parts['dirname'], strlen(SITE)) . '/' . $matches['relative_url'] . '"'; + return ' href="' . ($config['base-url'][0] ?? '') . substr($path_parts_target['dirname'], strlen(SITE)) . '/' . $matches['relative_url'] . '"'; }, $atom_entry_content); ob_start(); @@ -228,27 +220,45 @@ foreach($nodes as $node) { $unused) { + if ($level === 0) + continue; + + $dir = implode('/', array_slice(explode('/', $relative_addr), 0, $level)) . '/'; + $last_update = $node_info->getMTime(); + + $dirs_entries_per_lang['all'][$dir] = ($dirs_entries_per_lang['all'][$dir] ?? '') . $atom_entry; + $dirs_entries_per_lang[$lang][$dir] = ($dirs_entries_per_lang[$lang][$dir] ?? '') . $atom_entry; + + if (!isset($dirs_last_update['all'][$dir]) OR $last_update > $dirs_last_update['all'][$dir]) + $dirs_last_update['all'][$dir] = $last_update; + if (!isset($dirs_last_update[$lang][$dir]) OR $last_update > $dirs_last_update[$lang][$dir]) + $dirs_last_update[$lang][$dir] = $last_update; + } } -asort($files_dates); - -ob_start(); +foreach ($dirs_entries_per_lang as $lang => $dirs_entries) { + foreach ($dirs_entries as $dir => $entries) { + ob_start(); ?> - <?= $config['title'] ?? '' ?> - urn:publicid: + <?= ($config['title'] ?? '') . (($dir !== '/') ? ' ' . $dir : '') ?> + urn:publicid: ' . LF; + foreach ($config['base-url'] as $url) + echo ' ' . LF; ?> - + ' . LF); + file_put_contents(SITE . '/target' . $dir . 'feed.' . $lang . '.atom', ob_get_clean() . $entries . '' . LF); + } +} if ($config['announce-css']) copy(ROOT . '/style.css', SITE . '/target/mkht-php.css');