Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[themes] make a build_tag function which themes can override, see #1328 #1339

Merged
merged 1 commit into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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