changeset 5857:a89531afebe8

search/queryparser: no_highlight: search operator added to provide ability to suppress highlighting for (part of) search query.
author Eugene Syromyatnikov <evgsyr@gmail.com>
date Sun, 20 May 2012 22:59:04 +0400
parents 8e5559a0a08b
children be11f1e64e73
files MoinMoin/search/queryparser/__init__.py MoinMoin/search/queryparser/expressions.py
diffstat 2 files changed, 20 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/search/queryparser/__init__.py	Mon May 21 03:23:27 2012 +0400
+++ b/MoinMoin/search/queryparser/__init__.py	Sun May 20 22:59:04 2012 +0400
@@ -38,6 +38,7 @@
         self.titlesearch = kw.get('titlesearch', 0)
         self.case = kw.get('case', 0)
         self.regex = kw.get('regex', 0)
+        self.no_highlight = kw.get('no_highlight', 0)
         self._M = wikiutil.ParserPrefix('-')
 
     def _analyse_items(self, items):
@@ -89,6 +90,7 @@
                 title_search = self.titlesearch
                 regex = self.regex
                 case = self.case
+                no_highlight = self.no_highlight
                 linkto = False
                 lang = False
                 category = False
@@ -116,6 +118,8 @@
                         mimetype = True
                     elif "domain".startswith(m):
                         domain = True
+                    elif "no_highlight".startswith(m):
+                        no_highlight = True
                     else:
                         raise QueryError("Invalid search prefix")
                     item = item[1:]
@@ -136,6 +140,7 @@
                 else:
                     obj = TextSearch(text, use_re=regex, case=case)
                 obj.negated = negate
+                obj.highlight = not no_highlight
                 terms.append(obj)
             elif isinstance(item, list):
                 # strip off the opening parenthesis
--- a/MoinMoin/search/queryparser/expressions.py	Mon May 21 03:23:27 2012 +0400
+++ b/MoinMoin/search/queryparser/expressions.py	Sun May 20 22:59:04 2012 +0400
@@ -39,8 +39,9 @@
     # It allows to do the fast searches first.
     costs = 0
     _tag = ""
+    highlight = 1
 
-    def __init__(self, pattern, use_re=False, case=False):
+    def __init__(self, pattern, use_re=False, case=False, highlight=1):
         """ Init a text search
 
         @param pattern: pattern to search for, ascii string or unicode
@@ -51,6 +52,7 @@
         self.negated = 0
         self.use_re = use_re
         self.case = case
+        self.highlight = highlight
 
         if use_re:
             self._tag += 're:'
@@ -226,6 +228,9 @@
         return matches
 
     def highlight_re(self):
+        if not self.highlight:
+            return u''
+
         result = []
         for s in self._subterms:
             highlight_re = s.highlight_re()
@@ -360,6 +365,9 @@
     _field_to_search = 'content'
 
     def highlight_re(self):
+        if not self.highlight:
+            return u''
+
         return u"(%s)" % self.pattern
 
     def _get_matches(self, page):
@@ -455,6 +463,9 @@
         self.textsearch = TextSearch(self._textpattern, use_re=True, case=case)
 
     def highlight_re(self):
+        if not self.highlight:
+            return u''
+
         return u"(%s)" % self._textpattern
 
     def _get_matches(self, page):
@@ -535,6 +546,9 @@
         return matches
 
     def highlight_re(self):
+        if not self.highlight:
+            return u''
+
         return u'(\\b%s\\b)' % self._pattern
 
     def xapian_term(self, request, connection):