184 lines
		
	
	
		
			6.0 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			184 lines
		
	
	
		
			6.0 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
| # -*- coding: utf-8 -*-
 | |
| """
 | |
|   Copyright 2012-2013 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/>.
 | |
| """
 | |
| import os
 | |
| from xml.dom.minidom import parse, parseString
 | |
| from dynastie.generators.generator import DynastieGenerator
 | |
| from dynastie.generators.index import Index
 | |
| from django.db import models
 | |
| 
 | |
| class AllPosts(Index):
 | |
| 
 | |
|     filename = 'all_posts'
 | |
|     cur_post = 0
 | |
| 
 | |
|     def createPosts(self, posts, dom, root, node):
 | |
|         posts_elem = self.createElement(dom, 'posts')
 | |
| 
 | |
|         self.cur_post = 0
 | |
|         for p in posts:
 | |
|             self.cur_post_obj = p
 | |
| 
 | |
|             post_elem = self.createElement(dom, 'post', '', subtree=node)
 | |
| 
 | |
|             self._parse(self.hooks, posts, dom, post_elem)
 | |
|             posts_elem.appendChild(post_elem)
 | |
|             self.cur_post = self.cur_post + 1
 | |
| 
 | |
|         root.replaceChild(posts_elem, node)
 | |
|             
 | |
|         
 | |
|     def createMonth(self, posts, dom, root, node):
 | |
|         date_format = node.getAttribute('format')
 | |
|         if date_format == '':
 | |
|             date_format = '%B'
 | |
| 
 | |
|         self.cur_month = posts[0].creation_date.month
 | |
| 
 | |
|         root.removeChild(node)
 | |
|         cur_posts = []
 | |
|         month_elem = None
 | |
|         prev_month = None
 | |
|         for p in posts:
 | |
|             if p.creation_date.month == self.cur_month:
 | |
|                 cur_posts.append(p)
 | |
|                 continue
 | |
| 
 | |
|             cur_posts.reverse()
 | |
| 
 | |
|             month_elem = self.createElement(dom, 'month')
 | |
|             month_def = dom.createElement('month')
 | |
|             month_def.appendChild(dom.createTextNode(cur_posts[0].creation_date.strftime(date_format)))
 | |
|             month_elem.appendChild(month_def)
 | |
|             self.cloneSubtree(month_elem, node)
 | |
| 
 | |
|             # Parse inner HTML
 | |
|             self._parse(self.hooks, cur_posts, dom, month_elem)
 | |
| 
 | |
|             if not root.hasChildNodes():
 | |
|                 root.appendChild(month_elem)
 | |
|             else:
 | |
|                 root.insertBefore(month_elem, prev_month)
 | |
|             prev_month = month_elem
 | |
|             cur_posts = []
 | |
|             self.cur_month = p.creation_date.month
 | |
|             cur_posts.append(p)
 | |
| 
 | |
|         # Last month
 | |
|         if len(cur_posts) != 0:
 | |
|             cur_posts.reverse()
 | |
|             month_elem = self.createElement(dom, 'month')
 | |
|             month_def = dom.createElement('month')
 | |
|             month_def.appendChild(dom.createTextNode(cur_posts[0].creation_date.strftime(date_format)))
 | |
|             month_elem.appendChild(month_def)
 | |
|             self.cloneSubtree(month_elem, node)
 | |
| 
 | |
|             # Parse inner HTML
 | |
|             self._parse(self.hooks, cur_posts, dom, month_elem)
 | |
|             if not root.hasChildNodes():
 | |
|                 root.appendChild(month_elem)
 | |
|             else:
 | |
|                 root.insertBefore(month_elem, prev_month)
 | |
| 
 | |
| 
 | |
|         return None
 | |
| 
 | |
|     def createYear(self, posts, dom, root, node):
 | |
|         date_format = node.getAttribute('format')
 | |
|         if date_format == '':
 | |
|             date_format = '%Y'
 | |
| 
 | |
|         self.cur_year = posts[0].creation_date.year
 | |
| 
 | |
|         root.removeChild(node)
 | |
|         cur_posts = []
 | |
|         year_elem = None
 | |
|         for p in posts:
 | |
|             if p.creation_date.year == self.cur_year:
 | |
|                 cur_posts.append(p)
 | |
|                 continue
 | |
| 
 | |
|             year_elem = self.createElement(dom, 'year')
 | |
|             year_def = dom.createElement('year')
 | |
|             year_def.appendChild(dom.createTextNode(cur_posts[0].creation_date.strftime(date_format)))
 | |
|             year_elem.appendChild(year_def)
 | |
|             self.cloneSubtree(year_elem, node)
 | |
| 
 | |
|             # Parse inner HTML
 | |
|             self._parse(self.hooks, cur_posts, dom, year_elem)
 | |
| 
 | |
|             root.appendChild(year_elem)
 | |
|             cur_posts = []
 | |
|             self.cur_year = p.creation_date.year
 | |
|             cur_posts.append(p)
 | |
| 
 | |
|         # Last year
 | |
|         if len(cur_posts) != 0:
 | |
|             year_elem = self.createElement(dom, 'year')
 | |
|             year_def = dom.createElement('year')
 | |
|             year_def.appendChild(dom.createTextNode(cur_posts[0].creation_date.strftime(date_format)))
 | |
|             year_elem.appendChild(year_def)
 | |
|             self.cloneSubtree(year_elem, node)
 | |
| 
 | |
|             # Parse inner HTML
 | |
|             self._parse(self.hooks, cur_posts, dom, year_elem)
 | |
|             root.appendChild(year_elem)
 | |
| 
 | |
|         return None
 | |
| 
 | |
|     def createAllPosts(self, posts, dom, root, node):
 | |
|         posts_elem = self.createElement(dom, 'all_posts')
 | |
| 
 | |
|         if len(posts) == 0:
 | |
|             post_elem = self.createElement(dom, '', '<b>No posts yet</b>')
 | |
|             posts_elem.appendChild(post_elem)
 | |
|         else:
 | |
|             for cnode in node.childNodes:
 | |
|                 new_node = cnode.cloneNode(True)
 | |
|                 posts_elem.appendChild(new_node)
 | |
| 
 | |
|             self._parse(self.hooks, posts, dom, posts_elem)
 | |
| 
 | |
|         root.replaceChild(posts_elem, node)
 | |
|         return posts_elem
 | |
| 
 | |
|     def generate(self, blog, src, output):
 | |
|         from dynastie.models import Post, Blog, Category
 | |
| 
 | |
|         self.hooks['year'] = self.createYear
 | |
|         self.hooks['month'] = self.createMonth
 | |
|         self.hooks['posts'] = self.createPosts
 | |
|         self.hooks['all_posts'] = self.createAllPosts
 | |
| 
 | |
|         dom = self.parseTemplate(blog, src, output, 'all_posts')
 | |
|         if dom is None: return self.report
 | |
| 
 | |
|         posts = Post.objects.filter(published=True).order_by('-creation_date')
 | |
|         nodes = dom.getElementsByTagName("*")
 | |
| 
 | |
|         nodes[0] = self.parse(src, self.hooks, posts, dom, nodes[0])
 | |
| 
 | |
|         self.writeIfNotTheSame(output + "/" + self.filename + '.html', nodes[0])
 | |
| 
 | |
|         if not self.somethingWrote:
 | |
|             self.addReport('Nothing changed')
 | |
| 
 | |
|         return self.report
 | |
| 
 |