image generation in pages
authorEduardo <[email protected]>
Sun, 26 Nov 2023 19:23:24 +0000 (20:23 +0100)
committerEduardo <[email protected]>
Sun, 26 Nov 2023 19:23:24 +0000 (20:23 +0100)
page-generator/generator.php
page-generator/index.php

index 713e7ede6e92b20af306070f5818798e74be5bdb..4a1129ba0939e8ee6838d6631617c8b814841dd2 100644 (file)
@@ -144,34 +144,38 @@ class CodeGenerator
      * @param string | null $id - id for the image, if $url is not null used for the anchor tag else for the image tag
      * @param array | null $class - array of classes for the image
      */
-    public function img(string $src, string $alt, string | null $title, string | null $url, string | null $id, array | null $class): string
+    public function img(string $src, string | null $alt, string | null $title, string | null $url, string | null $id, array | null $class): string
     {
         $html = "";
 
-        if (!is_null($url)) {
+        if (!empty($url)) {
             $html .= "<a href='$url' id='$id'>";
         }
 
         $html .= "<img";
 
-        if (!is_null($class)) {
+        if (!empty($class)) {
             $html .= ' class="' . implode(' ', $class) . '"';
             print_r($class);
         }
 
-        $html .= " src='$src' alt='$alt'";
+        $html .= " src='$src'";
 
-        if (!is_null($title)) {
+        if (!empty($alt)) {
+            $html .= " alt='$alt'";
+        }
+
+        if (!empty($title)) {
             $html .= " title='$title'";
         }
 
-        if (is_null($url) && !is_null($id)) {
+        if (empty($url) && !empty($id)) {
             $html .= " id='$id'";
         }
 
         $html .= "/>";
 
-        if (!is_null($url)) {
+        if (!empty($url)) {
             $html .= "</a>";
         }
 
@@ -203,6 +207,11 @@ class CodeGenerator
             $url     = $this->str_val_or_empty($line, 'url');
             $title   = $this->str_val_or_empty($line, 'title');
             $class   = array_map('trim', explode(",", $this->str_val_or_empty($line, 'class')));
+            $image  =  null;
+
+            if (!empty($line['image'])) {
+                $image = $line['image'];
+            }
 
             // if new-col close p and ul and open div with col class
             if (in_array("new-col", $class)) {
@@ -219,6 +228,12 @@ class CodeGenerator
                 $is_div_closed = false;
             }
 
+            // if image is set add image and skipt switch
+            if (!is_null($image)) {
+                $html .= $this->img($image['path'], $image['description'], $image['title'], $image['url'], $image['id'], $image['class']);
+                continue;
+            }
+
             switch ($class) {
                 case in_array("list", $class):
                     // if previous iteration was diferent section close li
index 8c884c2de0dca1e416593be8f88e7ceafa81358f..ec856368ae2347b9a425de28f15f1d8c25931210 100644 (file)
@@ -209,6 +209,18 @@ foreach ($data['pages'] as $page) {
 
     $page_data = $data['pages'][$page['path']];
 
+    // put image object in page_data
+    foreach ($page_data as $dKey => $line) {
+        if (!empty($line['image'])) {
+            foreach ($data['images'] as $image) {
+                if ($image['id'] == $line['image']) {
+                    $page_data[$dKey]['image'] = $image;
+                    break;
+                }
+            }
+        }
+    }
+
     // generate page
     $generator->writeToFile($page_path, $generator->form_html($page_data, "en"));
     $generator->writeToFile($page_path, '</section>');