]> git.bts.cx Git - garden.git/commitdiff
Added category support
authorBen Sherratt <redacted>
Sat, 27 Dec 2025 11:15:55 +0000 (11:15 +0000)
committerBen Sherratt <redacted>
Sat, 27 Dec 2025 11:16:42 +0000 (11:16 +0000)
garden/garden.php
templates/base.php

index ada43bb5290854cd9bc6ebc8dc0a38fc9131d5cd..210ab58bc5242cba6694805271da16afd1cf7448 100644 (file)
@@ -92,16 +92,18 @@ class GardenItem {
        public $type;
        public $id;
        public $title;
+       public $category;
        public $url;
        public $date;
        public $source_filename;
        public $target_filename;
 
-       public function __construct($path, $type, $id, $title, $url, $date, $source_filename, $target_filename) {
+       public function __construct($path, $type, $id, $title, $category, $url, $date, $source_filename, $target_filename) {
                $this->path = $path;
                $this->type = $type;
                $this->id = $id;
                $this->title = $title;
+               $this->category = $category;
                $this->url = $url;
                $this->date = $date;
                $this->source_filename = $source_filename;
@@ -146,12 +148,15 @@ function garden_make_process_items($output_directory, $content_paths) {
                        $target_basename .= '.' . $target_extension;
                }
 
+               $category_components = explode(DIRECTORY_SEPARATOR, $item->path);
+               $category = count($category_components) >= 2 ? $category_components[1] : "";
+
                $url = GARDEN_SITE_BASE_URL . garden_url($item->path, $target_basename);
                $target_path = garden_path($output_directory, garden_url($item->path, $target_basename));
 
                $date = filemtime($item->full_path);
 
-               $output_items[] = new GardenItem($item, $type, $id, $item->filename, $url, $date, $item->full_path, $target_path);
+               $output_items[] = new GardenItem($item, $type, $id, $item->filename, $category, $url, $date, $item->full_path, $target_path);
        }
 
        return $output_items;
@@ -467,6 +472,18 @@ class GardenExtendedParsedown extends Parsedown {
 function garden_generate_html($output_directory, $content_items) {
        $output_items = [];
 
+       $categorised_items = [];
+       foreach ($content_items as $item) {
+               if ($item->type != GardenItemType::Article) {
+                       continue;
+               }
+
+               if (isset($categorised_items[$item->category]) == false) {
+                       $categorised_items[$item->category] = [];
+               }
+               $categorised_items[$item->category][] = $item;
+       }
+
        foreach ($content_items as $item) {
                if ($item->type != GardenItemType::Article) {
                        continue;
@@ -481,7 +498,7 @@ function garden_generate_html($output_directory, $content_items) {
                        array_push($output_items, ...$parsedown->post_process_items);
                }
 
-               $html_data = garden_template_render('article', [ 'article' => $item, 'article_content' => $markdown_html ]);
+               $html_data = garden_template_render('article', [ 'article' => $item, 'article_content' => $markdown_html, 'categorised_items' => $categorised_items ]);
 
                garden_write_file($item->target_filename, $html_data);
        }
index 95cd6253f6311f6591809313f3541b493062bb29..0a72302ba3ee5bbcedc9243e308c8f349dd783a0 100644 (file)
                        <p>Find me <a href="https://mastodon.gamedev.place/@btsherratt" rel="me">@btsherratt</a> or on <a href="https://bts.itch.io/">itch.io</a></p>
                </header>
 
+               <nav>
+                       <ul>
+                       <?php foreach($categorised_items as $category_name => $category_items): ?>
+                               <?php if ($category_name != ''): ?>
+                               <li>
+                                       <?= $category_name ?>
+                                       <ul>
+                                               <?php foreach($category_items as $category_item): ?>
+                                                       <li><a href="<?= $category_item->url ?>"><?= $category_item->title ?></a></li>
+                                               <?php endforeach; ?>
+                                       </ul>
+                               </li>
+                               <?php else: ?>
+                                       <?php foreach($category_items as $category_item): ?>
+                                               <li><a href="<?= $category_item->url ?>"><?= $category_item->title ?></a></li>
+                                       <?php endforeach; ?>
+                               <?php endif ?>
+                       <?php endforeach ?>
+                       </ul>
+               </nav>
+
                <?= garden_template_content(); ?>
 
                <footer>