Basic search works for now

This commit is contained in:
Grégory Soutadé 2012-12-10 19:30:25 +01:00
parent 0b61fa7ff2
commit 2f1f38ca5c
16 changed files with 185 additions and 29 deletions

View File

@ -60,7 +60,10 @@ class Archive(Index):
self.hooks = {'posts' : self.createPosts, self.hooks = {'posts' : self.createPosts,
'navigation' : self.createNavigation, 'navigation' : self.createNavigation,
'archive' : self.createArchive, 'archive' : self.createArchive,
'tags' : self.createTags} 'tags' : self.createTags,
'replace' : self.createReplace}
self.blog = blog
if not os.path.exists(src + '/_archive.html'): if not os.path.exists(src + '/_archive.html'):
self.addError('No _archive.html found, exiting') self.addError('No _archive.html found, exiting')

View File

@ -27,7 +27,10 @@ class Category(Index):
self.hooks = {'posts' : self.createPosts, self.hooks = {'posts' : self.createPosts,
'navigation' : self.createNavigation, 'navigation' : self.createNavigation,
'category' : self.createCategory, 'category' : self.createCategory,
'tags' : self.createTags} 'tags' : self.createTags,
'replace' : self.createReplace}
self.blog = blog
if not os.path.exists(src + '/_category.html'): if not os.path.exists(src + '/_category.html'):
self.addError('No _category.html found, exiting') self.addError('No _category.html found, exiting')

View File

@ -21,10 +21,10 @@ class StrictUTF8Writer(codecs.StreamWriter):
object = object.replace('>', '>') object = object.replace('>', '>')
object = object.replace('"', '"') object = object.replace('"', '"')
object = object.replace(''', "'") object = object.replace(''', "'")
object = object.replace('&', "&") object = object.replace('&', '&')
if not type(object) == unicode: if not type(object) == unicode:
self.value = self.value + unicode(object, "utf-8") self.value = self.value + unicode(object, 'utf-8')
else: else:
self.value = self.value + object self.value = self.value + object
return self.value return self.value

View File

@ -14,6 +14,25 @@ class Index(DynastieGenerator):
posts_per_page = 0 posts_per_page = 0
filename = 'index' filename = 'index'
dirname = '' dirname = ''
blog = None
def createReplace(self, posts, dom, root, replace_elem):
if not replace_elem.hasAttribute('div_name'):
self.addError('No attribute div_name for a replace tag')
return
div_element = replace_elem.cloneNode(True)
div_element.tagName = replace_elem.getAttribute('div_name')
div_element.removeAttribute('div_name')
for key,value in replace_elem.attributes.items():
if key == 'div_name': continue
value = value.replace('dyn:blog_id', str(self.blog.id))
div_element.setAttribute(key, value)
root.replaceChild(div_element, replace_elem)
return div_element
def createNavigation(self, posts, dom, root, node): def createNavigation(self, posts, dom, root, node):
if self.nb_pages == 0 or self.nb_pages == 1: if self.nb_pages == 0 or self.nb_pages == 1:
@ -262,7 +281,10 @@ class Index(DynastieGenerator):
self.hooks = {'posts' : self.createPosts, self.hooks = {'posts' : self.createPosts,
'navigation' : self.createNavigation, 'navigation' : self.createNavigation,
'recents' : self.createRecents, 'recents' : self.createRecents,
'tags' : self.createTags} 'tags' : self.createTags,
'replace' : self.createReplace}
self.blog = blog
if not os.path.exists(src + '/_index.html'): if not os.path.exists(src + '/_index.html'):
self.addError('No _index.html found, exiting') self.addError('No _index.html found, exiting')

View File

@ -31,6 +31,8 @@ class Post(Index):
else: else:
value = value.replace('dyn:comment_id', str(self.cur_comment.id)) value = value.replace('dyn:comment_id', str(self.cur_comment.id))
value = value.replace('dyn:blog_id', str(self.blog.id))
div_element.setAttribute(key, value) div_element.setAttribute(key, value)
root.replaceChild(div_element, replace_elem) root.replaceChild(div_element, replace_elem)
@ -177,6 +179,8 @@ class Post(Index):
def generate(self, blog, src, output): def generate(self, blog, src, output):
from dynastie.models import Post, Blog from dynastie.models import Post, Blog
self.blog = blog
posts = Post.objects.all() posts = Post.objects.all()
return self._generate(blog, src, output, posts) return self._generate(blog, src, output, posts)

57
generators/search.py Normal file
View File

@ -0,0 +1,57 @@
import os
from datetime import datetime
from xml.dom.minidom import parse, parseString
from dynastie.generators.generator import DynastieGenerator, StrictUTF8Writer
from dynastie.generators.index import Index
from django.db import models
class Search(Index):
def createNoResultFound(self, posts, dom, root, node):
posts_elem = self.createElement(dom, 'posts', '<b>No result found</b>')
root.replaceChild(posts_elem, node)
return posts_elem
def generate(self, blog, src, output, post_list):
from dynastie.models import Post, Blog
self.blog = blog
self.hooks = {'posts' : self.createPosts,
'replace' : self.createReplace}
if not os.path.exists(src + '/_search.html'):
self.addError('No _search.html found, exiting')
return self.report
try:
dom = parse(src + '/_search.html')
except xml.dom.DOMException as e:
self.addError('Error parsing _search.html : ' + e)
return self.report
post_nodes = dom.getElementsByTagNameNS(self.URI, "posts")
if not post_nodes is None:
if post_nodes[0].hasAttribute("limit"):
self.posts_per_page = int(post_nodes[0].getAttribute("limit"))
else:
self.posts_per_page = 25
else:
self.addError('No tag dyn:posts found')
if len(post_list) == 0:
self.hooks['posts'] = self.createNoResultFound
posts = []
for post_id in post_list:
post = Post.objects.get(pk=post_id)
if not post is None:
posts.append(post)
nodes = dom.getElementsByTagName("*")
nodes[0] = self.parse(src, self.hooks, posts, dom, nodes[0])
writer = StrictUTF8Writer()
nodes[0].writexml(writer)
return writer.getvalue().encode('utf-8')

View File

@ -27,7 +27,10 @@ class Tag(Index):
self.hooks = {'posts' : self.createPosts, self.hooks = {'posts' : self.createPosts,
'navigation' : self.createNavigation, 'navigation' : self.createNavigation,
'tag' : self.createTag, 'tag' : self.createTag,
'tags' : self.createTags} 'tags' : self.createTags,
'replace' : self.createReplace}
self.blog = blog
if not os.path.exists(src + '/_tag.html'): if not os.path.exists(src + '/_tag.html'):
self.addError('No _tag.html found, exiting') self.addError('No _tag.html found, exiting')

View File

@ -50,6 +50,7 @@ class Search:
filename = blog.src_path + '/_search.db' filename = blog.src_path + '/_search.db'
if not os.path.exists(filename): if not os.path.exists(filename):
print 'No search index !'
return None return None
f = open(filename, 'rb') f = open(filename, 'rb')
@ -169,16 +170,16 @@ class Search:
def search(self, blog, string): def search(self, blog, string):
hashtable = self._loadDatabase(blog) hashtable = self._loadDatabase(blog)
string = self._prepare_string(string) string = self._prepare_string(string.encode('utf-8'))
wordlist = re.findall(self.wordreg, string) wordlist = re.findall(self.wordreg, string)
res = {} res = {}
for word in wordlist: for word in wordlist:
if len(word) < 4: if len(word) < Search.MINIMUM_LETTERS:
continue continue
word = word.lower() word = word.lower()
while not word in hashtable and len(word) > 3: while not word in hashtable and len(word) > Search.MINIMUM_LETTERS:
word = word[:-1] word = word[:-1]
if word not in hashtable: if word not in hashtable:
continue continue
@ -186,7 +187,11 @@ class Search:
if not post in res: if not post in res:
res[post] = 0 res[post] = 0
res[post] = res[post] + 1 res[post] = res[post] + 1
sorted_res = sorted(res.iteritems(), key=operator.itemgetter(1)) sorted_res = sorted(res.iteritems(), key=operator.itemgetter(1))
return sorted_res.reverse() sorted_res.reverse()
res = []
for i in range(len(sorted_res)):
res .append(sorted_res[i][0])
return res

View File

@ -9,6 +9,7 @@
<link href="/rss.xml" rel="alternate" type="application/rss+xml" title="RSS 2.0" /> <link href="/rss.xml" rel="alternate" type="application/rss+xml" title="RSS 2.0" />
<link href="/atom.xml" rel="alternate" type="application/atom+xml" title="Atom 1.0" /> <link href="/atom.xml" rel="alternate" type="application/atom+xml" title="Atom 1.0" />
<link href="/css/blog.css" rel="stylesheet" type="text/css"/> <link href="/css/blog.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="/js/blog.js"> </script>
</head> </head>
<body> <body>
<img id="logo" src="/images/tux_final.png"/> <img id="logo" src="/images/tux_final.png"/>
@ -23,10 +24,14 @@
<dyn:block name="content"/> <dyn:block name="content"/>
</div> </div>
<div class="menu"> <div class="menu">
<!-- <div class="menu_content"> --> <div class="menu_content">
<!-- <div class="menu_content_header">Search</div> --> <div class="menu_content_header">Recherche</div>
<!-- <div class="menu_content_content">La recherche</div> --> <div id="menu_main">
<!-- </div> --> <dyn:replace div_name="form" id="search_form" method="POST" action="/search/dyn:blog_id">
<input type="text" name="text" onkeypress="handleKeyPress(event,this.form)"/>
</dyn:replace>
</div>
</div>
<div class="menu_content"> <div class="menu_content">
<div class="menu_content_header">Menu principal</div> <div class="menu_content_header">Menu principal</div>
<div id="menu_main"> <div id="menu_main">

View File

@ -26,10 +26,14 @@
<dyn:block name="content"/> <dyn:block name="content"/>
</div> </div>
<div class="menu"> <div class="menu">
<!-- <div class="menu_content"> --> <div class="menu_content">
<!-- <div class="menu_content_header">Search</div> --> <div class="menu_content_header">Recherche</div>
<!-- <div class="menu_content_content">La recherche</div> --> <div id="menu_main">
<!-- </div> --> <dyn:replace div_name="form" id="search_form" method="POST" action="/search/dyn:blog_id">
<input type="text" name="text" onkeypress="handleKeyPress(event,this.form)"/>
</dyn:replace>
</div>
</div>
<div class="menu_content"> <div class="menu_content">
<div class="menu_content_header">Menu principal</div> <div class="menu_content_header">Menu principal</div>
<div id="menu_main"> <div id="menu_main">

View File

@ -443,3 +443,9 @@ ul li
.color_emacs_vg { color: #B8860B } /* Name.Variable.Global */ .color_emacs_vg { color: #B8860B } /* Name.Variable.Global */
.color_emacs_vi { color: #B8860B } /* Name.Variable.Instance */ .color_emacs_vi { color: #B8860B } /* Name.Variable.Instance */
.color_emacs_il { color: #666666 } /* Literal.Number.Integer.Long */ .color_emacs_il { color: #666666 } /* Literal.Number.Integer.Long */
#search_form
{
padding:0;
margin:0;
}

View File

@ -91,3 +91,11 @@ function validateComment(id)
return true; return true;
} }
function handleKeyPress(e){
var key=e.keyCode || e.which;
if (key==13){
f = document.getElementById("search_form");
f.submit();
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 483 B

3
templates/search.html Normal file
View File

@ -0,0 +1,3 @@
{% autoescape off %}
{{ result }}
{% endautoescape %}

View File

@ -35,6 +35,7 @@ urlpatterns = patterns('',
url(r'^tag/edit/(\d+)$', 'dynastie.views.edit_tag', name='edit_tag'), url(r'^tag/edit/(\d+)$', 'dynastie.views.edit_tag', name='edit_tag'),
url(r'^tag/delete/(\d+)$', 'dynastie.views.delete_tag', name='delete_tag'), url(r'^tag/delete/(\d+)$', 'dynastie.views.delete_tag', name='delete_tag'),
url(r'^search/generate/(\d+)$', 'dynastie.views.generate_search',name='generate_search'), url(r'^search/generate/(\d+)$', 'dynastie.views.generate_search',name='generate_search'),
url(r'^search/(\d+)$', 'dynastie.views.search', name='search'),
# url(r'^dynastie/', include('dynastie.foo.urls')), # url(r'^dynastie/', include('dynastie.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation: # Uncomment the admin/doc line below to enable admin documentation:

View File

@ -526,6 +526,38 @@ def generate_search(request, blog_id):
return _generate(request, blog_id, report) return _generate(request, blog_id, report)
@csrf_exempt
def search(request, blog_id):
from dynastie.generators import search
ref = request.META['HTTP_REFERER']
b = Blog.objects.filter(pk=blog_id)
if len(b) == 0:
return HttpResponseRedirect(ref)
b = b[0]
if 'text' in request.POST:
text = request.POST['text']
else:
return HttpResponseRedirect(ref)
s = Search()
post_list = s.search(b, text)
if post_list is None: post_list = []
s = search.Search()
b.create_paths()
res = s.generate(b, b.src_path, b.output_path, post_list)
c = {'result' : res}
return render(request, 'templates/search.html', c)
@login_required @login_required
def preview(request, blog_id): def preview(request, blog_id):
from dynastie.generators import post from dynastie.generators import post