X-Git-Url: https://git.bts.cx/garden.git/blobdiff_plain/9b85fe10f66386c9af92a9a2d2bd79838ff8f883..cfc60d8f609562a48f36be4cadfb1e238a434500:/garden/garden.php diff --git a/garden/garden.php b/garden/garden.php index e73804a..210ab58 100644 --- a/garden/garden.php +++ b/garden/garden.php @@ -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; @@ -220,11 +225,13 @@ function garden_make_directories($content_items) { mkdir($directory , 0777, true); // FIXME, permissions... } } + + return $content_items; } function garden_move_raw($content_items) { foreach ($content_items as $item) { - if ($item->type != GardenItemType::Raw && $item->type != GardenItemType::Image) { + if ($item->type != GardenItemType::Raw && $item->type != GardenItemType::Image) { // FIXME, do we need to copy images? continue; } @@ -233,6 +240,8 @@ function garden_move_raw($content_items) { error('Failed to copy file: filename="', $item->source_filename, '"'); } } + + return $content_items; } /////////////////////////////////////////////////////////////////////////////// @@ -281,6 +290,34 @@ function garden_template_render($name, $variables = null) { return $output; } +/////////////////////////////////////////////////////////////////////////////// +// Images +/////////////////////////////////////////////////////////////////////////////// + +class GardenImage { + public $content_item; + public $width; + public $height; + public $url; + public $target_filename; + + public function __construct($content_item, $width, $height, $url, $target_filename) { + $this->content_item = $content_item; + $this->width = $width; + $this->height = $height; + $this->url = $url; + $this->target_filename = $target_filename; + } +} + +function garden_make_images($post_process_items) { + foreach ($post_process_items as $item) { + $image = new Imagick($item->content_item->source_filename); + $image->thumbnailImage($item->width, $item->height); + $image->writeImage($item->target_filename); + } +} + /////////////////////////////////////////////////////////////////////////////// // Template helpers /////////////////////////////////////////////////////////////////////////////// @@ -295,6 +332,10 @@ function garden_template_content() { return $garden_template_content(); } +function garden_site_url(...$url_segments) { + return GARDEN_SITE_BASE_URL . garden_url(...$url_segments); +} + /////////////////////////////////////////////////////////////////////////////// // HTML /////////////////////////////////////////////////////////////////////////////// @@ -302,9 +343,13 @@ function garden_template_content() { require_once(garden_path(__DIR__, 'third_party', 'parsedown', 'Parsedown.php')); class GardenExtendedParsedown extends Parsedown { + public $post_process_items; + private $output_directory; private $content_items; - public function __construct($content_items) { + public function __construct($output_directory, $content_items) { + $this->post_process_items = []; + $this->output_directory = $output_directory; $this->content_items = $content_items; $this->InlineTypes['!'][] = 'Youtube'; $this->InlineTypes['!'][] = 'Image'; @@ -323,18 +368,31 @@ class GardenExtendedParsedown extends Parsedown { return; } - $target_url = null; + $image_content_item = null; foreach ($this->content_items as $content_item) { if ($content_item->path->basename == $image_name) { - $target_url = $content_item->url; + $image_content_item = $content_item; break; } } - if ($target_url == null) { + if ($image_content_item == null) { return; } + $target_url = $image_content_item->url; + if ($image_width != null && $image_height != null) { + $original_path = $image_content_item->path; + + $target_basename = $original_path->filename . '_' . $image_width . 'x' . $image_height . '.png'; + + $target_url = GARDEN_SITE_BASE_URL . garden_url($original_path->path, $target_basename); + $target_path = garden_path($this->output_directory, garden_url($original_path->path, $target_basename)); + + $post_process_item = new GardenImage($image_content_item, $image_width, $image_height, $target_url, $target_path); + $this->post_process_items[] = $post_process_item; + } + return array( 'extent' => strlen($matches[0]), 'element' => array( @@ -411,8 +469,20 @@ class GardenExtendedParsedown extends Parsedown { } } -function garden_generate_html($content_items) { - $parsedown = new GardenExtendedParsedown($content_items); +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) { @@ -420,12 +490,20 @@ function garden_generate_html($content_items) { } $markdown_data = garden_read_file($item->source_filename); + + $parsedown = new GardenExtendedParsedown($output_directory, $content_items); $markdown_html = $parsedown->text($markdown_data); - - $html_data = garden_template_render('article', [ 'article' => $item, 'article_content' => $markdown_html ]); + + if (count($parsedown->post_process_items) > 0) { + array_push($output_items, ...$parsedown->post_process_items); + } + + $html_data = garden_template_render('article', [ 'article' => $item, 'article_content' => $markdown_html, 'categorised_items' => $categorised_items ]); garden_write_file($item->target_filename, $html_data); } + + return $output_items; } /////////////////////////////////////////////////////////////////////////////// @@ -438,7 +516,8 @@ function garden() { $process_items = garden_make_process_items(GARDEN_OUTPUT_DIR, $content_files); garden_make_directories($process_items); garden_move_raw($process_items); - garden_generate_html($process_items); + $post_process_items = garden_generate_html(GARDEN_OUTPUT_DIR, $process_items); + garden_make_images($post_process_items); } ///////////////////////////////////////////////////////////////////////////////