Add content to articles and link tag to titles

This commit is contained in:
Grégory Soutadé 2012-08-05 13:36:55 +02:00
parent 99c5a9e6e9
commit cc29c0a9d7
3 changed files with 54 additions and 12 deletions

View File

@ -14,7 +14,6 @@ class Article(DynastieGenerator):
if article.creation_date != None:
values['date'] = article.creation_date.strftime("%d/%m/%Y")
values['content'] = ''
print article.title
self.simpleTransform(values, dom, article_elem, root)

View File

@ -63,6 +63,14 @@ class DynastieGenerator:
self.somethingWrote = True
def createLinkElem(self, dom, path, title):
link_elem = dom.createElement('a')
link_elem.setAttribute('href', path)
text_elem = dom.createTextNode(title)
link_elem.appendChild(text_elem)
return link_elem
def createElement(self, dom, name='', content=''):
div = dom.createElement('div')
if name != '':
@ -76,7 +84,12 @@ class DynastieGenerator:
for node in root.childNodes:
if node.prefix == 'dyn':
if node.localName in values:
new_elem = self.createElement(dom, node.localName, values[node.localName])
content = values[node.localName]
if type(content) == unicode or type(content) == str:
new_elem = self.createElement(dom, node.localName, content)
else:
new_elem = self.createElement(dom, node.localName)
new_elem.appendChild(content)
elem.appendChild(new_elem)
else:
new_elem = node.cloneNode(False)

View File

@ -1,5 +1,6 @@
import os
import datetime
from xml.parsers.expat import *
from xml.dom.minidom import parse, parseString
from dynastie.generators.generator import DynastieGenerator
from django.db import models
@ -54,13 +55,42 @@ class Index(DynastieGenerator):
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'] = ''
values['title'] = self.createLinkElem(dom, article.getPath(), article.title)
values['author'] = article.author.first_name + ' ' + article.author.last_name
values['date'] = article.creation_date.strftime("%A, %d %B %Y %H:%m")
values['article_content'] = ''
blog = article.blog
blog.create_paths()
filename = blog.src_path + '/_articles/' + str(article.id)
if not os.path.exists(filename):
self.addError('File does not exists ' + filename)
return
f = open(filename, 'rb')
content = '<div id="123">' + f.read() + '</div>'
f.close()
dom2 = None
try:
dom2 = parseString(content)
except ExpatError, e:
self.addError('Error parsing ' + filename)
pass
self.simpleTransform(values, dom, article_elem, root)
content_nodes = article_elem.getElementsByTagName("div")
post_transform = ('article_content')
for content_node in content_nodes:
the_class = content_node.getAttribute('class')
if not the_class in post_transform:
continue
if the_class == 'article_content' and dom2 != None:
for article_node in dom2.firstChild.childNodes:
content_node.appendChild(article_node)
def createArticles(self, articles, dom, root, node):
articles_elem = self.createElement(dom, 'articles')
for i in range(0, self.articles_per_page):
@ -81,20 +111,20 @@ class Index(DynastieGenerator):
nb_recents = int(node.getAttribute("limit"))
else:
nb_recents = 5
recents_elem = self.createElement(dom, 'recents')
list_elem = dom.createElement('ul')
for i in range(0, nb_recents):
article_elem = dom.createElement('li')
if self.cur_article+i < len(articles):
link_elem = dom.createElement('a')
link_elem.setAttribute('href', articles[self.cur_article+i].getPath())
text_elem = dom.createTextNode(articles[self.cur_article+i].title)
link_elem.appendChild(text_elem)
article = articles[self.cur_article+i]
link_elem = self.createLinkElem(dom, article.getPath(), article.title)
article_elem.appendChild(link_elem)
else:
break
list_elem.appendChild(article_elem)
root.replaceChild(list_elem, node)
recents_elem.appendChild(list_elem)
root.replaceChild(recents_elem, node)
def generate(self, blog, src, output):
from dynastie.models import Article, Blog
@ -138,7 +168,7 @@ class Index(DynastieGenerator):
#print 'Generate ' + filename
nodes = dom.getElementsByTagName("*")
self.parse(hooks, articles, dom, nodes[0])
self.writeIfNotTheSame(output + '/' + filename, nodes[0].toxml('utf8'))
self.writeIfNotTheSame(output + '/' + filename, nodes[0].toxml(encoding='utf-8'))
self.cur_page = self.cur_page + 1
filename = 'index' + str(self.cur_page) + '.html'
dom = parse(src + '/_index.html')