]> git.bts.cx Git - garden.git/blobdiff - garden.php
Some small template fixes
[garden.git] / garden.php
index e4391e4b557993afb647a9c4ece95f9508ed60d9..8c0127705962891ee05cda45655bfa0539462977 100644 (file)
@@ -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,16 +522,21 @@ 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) {
                        continue;
                }
 
-               $recent_items[$item->date] = $item;
+               $date_key = $item->date;
+
+               // Hack to support sorting without missing entries
+               while (isset($recent_items[$date_key])) {
+                       $date_key += 1;
+               }
+
+               $recent_items[$date_key] = $item;
        }
 
        // Sort by date...
@@ -540,7 +547,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 +597,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 +614,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];