Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for accented characters #89

Merged
merged 4 commits into from
Jan 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions blueprints.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ form:
validate:
type: bool

ignore_accented_characters:
type: toggle
label: Ignote accented characters
help: If enabled, search terms will match accented characters regardless to their diacritics i.e. search results will show up for "cafe" and "café" no matter how you typed it.
highlight: 0
default: 0
options:
1: Enabled
0: Disabled
validate:
type: bool

min_query_length:
type: text
size: x-small
Expand Down
17 changes: 14 additions & 3 deletions simplesearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,13 +230,24 @@ public function onTwigSiteVariables()
}
}

private function matchText($haystack, $needle) {
if ($this->config->get('plugins.simplesearch.ignore_accented_characters')) {
setlocale(LC_ALL, 'en_US');
$result = mb_stripos(iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $haystack), iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $needle));
setlocale(LC_ALL, '');
return $result;
} else {
return mb_stripos($haystack, $needle);
}
}

private function notFound($query, $page, $taxonomies)
{
$searchable_types = ['title', 'content', 'taxonomy'];
$results = true;
foreach ($searchable_types as $type) {
if ($type === 'title') {
$result = mb_stripos(strip_tags($page->title()), $query) === false;
$result = $this->matchText(strip_tags($page->title()), $query) === false;
} elseif ($type === 'taxonomy') {
if ($taxonomies === false) {
continue;
Expand All @@ -250,14 +261,14 @@ private function notFound($query, $page, $taxonomies)
}

$taxonomy_values = implode('|',$values);
if (mb_stripos($taxonomy_values, $query) !== false) {
if ($this->matchText($taxonomy_values, $query) !== false) {
$taxonomy_match = true;
break;
}
}
$result = !$taxonomy_match;
} else {
$result = mb_stripos(strip_tags($page->content()), $query) === false;
$result = $this->matchText(strip_tags($page->content()), $query) === false;
}
$results = $results && $result;
if ($results === false ) {
Expand Down
1 change: 1 addition & 0 deletions simplesearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ template: simplesearch_results
filters:
category: blog
filter_combinator: and
ignore_accented_characters: true
order:
by: date
dir: desc