Adding.- For real.- The child logic.
This commit is contained in:
parent
4d7e0da333
commit
8135a1d679
@ -3,7 +3,7 @@
|
||||
<date>2022-11-11T23:09+00:00</date>
|
||||
<title>La Tienda de Noemi.</title>
|
||||
<ogdesc>La Tienda de Noemi.</ogdesc>
|
||||
<category>comercios</category>
|
||||
<category>donde-comer</category>
|
||||
<slug>tienda-noemi</slug>
|
||||
<content>
|
||||
<p>Hoy os traemos un negocio de nueva apertura, "La Tienda de Noemi" localizado en Calle Albahaca Local 2 ACC, ideal para comer en Burguillos.</p>
|
||||
|
@ -4,7 +4,7 @@
|
||||
<last_modification_date>2023-05-02T00:00+00:00</last_modification_date>
|
||||
<title>El Bocatón - Café - Bar - Pastelería - Pizzeria - Hamburguesería.</title>
|
||||
<ogdesc>El Bocatón - Café - Bar - Pastelería - Pizzeria - Hamburguesería.</ogdesc>
|
||||
<category>comercios</category>
|
||||
<category>donde-comer</category>
|
||||
<img src="/img/bocaton-preview.png"/>
|
||||
<slug>el-bocaton</slug>
|
||||
<content>
|
||||
|
@ -3,7 +3,7 @@
|
||||
<date>2022-11-22T09:47+00:00</date>
|
||||
<title>Mesón - Bar - Cristobal</title>
|
||||
<ogdesc>Mesón - Bar - Cristobal</ogdesc>
|
||||
<category>comercios</category>
|
||||
<category>donde-comer</category>
|
||||
<slug>bar-cristobal</slug>
|
||||
<img src="/img/cristobal-preview.png"/>
|
||||
<content>
|
||||
|
@ -4,7 +4,7 @@
|
||||
<title>Pizzería los Naranjos.</title>
|
||||
<ogdesc>Pizzería los Naranjos.</ogdesc>
|
||||
<img src="/img/pizzeria-los-naranjos-preview.png"/>
|
||||
<category>comercios</category>
|
||||
<category>donde-comer</category>
|
||||
<slug>pizzeria-los-naranjos</slug>
|
||||
<content>
|
||||
<img style="border: 1px black solid;" src="/img/pizzeria-los-naranjos.jpg" alt="The front door of the pizzeria"/>
|
||||
|
@ -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;
|
||||
|
@ -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);
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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%;
|
||||
|
@ -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;
|
||||
%><a class="<%=$selected && "selected" %>" href="<%= '/'.$category->{slug} %>"><%==$category->{menu_text}%></a><%
|
||||
}
|
||||
@ -43,7 +46,13 @@
|
||||
$categories->{$a}{priority} <=> $categories->{$b}{priority}
|
||||
} keys %$categories) {
|
||||
my $category = $categories->{$category_key};
|
||||
%><a href="<%= '/'.$category->{slug} %>"><%==$category->{menu_text}%></a><%
|
||||
if (defined $category->{parent}) {
|
||||
next;
|
||||
}
|
||||
%><a href="<%= '/'.$category->{slug} %>"><%==$category->{menu_text}%></a><div class="child-categories-mobile"><%
|
||||
for my $child_category ($category->{children}->@*) {
|
||||
%><a href="<%="/$child_category->{slug}"%>">* <%==$child_category->{menu_text}%></a><%
|
||||
}%></div><%
|
||||
}
|
||||
%></nav>
|
||||
|
||||
|
@ -1,3 +1,4 @@
|
||||
% use Data::Dumper;
|
||||
% use DateTime::Format::ISO8601;
|
||||
%
|
||||
% use Mojo::DOM;
|
||||
@ -10,12 +11,20 @@
|
||||
<div class="description">
|
||||
<h2><%= $current_category->{title} %></h2>
|
||||
<%== $current_category->{description} %>
|
||||
% my $children_categories = $current_category->{children};
|
||||
% if (@$children_categories) {
|
||||
<h3>Quizás te interese.</h3>
|
||||
% for my $child_category (@$children_categories) {
|
||||
<p><a href="/<%=$child_category->{slug}%>"><%==$child_category->{title}%></a></p>
|
||||
% }
|
||||
% }
|
||||
<h3>Artículos</h3>
|
||||
<div class="articles">
|
||||
% 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) {
|
||||
<p>Parece que aun no hay artículos.</p>
|
||||
% }
|
||||
% for my $post (@$category_posts) {
|
||||
<a href="/posts/<%=$post->{slug}%>">
|
||||
<article><%
|
||||
my $date_article = DateTime::Format::ISO8601->parse_datetime($post->{date});
|
||||
@ -33,9 +42,6 @@
|
||||
%><p><%=$text%></p><p class="author">Escrito por <%=$post->{author}%></p></article>
|
||||
</a>
|
||||
% }
|
||||
% } else {
|
||||
<p>Parece que aun no hay artículos.</p>
|
||||
% }
|
||||
|
||||
</div>
|
||||
<h3>Suscribete a esta categoría.</h3>
|
||||
|
Loading…
Reference in New Issue
Block a user