# -*- coding: utf-8 -*-
"""
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
aftercode = code[28:-13] code = '' + code + '' return code def parseTemplate(self, blog, src, output, name, directory=None, parsePostsTag=True): self.blog = blog if not os.path.exists(src + '/_%s.html' % name): self.addError('No _%s.html found, exiting' % name) return None try: dom = parse(src + '/_%s.html' % name) except xml.dom.DOMException as e: self.addError('Error parsing _%s.html : ' + e) return None if not directory is None and not os.path.exists(output + '/' + directory): os.mkdir(output + '/' + directory) if not parsePostsTag: return dom 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 = 5 else: self.addError('No tag dyn:posts found') return dom def generatePages(self, dom, posts, src, output, name): if len(posts) > self.posts_per_page: self.nb_pages = self.computeNbPages(len(posts), self.posts_per_page) if not os.path.exists(output + self.dirname): os.mkdir(output + self.dirname) filename = self.dirname + '/' + self.filename + '.html' impl = xml.dom.getDOMImplementation() while self.cur_page <= self.nb_pages: #print 'Generate ' + filename dom_ = impl.createDocument('', 'xml', None) dom_.replaceChild(dom.firstChild.cloneNode(0), dom_.firstChild) nodes = dom.getElementsByTagName("*") nodes[0] = self.parse(src, self.hooks, posts, dom_, nodes[0]) self.writeIfNotTheSame(output + filename, nodes[0]) self.cur_page = self.cur_page + 1 filename = self.dirname + '/' + self.filename + str(self.cur_page) + '.html' filename = output + filename while os.path.exists(filename): self.addReport('Removing unused ' + filename) os.unlink(filename) filename = filename + '.gz' if os.path.exists(filename): self.addReport('Removing unused ' + filename) os.unlink(filename) self.cur_page = self.cur_page + 1 filename = self.dirname + '/' + self.filename + str(self.cur_page) + '.html' filename = output + filename def generate(self, blog, src, output): from dynastie.models import Post, Blog dom = self.parseTemplate(blog, src, output, 'index') if dom is None: return self.report now = datetime.datetime.now() cur_year = now.year posts = Post.objects.filter(creation_date__year=cur_year, published=True, front_page=True).order_by('-creation_date') if posts.count() < self.posts_per_page: posts = Post.objects.filter(published=True, front_page=True).order_by('-creation_date')[:self.posts_per_page] self.dirname = '' self.generatePages(dom, posts, src, output, 'index') if not self.somethingWrote: self.addReport('Nothing changed') return self.report