From 8135a1d6791825415d028f954780d8cac8df0532 Mon Sep 17 00:00:00 2001 From: sergiotarxz Date: Wed, 3 May 2023 00:07:43 +0200 Subject: [PATCH] Adding.- For real.- The child logic. --- content/posts/0000005-kiosco-noemi.xml | 2 +- content/posts/0000012-bar-el-bocaton.xml | 2 +- content/posts/0000015-meson-bar-cristobal.xml | 2 +- .../posts/0000021-pizzeria-los-naranjos.xml | 2 +- lib/BurguillosInfo/Categories.pm | 50 +++++- lib/BurguillosInfo/Controller/Page.pm | 2 +- lib/BurguillosInfo/Posts.pm | 162 ++++++++++++------ public/css/styles.css | 2 + public/css/styles.scss | 3 + templates/layouts/default.html.ep | 11 +- templates/page/index.html.ep | 20 ++- 11 files changed, 189 insertions(+), 69 deletions(-) diff --git a/content/posts/0000005-kiosco-noemi.xml b/content/posts/0000005-kiosco-noemi.xml index 620ab01..4ec9b1a 100644 --- a/content/posts/0000005-kiosco-noemi.xml +++ b/content/posts/0000005-kiosco-noemi.xml @@ -3,7 +3,7 @@ 2022-11-11T23:09+00:00 La Tienda de Noemi. La Tienda de Noemi. - comercios + donde-comer tienda-noemi

Hoy os traemos un negocio de nueva apertura, "La Tienda de Noemi" localizado en Calle Albahaca Local 2 ACC, ideal para comer en Burguillos.

diff --git a/content/posts/0000012-bar-el-bocaton.xml b/content/posts/0000012-bar-el-bocaton.xml index 9bb6e1b..4756426 100644 --- a/content/posts/0000012-bar-el-bocaton.xml +++ b/content/posts/0000012-bar-el-bocaton.xml @@ -4,7 +4,7 @@ 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 + donde-comer el-bocaton diff --git a/content/posts/0000015-meson-bar-cristobal.xml b/content/posts/0000015-meson-bar-cristobal.xml index 2c1d79f..786c338 100644 --- a/content/posts/0000015-meson-bar-cristobal.xml +++ b/content/posts/0000015-meson-bar-cristobal.xml @@ -3,7 +3,7 @@ 2022-11-22T09:47+00:00 Mesón - Bar - Cristobal Mesón - Bar - Cristobal - comercios + donde-comer bar-cristobal diff --git a/content/posts/0000021-pizzeria-los-naranjos.xml b/content/posts/0000021-pizzeria-los-naranjos.xml index 2fb0610..ed1e650 100644 --- a/content/posts/0000021-pizzeria-los-naranjos.xml +++ b/content/posts/0000021-pizzeria-los-naranjos.xml @@ -4,7 +4,7 @@ Pizzería los Naranjos. Pizzería los Naranjos. - comercios + donde-comer pizzeria-los-naranjos The front door of the pizzeria diff --git a/lib/BurguillosInfo/Categories.pm b/lib/BurguillosInfo/Categories.pm index d7801d2..8f976cc 100644 --- a/lib/BurguillosInfo/Categories.pm +++ b/lib/BurguillosInfo/Categories.pm @@ -5,6 +5,8 @@ use v5.34.1; use strict; use warnings; +use feature 'signatures'; + use Const::Fast; use Mojo::DOM; use Path::Tiny; @@ -19,7 +21,7 @@ sub new { return bless {}, shift; } -sub Retrieve { +sub Retrieve($self) { if ( defined $cached_categories ) { return $cached_categories; } @@ -28,26 +30,64 @@ sub Retrieve { warn "Bad file $category_file, omiting...", next if !-f $category_file || $category_file !~ /\.xml$/; my $dom = Mojo::DOM->new( $category_file->slurp_utf8 ); - my $title = $dom->at(':root > title')->text + defined(my $title = $dom->at(':root > title')->text) or die "Missing title at $category_file."; - my $description = $dom->at(':root > description')->content + defined(my $description = $dom->at(':root > description')->content) or die "Missing description at $category_file"; - my $slug = $dom->at(':root > slug')->text + defined(my $slug = $dom->at(':root > slug')->text) or die "Missing slug at $category_file"; - my $menu_text = $dom->at(':root > menu_text')->content + defined (my $menu_text = $dom->at(':root > menu_text')->content) or die "Missing menu_text at $category_file"; defined (my $priority = $dom->at(':root > priority')->text) or die "Missing priority at $category_file"; + my $parent_tag = $dom->at(':root > parent'); + my $parent; + if (defined $parent_tag) { + $parent = $parent_tag->content; + } my $category = { title => $title, menu_text => $menu_text, slug => $slug, description => $description, priority => $priority, + ( + (defined $parent) ? + (parent => $parent) : + () + ), }; $cached_categories->{$slug} = $category; } + $self->_AvoidGrandChildCategories($cached_categories); + $self->_PopulateChildrenField($cached_categories); return $cached_categories; } +sub _PopulateChildrenField($self, $categories) { + for my $category_name (keys %$categories) { + my $category = $categories->{$category_name}; + $category->{children} //= []; + my $parent_name = $category->{parent}; + if (!defined $parent_name) { + next; + } + my $parent = $categories->{$parent_name}; + if (!defined $parent) { + die "Category $parent not exists and is the parent of $category_name."; + } + $parent->{children} //= []; + push $parent->{children}->@*, $category; + } +} + +sub _AvoidGrandChildCategories($self, $categories) { + for my $category_slug (keys %$categories) { + my $category = $categories->{$category_slug}; + my $parent = $category->{parent}; + if (defined $parent && defined $categories->{$parent}{parent}) { + die "$category_slug category is grandchild of $categories->{$parent}{parent}) category and this is not allowed."; + } + } +} 1; diff --git a/lib/BurguillosInfo/Controller/Page.pm b/lib/BurguillosInfo/Controller/Page.pm index 4768e76..ff621fa 100644 --- a/lib/BurguillosInfo/Controller/Page.pm +++ b/lib/BurguillosInfo/Controller/Page.pm @@ -64,7 +64,7 @@ sub category_rss { 'Todas las noticias de la categoria de Burguillos.info ' . $category->{title} ); my $link_tag = Mojo::DOM->new_tag( 'link', - 'https://burguillos.info/' . $category->{slur} ); + 'https://burguillos.info/' . $category->{slug} ); $channel_tag->child_nodes->first->append_content($title_tag); $channel_tag->child_nodes->first->append_content($description_tag); $channel_tag->child_nodes->first->append_content($link_tag); diff --git a/lib/BurguillosInfo/Posts.pm b/lib/BurguillosInfo/Posts.pm index 431a1c6..713a45b 100644 --- a/lib/BurguillosInfo/Posts.pm +++ b/lib/BurguillosInfo/Posts.pm @@ -5,9 +5,13 @@ use v5.34.1; use strict; use warnings; +use feature 'signatures'; + use Data::Dumper; use MIME::Base64; +use BurguillosInfo::Categories; + use Const::Fast; use Mojo::DOM; use Path::Tiny; @@ -41,7 +45,13 @@ sub _ReturnCacheFilter { 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} ); + my $date_post; + eval { + $date_post = $iso8601->parse_datetime( $post->{date} ); + }; + if ($@) { + print Data::Dumper::Dumper $post; + } if ( $date_post > $current_date ) { next; } @@ -53,64 +63,114 @@ sub _ReturnCacheFilter { return ( \%posts_by_category_filtered, \%posts_by_slug_filtered ); } +sub _GeneratePostFromFile ( $self, $post_file ) { + warn "Bad file $post_file, omiting...", return + 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."; + my $image_element = $dom->at(':root > img'); + my $image; + + if ( defined $image_element ) { + $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; + } + + return { + 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 ) : () ), + }; +} + +sub _GeneratePostCache ($self) { + $cached_posts_by_category = {}; + $cached_posts_by_slug = {}; + for my $post_file ( sort { $b cmp $a } $POSTS_DIR->children ) { + my $post = $self->_GeneratePostFromFile($post_file); + if (!defined $post) { + next; + } + my $category = $post->{category}; + $cached_posts_by_category->{$category} //= []; + my $slug = $post->{slug}; + my $category_posts = $cached_posts_by_category->{$category}; + $cached_posts_by_slug->{$slug} = $post; + push @$category_posts, $post; + } +} + sub Retrieve { my $self = shift; if ( defined $cached_posts_by_category && defined $cached_posts_by_slug ) { return $self->_ReturnCacheFilter; } - $cached_posts_by_category = {}; - $cached_posts_by_slug = {}; - for my $post_file ( sort { $b cmp $a } $POSTS_DIR->children ) { - 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."; - my $image_element = $dom->at(':root > img'); - my $image; - - if ( defined $image_element ) { - $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, - ( (defined $last_modification_date) ? (last_modification_date => $last_modification_date) : () ), - ( ( defined $image ) ? ( image => $image ) : () ), - }; - $cached_posts_by_category->{$category} //= []; - my $category_posts = $cached_posts_by_category->{$category}; - $cached_posts_by_slug->{$slug} = $post; - push @$category_posts, $post; - } + $self->_GeneratePostCache(); return $self->_ReturnCacheFilter; } +my $cache_all_post_categories = {}; +sub RetrieveAllPostsForCategory ( $self, $category_name ) { + if (defined $cache_all_post_categories->{$category_name}) { + return $cache_all_post_categories->{$category_name}; + } + my $categories = BurguillosInfo::Categories->new->Retrieve; + my $category = $categories->{$category_name}; + my $posts = $self->RetrieveDirectPostsForCategory($category_name); + for my $child_category ( $category->{children}->@* ) { + my $child_category_name = $child_category->{slug}; + push @$posts, + @{$self->RetrieveDirectPostsForCategory($child_category_name)}; + } + @$posts = sort { + DateTime::Format::ISO8601->parse_datetime($b->{date}) <=> + DateTime::Format::ISO8601->parse_datetime($a->{date}) + } @$posts; + $cache_all_post_categories->{$category_name} = $posts; + return $posts; +} + +sub RetrieveDirectPostsForCategory ( $self, $category_name ) { + my ($post_by_category) = $self->Retrieve; + my $categories = BurguillosInfo::Categories->new->Retrieve; + my $category = $categories->{$category_name}; + if ( !defined $category ) { + die "$category_name category does not exists"; + } + my $posts = $post_by_category->{$category_name}; + $posts //= []; + return [@$posts]; +} + sub PostPreviewOg { my $self = shift; my $post = shift; diff --git a/public/css/styles.css b/public/css/styles.css index e24f6c1..d660c65 100644 --- a/public/css/styles.css +++ b/public/css/styles.css @@ -32,6 +32,8 @@ body { left: 0%; height: 100%; width: 100%; } + body div.page-contents div.child-categories-mobile a { + padding-left: 2.5rem; } body div.page-contents img { max-width: 100%; margin-left: auto; diff --git a/public/css/styles.scss b/public/css/styles.scss index ddff8bd..5597a18 100644 --- a/public/css/styles.scss +++ b/public/css/styles.scss @@ -44,6 +44,9 @@ body { top: 0%; left: 0%; height: 100%; + div.child-categories-mobile a { + padding-left: 2.5rem; + } width: 100%; img { max-width: 100%; diff --git a/templates/layouts/default.html.ep b/templates/layouts/default.html.ep index 4c01dc7..36491a3 100644 --- a/templates/layouts/default.html.ep +++ b/templates/layouts/default.html.ep @@ -25,6 +25,9 @@ $categories->{$a}{priority} <=> $categories->{$b}{priority} } keys %$categories) { my $category = $categories->{$category_key}; + if (defined $category->{parent}) { + next; + } my $selected = defined($current_slug) && $category->{slug} eq $current_slug; %>" href="<%= '/'.$category->{slug} %>"><%==$category->{menu_text}%><% } @@ -43,7 +46,13 @@ $categories->{$a}{priority} <=> $categories->{$b}{priority} } keys %$categories) { my $category = $categories->{$category_key}; - %><%==$category->{menu_text}%><% + if (defined $category->{parent}) { + next; + } + %><%==$category->{menu_text}%>
<% + for my $child_category ($category->{children}->@*) { + %>{slug}"%>">* <%==$child_category->{menu_text}%><% + }%>
<% } %> diff --git a/templates/page/index.html.ep b/templates/page/index.html.ep index 03b64d2..77a7831 100644 --- a/templates/page/index.html.ep +++ b/templates/page/index.html.ep @@ -1,3 +1,4 @@ +% use Data::Dumper; % use DateTime::Format::ISO8601; % % use Mojo::DOM; @@ -10,12 +11,20 @@

<%= $current_category->{title} %>

<%== $current_category->{description} %> +% my $children_categories = $current_category->{children}; +% if (@$children_categories) { +

Quizás te interese.

+% for my $child_category (@$children_categories) { +

<%==$child_category->{title}%>

+% } +% }

Artículos

-% my ($cached_posts_by_category) = BurguillosInfo::Posts->new->Retrieve; -% my $category_post = $cached_posts_by_category->{$current_category->{slug}}; -% if (defined $category_post) { -% for my $post (@$category_post) { +% my ($category_posts) = BurguillosInfo::Posts->new->RetrieveAllPostsForCategory($current_category->{slug}); +% unless (@$category_posts) { +

Parece que aun no hay artículos.

+% } +% for my $post (@$category_posts) {
<% my $date_article = DateTime::Format::ISO8601->parse_datetime($post->{date}); @@ -33,9 +42,6 @@ %>

<%=$text%>

Escrito por <%=$post->{author}%>

% } -% } else { -

Parece que aun no hay artículos.

-% }

Suscribete a esta categoría.