[PATCHES] lucene plugin
Klaus Trainer <klaus.trainer-S0/[email protected]>
| Newsgroups | gmane.comp.web.pyblosxom.devel |
|---|---|
| Message-ID | <[email protected]> |
I've just added support for highlighting search hits (like in google cache). I've also added some linebreaks for better readability and updated the documentation. Unfortunately I could not manage to apply the highlighting feature to tags in the footer, without having to modify the tags plugin itself. The problem is that I don't know how to modify the tags in the footer _after_ they get rendered by the tags plugin. Although I could make it work so that the tags are highlighted and displayed correctly, their links were broken then. Please tell me, if you know a solution to this problem! // TODO: Add highlighting support for tags in the footer (see problem description above). ------------------------------------------------------------------------- This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2008. http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ _______________________________________________ Pyblosxom-devel mailing list Pyblosxom-devel-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org https://lists.sourceforge.net/lists/listinfo/pyblosxom-devel
lucene.py.diff
(text/x-patch, 6.5 KB)
--- lucene.py 2008-03-01 19:00:12.000000000 +0100
+++ mylucene.py 2008-03-01 20:26:06.000000000 +0100
@@ -30,13 +30,14 @@
this path
4) Download lucene from http://jakarta.apache.org/lucene and unzip/untar it.
In config.py, set py['lucene_home'] to the lucene directory (which contains
- lucene-1.3-final.jar and lucene-demos-1.3-final.jar)
+ lucene-core-XX.jar and lucene-demos-XX.jar)
5) In config.py set py['lucene_index'] to the name of the Lucene index file
(pybosxom must be able to read and write this file)
- 6) In config.py set py['JAVA_HOME'] to point at the 'java' command
- 7) Set up a cron job to run py['lucene_bin']/index.sh periodically to
+ 6) In config.py set py['lucene_highlight'] to True or False
+ 7) In config.py set py['JAVA_HOME'] to point at the 'java' command
+ 8) Set up a cron job to run py['lucene_bin']/index.sh periodically to
reindex your blog
- 8) Somewhere in your web page you need a lucene search form:
+ 9) Somewhere in your web page you need a lucene search form:
<form id="searchform" method="get" action="/blog">
<table>
@@ -49,19 +50,17 @@
The action of the form should be the top level URI of your blog
- 9) You should add $searchHeader somewhere in the header of your webpage; this
+10) You should add $searchHeader somewhere in the header of your webpage; this
is where statements like, "Your search returned X results for Y" are
placed. This statement is enclosed in a div tag with a class of
"searchtext" so that you can define it as you like in your stylesheet.
-10) Uses of Apple's Java on Mac OS X should be sure to use Lucene 1.3
-
"""
__author__ = "Ted Leung - [email protected]"
__version__ = "$Id$"
-import glob, os, urllib
+import glob, os, urllib, re
from Pyblosxom.entries import fileentry
def verify_installation(request):
@@ -70,15 +69,24 @@
retval = 1
if not config.has_key('lucene_home') or not os.path.isdir(config['lucene_home']):
- print 'The "lucene_home" property in the config file must refer to a directory that contains the lucene libraries.'
+ print 'The "lucene_home" property in the config file must refer to ' \
+ 'a directory that contains the lucene libraries.'
retval = 0
if not config.has_key('JAVA_HOME') or not os.path.isfile(config['JAVA_HOME']):
- print 'The "JAVA_HOME" property in the config file must refer to the "java" command on your system.'
+ print 'The "JAVA_HOME" property in the config file must refer to ' \
+ 'the "java" command on your system.'
retval = 0
if not config.has_key('lucene_bin') or not os.path.isdir(config['lucene_bin']):
- print 'The "lucene_bin" property in the config file must refer to a directory that contains the lucene plugin scripts index.sh and search.sh.'
+ print 'The "lucene_bin" property in the config file must refer to a ' \
+ 'directory that contains the lucene plugin scripts index.sh and ' \
+ 'search.sh.'
+ retval = 0
+
+ if not config.has_key('lucene_highlight'):
+ print 'The "lucene_highlight" property in the config file must be '\
+ 'set to either True or False'
retval = 0
return retval
@@ -107,15 +115,17 @@
@type term: a string
"""
urllib.quote(term)
- JAVA_HOME=config['JAVA_HOME']
- classpath = ':'.join(glob.glob(config['lucene_home']+'/*.jar')+[config['lucene_bin']])
+ JAVA_HOME = config['JAVA_HOME']
+ classpath = ':'.join(glob.glob(config['lucene_home'] + '/*.jar') \
+ + [config['lucene_bin']])
index = config['lucene_index']
- cmd =JAVA_HOME+' -cp '+classpath+' LuceneSearch '+index+' '+term
- pipe = os.popen(cmd,'r')
+ cmd = JAVA_HOME + ' -cp ' + classpath + ' LuceneSearch ' + index + ' ' \
+ + term
+ pipe = os.popen(cmd, "r")
results = pipe.readlines()
status = pipe.close()
results = [ os.path.join(config['datadir'], x[2:-1]) for x in results ]
- entries = [ makeEntry(x, request) for x in results]
+ entries = [ makeEntry(x, request) for x in results ]
entries = [ ( x._mtime, x ) for x in entries ]
entries.sort()
entries.reverse()
@@ -134,20 +144,20 @@
return None
data = request.getData()
-
resultnumber = len(data['luceneResults'])
if resultnumber < 1:
- data['searchHeader'] = "<div class=\"searchtext\">Your search returned no results. Maybe I just never talk about <b>" + form["search"].value + "</b>. Try again.</div><div>"
+ data['searchHeader'] = "<div class=\"searchtext\">Your search " \
+ "returned no results. Maybe I just never talk about <b>" \
+ + form["search"].value + "</b></div>"
else:
- data['searchHeader'] = "<div class=\"searchtext\">Your search returned " + str(resultnumber) + " results for <b>" + form["search"].value + "</b>. They are below:</div>"
+ data['searchHeader'] = "<div class=\"searchtext\">Your search " \
+ "returned " + str(resultnumber) + " results for <b>" \
+ + form["search"].value + "</b>:</div>"
def cb_filelist(args):
"""
Lucene search handling
-
- @param request: the Pyblosxom request
- @type request: a Pyblosxom request object
"""
# do nothing if the form is not a lucene form
request = args["request"]
@@ -160,3 +170,33 @@
data['luceneResults'] = search(request, config, form["search"].value)
return data['luceneResults']
+
+def cb_postformat(args):
+ """
+ Highlight matches if lucene_highlight is set to True in config.py
+ """
+ # do nothing if the form is not a lucene form
+ request = args["request"]
+ form = request.getHttp()["form"]
+ if not form.has_key("search"):
+ return None
+
+ # do nothing if highlighting is not enabled
+ config = request.getConfiguration()
+ if not config["lucene_highlight"]:
+ return None
+
+ title = args["entry_data"]["title"]
+ body = args["entry_data"]["body"]
+
+ term = form["search"].value
+ term = re.sub('\*', '\w*', term)
+ term = re.sub('\?', '\w', term)
+ pattern = re.compile('\\b' + term + '\\b', re.IGNORECASE)
+ args["entry_data"]["title"] = pattern.sub(lambda match: \
+ "<span style='color:black;background-color:#ffff66'>" \
+ + match.group() + "</span>", title)
+ args["entry_data"]["body"] = pattern.sub(lambda match: \
+ "<span style='color:black;background-color:#ffff66'>" \
+ + match.group() + "</span>", body)
+ return args["entry_data"]