4 _____ _ _____ _ _ _____ _
5 / ____| | | / ____(_) | / ____| | |
6 | | __ __ _ _ __ __| | ___ _ __ | (___ _| |_ ___ | | __ ___ _ __ ___ _ __ __ _| |_ ___ _ __
7 | | |_ |/ _` | '__/ _` |/ _ \ '_ \ \___ \| | __/ _ \ | | |_ |/ _ \ '_ \ / _ \ '__/ _` | __/ _ \| '__|
8 | |__| | (_| | | | (_| | __/ | | | ____) | | || __/ | |__| | __/ | | | __/ | | (_| | || (_) | |
9 \_____|\__,_|_| \__,_|\___|_| |_| |_____/|_|\__\___| \_____|\___|_| |_|\___|_| \__,_|\__\___/|_|
13 ///////////////////////////////////////////////////////////////////////////////
15 ///////////////////////////////////////////////////////////////////////////////
17 function output_debug(...$str_segments) {
18 $str = join('', $str_segments);
22 function output_error(...$str_segments) {
23 $str = join('', $str_segments);
27 ///////////////////////////////////////////////////////////////////////////////
29 ///////////////////////////////////////////////////////////////////////////////
31 function garden_path(...$path_segments) {
33 foreach ($path_segments as $path_segment) {
34 $inner_segments = explode(DIRECTORY_SEPARATOR, $path_segment);
35 foreach ($inner_segments as $inner_segment) {
36 if ($inner_segment != '') {
37 $segments[] = $inner_segment;
41 array_unshift($segments, '');
42 return join(DIRECTORY_SEPARATOR, $segments);
45 ///////////////////////////////////////////////////////////////////////////////
47 ///////////////////////////////////////////////////////////////////////////////
49 function garden_slug($str) {
50 $str = preg_replace('/\s+/', '-', $str);
51 $str = preg_replace('/\-+/', '-', $str);
52 $str = preg_replace('/[^\w\s\-\.]+/', '', $str);
53 $str = strtolower($str);
57 function garden_url(...$url_segments) {
59 foreach ($url_segments as $url_segment) {
60 $inner_segments = explode(DIRECTORY_SEPARATOR, $url_segment);
61 foreach ($inner_segments as $inner_segment) {
62 if ($inner_segment != '') {
63 $segments[] = garden_slug($inner_segment);
67 array_unshift($segments, '');
68 return join(DIRECTORY_SEPARATOR, $segments);
71 ///////////////////////////////////////////////////////////////////////////////
73 ///////////////////////////////////////////////////////////////////////////////
75 function garden_read_file($filename) {
76 return file_get_contents($filename);
79 function garden_write_file($filename, $content) {
80 $utf8_bom = "\xEF\xBB\xBF";
81 file_put_contents($filename, $utf8_bom . $content);
84 ///////////////////////////////////////////////////////////////////////////////
86 ///////////////////////////////////////////////////////////////////////////////
102 public $source_filename;
103 public $target_filename;
105 public function __construct($path, $type, $id, $title, $category, $url, $date, $source_filename, $target_filename) {
109 $this->title = $title;
110 $this->category = $category;
113 $this->source_filename = $source_filename;
114 $this->target_filename = $target_filename;
118 function garden_make_process_items($output_directory, $content_paths) {
121 foreach ($content_paths as $item) {
122 $id = garden_slug($item->filename);
124 $target_extension = $item->extension;
126 $ignore_file = false;
127 $type = GardenItemType::Raw;
128 switch ($item->extension) {
134 $type = GardenItemType::Article;
135 $target_extension = 'html';
142 $type = GardenItemType::Image;
146 if ($ignore_file == true) {
150 $target_basename = $item->filename;
151 if ($target_extension != '') {
152 $target_basename .= '.' . $target_extension;
155 $category_components = explode(DIRECTORY_SEPARATOR, $item->path);
156 $category = count($category_components) >= 2 ? $category_components[1] : "";
158 $url = GARDEN_SITE_BASE_URL . garden_url($item->path, $target_basename);
159 $target_path = garden_path($output_directory, garden_url($item->path, $target_basename));
161 $date = filemtime($item->full_path);
163 $output_items[] = new GardenItem($item, $type, $id, $item->filename, $category, $url, $date, $item->full_path, $target_path);
166 return $output_items;
169 ///////////////////////////////////////////////////////////////////////////////
171 ///////////////////////////////////////////////////////////////////////////////
173 class GardenContentPath {
181 public function __construct($full_path, $root, $path, $path_parts) {
182 $this->full_path = $full_path;
185 $this->basename = $path_parts['basename'];
186 $this->filename = $path_parts['filename'];
187 $this->extension = $path_parts['extension'];
191 function garden_find_content_files($content_dir) {
192 $content_dir = realpath($content_dir);
197 while (count($scan_paths) > 0) {
198 $scan_path = array_shift($scan_paths);
199 $path_contents = scandir(garden_path($content_dir, $scan_path));
200 foreach ($path_contents as $item) {
201 if (str_starts_with($item, '.')) {
205 $full_path = garden_path($content_dir, $scan_path, $item);
206 if (is_dir($full_path)) {
207 $scan_paths[] = garden_path($scan_path, $item);
211 $path_parts = pathinfo($full_path);
212 $output_paths[] = new GardenContentPath($full_path, $content_dir, $scan_path, $path_parts);
216 return $output_paths;
219 ///////////////////////////////////////////////////////////////////////////////
221 ///////////////////////////////////////////////////////////////////////////////
223 function garden_make_directories($content_items) {
224 foreach ($content_items as $item) {
225 $path_parts = pathinfo($item->target_filename);
227 $directory = $path_parts['dirname'];
228 if (is_dir($directory ) == false) {
229 mkdir($directory , 0777, true); // FIXME, permissions...
233 return $content_items;
236 function garden_move_raw($content_items) {
237 foreach ($content_items as $item) {
238 if ($item->type != GardenItemType::Raw && $item->type != GardenItemType::Image) { // FIXME, do we need to copy images?
242 $success = copy($item->source_filename, $item->target_filename);
243 if ($success != true) {
244 error('Failed to copy file: filename="', $item->source_filename, '"');
248 return $content_items;
251 ///////////////////////////////////////////////////////////////////////////////
253 ///////////////////////////////////////////////////////////////////////////////
255 function garden_template_render($name, $variables = null) {
256 global $garden_template_base, $garden_template_content;
258 $base_template = null;
261 while ($name != null) {
262 $path = garden_path(GARDEN_TEMPLATE_DIR, $name . '.php');
264 $base_template = null;
265 $base_template_variables = null;
267 $garden_template_base_previous = $garden_template_base;
268 $garden_template_base = function($name, $base_variables) use (&$base_template, &$base_template_variables) {
269 $base_template = $name;
270 $base_template_variables = $base_variables;
273 $garden_template_content_previous = $garden_template_content;
274 $garden_template_content = function() use ($output) {
278 if ($variables != null) {
284 $output = ob_get_contents();
287 $garden_template_base = $garden_template_base_previous;
288 $garden_template_content = $garden_template_content_previous;
290 $name = $base_template;
291 $variables = $base_template_variables;
297 ///////////////////////////////////////////////////////////////////////////////
299 ///////////////////////////////////////////////////////////////////////////////
301 function garden_template_base($name, $variables = null) {
302 global $garden_template_base;
303 $garden_template_base($name, $variables);
306 function garden_template_content() {
307 global $garden_template_content;
308 return $garden_template_content();
311 function garden_site_url(...$url_segments) {
312 return GARDEN_SITE_BASE_URL . garden_url(...$url_segments);
317 ///////////////////////////////////////////////////////////////////////////////
319 ///////////////////////////////////////////////////////////////////////////////
322 public $content_item;
326 public $target_filename;
328 public function __construct($content_item, $width, $height, $url, $target_filename) {
329 $this->content_item = $content_item;
330 $this->width = $width;
331 $this->height = $height;
333 $this->target_filename = $target_filename;
337 function garden_make_images($post_process_items) {
338 foreach ($post_process_items as $item) {
339 $image = new Imagick($item->content_item->source_filename);
340 $image->thumbnailImage($item->width, $item->height);
341 $image->writeImage($item->target_filename);
345 ///////////////////////////////////////////////////////////////////////////////
347 ///////////////////////////////////////////////////////////////////////////////
349 require_once(garden_path(__DIR__, 'third_party', 'parsedown', 'Parsedown.php'));
351 class GardenExtendedParsedown extends Parsedown {
352 public $post_process_items;
353 private $output_directory;
354 private $content_items;
356 public function __construct($output_directory, $content_items) {
357 $this->post_process_items = [];
358 $this->output_directory = $output_directory;
359 $this->content_items = $content_items;
360 $this->InlineTypes['!'][] = 'Youtube';
361 $this->InlineTypes['!'][] = 'Image';
362 $this->InlineTypes['['][] = 'WikiLinks';
363 $this->inlineMarkerList .= '!';
364 $this->inlineMarkerList .= '[';
367 protected function inlineImage($excerpt) {
368 if (preg_match('/^!\[\[([^\|\]]+)(\|([0-9]+)x([0-9]+))?\]\]/', $excerpt['text'], $matches)) {
369 $image_name = $matches[1];
370 $image_width = count($matches) == 5 ? $matches[3] : null;
371 $image_height = count($matches) == 5 ? $matches[4] : null;
373 if ($image_name == null) {
377 $image_content_item = null;
378 foreach ($this->content_items as $content_item) {
379 if ($content_item->path->basename == $image_name) {
380 $image_content_item = $content_item;
385 if ($image_content_item == null) {
389 $target_url = $image_content_item->url;
390 if ($image_width != null && $image_height != null) {
391 $original_path = $image_content_item->path;
393 $target_basename = $original_path->filename . '_' . $image_width . 'x' . $image_height . '.png';
395 $target_url = GARDEN_SITE_BASE_URL . garden_url($original_path->path, $target_basename);
396 $target_path = garden_path($this->output_directory, garden_url($original_path->path, $target_basename));
398 $post_process_item = new GardenImage($image_content_item, $image_width, $image_height, $target_url, $target_path);
399 $this->post_process_items[] = $post_process_item;
403 'extent' => strlen($matches[0]),
406 'attributes' => array(
407 'src' => $target_url,
414 protected function inlineYoutube($excerpt) {
415 if (preg_match('/^!\[\[yt:([^\]]+)\]\]/', $excerpt['text'], $matches)) {
416 $video_id = $matches[1];
418 if ($video_id == null) {
423 'extent' => strlen($matches[0]),
427 'attributes' => array(
429 'type' => "text/html",
432 'src' => "https://www.youtube.com/embed/" . $video_id,
433 'frameborder' => "0",
435 'referrerpolicy' => "no-referrer",
436 'sandbox' => "allow-same-origin allow-scripts",
443 protected function inlineWikiLinks($excerpt) {
444 if (preg_match('/^\[\[(.+)\]\]/', $excerpt['text'], $matches)) {
445 $target_title = $matches[1];
447 if ($target_title == null) {
452 foreach ($this->content_items as $content_item) {
453 if ($content_item->title == $target_title) {
454 $target_url = $content_item->url;
459 if ($target_url == null) {
464 'extent' => strlen($matches[0]),
467 'text' => $target_title,
468 'attributes' => array(
469 'href' => $target_url,
477 function garden_generate_html($output_directory, $content_items) {
480 $categorised_items = [];
481 foreach ($content_items as $item) {
482 if ($item->type != GardenItemType::Article) {
486 if (isset($categorised_items[$item->category]) == false) {
487 $categorised_items[$item->category] = [];
489 $categorised_items[$item->category][] = $item;
492 foreach ($content_items as $item) {
493 if ($item->type != GardenItemType::Article) {
497 $markdown_data = garden_read_file($item->source_filename);
499 $parsedown = new GardenExtendedParsedown($output_directory, $content_items);
500 $markdown_html = $parsedown->text($markdown_data);
502 if (count($parsedown->post_process_items) > 0) {
503 array_push($output_items, ...$parsedown->post_process_items);
506 $variables = GARDEN_TEMPLATE_CONSTANTS; // PHP will copy by default!
507 $variables['article'] = $item;
508 $variables['article_content'] = $markdown_html;
509 $variables['categories'] = $categorised_items;
511 $html_data = garden_template_render('article', $variables);
513 garden_write_file($item->target_filename, $html_data);
516 return $output_items;
519 ///////////////////////////////////////////////////////////////////////////////
521 ///////////////////////////////////////////////////////////////////////////////
523 function garden_generate_atom($output_directory, $content_items) {
525 foreach ($content_items as $item) {
526 if ($item->type != GardenItemType::Article) {
530 $recent_items[$item->date] = $item;
534 ksort($recent_items);
535 $recent_items = array_reverse($recent_items);
537 $variables = GARDEN_TEMPLATE_CONSTANTS; // PHP will copy by default!
538 $variables['recent_items'] = $recent_items;
539 $html_data = garden_template_render('atom', $variables);
541 garden_write_file(garden_path($output_directory, 'feed.atom'), $html_data);
544 function garden_generate_json($output_directory, $content_items) {
546 foreach ($content_items as $item) {
547 if ($item->type != GardenItemType::Article) {
551 $recent_items[$item->date] = $item;
555 ksort($recent_items);
556 $recent_items = array_reverse($recent_items);
559 $feed['version'] = 'https://jsonfeed.org/version/1.1';
560 $feed['title'] = GARDEN_TEMPLATE_CONSTANTS['site_name'];
561 $feed['home_page_url'] = garden_site_url('/');
562 $feed['feed_url'] = garden_site_url('/feed.json');
565 foreach ($recent_items as $item) {
567 $feed_item['id'] = $item->date + hexdec(hash('crc32', $item->title));
568 $feed_item['date_published'] = date(DATE_RFC3339, $item->date);
569 $feed_item['title'] = $item->title;
570 $feed_item['content_text'] = 'An update was published.';
571 $feed_item['url'] = $item->url;
572 $feed['items'][] = $feed_item;
575 $json_data = json_encode($feed);
576 garden_write_file(garden_path($output_directory, 'feed.json'), $json_data);
579 ///////////////////////////////////////////////////////////////////////////////
581 ///////////////////////////////////////////////////////////////////////////////
584 $content_files = garden_find_content_files(GARDEN_CONTENT_DIR);
585 array_push($content_files, ...garden_find_content_files(GARDEN_TEMPLATE_DIR));
586 $process_items = garden_make_process_items(GARDEN_OUTPUT_DIR, $content_files);
587 garden_make_directories($process_items);
588 garden_move_raw($process_items);
589 $post_process_items = garden_generate_html(GARDEN_OUTPUT_DIR, $process_items);
590 garden_make_images($post_process_items);
591 garden_generate_atom(GARDEN_OUTPUT_DIR, $process_items);
592 garden_generate_json(GARDEN_OUTPUT_DIR, $process_items);
595 ///////////////////////////////////////////////////////////////////////////////
597 ///////////////////////////////////////////////////////////////////////////////
599 assert(extension_loaded('imagick'), 'Needs Imagick');
600 assert($argc >= 2, 'Please provide configuration file');
602 // First parameter needs to be the configuration php
603 $config_file = $argv[1];
605 output_debug("Will use configuration: file='", $config_file, "'");
606 require_once($config_file);