From 986bd0149935fe4696f956d6e9a0c0abb9b075e7 Mon Sep 17 00:00:00 2001 From: sergiotarxz Date: Thu, 10 Nov 2022 10:49:53 +0100 Subject: [PATCH] Adding rss suscriptions. --- lib/BurguillosInfo.pm | 1 + lib/BurguillosInfo/Controller/Page.pm | 83 +++++++++++++++++++++++++++ public/css/styles.scss | 12 ++++ public/img/rss.svg | 7 +++ templates/layouts/default.html.ep | 5 +- templates/page/index.html.ep | 6 ++ 6 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 public/img/rss.svg diff --git a/lib/BurguillosInfo.pm b/lib/BurguillosInfo.pm index 42bfffe..3116104 100644 --- a/lib/BurguillosInfo.pm +++ b/lib/BurguillosInfo.pm @@ -11,6 +11,7 @@ sub startup ($self) { # Normal route to controller $r->get('/')->to('Page#index'); # $r->get('/:post')->to('Page#post'); + $r->get('/<:category>.rss')->to('Page#category_rss'); $r->get('/:category')->to('Page#category'); $r->get('/posts/:slug')->to('Page#post'); } diff --git a/lib/BurguillosInfo/Controller/Page.pm b/lib/BurguillosInfo/Controller/Page.pm index dee200b..8d38bbb 100644 --- a/lib/BurguillosInfo/Controller/Page.pm +++ b/lib/BurguillosInfo/Controller/Page.pm @@ -12,6 +12,9 @@ use Data::Dumper; use Mojo::Base 'Mojolicious::Controller'; +use DateTime::Format::ISO8601; +use DateTime::Format::Mail; + sub index { my $self = shift; my $categories = BurguillosInfo::Categories->new->Retrieve; @@ -24,6 +27,86 @@ sub index { ); } +sub category_rss { + my $self = shift; + my $categories = BurguillosInfo::Categories->new->Retrieve; + my $category_name = $self->param('category'); + my $current_category = $categories->{$category_name}; + my ( $posts_categories, $posts_slug ) = + BurguillosInfo::Posts->new->Retrieve; + if ( !defined $current_category && $category_name ne 'all' ) { + $self->render( template => '404', status => 404 ); + return; + } + my $dom = Mojo::DOM->new_tag( 'rss', version => '2.0', undef ); + my $channel_tag = Mojo::DOM->new_tag('channel'); + if ( $category_name eq 'all' ) { + my $title_tag = Mojo::DOM->new_tag( 'title', 'Burguillos.info' ); + my $description_tag = Mojo::DOM->new_tag( 'description', + 'Todas las noticias de Burguillos.info.' ); + my $link_tag = Mojo::DOM->new_tag( 'link', 'https://burguillos.info/' ); + $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); + for my $category ( keys %$posts_categories ) { + my $posts = $posts_categories->{$category}; + for my $post (@$posts) { + $channel_tag->child_nodes->first->append_content( + _post_to_rss($post) ); + } + } + } + else { + my $category = $current_category; + my $title_tag = Mojo::DOM->new_tag( 'title', + "Burguillos.info - " . $category->{title} ); + my $description_tag = Mojo::DOM->new_tag( 'description', + 'Todas las noticias de la categoria de Burguillos.info ' + . $category->{title} ); + my $link_tag = Mojo::DOM->new_tag( 'link', + 'https://burguillos.info/' . $category->{slur} ); + $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); + my $posts = $posts_categories->{$category_name}; + + for my $post (@$posts) { + $channel_tag->child_nodes->first->append_content( + _post_to_rss($post) ); + } + + } + $dom->child_nodes->first->append_content($channel_tag); + $self->render( + format => 'xml', + text => $dom, + ); +} + +sub _post_to_rss { + my $post = shift; + my $item_tag = Mojo::DOM->new_tag('item'); + my $title_tag = Mojo::DOM->new_tag( 'title', $post->{title} ); + my $link = Mojo::DOM->new_tag( 'link', + 'https://burguillos.info/posts/' . $post->{slug} ); + my $description = Mojo::DOM->new_tag( 'description', + Mojo::DOM->new( $post->{content} )->all_text ); + my $guid = Mojo::DOM->new_tag( 'guid', $post->{slug} ); + my $date = Mojo::DOM->new_tag( + 'pubDate', + ''.DateTime::Format::Mail->format_datetime( + DateTime::Format::ISO8601->parse_datetime( $post->{date} ) + ) + ); + + $item_tag->child_nodes->first->append_content($title_tag); + $item_tag->child_nodes->first->append_content($link); + $item_tag->child_nodes->first->append_content($description); + $item_tag->child_nodes->first->append_content($guid); + $item_tag->child_nodes->first->append_content($date); + return $item_tag; +} + sub post { my $self = shift; my $slug = $self->param('slug'); diff --git a/public/css/styles.scss b/public/css/styles.scss index 1398f3f..833d94c 100644 --- a/public/css/styles.scss +++ b/public/css/styles.scss @@ -119,6 +119,18 @@ body { } } } + a.suscribe-category-rss { + background: $background_div; + width: 40px; + height: 40px; + padding: 10px; + border-radius: 5px; + display: inline-block; + img { + width: 40px; + height: 40px; + } + } } nav > a > img { vertical-align: middle; diff --git a/public/img/rss.svg b/public/img/rss.svg new file mode 100644 index 0000000..52d3a25 --- /dev/null +++ b/public/img/rss.svg @@ -0,0 +1,7 @@ + + + + + Svg Vector Icons : http://www.onlinewebfonts.com/icon + + diff --git a/templates/layouts/default.html.ep b/templates/layouts/default.html.ep index 4c3b362..2a10ae3 100644 --- a/templates/layouts/default.html.ep +++ b/templates/layouts/default.html.ep @@ -20,7 +20,10 @@ my $selected = defined($current_slug) && $category->{slug} eq $current_slug; %>" href="<%= '/'.$category->{slug} %>"><%==$category->{menu_text}%><% } - %> + %> + Suscribete + +