From 77e4cdcc7d7b3a627fac489f886175c48cf858b7 Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Sat, 31 Oct 2015 13:52:50 +0000 Subject: [PATCH] Refactor context highlighter out of context extraction --- build/index.php | 28 +++++++++++++--------------- module_index.json | 2 +- modules/feature-search.php | 30 +++++++++++++----------------- 3 files changed, 27 insertions(+), 33 deletions(-) diff --git a/build/index.php b/build/index.php index 6193d00..9a5f16d 100644 --- a/build/index.php +++ b/build/index.php @@ -1376,6 +1376,7 @@ register_module([ $link = "?page=" . rawurlencode($result["pagename"]); $pagesource = file_get_contents($result["pagename"] . ".md"); $context = search::extract_context($_GET["query"], $pagesource); + $context = search::highlight_context($_GET["query"], $context); $content .= "
\n"; $content .= "

" . $result["pagename"] . "

\n"; @@ -1681,21 +1682,6 @@ class search // be broken anyway. $context = self::strip_markup($context); - // Make the matching words bold. - $extraoffset = 0; - foreach($group as $match) - { - $start = $match[1] + $extraoffset; - $length = strlen($match[0]); - $end = $start + $length; - - // Insert the end one first to make sure that we don't mess up - // the offsets. - $context = substr_replace($context, "", $end, 0); - $context = substr_replace($context, "", $start, 0); -// $extraoffset += strlen(""); - } - $contexts[] = $context; $basepos = $scanpos + 1; @@ -1703,6 +1689,18 @@ class search return implode(" ... ", $contexts); } + + public static function highlight_context($query, $context) + { + $qterms = self::tokenize($query); + + foreach($qterms as $qterm) + { + $context = preg_replace("/" . preg_quote($qterm) . "/i", "$0", $context); + } + + return $context; + } } /** diff --git a/module_index.json b/module_index.json index 37e5159..ce1ac27 100644 --- a/module_index.json +++ b/module_index.json @@ -50,7 +50,7 @@ "author": "Starbeamrainbowlabs", "description": "Adds proper search functionality to Pepperminty Wiki. Note that this module, at the moment, just contains test code while I figure out how best to write a search engine.", "id": "feature-search", - "lastupdate": 1446117613, + "lastupdate": 1446299530, "optional": false }, { diff --git a/modules/feature-search.php b/modules/feature-search.php index f7d5bbe..a187125 100644 --- a/modules/feature-search.php +++ b/modules/feature-search.php @@ -52,6 +52,7 @@ register_module([ $link = "?page=" . rawurlencode($result["pagename"]); $pagesource = file_get_contents($result["pagename"] . ".md"); $context = search::extract_context($_GET["query"], $pagesource); + $context = search::highlight_context($_GET["query"], $context); $content .= "
\n"; $content .= "

" . $result["pagename"] . "

\n"; @@ -357,23 +358,6 @@ class search // be broken anyway. $context = self::strip_markup($context); - // Make the matching words bold. - // Fixme Account for the fact that the offsets in $matches[] are relative to the beginning of the document, not the contextual snippet - // Todo Figure out why $extraoffset throws everything off - $extraoffset = 0; - foreach($group as $match) - { - $start = $match[1] + $extraoffset; - $length = strlen($match[0]); - $end = $start + $length; - - // Insert the end one first to make sure that we don't mess up - // the offsets. - $context = substr_replace($context, "
", $end, 0); - $context = substr_replace($context, "", $start, 0); -// $extraoffset += strlen(""); - } - $contexts[] = $context; $basepos = $scanpos + 1; @@ -381,6 +365,18 @@ class search return implode(" ... ", $contexts); } + + public static function highlight_context($query, $context) + { + $qterms = self::tokenize($query); + + foreach($qterms as $qterm) + { + $context = preg_replace("/" . preg_quote($qterm) . "/i", "$0", $context); + } + + return $context; + } } /**