Skip to content

Commit

Permalink
[tag_list] also use build_tag for tag maps
Browse files Browse the repository at this point in the history
  • Loading branch information
shish committed Dec 14, 2024
1 parent 7c254f3 commit 7587338
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 59 deletions.
10 changes: 7 additions & 3 deletions core/themelet.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,15 @@ private function get_common(): Themelet
return self::$common;
}

public function build_tag(string $tag, bool $show_underscores = true, bool $show_category = true): HTMLElement
{
public function build_tag(
string $tag,
bool $show_underscores = true,
bool $show_category = true,
?string $style = null,
): HTMLElement {
$c = self::get_common();
assert(is_a($c, CommonElementsTheme::class));
return $c->build_tag($tag, $show_underscores, $show_category);
return $c->build_tag($tag, $show_underscores, $show_category, $style);
}

public function build_thumb(Image $image): HTMLElement
Expand Down
9 changes: 7 additions & 2 deletions ext/common_elements/theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@

class CommonElementsTheme extends Themelet
{
public function build_tag(string $tag, bool $show_underscores = true, bool $show_category = true): HTMLElement
{
public function build_tag(
string $tag,
bool $show_underscores = true,
bool $show_category = true,
?string $style = null,
): HTMLElement {
$props = [
"href" => search_link([$tag]),
"class" => "tag",
"style" => $style,
"title" => "View all posts tagged $tag"
];
$body = $tag;
Expand Down
84 changes: 41 additions & 43 deletions ext/tag_list/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

namespace Shimmie2;

use MicroHTML\HTMLElement;

use function MicroHTML\{emptyHTML, BR, SPAN, A, P, HR};
use function MicroHTML\rawHTML;

require_once "config.php";

class TagList extends Extension
Expand Down Expand Up @@ -52,18 +57,15 @@ public function onPageRequest(PageRequestEvent $event): void
switch ($sub) {
case 'map':
$this->theme->set_heading("Tag Map");
$this->theme->set_tag_list($this->build_tag_map($starts_with, $tags_min));
$this->theme->display_page($page);
$this->theme->display_page($this->build_tag_map($starts_with, $tags_min));
break;
case 'alphabetic':
$this->theme->set_heading("Alphabetic Tag List");
$this->theme->set_tag_list($this->build_tag_alphabetic($starts_with, $tags_min));
$this->theme->display_page($page);
$this->theme->display_page($this->build_tag_alphabetic($starts_with, $tags_min));
break;
case 'popularity':
$this->theme->set_heading("Tag List by Popularity");
$this->theme->set_tag_list($this->build_tag_popularity($tags_min));
$this->theme->display_page($page);
$this->theme->display_page($this->build_tag_popularity($tags_min));
break;
default:
// don't display anything
Expand Down Expand Up @@ -195,7 +197,7 @@ private static function get_omitted_tags(): array
return $results;
}

private function build_az(int $tags_min): string
private function build_az(int $tags_min): HTMLElement
{
global $database;

Expand All @@ -207,16 +209,14 @@ private function build_az(int $tags_min): string
ORDER BY LOWER(substr(tag, 1, 1))
", ["tags_min" => $tags_min]);

$html = "<span class='atoz'>";
$html = SPAN(["class" => "atoz"]);
foreach ($tag_data as $a) {
$html .= " <a href='".modify_current_url(["starts_with" => $a])."'>$a</a>";
$html->appendChild(A(["href" => modify_current_url(["starts_with" => $a])], $a));
}
$html .= "</span>\n<p><hr>";

return $html;
return emptyHTML($html, P(), HR());
}

private function build_tag_map(string $starts_with, int $tags_min): string
private function build_tag_map(string $starts_with, int $tags_min): HTMLElement
{
global $config, $database;

Expand All @@ -230,28 +230,23 @@ private function build_tag_map(string $starts_with, int $tags_min): string
ORDER BY LOWER(tag)
", ["tags_min" => $tags_min, "starts_with" => $starts_with]);

$html = "";
$html = emptyHTML();
if ($config->get_bool(TagListConfig::PAGES)) {
$html .= $this->build_az($tags_min);
$html->appendChild($this->build_az($tags_min));
}
foreach ($tag_data as $row) {
$h_tag = html_escape($row['tag']);
$size = sprintf("%.2f", (float)$row['scaled']);
$link = search_link([$row['tag']]);
if ($size < 0.5) {
$size = 0.5;
}
$h_tag_no_underscores = str_replace("_", " ", $h_tag);
if (Extension::is_enabled(TagCategoriesInfo::KEY)) {
$h_tag_no_underscores = TagCategories::getTagHtml($h_tag);
}
$html .= "&nbsp;<a style='font-size: {$size}em' href='$link'>$h_tag_no_underscores</a>&nbsp;\n";
$tag = $row['tag'];
$scale = (float)$row['scaled'];
$size = sprintf("%.2f", $scale < 0.5 ? 0.5 : $scale);
$html->appendChild(rawHTML("&nbsp;"));
$html->appendChild($this->theme->build_tag($tag, style: "font-size: {$size}em"));
$html->appendChild(rawHTML("&nbsp;"));
}

return $html;
}

private function build_tag_alphabetic(string $starts_with, int $tags_min): string
private function build_tag_alphabetic(string $starts_with, int $tags_min): HTMLElement
{
global $config, $database;

Expand All @@ -263,9 +258,9 @@ private function build_tag_alphabetic(string $starts_with, int $tags_min): strin
ORDER BY LOWER(tag)
", ["tags_min" => $tags_min, "starts_with" => $starts_with]);

$html = "";
$html = emptyHTML();
if ($config->get_bool(TagListConfig::PAGES)) {
$html .= $this->build_az($tags_min);
$html->appendChild($this->build_az($tags_min));
}

/*
Expand All @@ -287,26 +282,26 @@ private function build_tag_alphabetic(string $starts_with, int $tags_min): strin
# postres utf8 string sort ignores punctuation, so we get "aza, a-zb, azc"
# which breaks down into "az, a-, az" :(
ksort($tag_data, SORT_STRING | SORT_FLAG_CASE);
$n = 0;
foreach ($tag_data as $tag => $count) {
// In PHP, $array["10"] sets the array key as int(10), not string("10")...
$tag = (string)$tag;
if ($lastLetter != mb_strtolower(substr($tag, 0, strlen($starts_with) + 1))) {
$lastLetter = mb_strtolower(substr($tag, 0, strlen($starts_with) + 1));
$h_lastLetter = html_escape($lastLetter);
$html .= "<p>$h_lastLetter<br>";
}
$link = search_link([$tag]);
$h_tag = html_escape($tag);
if (Extension::is_enabled(TagCategoriesInfo::KEY)) {
$h_tag = TagCategories::getTagHtml($h_tag, "&nbsp;($count)");
if ($n++ > 0) {
$html->appendChild(BR());
$html->appendChild(BR());
}
$html->appendChild($lastLetter);
$html->appendChild(BR());
}
$html .= "<a href='$link'>$h_tag</a>\n";
$html->appendChild($this->theme->build_tag($tag));
}

return $html;
}

private function build_tag_popularity(int $tags_min): string
private function build_tag_popularity(int $tags_min): HTMLElement
{
global $config, $database;

Expand All @@ -323,18 +318,21 @@ private function build_tag_popularity(int $tags_min): string
ORDER BY count DESC, tag ASC
", ["tags_min" => $tags_min]);

$html = "Results grouped by log<sub>10</sub>(n)";
$html = emptyHTML(rawHTML("Results grouped by log<sub>10</sub>(n)"));
$lastLog = "";
foreach ($tag_data as $row) {
$h_tag = html_escape($row['tag']);
$tag = $row['tag'];
$count = $row['count'];
$scaled = $row['scaled'];
if ($lastLog != $scaled) {
$lastLog = $scaled;
$html .= "<p>$lastLog<br>";
$html->appendChild(BR());
$html->appendChild(BR());
$html->appendChild("$lastLog");
$html->appendChild(BR());
}
$link = search_link([$row['tag']]);
$html .= "<a href='$link'>$h_tag&nbsp;($count)</a>\n";
$html->appendChild($this->theme->build_tag($tag));
$html->appendChild(rawHTML("&nbsp;($count)&nbsp;&nbsp;"));
}

return $html;
Expand Down
10 changes: 3 additions & 7 deletions ext/tag_list/theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,19 @@
class TagListTheme extends Themelet
{
public string $heading = "";
public string $list = "";

public function set_heading(string $text): void
{
$this->heading = $text;
}

public function set_tag_list(string $list): void
public function display_page(HTMLElement $list): void
{
$this->list = $list;
}
global $page;

public function display_page(Page $page): void
{
$page->set_title("Tag List");
$page->set_heading($this->heading);
$page->add_block(new Block("Tags", rawHTML($this->list)));
$page->add_block(new Block("Tags", $list));

$nav = joinHTML(
BR(),
Expand Down
7 changes: 5 additions & 2 deletions themes/danbooru/tag_list.theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@

namespace Shimmie2;

use MicroHTML\HTMLElement;

class DanbooruTagListTheme extends TagListTheme
{
public function display_page(Page $page): void
public function display_page(HTMLElement $list): void
{
global $page;
$page->set_layout("no-left");
parent::display_page($page);
parent::display_page($list);
}
}
7 changes: 5 additions & 2 deletions themes/danbooru2/tag_list.theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@

namespace Shimmie2;

use MicroHTML\HTMLElement;

class Danbooru2TagListTheme extends TagListTheme
{
public function display_page(Page $page): void
public function display_page(HTMLElement $list): void
{
global $page;
$page->set_layout("no-left");
parent::display_page($page);
parent::display_page($list);
}
}

0 comments on commit 7587338

Please sign in to comment.