Adding rss suscriptions.

This commit is contained in:
sergiotarxz 2022-11-10 10:49:53 +01:00
parent 698d80b36c
commit 986bd01499
6 changed files with 113 additions and 1 deletions

View File

@ -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');
}

View File

@ -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');

View File

@ -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;

7
public/img/rss.svg Normal file
View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Svg Vector Icons : http://www.onlinewebfonts.com/icon -->
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 1000 1000" enable-background="new 0 0 1000 1000" xml:space="preserve">
<metadata> Svg Vector Icons : http://www.onlinewebfonts.com/icon </metadata>
<g><g transform="translate(0.000000,512.000000) scale(0.100000,-0.100000)"><path style="fill:#f2eb8c" d="M425.4,4208.4v-809.7l254.6-13.4c1039.3-53.6,2090.2-342.6,3028-832.6c2350.5-1226.9,3948.7-3604.2,4258.8-6337.5c19.1-172.3,55.5-769.5,57.4-922.6v-72.7h775.2h775.2v63.2c-1.9,185.7-42.1,863.2-68.9,1094.8C9300.9-1711.8,8554.4,93.2,7363.9,1567C6077.6,3157.6,4332,4285,2416,4759.7C1826.5,4905.2,990,5020,517.3,5020h-91.9V4208.4z"/><path style="fill:#f2eb8c" d="M425.4,958.4V144.9l168.4-13.4c602.9-47.9,1138.9-197.2,1678.6-469C3660.2-1038,4607.6-2366.4,4856.4-3957c23-155,55.5-530.2,67-807.7c0-9.6,344.5-15.3,763.7-15.3c593.4,0,765.6,5.7,771.4,23c15.3,45.9-19.2,675.6-49.8,905.3c-105.3,767.5-296.7,1401.1-633.5,2086.3C5436.4-1076.3,5072.7-565.2,4540.6-33.1C4054.5,455,3533.8,832,2934.7,1130.6c-740.7,371.3-1493,576.1-2302.6,627.8l-206.7,11.5V958.4z"/><path style="fill:#f2eb8c" d="M1516.4-2073.5c-802-153.1-1276.7-1006.8-1004.9-1805c70.8-212.5,155-340.7,333-520.6c132.1-132.1,197.2-179.9,327.3-245c365.6-178,740.7-179.9,1100.6-3.8c281.4,137.8,478.5,338.8,614.4,624c97.6,206.7,130.2,352.2,130.2,602.9c1.9,266.1-30.6,398.1-153.1,646.9c-68.9,141.7-116.8,208.7-233.5,325.4c-262.2,262.2-507.2,369.4-855.6,379C1660-2067.8,1543.2-2069.7,1516.4-2073.5z"/></g></g>
</svg>

After

Width:  |  Height:  |  Size: 1.7 KiB

View File

@ -20,7 +20,10 @@
my $selected = defined($current_slug) && $category->{slug} eq $current_slug;
%><a class="<%=$selected && "selected" %>" href="<%= '/'.$category->{slug} %>"><%==$category->{menu_text}%></a><%
}
%></nav>
%><a href="/all.rss" target="_blank" rel="noopener">
Suscribete <img class="rss-icon" src="/img/rss.svg"/>
</a>
</nav>
<nav class="mobile-shortcuts">
<a class="go-to-index" href="<%='/'.$categories->{index}{slug}%>"><%== $categories->{index}{menu_text} %></a>
<a href="#mobile-foldable" class="menu-expand"><img src="/img/menu.png" alt="Expandir el menú."/></a>

View File

@ -30,4 +30,10 @@
% } else {
<p>Parece que aun no hay artículos.</p>
% }
</div>
<h3>Suscribete a esta catergoría.</h3>
<a class="suscribe-category-rss" href="/<%=$current_category->{slug}%>.rss">
<img src="/img/rss.svg" alt="Icono de suscripción rss"/>
</a>