burguillos.info/lib/BurguillosInfo/Posts.pm

265 lines
7.9 KiB
Perl
Raw Normal View History

2022-11-09 13:31:58 +01:00
package BurguillosInfo::Posts;
use v5.34.1;
use strict;
use warnings;
2022-11-13 02:01:24 +01:00
use Data::Dumper;
2022-11-13 04:37:53 +01:00
use MIME::Base64;
2022-11-13 02:01:24 +01:00
2022-11-09 13:31:58 +01:00
use Const::Fast;
use Mojo::DOM;
use Path::Tiny;
use DateTime::Format::ISO8601;
use DateTime;
2022-11-13 02:01:24 +01:00
use SVG;
use Capture::Tiny qw/capture/;
2022-11-09 13:31:58 +01:00
2022-11-14 00:30:05 +01:00
const my $CURRENT_FILE => __FILE__;
const my $ROOT_PROJECT => path($CURRENT_FILE)->parent->parent->parent;
const my $PUBLIC_DIR => $ROOT_PROJECT->child('public');
const my $POSTS_DIR => $ROOT_PROJECT->child('content/posts');
const my $BURGUILLOS_LOGO => $PUBLIC_DIR->child('img/burguillos.png');
const my $SVG_WIDTH => 1200;
const my $SVG_HEIGHT => 627;
const my $SVG_EMBEDDED_IMAGE_MAX_WIDTH => 1000;
const my $SVG_EMBEDDED_IMAGE_MAX_HEIGHT => 200;
2022-11-09 13:31:58 +01:00
my $cached_posts_by_category;
my $cached_posts_by_slug;
sub new {
return bless {}, shift;
}
sub _ReturnCacheFilter {
my $self = shift;
my %posts_by_category_filtered;
my %posts_by_slug_filtered;
my $iso8601 = DateTime::Format::ISO8601->new;
my $current_date = DateTime->now;
for my $category ( keys %$cached_posts_by_category ) {
for my $post ( @{ $cached_posts_by_category->{$category} } ) {
my $date_post = $iso8601->parse_datetime( $post->{date} );
if ( $date_post > $current_date ) {
next;
}
$posts_by_slug_filtered{ $post->{slug} } = $post;
$posts_by_category_filtered{ $post->{category} } //= [];
push @{ $posts_by_category_filtered{ $post->{category} } }, $post;
}
}
2022-11-19 20:00:37 +01:00
return ( \%posts_by_category_filtered, \%posts_by_slug_filtered );
}
2022-11-09 13:31:58 +01:00
sub Retrieve {
my $self = shift;
2022-11-09 13:31:58 +01:00
if ( defined $cached_posts_by_category && defined $cached_posts_by_slug ) {
return $self->_ReturnCacheFilter;
2022-11-09 13:31:58 +01:00
}
$cached_posts_by_category = {};
$cached_posts_by_slug = {};
2022-11-10 23:49:13 +01:00
for my $post_file ( sort { $b cmp $a } $POSTS_DIR->children ) {
2022-11-09 13:31:58 +01:00
warn "Bad file $post_file, omiting...", next
if !-f $post_file || $post_file !~ /\.xml$/;
my $dom = Mojo::DOM->new( $post_file->slurp_utf8 );
my $title = $dom->at(':root > title')->text
or die "Missing title at $post_file.";
my $author = $dom->at(':root > author')->text
or die "Missing author at $post_file.";
my $date = $dom->at(':root > date')->text
or die "Missing date at $post_file.";
my $ogdesc = $dom->at(':root > ogdesc')->text
or die "Missing ogdesc at $post_file";
my $category = $dom->at(':root > category')->text
or die "Missing category at $post_file.";
my $slug = $dom->at(':root > slug')->text
or die "Missing slug at $post_file.";
my $content = $dom->at(':root > content')->content
or die "Missing content at $post_file.";
2022-11-14 00:30:05 +01:00
my $image_element = $dom->at(':root > img');
my $image;
if ( defined $image_element ) {
$image = $image_element->attr->{src};
}
2022-11-09 13:31:58 +01:00
my $last_modification_date_element =
$dom->at(':root > last_modification_date');
my $last_modification_date;
if ( defined $last_modification_date_element ) {
$last_modification_date = $last_modification_date_element->content;
}
2022-11-09 13:31:58 +01:00
my $post = {
title => $title,
author => $author,
date => $date,
ogdesc => $ogdesc,
category => $category,
slug => $slug,
content => $content,
( (defined $last_modification_date) ? (last_modification_date => $last_modification_date) : () ),
2022-11-14 00:30:05 +01:00
( ( defined $image ) ? ( image => $image ) : () ),
2022-11-09 13:31:58 +01:00
};
2022-11-13 02:01:24 +01:00
$cached_posts_by_category->{$category} //= [];
2022-11-09 13:31:58 +01:00
my $category_posts = $cached_posts_by_category->{$category};
2022-11-13 02:01:24 +01:00
$cached_posts_by_slug->{$slug} = $post;
push @$category_posts, $post;
}
return $self->_ReturnCacheFilter;
2022-11-13 02:01:24 +01:00
}
sub PostPreviewOg {
my $self = shift;
my $post = shift;
my $title = $post->{title};
my $content = $post->{content};
my $dom = Mojo::DOM->new($content);
$content = $dom->all_text;
my @content_divided_in_lines = split /\n/, $content;
my @new_content;
2022-11-13 03:53:58 +01:00
my $n_chars_per_line = 70;
2022-11-13 02:01:24 +01:00
for my $line (@content_divided_in_lines) {
if ( length($line) <= $n_chars_per_line ) {
push @new_content, $line;
next;
}
2022-11-14 00:30:05 +01:00
my $last_word = '';
2022-11-13 02:01:24 +01:00
while ( $line =~ /(.{1,${n_chars_per_line}})/g ) {
2022-11-14 00:30:05 +01:00
my $new_line = $last_word . $1;
$new_line =~ s/(\S*)$//;
$last_word = $1;
2022-11-13 02:01:24 +01:00
push @new_content, $new_line;
}
2022-11-14 00:30:05 +01:00
if ($last_word) {
$new_content[$#new_content] .= $last_word;
}
2022-11-13 02:01:24 +01:00
}
2022-11-13 03:53:58 +01:00
2022-11-14 00:30:05 +01:00
my $svg =
$self->_GenerateSVGPostPreview( $title, \@new_content, $post->{image} );
2022-11-19 20:00:37 +01:00
my ( $stdout, $stderr ) = capture {
open my $fh, '|-', qw{convert /dev/stdin png:fd:1};
binmode $fh, 'utf8';
2022-11-19 20:00:37 +01:00
print $fh $svg;
close $fh;
};
say STDERR $stderr;
return $stdout;
2022-11-14 00:30:05 +01:00
}
sub _AttachImageSVG {
my $self = shift;
my $svg = shift;
my $image = shift;
$image = $PUBLIC_DIR->child( './' . $image );
my ( $stdout, $stderr, $error ) = capture {
system qw/identify -format "%wx%h"/, $image;
};
if ($error) {
warn "$image not recognized by identify.";
return;
}
my ( $width, $height ) = $stdout =~ /^"(\d+)x(\d+)"$/;
if ( $height > $SVG_EMBEDDED_IMAGE_MAX_HEIGHT ) {
$width /= $height / $SVG_EMBEDDED_IMAGE_MAX_HEIGHT;
$width = int($width);
2022-11-14 00:30:05 +01:00
$height = $SVG_EMBEDDED_IMAGE_MAX_HEIGHT;
}
if ( $width > $SVG_EMBEDDED_IMAGE_MAX_WIDTH ) {
$height /= $width / $SVG_EMBEDDED_IMAGE_MAX_WIDTH;
$height = int($height);
$width = $SVG_EMBEDDED_IMAGE_MAX_WIDTH;
2022-11-14 00:30:05 +01:00
}
my $x = int( ( $SVG_WIDTH / 2 ) - ( $width / 2 ) );
my $y = 90;
2022-11-14 00:30:05 +01:00
my ($output) = capture {
system qw/file --mime-type/, $image;
2022-11-14 00:30:05 +01:00
};
my ($format) = $output =~ /(\S+)$/;
$svg->image(
x => $x,
y => $y,
width => $width,
height => $height,
-href => "data:$format;base64," . encode_base64( $image->slurp )
2022-11-14 00:30:05 +01:00
);
return $y + $height + 50;
2022-11-13 02:01:24 +01:00
}
sub _GenerateSVGPostPreview {
my $self = shift;
my $title = shift;
my $content = shift;
2022-11-14 00:30:05 +01:00
my $image = shift;
2022-11-13 02:01:24 +01:00
my @content = @$content;
2022-11-14 00:30:05 +01:00
my $svg = SVG->new( width => $SVG_WIDTH, height => $SVG_HEIGHT );
2022-11-13 02:01:24 +01:00
$svg->rect(
x => 0,
y => 0,
width => 1200,
height => 50,
style => { fill => 'blueviolet' }
);
$svg->rect(
x => 0,
y => 50,
width => 1200,
2022-11-13 02:26:27 +01:00
height => 627,
2022-11-13 02:01:24 +01:00
style => { fill => '#F8F8FF' }
);
my $group = $svg->group(
id => 'group',
style => {
font => 'Arial',
'font-size' => 30,
}
);
2022-11-13 04:37:53 +01:00
$group->image(
2022-11-14 00:30:05 +01:00
x => 10,
y => 5,
width => 40,
height => 40,
-href => 'data:image/png;base64,'
. encode_base64( $BURGUILLOS_LOGO->slurp )
2022-11-13 04:37:53 +01:00
);
2022-11-13 02:01:24 +01:00
$group->text(
2022-11-14 00:30:05 +01:00
x => 60,
y => 40,
style => { 'font-size' => 50, fill => '#f2eb8c' }
2022-11-13 02:01:24 +01:00
)->cdata('Burguillos.info');
2022-11-14 00:30:05 +01:00
my $new_y;
2022-11-13 02:01:24 +01:00
2022-11-14 00:30:05 +01:00
if ( defined $image ) {
$new_y = $self->_AttachImageSVG( $group, $image );
}
$new_y //= 100;
2022-11-13 02:01:24 +01:00
$group->text(
x => 10,
2022-11-14 00:30:05 +01:00
y => $new_y,
2022-11-13 02:01:24 +01:00
style => { 'font-size' => 50 }
)->cdata($title);
my $n = 0;
for my $line (@content) {
$group->text(
x => 10,
2022-11-14 00:30:05 +01:00
y => $new_y + 40 + ( 30 * $n ),
2022-11-13 02:01:24 +01:00
style => { 'font-size' => 38 }
)->cdata($line);
$n++;
2022-11-09 13:31:58 +01:00
}
2022-11-19 20:00:37 +01:00
path($ROOT_PROJECT)->child('a.svg')->spew_utf8( $svg->xmlify );
2022-11-13 02:01:24 +01:00
return $svg->xmlify;
2022-11-09 13:31:58 +01:00
}
1;