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');
require('tablesort/src/sorts/tablesort.number');
let fakeSearchInput
let searchMobile
window.onload = () => {
const menu_expand = document.querySelector('a.menu-expand');
const mobile_foldable = document.querySelector('nav.mobile-foldable');
@ -31,14 +33,27 @@ window.onload = () => {
}
if (window !== undefined && window.Android !== undefined) {
executeAndroidExclusiveCode(Android)
}
}
searchMobile = document.querySelector('nav.mobile-shortcuts div.search')
fakeSearchInput = searchMobile.querySelector('input')
addListenersSearch()
};
function addListenersSearch() {
const searchMobile = document.querySelector('nav.mobile-shortcuts div.search')
if (searchMobile !== null) {
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')
if (exitSearch !== null) {
@ -57,6 +72,7 @@ function onSearchChange() {
return;
}
const query = search.value;
fakeSearchInput.value = search.value
const url = new URL(window.location.protocol
+ "//"
+ window.location.hostname
@ -77,6 +93,7 @@ function onSearchChange() {
}
showResults(searchResults, json.searchObjects);
})
search.focus()
}
function showResults(searchResults, searchObjects) {

View File

@ -17,6 +17,9 @@ use Mojo::UserAgent;
use BurguillosInfo::Posts;
use BurguillosInfo::Categories;
use BurguillosInfo::IndexUtils;
my $index_utils = BurguillosInfo::IndexUtils->new;
sub run ( $self, @args ) {
require BurguillosInfo;
@ -38,36 +41,47 @@ sub run ( $self, @args ) {
sub _index_categories ( $self, $index, $categories ) {
my @categories_keys = keys %$categories;
for my $category_key (@categories_keys) {
my $category = $categories->{$category_key};
my $category = $categories->{$category_key};
my $slug = $category->{slug};
my $url = "/$slug";
my $content =
Mojo::DOM->new( '<html>' . $category->{description} =~ s/\s+/ /gr . '</html>' )->all_text;
my $title = $category->{title};
Mojo::DOM->new(
'<html>' . $category->{description} =~ s/\s+/ /gr . '</html>' )
->all_text;
my $title = $category->{title};
my $attributes = $category->{attributes};
$self->_index_attributes($index, $slug, $attributes);
$self->_index_attributes( $index, $slug, $attributes );
push @$index,
{
title => $title,
content => $content,
url => $url,
title => $title,
titleNormalized => $index_utils->n($title),
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;
for my $attribute_key (@attributes_keys) {
my $attribute = $attributes->{$attribute_key};
my $slug = $attribute->{identifier};
my $url = "/$category_slug/atributo/$slug";
my $title = $attribute->{title};
my $content = Mojo::DOM->new( '<html>' . $attribute->{description} . '</html>' )->all_text;
push @$index, {
title => $title,
content => $content =~ s/\s+/ /gr,
url => $url,
};
my $slug = $attribute->{identifier};
my $url = "/$category_slug/atributo/$slug";
my $title = $attribute->{title};
my $content =
Mojo::DOM->new( '<html>' . $attribute->{description} . '</html>' )
->all_text;
push @$index,
{
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};
push @$index,
{
title => $title,
author => $author,
content => $content =~ s/\s+/ /gr,
url => $url,
urlImage => $urlImage,
titleNormalized => $index_utils->n($title),
title => $title,
authorNormalized => $index_utils->n($author),
author => $author,
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::UserAgent;
use BurguillosInfo::IndexUtils;
my $index_utils = BurguillosInfo::IndexUtils->new;
sub search ($self) {
my $ua = Mojo::UserAgent->new;
my $query = $self->param('q');
my $config = $self->config;
my $search_backend = $config->{search_backend};
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,
{}, form => { q => $query } );
{}, form => { q => $index_utils->n($query) } );
my $result = $tx->result;
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="bounding-search-bar">
%= include 'page/_search_bar'
<a class="exit-search">
<a href="#" class="exit-search">
<img alt="Exit search" src="/img/exit.svg"/>
</a>
</div>

View File

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