From 77d10c8c7034c137f3b49ba0c655192c0936bec4 Mon Sep 17 00:00:00 2001 From: swag Date: Tue, 27 Sep 2022 14:37:55 -0400 Subject: [PATCH] Truncate threads > 500 characters in the thread list page --- lib/PostText.pm | 22 ++++++++++++++++------ templates/list_page.html.ep | 2 +- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/lib/PostText.pm b/lib/PostText.pm index c2a4ca7..45af8a5 100644 --- a/lib/PostText.pm +++ b/lib/PostText.pm @@ -14,14 +14,24 @@ sub startup($self) { $self->plugin('TagHelpers::Pagination'); $self->plugin(AssetPack => {pipes => [qw{Css Combine}]}); - $self->helper(pg => sub { - state $pg = Mojo::Pg->new($self->config->{$self->mode}{'pg_string'}) + # Helpers + $self->helper(pg => sub ($c) { + state $pg = Mojo::Pg->new($c->config->{$self->mode}{'pg_string'}) }); - $self->helper(thread => sub { - state $thread = PostText::Model::Thread->new(pg => shift->pg) + + $self->helper(thread => sub ($c) { + state $thread = PostText::Model::Thread->new(pg => $c->pg) }); - $self->helper(remark => sub { - state $remark = PostText::Model::Remark->new(pg => shift->pg) + + $self->helper(remark => sub ($c) { + state $remark = PostText::Model::Remark->new(pg => $c->pg) + }); + + $self->helper(truncate_text => sub ($c, $input_text) { + my $truncated_text = 500 < length($input_text) + ? substr($input_text, 0, 500) . '...' : $input_text; + + return $truncated_text; }); # Finish configuring some things diff --git a/templates/list_page.html.ep b/templates/list_page.html.ep index c4454e4..5472b3d 100644 --- a/templates/list_page.html.ep +++ b/templates/list_page.html.ep @@ -11,7 +11,7 @@

<%= $thread->{'title'} %>

<%= $thread->{'date'} %>

<%= $thread->{'author'} %>
-

<%= $thread->{'body'} %>

+

<%= truncate_text $thread->{'body'} %>