From: Mischa POSLAWSKY Date: Tue, 16 Oct 2018 16:59:36 +0000 (+0200) Subject: search: prepend fuzzy filename matches X-Git-Tag: v3.8~20 X-Git-Url: http://git.shiar.net/minimedit.git/commitdiff_plain/ead24d57321f965db99178336e9a72c03c966bd1 search: prepend fuzzy filename matches Prefer similar url over contents. --- diff --git a/search.php b/search.php index e1da846..5bd03bb 100644 --- a/search.php +++ b/search.php @@ -1,10 +1,31 @@ $limit) { + shuffle($results); + array_splice($results, $limit); +} + +$cmd = "git ls-files -- $path"; +exec($cmd, $ls); +if ($ls) { + # order files by similarity to query + $ls = array_combine($ls, array_map(function ($row) use ($Page) { + $row = preg_replace('{(?:^|/)index\.html$}', '', $row); + return similar_text($row, $Page) - strlen($row) / 8; + }, $ls)); + arsort($ls); + + # prepend best match, replace unless duplicate + array_unshift($results, key($ls)); + $results = array_unique($results); + array_splice($results, $limit); +} -if (!$results) { +elseif (!$results) { $results = ['index.html']; }