Skip to content

Commit

Permalink
[themes] make a build_tag function which themes can override, see #1328
Browse files Browse the repository at this point in the history
  • Loading branch information
shish committed Dec 13, 2024
1 parent 7396a04 commit 54eb01d
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 5 deletions.
7 changes: 7 additions & 0 deletions core/themelet.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
25 changes: 25 additions & 0 deletions ext/common_elements/theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -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...
Expand Down
6 changes: 1 addition & 5 deletions ext/post_tags/theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
20 changes: 20 additions & 0 deletions ext/tag_categories/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 54eb01d

Please sign in to comment.