[Moin-devel] CVS: MoinMoin wikiaction.py,1.59,1.60 wikimacro.py,1.35,1.36 wikiutil.py,1.90,1.91

J?rgen Hermann jhermann at users.sourceforge.net
Wed Mar 20 14:41:06 EST 2002


Update of /cvsroot/moin/MoinMoin
In directory usw-pr-cvs1:/tmp/cvs-serv27775/MoinMoin

Modified Files:
	wikiaction.py wikimacro.py wikiutil.py 
Log Message:
Search result contexts


Index: wikiaction.py
===================================================================
RCS file: /cvsroot/moin/MoinMoin/wikiaction.py,v
retrieving revision 1.59
retrieving revision 1.60
diff -C2 -r1.59 -r1.60
*** wikiaction.py	13 Mar 2002 22:46:25 -0000	1.59
--- wikiaction.py	20 Mar 2002 22:40:46 -0000	1.60
***************
*** 43,46 ****
--- 43,51 ----
      else:
          needle = ''
+     try:
+         context = int(form['context'].value)
+     except (KeyError, ValueError):
+         context = 0
+     max_context = 10 # only show first `max_context` contexts
  
      # send title
***************
*** 48,60 ****
  
      # search the pages
!     pagecount, hits = wikiutil.searchPages(needle, literal=form.has_key('literal'))
  
      # print the result
      print "<UL>"
!     for (count, page_name) in hits:
          print '<LI>' + Page(page_name).link_to(querystr=
              'action=highlight&value=%s' % urllib.quote_plus(needle))
          print ' . . . . ' + `count`
          print (_(' match'), _(' matches'))[count != 1]
      print "</UL>"
  
--- 53,73 ----
  
      # search the pages
!     pagecount, hits = wikiutil.searchPages(needle,
!         literal=form.has_key('literal'),
!         context=context)
  
      # print the result
      print "<UL>"
!     for (count, page_name, fragments) in hits:
          print '<LI>' + Page(page_name).link_to(querystr=
              'action=highlight&value=%s' % urllib.quote_plus(needle))
          print ' . . . . ' + `count`
          print (_(' match'), _(' matches'))[count != 1]
+         if context:
+             for hit in fragments[:max_context]:
+                 print '<br>', ' '*8, '<font color="#808080">...%s<b>%s</b>%s...</font>' \
+                     % tuple(map(cgi.escape, hit))
+             if len(fragments) > max_context:
+                 print '<br>', ' '*8, '<font color="#808080">...</font>'
      print "</UL>"
  

Index: wikimacro.py
===================================================================
RCS file: /cvsroot/moin/MoinMoin/wikimacro.py,v
retrieving revision 1.35
retrieving revision 1.36
diff -C2 -r1.35 -r1.36
*** wikimacro.py	17 Mar 2002 11:03:57 -0000	1.35
--- wikimacro.py	20 Mar 2002 22:40:46 -0000	1.36
***************
*** 97,105 ****
          else:
              default = ''
!         return self.formatter.rawHTML("""<form method="get">
!         <input type="hidden" name="action" value="%s"> 
!         <input name="value" size="30" value="%s"> 
!         <input type="submit" value="%s">
!         </form>""" % (type, default, _("Go")))
  
      def _macro_GoTo(self, args):
--- 97,110 ----
          else:
              default = ''
!         context = ''
!         if type == "fullsearch":
!             context = '<br><input type="checkbox" name="context" value="40" checked>' + \
!                 _('Display context of search results')
!         return self.formatter.rawHTML((
!             '<form method="GET">'
!             '<input type="hidden" name="action" value="%s">'
!             '<input name="value" size="30" value="%s">'
!             '<input type="submit" value="%s">'
!             '%s</form>') % (type, default, _("Go"), context))
  
      def _macro_GoTo(self, args):

Index: wikiutil.py
===================================================================
RCS file: /cvsroot/moin/MoinMoin/wikiutil.py,v
retrieving revision 1.90
retrieving revision 1.91
diff -C2 -r1.90 -r1.91
*** wikiutil.py	19 Mar 2002 23:06:55 -0000	1.90
--- wikiutil.py	20 Mar 2002 22:40:46 -0000	1.91
***************
*** 241,265 ****
          (number of pages, hits).
  
          literal = 0: try to treat "needle" as a regex, case-insensitive
          literal = 1: "needle" is definitely NOT a regex and searched case-sensitive
      """
      from MoinMoin.Page import Page
  
!     try:
!         needle_re = re.compile(needle, re.IGNORECASE)
!     except re.error:
          needle_re = re.compile(re.escape(needle), re.IGNORECASE)
  
      hits = []
-     literal = kw.get('literal', 0)
      all_pages = getPageList(config.text_dir)
      for page_name in all_pages:
          body = Page(page_name).get_raw_body()
!         if literal:
!             count = string.count(body, needle)
          else:
!             count = len(needle_re.findall(body))
!         if count:
!             hits.append((count, page_name))
  
      # The default comparison for tuples compares elements in order,
--- 241,291 ----
          (number of pages, hits).
  
+         `hits` is a list of tuples containing the number of hits on a page
+         and the pagename. When context>0, a list of triples with the text of
+         the hit and the text on each side of it is added; otherwise, the
+         third element is None.
+ 
          literal = 0: try to treat "needle" as a regex, case-insensitive
          literal = 1: "needle" is definitely NOT a regex and searched case-sensitive
+         context != 0: Provide `context` chars of text on each side of a hit
      """
      from MoinMoin.Page import Page
  
!     literal = kw.get('literal', 0)
!     context = int(kw.get('context', 0))
! 
!     if literal and context:
          needle_re = re.compile(re.escape(needle), re.IGNORECASE)
+     elif not literal:
+         try:
+             needle_re = re.compile(needle, re.IGNORECASE)
+         except re.error:
+             needle_re = re.compile(re.escape(needle), re.IGNORECASE)
  
      hits = []
      all_pages = getPageList(config.text_dir)
      for page_name in all_pages:
          body = Page(page_name).get_raw_body()
!         if context:
!             pos = 0
!             fragments = []
!             while 1:
!                 match = needle_re.search(body, pos)
!                 if not match: break
!                 pos = match.end()+1
!                 fragments.append((
!                     body[match.start()-context:match.start()],
!                     body[match.start():match.end()],
!                     body[match.end():match.end()+context],
!                 ))
!             if fragments:
!                 hits.append((len(fragments), page_name, fragments))
          else:
!             if literal:
!                 count = string.count(body, needle)
!             else:
!                 count = len(needle_re.findall(body))
!             if count:
!                 hits.append((count, page_name, None))
  
      # The default comparison for tuples compares elements in order,
***************
*** 604,607 ****
--- 630,634 ----
  <tr><td>
  <input type="hidden" name="action" value="inlinesearch">
+ <input type="hidden" name="context" value="40">
  """ % (webapi.getScriptname(), cgi.escape(pagename, 1))
  





More information about the Moin-devel mailing list