]> git.bts.cx Git - garden.git/blobdiff - garden/garden.php
Added category support
[garden.git] / garden / garden.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);
        }