diff --git a/burguillos_info.json.example b/burguillos_info.json.example index bf65a22..c914b43 100644 --- a/burguillos_info.json.example +++ b/burguillos_info.json.example @@ -3,5 +3,6 @@ "bcrypt_pass_stats": ["change_for_bcrypted_password"], "db": { "database": "example" - } + }, + "base_url": "https://burguillos.info" } diff --git a/content/posts/0000012-bar-el-bocaton.xml b/content/posts/0000012-bar-el-bocaton.xml index 12c4538..9bb6e1b 100644 --- a/content/posts/0000012-bar-el-bocaton.xml +++ b/content/posts/0000012-bar-el-bocaton.xml @@ -1,6 +1,7 @@ Burguillos.info 2022-11-18T00:00+00:00 + 2023-05-02T00:00+00:00 El Bocatón - Café - Bar - Pastelería - Pizzeria - Hamburguesería. El Bocatón - Café - Bar - Pastelería - Pizzeria - Hamburguesería. comercios @@ -130,9 +131,5 @@

Aprovechamos este espacio final tras terminar de listar el menú para adjuntaros más imágenes del local y desearos una feliz comida.

- Exterior del Bar Bocatón. - Exterior del Bar Bocatón. - Exterior del Bar Bocatón. -
diff --git a/lib/BurguillosInfo.pm b/lib/BurguillosInfo.pm index 3da92a0..708cd87 100644 --- a/lib/BurguillosInfo.pm +++ b/lib/BurguillosInfo.pm @@ -28,6 +28,7 @@ sub startup ($self) { # Normal route to controller $r->get('/')->to('Page#index'); + $r->get('/sitemap.xml')->to('Sitemap#sitemap'); # $r->get('/:post')->to('Page#post'); $r->get('/stats')->to('Metrics#stats'); diff --git a/lib/BurguillosInfo/Controller/Sitemap.pm b/lib/BurguillosInfo/Controller/Sitemap.pm new file mode 100644 index 0000000..ad4ccf7 --- /dev/null +++ b/lib/BurguillosInfo/Controller/Sitemap.pm @@ -0,0 +1,111 @@ +package BurguillosInfo::Controller::Sitemap; + +use v5.34.1; + +use strict; +use warnings; + +use BurguillosInfo::Categories; +use BurguillosInfo::Posts; + +use DateTime::Format::ISO8601; + +use Mojo::Base 'Mojolicious::Controller', '-signatures'; + +sub sitemap ($self) { + my $categories = BurguillosInfo::Categories->new->Retrieve; + my $dom = Mojo::DOM->new_tag( + 'urlset', + xmlns => 'http://www.sitemaps.org/schemas/sitemap/0.9', + undef + ); + $dom->xml(1); + for my $category_key ( keys %$categories ) { + $self->_append_category_dom( $dom, $category_key, $categories ); + } + $self->render(text => $dom, format => 'xml'); +} + +sub _append_category_dom ( $self, $dom, $category_key, $categories ) { + my $category = $categories->{$category_key}; + my $slug = $category->{'slug'}; + my $date_publish_category; + my $date_last_modification_category; + + my ( $posts_categories, $posts_slug ) = + BurguillosInfo::Posts->new->Retrieve; + for my $post ( $posts_categories->{$category_key}->@* ) { + ( $date_publish_category, $date_last_modification_category ) = + _get_dates_for_category( $date_publish_category, + $date_last_modification_category, $post ); + my $url_post = $self->_generate_url_for_post($post); + $dom->child_nodes->first->append_content($url_post); + } + my $url = Mojo::DOM->new_tag('url'); + my $base_url = $self->config('base_url'); + my $location_tag = Mojo::DOM->new_tag( loc => "$base_url/$slug" ); + my $final_date_last_modification_category = + _compare_dates_return_most_recent( $date_publish_category, + $date_last_modification_category ); + my $last_modification_tag = + Mojo::DOM->new_tag( lastmod => $final_date_last_modification_category ); + my $priority_tag = Mojo::DOM->new_tag( priority => 0.6 ); + $url->child_nodes->first->append_content($location_tag); + $url->child_nodes->first->append_content($last_modification_tag); + $url->child_nodes->first->append_content($priority_tag); + + $dom->child_nodes->first->append_content($url); +} + +sub _generate_url_for_post ( $self, $post ) { + my $url_tag = Mojo::DOM->new_tag('url'); + my $date = $post->{date}; + my $date_last_modification_post = $post->{last_modification_date}; + my $final_date_last_modification_post = + _compare_dates_return_most_recent( $date, $date_last_modification_post ); + my $base_url = $self->config('base_url'); + my $url_resource = "$base_url/@{[$post->{slug}]}"; + my $last_modification_tag = + Mojo::DOM->new_tag( lastmod => $final_date_last_modification_post ); + my $location_tag = Mojo::DOM->new_tag( loc => $url_resource ); + $url_tag->child_nodes->first->append_content($location_tag); + $url_tag->child_nodes->first->append_content($last_modification_tag); + return $url_tag; +} + +my $error_no_dates = "Undefined dates"; + +sub _get_dates_for_category ( $current_date_publish, + $current_date_modification, $post ) +{ + my @return_list; + @return_list = ( + _compare_dates_return_most_recent( + $current_date_publish, $post->{date} + ), + _compare_dates_return_most_recent( + $current_date_publish, $post->{last_modification_date} + ), + ); + return @return_list; + +} + +sub _compare_dates_return_most_recent ( $date_a, $date_b ) { + if ( !defined $date_a && !defined $date_b ) { + return undef; + } + if ( !defined $date_a ) { + return $date_b; + } + if ( !defined $date_b ) { + return $date_a; + } + my $date_a_dt = DateTime::Format::ISO8601->parse_datetime($date_a); + my $date_b_dt = DateTime::Format::ISO8601->parse_datetime($date_b); + if ( $date_a_dt >= $date_b_dt ) { + return $date_a; + } + return $date_b; +} +1; diff --git a/lib/BurguillosInfo/Posts.pm b/lib/BurguillosInfo/Posts.pm index f2e71d9..431a1c6 100644 --- a/lib/BurguillosInfo/Posts.pm +++ b/lib/BurguillosInfo/Posts.pm @@ -85,14 +85,22 @@ sub Retrieve { $image = $image_element->attr->{src}; } + 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; + } + my $post = { - title => $title, - author => $author, - date => $date, - ogdesc => $ogdesc, - category => $category, - slug => $slug, - content => $content, + title => $title, + author => $author, + date => $date, + ogdesc => $ogdesc, + category => $category, + slug => $slug, + content => $content, + ( (defined $last_modification_date) ? (last_modification_date => $last_modification_date) : () ), ( ( defined $image ) ? ( image => $image ) : () ), }; $cached_posts_by_category->{$category} //= []; @@ -136,7 +144,7 @@ sub PostPreviewOg { $self->_GenerateSVGPostPreview( $title, \@new_content, $post->{image} ); my ( $stdout, $stderr ) = capture { open my $fh, '|-', qw{convert /dev/stdin png:fd:1}; - binmode $fh, 'utf8'; + binmode $fh, 'utf8'; print $fh $svg; close $fh; };