From 0ae1e5e802871903d73d6542252aa0a8d08fba39 Mon Sep 17 00:00:00 2001 From: yvesf Date: Sat, 20 Nov 2010 01:16:56 +0100 Subject: web stuff --- templates/search.html | 30 ++++++++++++++++++++++++++++++ web.py | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 templates/search.html create mode 100644 web.py diff --git a/templates/search.html b/templates/search.html new file mode 100644 index 0000000..e6bfe07 --- /dev/null +++ b/templates/search.html @@ -0,0 +1,30 @@ + + + {{ objects.__len__() + skip}} matches + + + {% if objects.__len__() == 0 %} + No Matches + {% else %} + {% for obj in objects %} +
+

{{ obj['title'] }}

+
 {{ obj['path'] }} 
+ {% autoescape false %} +
{{ obj['excerpt'] }}
+ {% endautoescape %} +
+ {% endfor %} + {% endif %} + +
+ {% if skip > 0 %} + Previous 5 + {% else %} + Previous 5 + {% endif %} + - + Next 5 +
+ + diff --git a/web.py b/web.py new file mode 100644 index 0000000..429c516 --- /dev/null +++ b/web.py @@ -0,0 +1,40 @@ +#!/usr/bin/python2.6 +# coding: utf-8 +from whoosh.index import open_dir +from whoosh.qparser import QueryParser +import whoosh.fields as fields +import whoosh.analysis as analysis +from whoosh import highlight +import flask +from flask import Flask + +app = Flask("booksearch") + +index = open_dir(u"index", mapped=False) +searcher = index.searcher() + + +@app.route("/search/skip=/",methods=["GET"]) +@app.route("/search/",methods=["GET"]) +def do_search(skip=0,term=None): + query = QueryParser("content").parse(term) + results = searcher.search(query, limit=skip+5) + + terms = [text for fieldname, text in query.all_terms() + if fieldname == "content"] + objects = [] + for result in results[skip:skip+5]: + title = result.get("title") + path = result.get("path") +# high = highlight.highlight(result.get("content"), +# terms, +# analysis.StandardAnalyzer(), +# highlight.SimpleFragmenter(), +# highlight.HtmlFormatter()) + objects.append({ 'title' : title, 'path' : path, 'excerpt' : 'TODO' }) + return flask.render_template('search.html', objects=objects, term=term, skip=skip) + + +if __name__ == "__main__": + app.debug = True + app.run(host="0.0.0.0") -- cgit v1.2.1