Add content to articles and link tag to titles
This commit is contained in:
parent
99c5a9e6e9
commit
cc29c0a9d7
|
@ -14,7 +14,6 @@ class Article(DynastieGenerator):
|
||||||
if article.creation_date != None:
|
if article.creation_date != None:
|
||||||
values['date'] = article.creation_date.strftime("%d/%m/%Y")
|
values['date'] = article.creation_date.strftime("%d/%m/%Y")
|
||||||
values['content'] = ''
|
values['content'] = ''
|
||||||
print article.title
|
|
||||||
|
|
||||||
self.simpleTransform(values, dom, article_elem, root)
|
self.simpleTransform(values, dom, article_elem, root)
|
||||||
|
|
||||||
|
|
|
@ -63,6 +63,14 @@ class DynastieGenerator:
|
||||||
|
|
||||||
self.somethingWrote = True
|
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=''):
|
def createElement(self, dom, name='', content=''):
|
||||||
div = dom.createElement('div')
|
div = dom.createElement('div')
|
||||||
if name != '':
|
if name != '':
|
||||||
|
@ -76,7 +84,12 @@ class DynastieGenerator:
|
||||||
for node in root.childNodes:
|
for node in root.childNodes:
|
||||||
if node.prefix == 'dyn':
|
if node.prefix == 'dyn':
|
||||||
if node.localName in values:
|
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)
|
elem.appendChild(new_elem)
|
||||||
else:
|
else:
|
||||||
new_elem = node.cloneNode(False)
|
new_elem = node.cloneNode(False)
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import os
|
import os
|
||||||
import datetime
|
import datetime
|
||||||
|
from xml.parsers.expat import *
|
||||||
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
|
||||||
|
@ -54,13 +55,42 @@ class Index(DynastieGenerator):
|
||||||
|
|
||||||
def createArticle(self, article, dom, article_elem, root):
|
def createArticle(self, article, dom, article_elem, root):
|
||||||
values = {}
|
values = {}
|
||||||
values['title'] = article.title
|
values['title'] = self.createLinkElem(dom, article.getPath(), article.title)
|
||||||
values['author'] = article.author.first_name
|
values['author'] = article.author.first_name + ' ' + article.author.last_name
|
||||||
values['date'] = article.creation_date.strftime("%d/%m/%Y")
|
values['date'] = article.creation_date.strftime("%A, %d %B %Y %H:%m")
|
||||||
values['content'] = ''
|
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)
|
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):
|
def createArticles(self, articles, dom, root, node):
|
||||||
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):
|
||||||
|
@ -81,20 +111,20 @@ class Index(DynastieGenerator):
|
||||||
nb_recents = int(node.getAttribute("limit"))
|
nb_recents = int(node.getAttribute("limit"))
|
||||||
else:
|
else:
|
||||||
nb_recents = 5
|
nb_recents = 5
|
||||||
|
recents_elem = self.createElement(dom, 'recents')
|
||||||
list_elem = dom.createElement('ul')
|
list_elem = dom.createElement('ul')
|
||||||
for i in range(0, nb_recents):
|
for i in range(0, nb_recents):
|
||||||
article_elem = dom.createElement('li')
|
article_elem = dom.createElement('li')
|
||||||
if self.cur_article+i < len(articles):
|
if self.cur_article+i < len(articles):
|
||||||
link_elem = dom.createElement('a')
|
article = articles[self.cur_article+i]
|
||||||
link_elem.setAttribute('href', articles[self.cur_article+i].getPath())
|
link_elem = self.createLinkElem(dom, article.getPath(), article.title)
|
||||||
text_elem = dom.createTextNode(articles[self.cur_article+i].title)
|
|
||||||
link_elem.appendChild(text_elem)
|
|
||||||
article_elem.appendChild(link_elem)
|
article_elem.appendChild(link_elem)
|
||||||
else:
|
else:
|
||||||
break
|
break
|
||||||
list_elem.appendChild(article_elem)
|
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):
|
def generate(self, blog, src, output):
|
||||||
from dynastie.models import Article, Blog
|
from dynastie.models import Article, Blog
|
||||||
|
@ -138,7 +168,7 @@ class Index(DynastieGenerator):
|
||||||
#print 'Generate ' + filename
|
#print 'Generate ' + filename
|
||||||
nodes = dom.getElementsByTagName("*")
|
nodes = dom.getElementsByTagName("*")
|
||||||
self.parse(hooks, articles, dom, nodes[0])
|
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
|
self.cur_page = self.cur_page + 1
|
||||||
filename = 'index' + str(self.cur_page) + '.html'
|
filename = 'index' + str(self.cur_page) + '.html'
|
||||||
dom = parse(src + '/_index.html')
|
dom = parse(src + '/_index.html')
|
||||||
|
|
Loading…
Reference in New Issue
Block a user