diff --git a/core/themelet.php b/core/themelet.php index 769b5d48d..77206eebe 100644 --- a/core/themelet.php +++ b/core/themelet.php @@ -28,6 +28,13 @@ private function get_common(): Themelet return self::$common; } + public function build_tag(string $tag, bool $show_underscores = true, bool $show_category = true): HTMLElement + { + $c = self::get_common(); + assert(is_a($c, CommonElementsTheme::class)); + return $c->build_tag($tag, $show_underscores, $show_category); + } + public function build_thumb(Image $image): HTMLElement { $c = self::get_common(); diff --git a/ext/common_elements/theme.php b/ext/common_elements/theme.php index d25f174ba..e6805a77f 100644 --- a/ext/common_elements/theme.php +++ b/ext/common_elements/theme.php @@ -10,6 +10,31 @@ class CommonElementsTheme extends Themelet { + public function build_tag(string $tag, bool $show_underscores = true, bool $show_category = true): HTMLElement + { + $props = [ + "href" => search_link([$tag]), + "class" => "tag", + "title" => "View all posts tagged $tag" + ]; + $body = $tag; + + if (Extension::is_enabled(TagCategoriesInfo::KEY)) { + $category = TagCategories::getTagCategory($tag); + if (!is_null($category)) { + $tag_category_dict = TagCategories::getKeyedDict(); + $props["class"] = "tag tag_category_$category"; + $props["style"] = "color:".$tag_category_dict[$category]['color'].";"; + + if ($show_category === false) { + $body = TagCategories::getTagBody($tag); + } + } + } + + return A($props, $show_underscores ? str_replace("_", " ", $body) : $body); + } + /** * Generic thumbnail code; returns HTML rather than adding * a block since thumbs tend to go inside blocks... diff --git a/ext/post_tags/theme.php b/ext/post_tags/theme.php index 4dab4dcb4..bea8b4b73 100644 --- a/ext/post_tags/theme.php +++ b/ext/post_tags/theme.php @@ -31,11 +31,7 @@ public function get_tag_editor_html(Image $image): HTMLElement $tag_links = []; foreach ($image->get_tag_array() as $tag) { - $tag_links[] = A([ - "href" => search_link([$tag]), - "class" => "tag", - "title" => "View all posts tagged $tag" - ], $tag); + $tag_links[] = $this->build_tag($tag); } return SHM_POST_INFO( diff --git a/ext/tag_categories/main.php b/ext/tag_categories/main.php index 6f9e2dcc8..11b3d3dee 100644 --- a/ext/tag_categories/main.php +++ b/ext/tag_categories/main.php @@ -131,6 +131,26 @@ public static function getKeyedDict(): array return $tc_keyed_dict; } + public static function getTagCategory(string $tag): ?string + { + $tag_category_dict = static::getKeyedDict(); + $tag_split = explode(':', $tag, 2); + if (count($tag_split) > 1 && array_key_exists($tag_split[0], $tag_category_dict)) { + return $tag_split[0]; + } + return null; + } + + public static function getTagBody(string $tag): string + { + $tag_category_dict = static::getKeyedDict(); + $tag_split = explode(':', $tag, 2); + if (count($tag_split) > 1 && array_key_exists($tag_split[0], $tag_category_dict)) { + return $tag_split[1]; + } + return $tag; + } + public static function getTagHtml(string $h_tag, string $extra_text = ''): string { $h_tag_no_underscores = str_replace("_", " ", $h_tag);