X-Git-Url: https://git.bts.cx/garden.git/blobdiff_plain/1ff3a665663a5095c770cc63c3d09572944b30f1..e5a40d56a7e31209bae79ffddeda5f70749f6d2f:/garden.php diff --git a/garden.php b/garden.php index e4391e4..6035ee8 100644 --- a/garden.php +++ b/garden.php @@ -260,7 +260,11 @@ function garden_template_render($name, $variables = null) { $output = ''; while ($name != null) { $path = garden_path(GARDEN_TEMPLATE_DIR, $name . '.php'); - + + if (file_exists($path) == false) { + $path = garden_path(GARDEN_FALLBACK_TEMPLATE_DIR, $name . '.php'); + } + $base_template = null; $base_template_variables = null; @@ -312,8 +316,6 @@ function garden_site_url(...$url_segments) { return GARDEN_SITE_BASE_URL . garden_url(...$url_segments); } - - /////////////////////////////////////////////////////////////////////////////// // Images /////////////////////////////////////////////////////////////////////////////// @@ -520,9 +522,7 @@ function garden_generate_html($output_directory, $content_items) { // Feed /////////////////////////////////////////////////////////////////////////////// -function garden_generate_atom($output_directory, $content_items) { - $output_items = []; - +function garden_generate_atom($output_directory, $output_filename, $content_items) { $recent_items = []; foreach ($content_items as $item) { if ($item->type != GardenItemType::Article) { @@ -540,7 +540,42 @@ function garden_generate_atom($output_directory, $content_items) { $variables['recent_items'] = $recent_items; $html_data = garden_template_render('atom', $variables); - garden_write_file(garden_path($output_directory, 'feed.atom'), $html_data); + garden_write_file(garden_path($output_directory, $output_filename), $html_data); +} + +function garden_generate_json($output_directory, $output_filename, $content_items) { + $recent_items = []; + foreach ($content_items as $item) { + if ($item->type != GardenItemType::Article) { + continue; + } + + $recent_items[$item->date] = $item; + } + + // Sort by date... + ksort($recent_items); + $recent_items = array_reverse($recent_items); + + $feed = []; + $feed['version'] = 'https://jsonfeed.org/version/1.1'; + $feed['title'] = GARDEN_TEMPLATE_CONSTANTS['site_name']; + $feed['home_page_url'] = garden_site_url('/'); + $feed['feed_url'] = garden_site_url($output_filename); + $feed['items'] = []; + + foreach ($recent_items as $item) { + $feed_item = []; + $feed_item['id'] = $item->date + hexdec(hash('crc32', $item->title)); + $feed_item['date_published'] = date(DATE_RFC3339, $item->date); + $feed_item['title'] = $item->title; + $feed_item['content_text'] = 'An update was published.'; + $feed_item['url'] = $item->url; + $feed['items'][] = $feed_item; + } + + $json_data = json_encode($feed); + garden_write_file(garden_path($output_directory, $output_filename), $json_data); } /////////////////////////////////////////////////////////////////////////////// @@ -555,7 +590,14 @@ function garden() { garden_move_raw($process_items); $post_process_items = garden_generate_html(GARDEN_OUTPUT_DIR, $process_items); garden_make_images($post_process_items); - garden_generate_atom(GARDEN_OUTPUT_DIR, $process_items); + + if (defined('GARDEN_ATOM_FEED_FILENAME')) { + garden_generate_atom(GARDEN_OUTPUT_DIR, GARDEN_ATOM_FEED_FILENAME, $process_items); + } + + if (defined('GARDEN_JSON_FEED_FILENAME')) { + garden_generate_json(GARDEN_OUTPUT_DIR, GARDEN_JSON_FEED_FILENAME, $process_items); + } } /////////////////////////////////////////////////////////////////////////////// @@ -565,6 +607,8 @@ function garden() { assert(extension_loaded('imagick'), 'Needs Imagick'); assert($argc >= 2, 'Please provide configuration file'); +define('GARDEN_FALLBACK_TEMPLATE_DIR', garden_path(__DIR__, 'templates')); + // First parameter needs to be the configuration php $config_file = $argv[1];