From 2f1f38ca5c3a7698f580665dc9f2469e1dacb058 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9gory=20Soutad=C3=A9?= Date: Mon, 10 Dec 2012 19:30:25 +0100 Subject: [PATCH] Basic search works for now --- generators/archive.py | 9 ++- generators/category.py | 9 ++- generators/generator.py | 4 +- generators/index.py | 24 +++++++- generators/post.py | 4 ++ generators/search.py | 57 ++++++++++++++++++ generators/tag.py | 9 ++- search.py | 15 +++-- sites/blog.soutade.fr/_base.html | 21 ++++--- sites/blog.soutade.fr/_base_post.html | 12 ++-- sites/blog.soutade.fr/css/blog.css | 6 ++ sites/blog.soutade.fr/js/blog.js | 8 +++ .../dynastiecolor/img/dynastiecolor.png | Bin 0 -> 483 bytes templates/search.html | 3 + urls.py | 1 + views.py | 32 ++++++++++ 16 files changed, 185 insertions(+), 29 deletions(-) create mode 100644 generators/search.py create mode 100644 static/js/tinymce/jscripts/tiny_mce/plugins/dynastiecolor/img/dynastiecolor.png create mode 100644 templates/search.html diff --git a/generators/archive.py b/generators/archive.py index 808430f..a3aaaf3 100644 --- a/generators/archive.py +++ b/generators/archive.py @@ -58,9 +58,12 @@ class Archive(Index): from dynastie.models import Post, Blog self.hooks = {'posts' : self.createPosts, - 'navigation' : self.createNavigation, - 'archive' : self.createArchive, - 'tags' : self.createTags} + 'navigation' : self.createNavigation, + 'archive' : self.createArchive, + 'tags' : self.createTags, + 'replace' : self.createReplace} + + self.blog = blog if not os.path.exists(src + '/_archive.html'): self.addError('No _archive.html found, exiting') diff --git a/generators/category.py b/generators/category.py index 28a2f1a..a8bddcb 100644 --- a/generators/category.py +++ b/generators/category.py @@ -25,9 +25,12 @@ class Category(Index): from dynastie.models import Post, Blog, Category self.hooks = {'posts' : self.createPosts, - 'navigation' : self.createNavigation, - 'category' : self.createCategory, - 'tags' : self.createTags} + 'navigation' : self.createNavigation, + 'category' : self.createCategory, + 'tags' : self.createTags, + 'replace' : self.createReplace} + + self.blog = blog if not os.path.exists(src + '/_category.html'): self.addError('No _category.html found, exiting') diff --git a/generators/generator.py b/generators/generator.py index 0851fa7..0bf2123 100644 --- a/generators/generator.py +++ b/generators/generator.py @@ -21,10 +21,10 @@ class StrictUTF8Writer(codecs.StreamWriter): object = object.replace('>', '>') object = object.replace('"', '"') object = object.replace(''', "'") - object = object.replace('&', "&") + object = object.replace('&', '&') if not type(object) == unicode: - self.value = self.value + unicode(object, "utf-8") + self.value = self.value + unicode(object, 'utf-8') else: self.value = self.value + object return self.value diff --git a/generators/index.py b/generators/index.py index 76977eb..6fc76e6 100644 --- a/generators/index.py +++ b/generators/index.py @@ -14,6 +14,25 @@ class Index(DynastieGenerator): posts_per_page = 0 filename = 'index' 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): if self.nb_pages == 0 or self.nb_pages == 1: @@ -262,7 +281,10 @@ class Index(DynastieGenerator): self.hooks = {'posts' : self.createPosts, 'navigation' : self.createNavigation, 'recents' : self.createRecents, - 'tags' : self.createTags} + 'tags' : self.createTags, + 'replace' : self.createReplace} + + self.blog = blog if not os.path.exists(src + '/_index.html'): self.addError('No _index.html found, exiting') diff --git a/generators/post.py b/generators/post.py index 54102c6..05689cd 100644 --- a/generators/post.py +++ b/generators/post.py @@ -31,6 +31,8 @@ class Post(Index): else: 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) root.replaceChild(div_element, replace_elem) @@ -177,6 +179,8 @@ class Post(Index): def generate(self, blog, src, output): from dynastie.models import Post, Blog + self.blog = blog + posts = Post.objects.all() return self._generate(blog, src, output, posts) diff --git a/generators/search.py b/generators/search.py new file mode 100644 index 0000000..8e99547 --- /dev/null +++ b/generators/search.py @@ -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', 'No result found') + 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') + diff --git a/generators/tag.py b/generators/tag.py index 3dcd73b..074a66e 100644 --- a/generators/tag.py +++ b/generators/tag.py @@ -25,9 +25,12 @@ class Tag(Index): from dynastie.models import Post, Blog, Tag self.hooks = {'posts' : self.createPosts, - 'navigation' : self.createNavigation, - 'tag' : self.createTag, - 'tags' : self.createTags} + 'navigation' : self.createNavigation, + 'tag' : self.createTag, + 'tags' : self.createTags, + 'replace' : self.createReplace} + + self.blog = blog if not os.path.exists(src + '/_tag.html'): self.addError('No _tag.html found, exiting') diff --git a/search.py b/search.py index 0d77d66..503a27f 100644 --- a/search.py +++ b/search.py @@ -50,6 +50,7 @@ class Search: filename = blog.src_path + '/_search.db' if not os.path.exists(filename): + print 'No search index !' return None f = open(filename, 'rb') @@ -169,16 +170,16 @@ class Search: def search(self, blog, string): hashtable = self._loadDatabase(blog) - string = self._prepare_string(string) + string = self._prepare_string(string.encode('utf-8')) wordlist = re.findall(self.wordreg, string) res = {} for word in wordlist: - if len(word) < 4: + if len(word) < Search.MINIMUM_LETTERS: continue 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] if word not in hashtable: continue @@ -186,7 +187,11 @@ class Search: if not post in res: res[post] = 0 res[post] = res[post] + 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 diff --git a/sites/blog.soutade.fr/_base.html b/sites/blog.soutade.fr/_base.html index 8239cd8..2a35b3c 100755 --- a/sites/blog.soutade.fr/_base.html +++ b/sites/blog.soutade.fr/_base.html @@ -9,6 +9,7 @@ + @@ -23,17 +24,21 @@