From: Eduardo Date: Thu, 23 Nov 2023 19:36:19 +0000 (+0100) Subject: generation of titles, lists and other pages done! X-Git-Url: http://git.edufdez.es/?a=commitdiff_plain;h=324be8a97ecd32421908df644d4c6108f5ee5f5b;p=thericegoat.com.git generation of titles, lists and other pages done! --- diff --git a/page-generator/generator.php b/page-generator/generator.php index e2c1b91..6a6f0a6 100644 --- a/page-generator/generator.php +++ b/page-generator/generator.php @@ -1,8 +1,10 @@ $line) { + $section = $this->str_val_or_empty($line, 'section'); + $text = $this->str_val_or_empty($line, 'text'); + $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'))); + + // if new-col close p and ul and open div with col class + if (in_array("new-col", $class)) { + [$closing_tag, $is_p_closed] = $this->open_or_close_p($is_p_closed, $class, CLOSE); + $html .= $closing_tag; + [$closing_tag, $is_list_closed] = $this->open_or_close_ul($is_list_closed, $class, CLOSE, $text); + $html .= $closing_tag; + + if (!$is_div_closed) { + $html .= ''; + $is_div_closed = true; + } + $html .= '
'; + $is_div_closed = false; + } + + + switch ($class) { + case in_array("list", $class): + // if previous iteration was diferent section close li + if ($i != 0 && $data[$i - 1]['section'] != $section) { + [$closing_tag, $is_list_closed] = $this->open_or_close_ul($is_list_closed, $class, CLOSE, $text); + $html .= $closing_tag; + } + + $html .= $this->form_h2_title($data, $i, $section, $url, $title, $class); + [$closing_tag, $is_list_closed] = $this->open_or_close_ul($is_list_closed, $class, OPEN, $text); + $html .= $closing_tag; + $html .= $this->form_list($text, $class, $url, $title); + + break; + + default: + // if previous iteration was diferent section close p + if ($i != 0 && $data[$i - 1]['section'] != $section) { + [$closing_tag, $is_p_closed] = $this->open_or_close_p($is_p_closed, $class, CLOSE); + $html .= $closing_tag; + } + + $html .= $this->form_h2_title($data, $i, $section, $url, $title, $class); + [$closing_tag, $is_p_closed] = $this->open_or_close_p($is_p_closed, $class, OPEN); + $html .= $closing_tag; + $html .= $this->form_content($text, $class, $url, $title); + + break; + } + } + + // close last p + $html .= $this->open_or_close_p($is_p_closed, null, CLOSE)[0]; + return $html; + } + + private function open_or_close_p(bool $is_p_closed, array | null $class, bool $open): array + { + $html = ""; + + if ($is_p_closed && $open) { + $html .= 'filter_meta_classes($class); + if (!empty($filtered_classes)) + $html .= ' class="' . implode(' ', $filtered_classes) . '"'; + + $html .= '>'; + } elseif (!$is_p_closed && !$open) { + $html .= '

'; + $is_p_closed = true; + } + + return [$html, $is_p_closed]; + } + + private function open_or_close_ul(bool $is_list_closed, array | null $class, bool $open, string $text): array + { + $html = ""; + + if ($is_list_closed && $open) { + $html .= 'filter_meta_classes($class); + if (empty($text) && !empty($filtered_classes)) + $html .= ' class="' . implode(' ', $filtered_classes) . '"'; + + $html .= '>'; + } elseif (!$is_list_closed && !$open) { + $html .= ''; + $is_list_closed = true; + } + + return [$html, $is_list_closed]; + } + + /** + * Will return an empty string if the title is not required + */ + private function form_h2_title(array $data, int $currentIndex, string $section, string $url, string $title, array $class): string + { + $html = ""; + + // if previous iteration was diferent section or is first iteration and new section is not empty add h2 + if (($currentIndex != 0 && $data[$currentIndex - 1]['section'] != $section) || ($currentIndex == 0 && !empty($section))) { + $html .= 'filter_meta_classes($class); + if (empty($text) && !empty($filtered_classes)) + $html .= ' class="' . implode(' ', $filtered_classes) . '"'; + $html .= '>'; + + if (empty($text) && !empty($url)) + $html .= $this->a($url, $section, $title); + elseif (empty($text) && !empty($title)) + $html .= $this->spanTitle($title, $section); + else + $html .= $section; + + $html .= ''; + } + + return $html; + } + + /** + * Returns an array with the meta classes removed + */ + private function filter_meta_classes(array $class): array + { + $meta_classes = ["same-line", "new-col", "list"]; + return array_diff($class, $meta_classes); + } + + + private function form_content(string $text, array $class, string $url, string $title): string + { + $html = ""; + + // if has title and url = a with title + if (!empty($title) && !empty($url)) + $html .= $this->a($url, $text, $title); + // if has only title = span with title + elseif (!empty($title)) { + $html .= $this->spanTitle($title, $text); + } + // if has only url = a + elseif (!empty($url)) + $html .= $this->a($url, $text); + // else just text = p + else + $html .= $text; + + // if in class array is not "same-line" add br + if (!in_array("same-line", $class)) + $html .= '
'; + else + $html .= ' '; + + return $html; + } + + private function form_list(string $text, array $class, string $url, string $title): string + { + $html = "filter_meta_classes($class); + if (!empty($filtered_classes)) + $html .= ' class="' . implode(' ', $filtered_classes) . '"'; + + // if has title and url = a with title + if (!empty($title) && !empty($url)) { + $html .= '>'; + $html .= $this->a($url, $text, $title); + } + // if has only title = span with title + elseif (!empty($title)) { + $html .= " title='$title'>$text"; + } else + $html .= ">$text"; + + $html .= ""; + + return $html; + } + + private function str_val_or_empty(array $arr, string $key): string + { + return isset($arr[$key]) ? $arr[$key] : ""; + } }