Basic search works for now
This commit is contained in:
@@ -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')
|
||||
|
||||
@@ -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')
|
||||
|
||||
@@ -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
|
||||
|
||||
+23
-1
@@ -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')
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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')
|
||||
|
||||
+6
-3
@@ -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')
|
||||
|
||||
Reference in New Issue
Block a user