Dynastie/dynastie/generators/search.py

76 lines
2.5 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
"""
2014-01-04 13:55:30 +01:00
Copyright 2012-2014 Grégory Soutadé
This file is part of Dynastie.
Dynastie is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Dynastie is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Dynastie. If not, see <http://www.gnu.org/licenses/>.
"""
2012-12-10 19:30:25 +01:00
import os
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
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")
2014-09-24 20:27:27 +02:00
if post_nodes:
2012-12-10 19:30:25 +01:00
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:
2012-12-24 18:12:51 +01:00
try:
post = Post.objects.get(pk=post_id)
except:
continue
2014-09-24 20:27:27 +02:00
if post:
2012-12-10 19:30:25 +01:00
posts.append(post)
nodes = dom.getElementsByTagName("*")
nodes[0] = self.parse(src, self.hooks, posts, dom, nodes[0])
writer = StrictUTF8Writer()
nodes[0].writexml(writer)
2022-06-19 10:12:51 +02:00
return writer.getvalue().decode('utf-8')
2012-12-10 19:30:25 +01:00