microhtml for themelet functions, see #828

This commit is contained in:
Shish 2023-06-27 14:10:53 +01:00
parent 928c9d95d1
commit dc7e8ee231
6 changed files with 145 additions and 89 deletions

View file

@ -4,6 +4,10 @@ declare(strict_types=1);
namespace Shimmie2;
use MicroHTML\HTMLElement;
use function MicroHTML\{A,B,BR,IMG,emptyHTML};
/**
* Class BaseThemelet
*
@ -46,15 +50,15 @@ class BaseThemelet
* Generic thumbnail code; returns HTML rather than adding
* a block since thumbs tend to go inside blocks...
*/
public function build_thumb_html(Image $image): string
public function build_thumb_html(Image $image): HTMLElement
{
global $config;
$i_id = (int) $image->id;
$h_view_link = make_link('post/view/'.$i_id);
$h_thumb_link = $image->get_thumb_link();
$h_tip = html_escape($image->get_tooltip());
$h_tags = html_escape(strtolower($image->get_tag_list()));
$id = $image->id;
$view_link = make_link('post/view/'.$id);
$thumb_link = $image->get_thumb_link();
$tip = $image->get_tooltip();
$tags = strtolower($image->get_tag_list());
// TODO: Set up a function for fetching what kind of files are currently thumbnailable
$mimeArr = array_flip([MimeType::MP3]); //List of thumbless filetypes
@ -75,9 +79,27 @@ class BaseThemelet
}
}
return "<a href='$h_view_link' class='thumb shm-thumb shm-thumb-link {$custom_classes}' data-tags='$h_tags' data-height='$image->height' data-width='$image->width' data-mime='{$image->get_mime()}' data-post-id='$i_id'>".
"<img id='thumb_$i_id' title='$h_tip' alt='$h_tip' height='{$tsize[1]}' width='{$tsize[0]}' src='$h_thumb_link'>".
"</a>\n";
return A(
[
"href"=>$view_link,
"class"=>"thumb shm-thumb shm-thumb-link $custom_classes",
"data-tags"=>$tags,
"data-height"=>$image->height,
"data-width"=>$image->width,
"data-mime"=>$image->get_mime(),
"data-post-id"=>$id,
],
IMG(
[
"id"=>"thumb_$id",
"title"=>$tip,
"alt"=>$tip,
"height"=>$tsize[1],
"width"=>$tsize[0],
"src"=>$thumb_link,
]
)
);
}
public function display_paginator(Page $page, string $base, ?string $query, int $page_number, int $total_pages, bool $show_random = false)
@ -99,26 +121,34 @@ class BaseThemelet
$page->add_html_header("<link rel='last' href='".make_http(make_link($base.'/'.$total_pages, $query))."'>");
}
private function gen_page_link(string $base_url, ?string $query, int $page, string $name): string
private function gen_page_link(string $base_url, ?string $query, int $page, string $name): HTMLElement
{
$link = make_link($base_url.'/'.$page, $query);
return '<a href="'.$link.'">'.$name.'</a>';
return A(["href"=>make_link($base_url.'/'.$page, $query)], $name);
}
private function gen_page_link_block(string $base_url, ?string $query, int $page, int $current_page, string $name): string
private function gen_page_link_block(string $base_url, ?string $query, int $page, int $current_page, string $name): HTMLElement
{
$paginator = "";
$paginator = $this->gen_page_link($base_url, $query, $page, $name);
if ($page == $current_page) {
$paginator .= "<b>";
}
$paginator .= $this->gen_page_link($base_url, $query, $page, $name);
if ($page == $current_page) {
$paginator .= "</b>";
$paginator = B($paginator);
}
return $paginator;
}
private function build_paginator(int $current_page, int $total_pages, string $base_url, ?string $query, bool $show_random): string
protected function implode(string|HTMLElement $glue, array $pieces): HTMLElement
{
$out = emptyHTML();
$n = 0;
foreach ($pieces as $piece) {
if($n++ > 0) {
$out->appendChild($glue);
}
$out->appendChild($piece);
}
return $out;
}
private function build_paginator(int $current_page, int $total_pages, string $base_url, ?string $query, bool $show_random): HTMLElement
{
$next = $current_page + 1;
$prev = $current_page - 1;
@ -145,9 +175,20 @@ class BaseThemelet
foreach (range($start, $end) as $i) {
$pages[] = $this->gen_page_link_block($base_url, $query, $i, $current_page, (string)$i);
}
$pages_html = implode(" | ", $pages);
$pages_html = $this->implode(" | ", $pages);
return $first_html.' | '.$prev_html.' | '.$random_html.' | '.$next_html.' | '.$last_html
.'<br>&lt;&lt; '.$pages_html.' &gt;&gt;';
return emptyHTML(
$this->implode(" | ", [
$first_html,
$prev_html,
$random_html,
$next_html,
$last_html,
]),
BR(),
'<< ',
$pages_html,
' >>'
);
}
}

View file

@ -53,7 +53,7 @@ class Block
$this->position = $position;
if (is_null($id)) {
$id = (empty($header) ? md5($body ?? '') : $header) . $section;
$id = (empty($header) ? md5($this->body ?? '') : $header) . $section;
}
$str_id = preg_replace('/[^\w-]/', '', str_replace(' ', '_', $id));
assert(is_string($str_id));

View file

@ -4,6 +4,10 @@ declare(strict_types=1);
namespace Shimmie2;
use MicroHTML\HTMLElement;
use function MicroHTML\{A, B, DIV};
class Themelet extends BaseThemelet
{
public function display_paginator(Page $page, string $base, ?string $query, int $page_number, int $total_pages, bool $show_random = false)
@ -15,24 +19,22 @@ class Themelet extends BaseThemelet
$page->add_block(new Block(null, $body, "main", 90));
}
private function gen_page_link(string $base_url, ?string $query, int $page, string $name): string
private function gen_page_link(string $base_url, ?string $query, int $page, string $name): HTMLElement
{
$link = make_link("$base_url/$page", $query);
return "<a href='$link'>$name</a>";
return A(["href" => make_link("$base_url/$page", $query)], $name);
}
private function gen_page_link_block(string $base_url, ?string $query, int $page, int $current_page, string $name): string
private function gen_page_link_block(string $base_url, ?string $query, int $page, int $current_page, string $name): HTMLElement
{
$paginator = "";
if ($page == $current_page) {
$paginator .= "<b>$page</b>";
$paginator = B($page);
} else {
$paginator .= $this->gen_page_link($base_url, $query, $page, $name);
$paginator = $this->gen_page_link($base_url, $query, $page, $name);
}
return $paginator;
}
private function build_paginator(int $current_page, int $total_pages, string $base_url, ?string $query): string
private function build_paginator(int $current_page, int $total_pages, string $base_url, ?string $query): HTMLElement
{
$next = $current_page + 1;
$prev = $current_page - 1;
@ -41,8 +43,8 @@ class Themelet extends BaseThemelet
$at_end = ($current_page >= $total_pages -2);
$first_html = $at_start ? "" : $this->gen_page_link($base_url, $query, 1, "1");
$prev_html = $at_start ? "" : $this->gen_page_link($base_url, $query, $prev, "&lt;&lt;");
$next_html = $at_end ? "" : $this->gen_page_link($base_url, $query, $next, "&gt;&gt;");
$prev_html = $at_start ? "" : $this->gen_page_link($base_url, $query, $prev, "<<");
$next_html = $at_end ? "" : $this->gen_page_link($base_url, $query, $next, ">>");
$last_html = $at_end ? "" : $this->gen_page_link($base_url, $query, $total_pages, "$total_pages");
$start = $current_page-2 > 1 ? $current_page-2 : 1;
@ -52,20 +54,20 @@ class Themelet extends BaseThemelet
foreach (range($start, $end) as $i) {
$pages[] = $this->gen_page_link_block($base_url, $query, $i, $current_page, (string)$i);
}
$pages_html = implode(" ", $pages);
$pages_html = $this->implode(" ", $pages);
if (strlen($first_html) > 0) {
if ($first_html) {
$pdots = "...";
} else {
$pdots = "";
}
if (strlen($last_html) > 0) {
if ($last_html) {
$ndots = "...";
} else {
$ndots = "";
}
return "<div id='paginator'>$prev_html $first_html $pdots $pages_html $ndots $last_html $next_html</div>";
return DIV(["id"=>'paginator'], $this->implode(" ", [$prev_html, $first_html, $pdots, $pages_html, $ndots, $last_html, $next_html]));
}
}

View file

@ -4,6 +4,10 @@ declare(strict_types=1);
namespace Shimmie2;
use MicroHTML\HTMLElement;
use function MicroHTML\{A,B,DIV};
class Themelet extends BaseThemelet
{
public function display_paginator(Page $page, string $base, ?string $query, int $page_number, int $total_pages, bool $show_random = false)
@ -15,24 +19,22 @@ class Themelet extends BaseThemelet
$page->add_block(new Block(null, $body, "main", 90));
}
private function gen_page_link(string $base_url, ?string $query, int $page, string $name): string
private function gen_page_link(string $base_url, ?string $query, int $page, string $name): HTMLElement
{
$link = make_link("$base_url/$page", $query);
return "<a href='$link'>$name</a>";
return A(["href"=>make_link("$base_url/$page", $query)], $name);
}
private function gen_page_link_block(string $base_url, ?string $query, int $page, int $current_page, string $name): string
private function gen_page_link_block(string $base_url, ?string $query, int $page, int $current_page, string $name): HTMLElement
{
$paginator = "";
if ($page == $current_page) {
$paginator .= "<b>$page</b>";
$paginator = B($page);
} else {
$paginator .= $this->gen_page_link($base_url, $query, $page, $name);
$paginator = $this->gen_page_link($base_url, $query, $page, $name);
}
return $paginator;
}
private function build_paginator(int $current_page, int $total_pages, string $base_url, ?string $query): string
private function build_paginator(int $current_page, int $total_pages, string $base_url, ?string $query): HTMLElement
{
$next = $current_page + 1;
$prev = $current_page - 1;
@ -41,8 +43,8 @@ class Themelet extends BaseThemelet
$at_end = ($current_page >= $total_pages -2);
$first_html = $at_start ? "" : $this->gen_page_link($base_url, $query, 1, "1");
$prev_html = $at_start ? "" : $this->gen_page_link($base_url, $query, $prev, "&lt;&lt;");
$next_html = $at_end ? "" : $this->gen_page_link($base_url, $query, $next, "&gt;&gt;");
$prev_html = $at_start ? "" : $this->gen_page_link($base_url, $query, $prev, "<<");
$next_html = $at_end ? "" : $this->gen_page_link($base_url, $query, $next, ">>");
$last_html = $at_end ? "" : $this->gen_page_link($base_url, $query, $total_pages, "$total_pages");
$start = $current_page-2 > 1 ? $current_page-2 : 1;
@ -52,20 +54,20 @@ class Themelet extends BaseThemelet
foreach (range($start, $end) as $i) {
$pages[] = $this->gen_page_link_block($base_url, $query, $i, $current_page, (string)$i);
}
$pages_html = implode(" ", $pages);
$pages_html = $this->implode(" ", $pages);
if (strlen($first_html) > 0) {
if ($first_html) {
$pdots = "...";
} else {
$pdots = "";
}
if (strlen($last_html) > 0) {
if ($last_html) {
$ndots = "...";
} else {
$ndots = "";
}
return "<div id='paginator'>$prev_html $first_html $pdots $pages_html $ndots $last_html $next_html</div>";
return DIV(["id"=>"paginator"], $this->implode(" ", [$prev_html, $first_html, $pdots, $pages_html, $ndots, $last_html, $next_html]));
}
}

View file

@ -4,6 +4,10 @@ declare(strict_types=1);
namespace Shimmie2;
use MicroHTML\HTMLElement;
use function MicroHTML\{A,DIV,SPAN};
/**
* Class Themelet
*/
@ -30,27 +34,22 @@ class Themelet extends BaseThemelet
$page->add_block(new Block("Paginator", $body, "main", 90));
}
public function litetheme_gen_page_link(string $base_url, ?string $query, int $page, string $name, ?string $link_class=null): string
public function litetheme_gen_page_link(string $base_url, ?string $query, int $page, string $name, ?string $link_class=null): HTMLElement
{
$link = make_link("$base_url/$page", $query);
return "<a class='$link_class' href='$link'>$name</a>";
return A(["href" => make_link("$base_url/$page", $query), "class" => $link_class], $name);
}
public function litetheme_gen_page_link_block(string $base_url, ?string $query, int $page, int $current_page, string $name): string
public function litetheme_gen_page_link_block(string $base_url, ?string $query, int $page, int $current_page, string $name): HTMLElement
{
$paginator = "";
if ($page == $current_page) {
$link_class = "tab-selected";
} else {
$link_class = "";
}
$paginator .= $this->litetheme_gen_page_link($base_url, $query, $page, $name, $link_class);
return $paginator;
return $this->litetheme_gen_page_link($base_url, $query, $page, $name, $link_class);
}
public function litetheme_build_paginator(int $current_page, int $total_pages, string $base_url, ?string $query, bool $show_random): string
public function litetheme_build_paginator(int $current_page, int $total_pages, string $base_url, ?string $query, bool $show_random): HTMLElement
{
$next = $current_page + 1;
$prev = $current_page - 1;
@ -58,8 +57,8 @@ class Themelet extends BaseThemelet
$at_start = ($current_page <= 1 || $total_pages <= 1);
$at_end = ($current_page >= $total_pages);
$first_html = $at_start ? "<span class='tab'>First</span>" : $this->litetheme_gen_page_link($base_url, $query, 1, "First");
$prev_html = $at_start ? "<span class='tab'>Prev</span>" : $this->litetheme_gen_page_link($base_url, $query, $prev, "Prev");
$first_html = $at_start ? SPAN(["class"=>"tab"], "First") : $this->litetheme_gen_page_link($base_url, $query, 1, "First");
$prev_html = $at_start ? SPAN(["class"=>"tab"], "Prev") : $this->litetheme_gen_page_link($base_url, $query, $prev, "Prev");
$random_html = "";
if ($show_random) {
@ -67,8 +66,8 @@ class Themelet extends BaseThemelet
$random_html = $this->litetheme_gen_page_link($base_url, $query, $rand, "Random");
}
$next_html = $at_end ? "<span class='tab'>Next</span>" : $this->litetheme_gen_page_link($base_url, $query, $next, "Next");
$last_html = $at_end ? "<span class='tab'>Last</span>" : $this->litetheme_gen_page_link($base_url, $query, $total_pages, "Last");
$next_html = $at_end ? SPAN(["class"=>"tab"], "Next") : $this->litetheme_gen_page_link($base_url, $query, $next, "Next");
$last_html = $at_end ? SPAN(["class"=>"tab"], "Last") : $this->litetheme_gen_page_link($base_url, $query, $total_pages, "Last");
$start = $current_page-5 > 1 ? $current_page-5 : 1;
$end = $start+10 < $total_pages ? $start+10 : $total_pages;
@ -77,14 +76,17 @@ class Themelet extends BaseThemelet
foreach (range($start, $end) as $i) {
$pages[] = $this->litetheme_gen_page_link_block($base_url, $query, $i, $current_page, strval($i));
}
$pages_html = implode(" ", $pages);
return "<div class='paginator sfoot'>
$first_html
$prev_html
$random_html
&lt;&lt; $pages_html &gt;&gt;
$next_html $last_html
</div>";
return DIV(
["class"=>"paginator sfoot"],
$first_html,
$prev_html,
$random_html,
"<< ",
$this->implode(" ", $pages),
" >>",
$next_html,
$last_html
);
}
}

View file

@ -4,24 +4,28 @@ declare(strict_types=1);
namespace Shimmie2;
use MicroHTML\HTMLElement;
use function MicroHTML\{A,BR,DIV,IMG,SPAN,rawHTML};
class Themelet extends BaseThemelet
{
public function build_thumb_html(Image $image): string
public function build_thumb_html(Image $image): HTMLElement
{
global $cache, $config;
$cached = $cache->get("thumb-block:{$image->id}");
if (!is_null($cached)) {
return $cached;
return rawHTML($cached);
}
$i_id = (int) $image->id;
$h_view_link = make_link('post/view/'.$i_id);
$h_image_link = $image->get_image_link();
$h_thumb_link = $image->get_thumb_link();
$h_tip = html_escape($image->get_tooltip());
$h_tags = strtolower($image->get_tag_list());
$h_ext = strtolower($image->get_ext());
$id = $image->id;
$view_link = make_link('post/view/'.$id);
$image_link = $image->get_image_link();
$thumb_link = $image->get_thumb_link();
$tip = $image->get_tooltip();
$tags = strtolower($image->get_tag_list());
$ext = strtolower($image->get_ext());
// If file is flash or svg then sets thumbnail to max size.
if ($image->get_mime() === MimeType::FLASH || $image->get_mime() === MimeType::SVG) {
@ -30,14 +34,19 @@ class Themelet extends BaseThemelet
$tsize = get_thumbnail_size($image->width, $image->height);
}
$html = "<div class='shm-thumb thumb' data-ext=\"$h_ext\" data-tags=\"$h_tags\" data-post-id=\"$i_id\"><a class='shm-thumb-link' href='$h_view_link'>".
'<img id="thumb_'.$i_id.'" title="'.$h_tip.'" alt="'.$h_tip.'" height="'.$tsize[1].'" width="'.$tsize[0].'" src="'.$h_thumb_link.'" loading="lazy"></a>'.
'<br><a href="'.$h_image_link.'">File Only</a>'.
"<span class='need-del'> - <a href='#' onclick='image_hash_ban($i_id); return false;'>Ban</a></span>".
"</div>\n";
$html = DIV(
['class'=>'shm-thumb thumb', 'data-ext'=>$ext, 'data-tags'=>$tags, 'data-post-id'=>$id],
A(
['class'=>'shm-thumb-link', 'href'=>$view_link],
IMG(['id'=>"thumb_$id", 'title'=>$tip, 'alt'=>$tip, 'height'=>$tsize[1], 'width'=>$tsize[0], 'src'=>$thumb_link, 'loading'=>'lazy'])
),
BR(),
A(['href'=>$image_link], 'File Only'),
SPAN(['class'=>'need-del'], ' - ', A(['href'=>'#', 'onclick'=>"image_hash_ban($id); return false;"], 'Ban'))
);
// cache for ages; will be cleared in ext/index:onImageInfoSet
$cache->set("thumb-block:{$image->id}", $html, rand(43200, 86400));
$cache->set("thumb-block:{$image->id}", (string)$html, rand(43200, 86400));
return $html;
}