Add archive generator
This commit is contained in:
parent
47d68daec0
commit
7a4b0892d3
|
@ -1 +1 @@
|
||||||
__all__ = ["generator", "index", "article", "category"]
|
__all__ = ["generator", "index", "article", "category", "archive"]
|
||||||
|
|
104
generators/archive.py
Normal file
104
generators/archive.py
Normal file
|
@ -0,0 +1,104 @@
|
||||||
|
import os
|
||||||
|
from datetime import datetime
|
||||||
|
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 Archive(Index):
|
||||||
|
|
||||||
|
cur_page = 0
|
||||||
|
nb_pages = 0
|
||||||
|
cur_article = 0
|
||||||
|
articles_per_page = 0
|
||||||
|
filename = 'index'
|
||||||
|
cur_year = 0
|
||||||
|
|
||||||
|
def createArchive(self, articles, dom, root, node):
|
||||||
|
if node.hasAttribute('year'):
|
||||||
|
self.replaceByText(dom, root, node, str(self.cur_year))
|
||||||
|
|
||||||
|
def createArchives(self, src, output, dom, hooks, articles):
|
||||||
|
filename = self.filename + '.html'
|
||||||
|
self.nb_pages = 0
|
||||||
|
self.cur_page = 0
|
||||||
|
if len(articles) > self.articles_per_page:
|
||||||
|
self.nb_pages = len(articles) / self.articles_per_page
|
||||||
|
|
||||||
|
if not os.path.exists(output + '/archive/' + str(self.cur_year)):
|
||||||
|
os.mkdir(output + '/archive/' + str(self.cur_year))
|
||||||
|
|
||||||
|
while self.cur_page <= self.nb_pages:
|
||||||
|
#print 'Generate ' + filename
|
||||||
|
nodes = dom.getElementsByTagName("*")
|
||||||
|
self.parse(hooks, articles, dom, nodes[0])
|
||||||
|
self.writeIfNotTheSame(output + '/archive/' + str(self.cur_year) + '/' + filename, nodes[0].toxml('utf8'))
|
||||||
|
self.cur_page = self.cur_page + 1
|
||||||
|
filename = self.filename + str(self.cur_page) + '.html'
|
||||||
|
dom = parse(src + '/_archive.html')
|
||||||
|
|
||||||
|
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 = output + '/archive/' + str(self.cur_year) + '/' + self.filename + str(self.cur_page) + '.html'
|
||||||
|
|
||||||
|
def generate(self, blog, src, output):
|
||||||
|
from dynastie.models import Article, Blog
|
||||||
|
|
||||||
|
hooks = {'articles' : self.createArticles,
|
||||||
|
'navigation' : self.createNavigation,
|
||||||
|
'archive' : self.createArchive}
|
||||||
|
|
||||||
|
if not os.path.exists(src + '/_archive.html'):
|
||||||
|
self.addError('No _archive.html found, exiting')
|
||||||
|
return self.report
|
||||||
|
|
||||||
|
try:
|
||||||
|
dom = parse(src + '/_archive.html')
|
||||||
|
except xml.dom.DOMException as e:
|
||||||
|
self.addError('Error parsing _archive.html : ' + e)
|
||||||
|
return self.report
|
||||||
|
|
||||||
|
if not os.path.exists(output + '/archive'):
|
||||||
|
os.mkdir(output + '/archive')
|
||||||
|
|
||||||
|
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')
|
||||||
|
|
||||||
|
articles = Article.objects.all()
|
||||||
|
|
||||||
|
if articles.count() != 0:
|
||||||
|
self.cur_year = articles[0].creation_date.year
|
||||||
|
|
||||||
|
my_articles = []
|
||||||
|
now = datetime.now()
|
||||||
|
for article in articles:
|
||||||
|
# if self.cur_year == now.year:
|
||||||
|
# break
|
||||||
|
|
||||||
|
if article.creation_date.year != self.cur_year:
|
||||||
|
self.createArchives(src, output, dom, hooks, my_articles)
|
||||||
|
self.cur_year = article.creation_date.year
|
||||||
|
#print 'New year ' + str(self.cur_year)
|
||||||
|
# if self.cur_year == now.year:
|
||||||
|
# continue
|
||||||
|
my_articles = []
|
||||||
|
else:
|
||||||
|
my_articles.append(article)
|
||||||
|
|
||||||
|
if len(my_articles) != 0:
|
||||||
|
self.createArchives(src, output, dom, hooks, my_articles)
|
||||||
|
|
||||||
|
if not self.somethingWrote:
|
||||||
|
self.addReport('Nothing changed')
|
||||||
|
|
||||||
|
return self.report
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
import os
|
import os
|
||||||
import datetime
|
|
||||||
from xml.dom.minidom import parse, parseString
|
from xml.dom.minidom import parse, parseString
|
||||||
from dynastie.generators.generator import DynastieGenerator
|
from dynastie.generators.generator import DynastieGenerator
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
import os
|
import os
|
||||||
import datetime
|
|
||||||
from xml.dom.minidom import parse, parseString
|
from xml.dom.minidom import parse, parseString
|
||||||
from dynastie.generators.generator import DynastieGenerator
|
from dynastie.generators.generator import DynastieGenerator
|
||||||
from dynastie.generators.index import Index
|
from dynastie.generators.index import Index
|
||||||
|
|
|
@ -60,14 +60,14 @@ class Index(DynastieGenerator):
|
||||||
articles_elem = self.createElement(dom, 'articles')
|
articles_elem = self.createElement(dom, 'articles')
|
||||||
for i in range(0, self.articles_per_page):
|
for i in range(0, self.articles_per_page):
|
||||||
article_elem = self.createElement(dom, 'article')
|
article_elem = self.createElement(dom, 'article')
|
||||||
if articles.count() > self.cur_article:
|
if len(articles) > self.cur_article:
|
||||||
self.createArticle(articles[self.cur_article], dom, article_elem, node)
|
self.createArticle(articles[self.cur_article], dom, article_elem, node)
|
||||||
else:
|
else:
|
||||||
article_elem = self.createElement(dom, '', '<b>No articles yet</b>')
|
article_elem = self.createElement(dom, '', '<b>No articles yet</b>')
|
||||||
articles_elem.appendChild(article_elem)
|
articles_elem.appendChild(article_elem)
|
||||||
|
|
||||||
self.cur_article = self.cur_article + 1
|
self.cur_article = self.cur_article + 1
|
||||||
if self.cur_article == articles.count():
|
if self.cur_article == len(articles):
|
||||||
break
|
break
|
||||||
root.replaceChild(articles_elem, node)
|
root.replaceChild(articles_elem, node)
|
||||||
|
|
||||||
|
|
42
models.py
42
models.py
|
@ -49,12 +49,15 @@ class Blog(models.Model):
|
||||||
if os.path.exists(self.src_path + '/_generators'):
|
if os.path.exists(self.src_path + '/_generators'):
|
||||||
f = open(self.src_path + '/_generators', 'r')
|
f = open(self.src_path + '/_generators', 'r')
|
||||||
for line in f:
|
for line in f:
|
||||||
|
if line.startswith("#"):
|
||||||
|
continue
|
||||||
self.engines.append(globals()[line.strip()])
|
self.engines.append(globals()[line.strip()])
|
||||||
f.close()
|
f.close()
|
||||||
else:
|
else:
|
||||||
|
self.engines.append(globals()['article'])
|
||||||
self.engines.append(globals()['index'])
|
self.engines.append(globals()['index'])
|
||||||
self.engines.append(globals()['categories'])
|
self.engines.append(globals()['category'])
|
||||||
self.engines.append(globals()['archives'])
|
self.engines.append(globals()['archive'])
|
||||||
|
|
||||||
def copytree(self, src, dst):
|
def copytree(self, src, dst):
|
||||||
names = os.listdir(src)
|
names = os.listdir(src)
|
||||||
|
@ -106,11 +109,16 @@ class Blog(models.Model):
|
||||||
self.report = '<br/><br/>Generation of ' + datetime.now().strftime("%d/%m/%Y at %H:%M:%S") + '<br/>\n'
|
self.report = '<br/><br/>Generation of ' + datetime.now().strftime("%d/%m/%Y at %H:%M:%S") + '<br/>\n'
|
||||||
self.load_generators()
|
self.load_generators()
|
||||||
self.copytree(self.src_path, self.output_path)
|
self.copytree(self.src_path, self.output_path)
|
||||||
|
generated = []
|
||||||
for engine in self.engines:
|
for engine in self.engines:
|
||||||
|
if not inspect.ismodule(engine):
|
||||||
|
continue
|
||||||
for name, obj in inspect.getmembers(engine):
|
for name, obj in inspect.getmembers(engine):
|
||||||
if inspect.isclass(obj):
|
if inspect.isclass(obj) and obj.__module__.startswith("dynastie"):
|
||||||
|
if obj.__module__ in generated: continue
|
||||||
e = obj()
|
e = obj()
|
||||||
r = e.generate(self, self.src_path, self.output_path)
|
r = e.generate(self, self.src_path, self.output_path)
|
||||||
|
generated.append(obj.__module__)
|
||||||
if not r is None:
|
if not r is None:
|
||||||
self.report = self.report + '<br/>\n' + r
|
self.report = self.report + '<br/>\n' + r
|
||||||
|
|
||||||
|
@ -137,6 +145,34 @@ class Category(models.Model):
|
||||||
self.slugify()
|
self.slugify()
|
||||||
super(Category, self).save()
|
super(Category, self).save()
|
||||||
|
|
||||||
|
def remove(self, blog):
|
||||||
|
blog.create_paths()
|
||||||
|
|
||||||
|
output = blob.output_path
|
||||||
|
cur_cat = 0
|
||||||
|
while True:
|
||||||
|
found = False
|
||||||
|
filename = output + '/category/'
|
||||||
|
if cur_cat == 0:
|
||||||
|
filename = filename + self.name_slug + '.html'
|
||||||
|
else:
|
||||||
|
filename = filename + self.name_slug + str(cur_cat) + '.html'
|
||||||
|
if os.path.exists(filename):
|
||||||
|
os.unlink(filename)
|
||||||
|
found = True
|
||||||
|
filename = filename + '.gz'
|
||||||
|
if os.path.exists(filename):
|
||||||
|
os.unlink(filename)
|
||||||
|
found = True
|
||||||
|
if not found:
|
||||||
|
break
|
||||||
|
cur_cat = cur_cat + 1
|
||||||
|
|
||||||
|
filename = output + '/category/'
|
||||||
|
if len(os.listdir(filename)) == 0:
|
||||||
|
os.rmdir(filename)
|
||||||
|
|
||||||
|
|
||||||
class Tag(models.Model):
|
class Tag(models.Model):
|
||||||
name = models.CharField(max_length=255, unique=True)
|
name = models.CharField(max_length=255, unique=True)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user