Cropping the title if too long with the maximum number of possible

chars.
This commit is contained in:
Sergiotarxz 2023-09-17 06:43:45 +02:00
parent 4a10dcb0e5
commit 75fb55ca54
2 changed files with 47 additions and 37 deletions

View File

@ -14,8 +14,7 @@
<h2>Información de contacto para preparación de pedidos.</h2>
<p>El Bar-Restaurante Durán - Centro de día,
ubicado en avenida de Andalucía número 4 ofrece una propuesta única con sus platos de calidad excepcional.</p>
<p>El Bar-Restaurante Durán - Centro de día, ubicado en avenida de Andalucía número 4 ofrece una propuesta única con sus platos de calidad excepcional.</p>
<p>Os recomendamos probar los crepes de pollo, desde Burguillos.info los hemos probado y hemos quedado encantados por lo deliciosos que están.</p>

View File

@ -23,13 +23,20 @@ const my $SVG_HEIGHT => 627;
const my $SVG_EMBEDDED_IMAGE_MAX_WIDTH => 1200;
const my $SVG_EMBEDDED_IMAGE_MAX_HEIGHT => 400;
sub Generate($self, $title, $content, $image_file = undef, $image_bottom_preview = undef) {
sub Generate (
$self, $title, $content,
$image_file = undef,
$image_bottom_preview = undef
)
{
my $dom = Mojo::DOM->new($content);
$content = $dom->all_text;
my $svg =
$self->_GenerateSVGPreview( $title, $self->_DivideTextContentInLines($content), $image_file, $image_bottom_preview );
my $svg = $self->_GenerateSVGPreview(
$self->_DivideTextContentInLines($title, 62)->[0],
$self->_DivideTextContentInLines($content),
$image_file, $image_bottom_preview
);
return $self->_SVGToPNG($svg);
}
@ -61,7 +68,6 @@ sub _GenerateSVGPreviewHeaderBar($self, $svg, $group) {
style => { fill => '#F8F8FF' }
);
my $burguillos_logo_png = path( $self->_ToPng($BURGUILLOS_LOGO) );
say $burguillos_logo_png;
say '' . $burguillos_logo_png;
@ -80,7 +86,9 @@ sub _GenerateSVGPreviewHeaderBar($self, $svg, $group) {
)->cdata('Burguillos.info');
}
sub _GenerateSVGPreview($self, $title, $content, $image_file, $image_bottom_preview) {
sub _GenerateSVGPreview ( $self, $title, $content, $image_file,
$image_bottom_preview )
{
my @content = @$content;
my $svg = SVG->new( width => $SVG_WIDTH, height => $SVG_HEIGHT );
@ -97,7 +105,8 @@ sub _GenerateSVGPreview($self, $title, $content, $image_file, $image_bottom_prev
my $new_y;
if ( defined $image_file ) {
$new_y = $self->_AttachImageSVG( $svg, $group, $image_file, $image_bottom_preview );
$new_y = $self->_AttachImageSVG( $svg, $group, $image_file,
$image_bottom_preview );
}
$new_y //= 100;
@ -133,11 +142,10 @@ sub _SVGToPNG($self, $svg) {
return $stdout;
}
sub _DivideTextContentInLines($self, $content) {
sub _DivideTextContentInLines ( $self, $content, $n_chars_per_line = 79 ) {
$content =~ s/(\s)\s+/$1/g;
my @content_divided_in_lines = split /\n/, $content;
my @new_content;
my $n_chars_per_line = 79;
for my $line (@content_divided_in_lines) {
if ( length($line) <= $n_chars_per_line ) {
@ -158,7 +166,8 @@ sub _DivideTextContentInLines($self, $content) {
return \@new_content;
}
sub _AttachImageSVG($self, $svg, $group, $image_file, $image_bottom_preview) {
sub _AttachImageSVG ( $self, $svg, $group, $image_file, $image_bottom_preview )
{
$image_file = $PUBLIC_DIR->child( './' . $image_file );
$image_file = path( $self->_ToPng($image_file) );
my ( $stdout, $stderr, $error ) = capture {
@ -191,7 +200,9 @@ sub _AttachImageSVG($self, $svg, $group, $image_file, $image_bottom_preview) {
my $x = 0;
my $y_image = 50 - $height_complete_image + $height;
if (defined $image_bottom_preview && $height_complete_image > $SVG_EMBEDDED_IMAGE_MAX_HEIGHT) {
if ( defined $image_bottom_preview
&& $height_complete_image > $SVG_EMBEDDED_IMAGE_MAX_HEIGHT )
{
$y_image += $height_complete_image - $image_bottom_preview;
}
my $y = 50;