Dynastie/dynastie/generators/ljdc.py

101 lines
3.1 KiB
Python
Executable File

# -*- coding: utf-8 -*-
"""
Copyright 2012-2014 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
import xml.parsers.expat
from dynastie.generators.index import Index
from django.db import models
class LJDC(Index):
def createPost(self, posts, dom, post_elem, root):
new_elem = self.createElement(dom, 'ljdc')
address = self.cur_post_obj.getElementsByTagName('address')[0]
a = dom.createElement('a')
a.setAttribute('href', address.childNodes[0].nodeValue)
title_value = self.cur_post_obj.getElementsByTagName('title')[0]
title_value = title_value.childNodes[0].nodeValue
title = self.createElement(dom, 'title', title_value)
a.appendChild(title)
img_src = self.cur_post_obj.getElementsByTagName('img')[0]
img = dom.createElement('img')
img.setAttribute('src', img_src.childNodes[0].nodeValue)
a.appendChild(img)
new_elem.appendChild(a)
self.cur_post_obj = None
return new_elem
def _load_references(self, src):
name = '_ljdc.xml'
if not os.path.exists(src + '/%s' % name):
self.addWarning('No %s found, exiting' % name)
return None
try:
srcdom = parse(src + '/%s' % name)
except xml.dom.DOMException as e:
self.addError('Error parsing %s : ' + e)
return None
return srcdom
def generate(self, blog, src, output):
self.posts_per_page = 20
self.dirname = '/ljdc'
srcdom = self._load_references(src)
if srcdom is None: return None
posts = srcdom.getElementsByTagName("entry")
dom = self.parseTemplate(blog, src, output, 'ljdc', 'ljdc')
if dom is None: return self.report
self.generatePages(dom, posts, src, output, 'ljdc')
if not self.somethingWrote:
self.addReport('Nothing changed')
return self.report
def getLast(self, dom, src):
srcdom = self._load_references(src)
if srcdom is None: return None
images = srcdom.getElementsByTagName("img")
if len(images) == 0: return None
img = dom.createElement('img')
img.setAttribute('src', images[0].childNodes[0].nodeValue)
title = srcdom.getElementsByTagName("title")[0]
title = title.childNodes[0].nodeValue
img.setAttribute('alt', title.replace("\"", "'"))
return img