Add first generator
This commit is contained in:
parent
d6f9dfa76d
commit
8d6918e108
|
@ -1,6 +1,65 @@
|
|||
import os
|
||||
import hashlib
|
||||
from xml.dom import *
|
||||
|
||||
class DynastieGenerator:
|
||||
|
||||
URI = "http://indefero.soutade.fr/p/dynastie"
|
||||
report = ''
|
||||
|
||||
def addReport(self, string, color=''):
|
||||
if color != '':
|
||||
self.report = self.report + '<span style="color:' + color + '">'
|
||||
self.report = self.report + '<b>' + self.__class__.__name__ + '</b> : '
|
||||
self.report = self.report + string
|
||||
if color != '':
|
||||
self.report = self.report + '</span>'
|
||||
self.report = self.report + '<br/>\n'
|
||||
|
||||
def addWarning(self, string):
|
||||
self.addReport(string, 'yellow')
|
||||
|
||||
def addError(self, string):
|
||||
self.addReport(string, 'red')
|
||||
|
||||
def generate(self, blog, src, output):
|
||||
return
|
||||
|
||||
def writeIfNotTheSame(self, filename, content):
|
||||
if os.path.exists(filename):
|
||||
src_md5 = hashlib.md5()
|
||||
f = open(filename,'rb')
|
||||
src_md5.update(f.read())
|
||||
f.close()
|
||||
|
||||
dst_md5 = hashlib.md5()
|
||||
dst_md5.update(content)
|
||||
|
||||
if src_md5.digest() == dst_md5.digest():
|
||||
self.addReport(filename + ' regenerated with the same content, skipping...')
|
||||
return
|
||||
os.unlink(filename)
|
||||
|
||||
self.addReport('Write ' + filename)
|
||||
f = open(filename,'wb')
|
||||
f.write(content)
|
||||
f.close()
|
||||
|
||||
|
||||
def createElement(self, dom, name, content):
|
||||
div = dom.createElement('div')
|
||||
div.setAttribute('class', name)
|
||||
div.appendChild(dom.createTextNode(content))
|
||||
|
||||
return div
|
||||
|
||||
def simpleTransform(self, values, dom, elem, root):
|
||||
for node in root.childNodes:
|
||||
if node.prefix == 'dyn':
|
||||
if node.localName in values:
|
||||
new_elem = self.createElement(dom, node.localName, values[node.localName])
|
||||
elem.appendChild(new_elem)
|
||||
else:
|
||||
elem.appendChild(node.cloneNode(True))
|
||||
|
||||
|
||||
|
|
|
@ -1,16 +1,87 @@
|
|||
import datetime
|
||||
from xml.dom.minidom import parse
|
||||
from dynastie.generators.generator import DynastieGenerator
|
||||
from django.db import models
|
||||
|
||||
# TODO : links between indexes
|
||||
# TODO : gzip
|
||||
|
||||
class Index(DynastieGenerator):
|
||||
|
||||
cur_page = 0
|
||||
cur_article = 0
|
||||
articles_per_page = 0
|
||||
|
||||
def createArticle(self, article, dom, article_elem, root):
|
||||
values = {}
|
||||
values['title'] = article.title
|
||||
values['author'] = article.author.first_name
|
||||
values['date'] = article.creation_date.strftime("%d/%m/%Y")
|
||||
values['content'] = ''
|
||||
|
||||
self.simpleTransform(values, dom, article_elem, root)
|
||||
|
||||
def parse(self, articles, dom, root):
|
||||
for node in root.childNodes:
|
||||
if node.prefix == 'dyn':
|
||||
if node.localName == 'articles':
|
||||
articles_elem = dom.createElement("articles")
|
||||
for i in range(0, self.articles_per_page):
|
||||
article_elem = dom.createElementNS(self.URI, "article")
|
||||
self.createArticle(articles[self.cur_article], dom, article_elem, node)
|
||||
articles_elem.appendChild(article_elem)
|
||||
|
||||
self.cur_article = self.cur_article + 1
|
||||
if self.cur_article == articles.count():
|
||||
break
|
||||
root.replaceChild(articles_elem, node)
|
||||
continue
|
||||
if node.hasChildNodes():
|
||||
self.parse(articles, dom, node)
|
||||
return
|
||||
|
||||
def generate(self, blog, src, output):
|
||||
from dynastie.models import *
|
||||
articles = Article.objects.all()
|
||||
|
||||
dom = parse(src + '/_index.html')
|
||||
self.report = ''
|
||||
|
||||
for node in dom.NodeList:
|
||||
print node
|
||||
if not os.path.exists(src + '/_index.html'):
|
||||
self.addError('No _index.html found, exiting')
|
||||
return self.report
|
||||
|
||||
try:
|
||||
dom = parse(src + '/_index.html')
|
||||
except xml.dom.DOMException as e:
|
||||
self.addError('Error parsing _index.html : ' + e)
|
||||
return self.report
|
||||
|
||||
article_nodes = dom.getElementsByTagNameNS(self.URI, "articles")
|
||||
|
||||
if not article_nodes is None:
|
||||
self.articles_per_page = int(article_nodes[0].getAttribute("limit"))
|
||||
else:
|
||||
self.addError('No tag dyn:articles found')
|
||||
|
||||
now = datetime.datetime.now()
|
||||
cur_year = now.year
|
||||
articles = Article.objects.filter(creation_date__year=cur_year)
|
||||
|
||||
if articles.count() < self.articles_per_page:
|
||||
articles = Article.objects.all()[:self.articles_per_page]
|
||||
|
||||
filename = 'index.html'
|
||||
while self.cur_article < articles.count():
|
||||
nodes = dom.getElementsByTagName("*")
|
||||
self.parse(articles, dom, nodes[0])
|
||||
self.writeIfNotTheSame(output + '/' + filename, nodes[0].toxml('utf8'))
|
||||
self.cur_page = self.cur_page + 1
|
||||
filename = 'index' + str(self.cur_page) + '.html'
|
||||
|
||||
while os.path.exists(filename):
|
||||
self.addReport('Removing unused ' + filename)
|
||||
os.unlink(filename)
|
||||
self.cur_page = self.cur_page + 1
|
||||
filename = 'index' + str(self.cur_page) + '.html'
|
||||
|
||||
return self.report
|
||||
|
||||
return
|
||||
|
|
|
@ -97,11 +97,15 @@ class Blog(models.Model):
|
|||
def generate(self):
|
||||
self.load_generators()
|
||||
self.copytree(self.src_path, self.output_path)
|
||||
report = ''
|
||||
for engine in self.engines:
|
||||
for name, obj in inspect.getmembers(engine):
|
||||
if inspect.isclass(obj):
|
||||
e = obj()
|
||||
e.generate(self, self.src_path, self.output_path)
|
||||
r = e.generate(self, self.src_path, self.output_path)
|
||||
if not r is None:
|
||||
report = report + '<br/>\n' + r
|
||||
return report
|
||||
|
||||
class Editor(models.Model):
|
||||
name = models.CharField(max_length=255, unique=True)
|
||||
|
|
11
templates/generate.html
Normal file
11
templates/generate.html
Normal file
|
@ -0,0 +1,11 @@
|
|||
{% extends "templates/base.html" %}
|
||||
|
||||
{% block content %}
|
||||
{% if report|length == 0 %}
|
||||
<b style="color:red">Any engine selected</b><br/><br/>
|
||||
{% else %}
|
||||
{% autoescape off %}
|
||||
{{ report }}
|
||||
{% endautoescape %}
|
||||
{% endif %}
|
||||
{% endblock %}
|
Loading…
Reference in New Issue
Block a user