Merge branch 'main' of git.owlcode.tech:sergiotarxz/burguillos.info

This commit is contained in:
sergiotarxz 2023-09-04 22:06:14 +02:00
commit 79cd0c959a
7 changed files with 97 additions and 28 deletions

View File

@ -3,6 +3,8 @@ import Tablesort from 'tablesort';
window.Tablesort = require('tablesort'); window.Tablesort = require('tablesort');
require('tablesort/src/sorts/tablesort.number'); require('tablesort/src/sorts/tablesort.number');
let fakeSearchInput
let searchMobile
window.onload = () => { window.onload = () => {
const menu_expand = document.querySelector('a.menu-expand'); const menu_expand = document.querySelector('a.menu-expand');
const mobile_foldable = document.querySelector('nav.mobile-foldable'); const mobile_foldable = document.querySelector('nav.mobile-foldable');
@ -32,13 +34,26 @@ window.onload = () => {
if (window !== undefined && window.Android !== undefined) { if (window !== undefined && window.Android !== undefined) {
executeAndroidExclusiveCode(Android) executeAndroidExclusiveCode(Android)
} }
searchMobile = document.querySelector('nav.mobile-shortcuts div.search')
fakeSearchInput = searchMobile.querySelector('input')
addListenersSearch() addListenersSearch()
}; };
function addListenersSearch() { function addListenersSearch() {
const searchMobile = document.querySelector('nav.mobile-shortcuts div.search')
if (searchMobile !== null) { if (searchMobile !== null) {
searchMobile.addEventListener('click', onFakeSearchClick); searchMobile.addEventListener('click', onFakeSearchClick);
fakeSearchInput.addEventListener('focus', (e) => {
onFakeSearchClick(e)
});
fakeSearchInput.addEventListener('change', (e) => {
if (fakeSearchInput.value !== "") {
const searchOverlay = document.querySelector('div.search-overlay');
const searchInput = searchOverlay.querySelector('div.search input');
searchInput.value = fakeSearchInput.value;
onSearchChange(e)
}
onFakeSearchClick(e)
});
} }
const exitSearch = document.querySelector('a.exit-search') const exitSearch = document.querySelector('a.exit-search')
if (exitSearch !== null) { if (exitSearch !== null) {
@ -57,6 +72,7 @@ function onSearchChange() {
return; return;
} }
const query = search.value; const query = search.value;
fakeSearchInput.value = search.value
const url = new URL(window.location.protocol const url = new URL(window.location.protocol
+ "//" + "//"
+ window.location.hostname + window.location.hostname
@ -77,6 +93,7 @@ function onSearchChange() {
} }
showResults(searchResults, json.searchObjects); showResults(searchResults, json.searchObjects);
}) })
search.focus()
} }
function showResults(searchResults, searchObjects) { function showResults(searchResults, searchObjects) {

View File

@ -17,6 +17,9 @@ use Mojo::UserAgent;
use BurguillosInfo::Posts; use BurguillosInfo::Posts;
use BurguillosInfo::Categories; use BurguillosInfo::Categories;
use BurguillosInfo::IndexUtils;
my $index_utils = BurguillosInfo::IndexUtils->new;
sub run ( $self, @args ) { sub run ( $self, @args ) {
require BurguillosInfo; require BurguillosInfo;
@ -38,36 +41,47 @@ sub run ( $self, @args ) {
sub _index_categories ( $self, $index, $categories ) { sub _index_categories ( $self, $index, $categories ) {
my @categories_keys = keys %$categories; my @categories_keys = keys %$categories;
for my $category_key (@categories_keys) { for my $category_key (@categories_keys) {
my $category = $categories->{$category_key}; my $category = $categories->{$category_key};
my $slug = $category->{slug}; my $slug = $category->{slug};
my $url = "/$slug"; my $url = "/$slug";
my $content = my $content =
Mojo::DOM->new( '<html>' . $category->{description} =~ s/\s+/ /gr . '</html>' )->all_text; Mojo::DOM->new(
my $title = $category->{title}; '<html>' . $category->{description} =~ s/\s+/ /gr . '</html>' )
->all_text;
my $title = $category->{title};
my $attributes = $category->{attributes}; my $attributes = $category->{attributes};
$self->_index_attributes($index, $slug, $attributes); $self->_index_attributes( $index, $slug, $attributes );
push @$index, push @$index,
{ {
title => $title, title => $title,
content => $content, titleNormalized => $index_utils->n($title),
url => $url, content => $content,
contentNormalized => $index_utils->n( $content =~ s/\s+/ /gr ),
url => $url,
urlNormalized => $index_utils->n($url),
}; };
} }
} }
sub _index_attributes($self, $index, $category_slug, $attributes) { sub _index_attributes ( $self, $index, $category_slug, $attributes ) {
my @attributes_keys = keys %$attributes; my @attributes_keys = keys %$attributes;
for my $attribute_key (@attributes_keys) { for my $attribute_key (@attributes_keys) {
my $attribute = $attributes->{$attribute_key}; my $attribute = $attributes->{$attribute_key};
my $slug = $attribute->{identifier}; my $slug = $attribute->{identifier};
my $url = "/$category_slug/atributo/$slug"; my $url = "/$category_slug/atributo/$slug";
my $title = $attribute->{title}; my $title = $attribute->{title};
my $content = Mojo::DOM->new( '<html>' . $attribute->{description} . '</html>' )->all_text; my $content =
push @$index, { Mojo::DOM->new( '<html>' . $attribute->{description} . '</html>' )
title => $title, ->all_text;
content => $content =~ s/\s+/ /gr, push @$index,
url => $url, {
}; titleNormalized => $index_utils->n($title),
title => $title,
contentNormalized => $index_utils->n( $content =~ s/\s+/ /gr ),
content => $content =~ s/\s+/ /gr,
urlNormalized => $index_utils->n($url),
url => $url,
};
} }
} }
@ -84,11 +98,16 @@ sub _index_posts ( $self, $index, $posts ) {
my $author = $post->{author}; my $author = $post->{author};
push @$index, push @$index,
{ {
title => $title, titleNormalized => $index_utils->n($title),
author => $author, title => $title,
content => $content =~ s/\s+/ /gr, authorNormalized => $index_utils->n($author),
url => $url, author => $author,
urlImage => $urlImage, contentNormalized => $index_utils->n( $content =~ s/\s+/ /gr ),
content => $content =~ s/\s+/ /gr,
urlNormalized => $index_utils->n($url),
url => $url,
urlImageNormalized => $index_utils->n($urlImage),
urlImage => $urlImage,
}; };
} }
} }

View File

@ -10,14 +10,20 @@ use Data::Dumper;
use Mojo::Base 'Mojolicious::Controller', '-signatures'; use Mojo::Base 'Mojolicious::Controller', '-signatures';
use Mojo::UserAgent; use Mojo::UserAgent;
use BurguillosInfo::IndexUtils;
my $index_utils = BurguillosInfo::IndexUtils->new;
sub search ($self) { sub search ($self) {
my $ua = Mojo::UserAgent->new; my $ua = Mojo::UserAgent->new;
my $query = $self->param('q'); my $query = $self->param('q');
my $config = $self->config; my $config = $self->config;
my $search_backend = $config->{search_backend}; my $search_backend = $config->{search_backend};
my $search_index = $config->{search_index}; my $search_index = $config->{search_index};
$query =~ s/\btitle:/titleNormalized:/g;
$query =~ s/\bcontent:/contentNormalized:/g;
my $tx = $ua->get( $search_backend . '/search/' . $search_index, my $tx = $ua->get( $search_backend . '/search/' . $search_index,
{}, form => { q => $query } ); {}, form => { q => $index_utils->n($query) } );
my $result = $tx->result; my $result = $tx->result;
my $output = $result->json; my $output = $result->json;

View File

@ -0,0 +1,27 @@
package BurguillosInfo::IndexUtils;
use v5.36.0;
use strict;
use warnings;
use utf8;
use feature 'signatures';
use Unicode::Normalize qw/NFKD/;
use Moo;
sub normalize($self, $text) {
return undef if !defined $text;
my $decomposed = NFKD( $text );
$decomposed =~ s/\p{NonspacingMark}//g;
$decomposed =~ s/s\b//g;
$decomposed =~ s/a\b/o/g;
return $decomposed;
}
sub n(@args) {
normalize(@args);
}
1;

File diff suppressed because one or more lines are too long

View File

@ -35,7 +35,7 @@
<div class="search-overlay"> <div class="search-overlay">
<div class="bounding-search-bar"> <div class="bounding-search-bar">
%= include 'page/_search_bar' %= include 'page/_search_bar'
<a class="exit-search"> <a href="#" class="exit-search">
<img alt="Exit search" src="/img/exit.svg"/> <img alt="Exit search" src="/img/exit.svg"/>
</a> </a>
</div> </div>

View File

@ -1,4 +1,4 @@
<div tabindex="0" class="search"> <div class="search">
<div class="search-icon"> <div class="search-icon">
<img alt="" src="/img/search.svg"/> <img alt="" src="/img/search.svg"/>
</div> </div>