changeset 3285:bfd3f600650b

fix highlighting for search results and WantedPages
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Mon, 17 Mar 2008 14:53:40 +0100
parents d519735ca765
children 3d0fc2e2f526
files MoinMoin/Page.py MoinMoin/formatter/__init__.py MoinMoin/macro/WantedPages.py MoinMoin/search/queryparser.py
diffstat 4 files changed, 25 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/Page.py	Mon Mar 17 10:29:18 2008 +0100
+++ b/MoinMoin/Page.py	Mon Mar 17 14:53:40 2008 +0100
@@ -34,7 +34,7 @@
     @license: GNU GPL, see COPYING for details.
 """
 
-import os, codecs
+import os, re, codecs
 
 from MoinMoin import log
 logging = log.getLogger(__name__)
@@ -1066,7 +1066,13 @@
 
         self.formatter.setPage(self)
         if self.hilite_re:
-            self.formatter.set_highlight_re(self.hilite_re)
+            try:
+                self.formatter.set_highlight_re(self.hilite_re)
+            except re.error, err:
+                request.theme.add_msg(_('Invalid highlighting regular expression "%(regex)s": %(error)s') % {
+                                          'regex': self.hilite_re,
+                                          'error': str(err),
+                                      }, "warning")
 
         if 'deprecated' in pi:
             # deprecated page, append last backup version to current contents
--- a/MoinMoin/formatter/__init__.py	Mon Mar 17 10:29:18 2008 +0100
+++ b/MoinMoin/formatter/__init__.py	Mon Mar 17 14:53:40 2008 +0100
@@ -42,15 +42,16 @@
         self._base_depth = 0
 
     def set_highlight_re(self, hi_re=None):
+        """ set the highlighting regular expression (e.g. for search terms)
+        
+        @param hi_re: either a valid re as str/unicode (you may want to use
+                      re.escape before passing generic strings!) or a compiled
+                      re object. raises re.error for invalid re.
+        """
         if isinstance(hi_re, (str, unicode)):
-            try:
-                self._highlight_re = re.compile(hi_re, re.U + re.IGNORECASE)
-            except re.error:
-                hi_re = re.escape(hi_re)
-                self._highlight_re = re.compile(hi_re, re.U + re.IGNORECASE)
-        else:
-            self._highlight_re = hi_re
-
+            hi_re = re.compile(hi_re, re.U + re.IGNORECASE)
+        self._highlight_re = hi_re
+    
     def lang(self, on, lang_name):
         return ""
 
--- a/MoinMoin/macro/WantedPages.py	Mon Mar 17 10:29:18 2008 +0100
+++ b/MoinMoin/macro/WantedPages.py	Mon Mar 17 14:53:40 2008 +0100
@@ -75,8 +75,7 @@
         where.sort()
         if macro.formatter.page.page_name in where:
             where.remove(macro.formatter.page.page_name)
-        querystr = 'highlight=%s' % wikiutil.url_quote_plus(name)
-        wherelinks = [pages[pagename].link_to(request, querystr=querystr, rel='nofollow')
+        wherelinks = [pages[pagename].link_to(request, querystr={'highlight': name}, rel='nofollow')
                       for pagename in where]
         result.append(": " + ', '.join(wherelinks))
         result.append(macro.formatter.listitem(0))
--- a/MoinMoin/search/queryparser.py	Mon Mar 17 10:29:18 2008 +0100
+++ b/MoinMoin/search/queryparser.py	Mon Mar 17 14:53:40 2008 +0100
@@ -78,7 +78,7 @@
 
         Used to display the needle in the page.
         """
-        return ''
+        return u''
 
     def _build_re(self, pattern, use_re=False, case=False, stemmed=False):
         """ Make a regular expression out of a text pattern """
@@ -182,7 +182,7 @@
             if highlight_re:
                 result.append(highlight_re)
 
-        return '|'.join(result)
+        return u'|'.join(result)
 
     def xapian_wanted(self):
         wanted = True
@@ -309,7 +309,7 @@
         return u'%s"%s"' % (neg, unicode(self._pattern))
 
     def highlight_re(self):
-        return u"(%s)" % self._pattern
+        return u"(%s)" % self.pattern
 
     def search(self, page):
         logging.debug("TextSearch searching page %r for (negated = %r) %r" % (page.page_name, self.negated, self._pattern))
@@ -560,6 +560,7 @@
         flags = case and re.U or (re.I | re.U)
         if use_re:
             self.search_re = re.compile(pattern, flags)
+            self.pattern = pattern
             self.static = False
         else:
             self.pattern = pattern
@@ -664,7 +665,7 @@
         return u'%s!"%s"' % (neg, unicode(self._pattern))
 
     def highlight_re(self):
-        return ""
+        return u""
 
     def search(self, page):
         logging.debug("LanguageSearch searching page %r for (negated = %r) %r" % (page.page_name, self.negated, self._pattern))
@@ -800,7 +801,7 @@
         return u'%s!"%s"' % (neg, unicode(self._pattern))
 
     def highlight_re(self):
-        return ""
+        return u""
 
     def search(self, page):
         return None
@@ -860,7 +861,7 @@
         return u'%s!"%s"' % (neg, unicode(self._pattern))
 
     def highlight_re(self):
-        return ""
+        return u""
 
     def search(self, page):
         logging.debug("DomainSearch searching page %r for (negated = %r) %r" % (page.page_name, self.negated, self._pattern))