span and a generators are now prepared to handle classes
authorEduardo <[email protected]>
Sat, 25 Nov 2023 01:10:59 +0000 (02:10 +0100)
committerEduardo <[email protected]>
Sat, 25 Nov 2023 01:10:59 +0000 (02:10 +0100)
page-generator/generator.php
page-generator/index.php

index 6d3ebea961d3a4a339522a517e90b6b524d36fbf..c9a72e76103159ec117afca73617d1dd6659125d 100644 (file)
@@ -94,23 +94,36 @@ class CodeGenerator
     /**
      * Returns a string with a span tag with a title set
      */
-    function spanTitle(string $title, string $text): string
+    function spanTitle(string $title, string $text, array | null $class): string
     {
-        return "<span title='$title'>$text</span> ";
+        $html = "<span";
+
+        if (!is_null($class))
+            $html .= ' class="' . implode(' ', $class) . '"';
+
+        $html .= " title='$title'>$text</span> ";
+
+        return $html;
     }
 
     /**
      * Returns a string with an anchor tag
      */
-    function a(string $url, string $text, string $title = null): string
+    function a(string $url, string $text, string | null $title, array | null $class): string
     {
-        $str = "<a href='$url'";
-        if (!is_null($title)) {
-            $str .= " title='$title'";
-        }
-        $str .= ">$text</a> ";
+        $html = "<a";
+
+        if (!is_null($class))
+            $html .= ' class="' . implode(' ', $class) . '"';
 
-        return $str;
+        $html .= " href='$url'";
+
+        if (!is_null($title))
+            $html .= " title='$title'";
+
+        $html .= ">$text</a> ";
+
+        return $html;
     }
 
     /**
@@ -253,9 +266,9 @@ class CodeGenerator
             $html .= '>';
 
             if (empty($text) && !empty($url))
-                $html .= $this->a($url, $section, $title);
+                $html .= $this->a($url, $section, $title, $filtered_classes);
             elseif (empty($text) && !empty($title))
-                $html .= $this->spanTitle($title, $section);
+                $html .= $this->spanTitle($title, $section, $filtered_classes);
             else
                 $html .= $section;
 
@@ -278,17 +291,18 @@ class CodeGenerator
     private function form_content(string $text, array $class, string $url, string $title): string
     {
         $html = "";
+        $filtered_classes = $this->filter_meta_classes($class);
 
         // if has title and url = a with title
         if (!empty($title) && !empty($url))
-            $html .= $this->a($url, $text, $title);
+            $html .= $this->a($url, $text, $title, $filtered_classes);
         // if has only title = span with title
         elseif (!empty($title)) {
-            $html .= $this->spanTitle($title, $text);
+            $html .= $this->spanTitle($title, $text, $filtered_classes);
         }
         // if has only url = a
         elseif (!empty($url))
-            $html .= $this->a($url, $text);
+            $html .= $this->a($url, $text, null, $filtered_classes);
         // else just text = p
         else
             $html .= $text;
@@ -301,7 +315,7 @@ class CodeGenerator
 
         if (in_array("no-space", $class))
             $html = trim($html);
-        
+
         return $html;
     }
 
@@ -317,7 +331,7 @@ class CodeGenerator
         // if has title and url = a with title
         if (!empty($title) && !empty($url)) {
             $html .= '>';
-            $html .= $this->a($url, $text, $title);
+            $html .= $this->a($url, $text, $title, null);
         }
         // if has only title = span with title
         elseif (!empty($title)) {
index 4edba1e0d4d9847b2c36903a9b68efb6b5197ac0..5f059555e09e682b9d46f24ba1de4d8ec85961d4 100644 (file)
@@ -127,7 +127,7 @@ $generator->writeToFile($header_path, '<nav id="menu">');
 $generator->writeToFile($header_path, '<ul>');
 foreach ($data['menu'] as $item) {
     $generator->writeToFile($header_path, '<li>');
-    $generator->writeToFile($header_path, $generator->a('../' . $item['path'], $item['item']));
+    $generator->writeToFile($header_path, $generator->a('../' . $item['path'], $item['item'], null, null));
     $generator->writeToFile($header_path, '</li>');
 }
 $generator->writeToFile($header_path, '</ul>');