2010-10-30 00:56:40 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
import os
|
|
|
|
|
from codecs import open
|
|
|
|
|
|
|
|
|
|
from jinja2 import Environment, FileSystemLoader
|
|
|
|
|
from jinja2.exceptions import TemplateNotFound
|
|
|
|
|
from feedgenerator import Atom1Feed
|
|
|
|
|
|
|
|
|
|
from pelican.settings import read_settings
|
2010-11-20 00:54:33 +00:00
|
|
|
from pelican.utils import clean_output_dir
|
2010-10-30 00:56:40 +01:00
|
|
|
|
|
|
|
|
_TEMPLATES = ('index', 'tag', 'tags', 'article', 'category', 'categories',
|
2010-10-30 20:17:23 +01:00
|
|
|
'archives', 'page')
|
2010-10-30 00:56:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class Generator(object):
|
2010-11-20 02:25:42 +00:00
|
|
|
"""Handle all generation process: files writes, feed creation, and this
|
|
|
|
|
kind of basic stuff"""
|
2010-11-21 01:49:37 +00:00
|
|
|
|
|
|
|
|
def __init__(self, settings=None, path=None, theme=None, output_path=None,
|
2010-10-31 00:08:16 +01:00
|
|
|
markup=None):
|
2010-11-21 01:49:37 +00:00
|
|
|
"""Read the settings, and performs some checks on the environment
|
2010-11-20 02:25:42 +00:00
|
|
|
before doing anything else.
|
|
|
|
|
"""
|
2010-11-05 00:22:03 +00:00
|
|
|
if settings is None:
|
|
|
|
|
settings = {}
|
|
|
|
|
self.settings = read_settings(settings)
|
2010-10-30 00:56:40 +01:00
|
|
|
self.path = path or self.settings['PATH']
|
2010-11-08 22:59:16 +00:00
|
|
|
if self.path.endswith('/'):
|
|
|
|
|
self.path = self.path[:-1]
|
|
|
|
|
|
2010-10-30 00:56:40 +01:00
|
|
|
self.theme = theme or self.settings['THEME']
|
|
|
|
|
output_path = output_path or self.settings['OUTPUT_PATH']
|
|
|
|
|
self.output_path = os.path.realpath(output_path)
|
2010-10-31 00:08:16 +01:00
|
|
|
self.markup = markup or self.settings['MARKUP']
|
2010-10-30 00:56:40 +01:00
|
|
|
|
2010-11-08 01:26:45 +00:00
|
|
|
if not os.path.exists(self.theme):
|
|
|
|
|
theme_path = os.sep.join([os.path.dirname(
|
|
|
|
|
os.path.abspath(__file__)), "themes/%s" % self.theme])
|
|
|
|
|
if os.path.exists(theme_path):
|
|
|
|
|
self.theme = theme_path
|
|
|
|
|
else:
|
|
|
|
|
raise Exception("Impossible to find the theme %s" % self.theme)
|
|
|
|
|
|
2010-11-05 00:22:03 +00:00
|
|
|
if 'SITEURL' not in self.settings:
|
|
|
|
|
self.settings['SITEURL'] = self.output_path
|
|
|
|
|
|
2010-10-30 00:56:40 +01:00
|
|
|
# get the list of files to parse
|
|
|
|
|
if not path:
|
|
|
|
|
raise Exception('you need to specify a path to search the docs on !')
|
|
|
|
|
|
2010-11-05 00:22:03 +00:00
|
|
|
def run(self, processors):
|
2010-11-20 02:25:42 +00:00
|
|
|
"""Get the context from each processor, and then process them"""
|
2010-11-05 00:22:03 +00:00
|
|
|
context = self.settings.copy()
|
|
|
|
|
processors = [p() for p in processors]
|
|
|
|
|
|
|
|
|
|
for p in processors:
|
|
|
|
|
if hasattr(p, 'preprocess'):
|
|
|
|
|
p.preprocess(context, self)
|
|
|
|
|
|
2010-11-20 00:54:33 +00:00
|
|
|
if self.output_path not in os.path.realpath(self.path):
|
|
|
|
|
clean_output_dir(self.output_path)
|
|
|
|
|
|
2010-11-05 00:22:03 +00:00
|
|
|
for p in processors:
|
|
|
|
|
p.process(context, self)
|
|
|
|
|
|
2010-12-01 02:30:38 +01:00
|
|
|
def generate_feed(self, elements, context, filename=None):
|
2010-10-30 00:56:40 +01:00
|
|
|
"""Generate a feed with the list of articles provided
|
|
|
|
|
|
|
|
|
|
Return the feed. If no output_path or filename is specified, just return
|
|
|
|
|
the feed object.
|
|
|
|
|
|
|
|
|
|
:param articles: the articles to put on the feed.
|
2010-11-21 01:49:37 +00:00
|
|
|
:param context: the context to get the feed metadata.
|
2010-10-30 00:56:40 +01:00
|
|
|
:param output_path: where to output the file.
|
|
|
|
|
:param filename: the filename to output.
|
|
|
|
|
"""
|
2010-12-01 02:30:38 +01:00
|
|
|
site_url = context.get('SITEURL', self._get_relative_siteurl(filename))
|
2010-11-20 02:26:49 +00:00
|
|
|
|
2010-10-30 00:56:40 +01:00
|
|
|
feed = Atom1Feed(
|
|
|
|
|
title=context['SITENAME'],
|
2010-11-20 02:26:49 +00:00
|
|
|
link=site_url,
|
2010-12-01 02:30:38 +01:00
|
|
|
feed_url= "%s/%s" % (site_url, filename),
|
2010-10-30 00:56:40 +01:00
|
|
|
description=context.get('SITESUBTITLE', ''))
|
|
|
|
|
for element in elements:
|
|
|
|
|
feed.add_item(
|
|
|
|
|
title=element.title,
|
2010-12-01 02:30:38 +01:00
|
|
|
link= "%s/%s" % (site_url, element.url),
|
2010-10-30 00:56:40 +01:00
|
|
|
description=element.content,
|
|
|
|
|
author_name=getattr(element, 'author', 'John Doe'),
|
|
|
|
|
pubdate=element.date)
|
|
|
|
|
|
2010-11-05 00:22:03 +00:00
|
|
|
if filename:
|
|
|
|
|
complete_path = os.path.join(self.output_path, filename)
|
2010-10-30 00:56:40 +01:00
|
|
|
try:
|
|
|
|
|
os.makedirs(os.path.dirname(complete_path))
|
|
|
|
|
except Exception:
|
|
|
|
|
pass
|
|
|
|
|
fp = open(complete_path, 'w')
|
|
|
|
|
feed.write(fp, 'utf-8')
|
2010-10-30 17:06:56 +01:00
|
|
|
print u' [ok] writing %s' % complete_path
|
2010-11-21 01:49:37 +00:00
|
|
|
|
2010-10-30 00:56:40 +01:00
|
|
|
fp.close()
|
|
|
|
|
return feed
|
|
|
|
|
|
2010-11-21 01:49:37 +00:00
|
|
|
def generate_file(self, name, template, context, relative_urls=True,
|
2010-11-20 02:26:49 +00:00
|
|
|
**kwargs):
|
2010-10-30 00:56:40 +01:00
|
|
|
"""Write the file with the given informations
|
|
|
|
|
|
|
|
|
|
:param name: name of the file to output
|
|
|
|
|
:param template: template to use to generate the content
|
|
|
|
|
:param context: dict to pass to the templates.
|
2010-11-20 02:26:49 +00:00
|
|
|
:param relative_urls: use relative urls or absolutes ones
|
2010-10-30 00:56:40 +01:00
|
|
|
:param **kwargs: additional variables to pass to the templates
|
|
|
|
|
"""
|
2010-10-30 20:17:23 +01:00
|
|
|
context = context.copy()
|
2010-11-20 02:26:49 +00:00
|
|
|
if relative_urls:
|
|
|
|
|
context['SITEURL'] = self._get_relative_siteurl(name)
|
|
|
|
|
|
2010-10-30 00:56:40 +01:00
|
|
|
context.update(kwargs)
|
|
|
|
|
output = template.render(context)
|
|
|
|
|
filename = os.sep.join((self.output_path, name))
|
|
|
|
|
try:
|
|
|
|
|
os.makedirs(os.path.dirname(filename))
|
|
|
|
|
except Exception:
|
|
|
|
|
pass
|
|
|
|
|
with open(filename, 'w', encoding='utf-8') as f:
|
|
|
|
|
f.write(output)
|
2010-10-30 17:04:15 +01:00
|
|
|
print u' [ok] writing %s' % filename
|
2010-10-30 00:56:40 +01:00
|
|
|
|
2010-11-05 00:22:03 +00:00
|
|
|
def get_templates(self):
|
2010-11-20 02:25:42 +00:00
|
|
|
"""Return the templates to use.
|
|
|
|
|
Use self.theme to get the templates to use, and return a list of
|
|
|
|
|
templates ready to use with Jinja2.
|
|
|
|
|
"""
|
2010-11-05 00:22:03 +00:00
|
|
|
path = os.path.expanduser(os.path.join(self.theme, 'templates'))
|
2010-10-30 00:56:40 +01:00
|
|
|
env = Environment(loader=FileSystemLoader(path))
|
|
|
|
|
templates = {}
|
|
|
|
|
for template in _TEMPLATES:
|
|
|
|
|
try:
|
|
|
|
|
templates[template] = env.get_template('%s.html' % template)
|
|
|
|
|
except TemplateNotFound:
|
2010-11-20 02:25:42 +00:00
|
|
|
raise Exception('[templates] unable to load %s.html from %s' % (
|
2010-10-30 00:56:40 +01:00
|
|
|
template, path))
|
|
|
|
|
return templates
|
|
|
|
|
|
2010-11-20 02:25:42 +00:00
|
|
|
def get_files(self, path, exclude=[], extensions=None):
|
|
|
|
|
"""Return a list of files to use, based on rules
|
2010-10-30 20:17:23 +01:00
|
|
|
|
|
|
|
|
:param path: the path to search the file on
|
|
|
|
|
:param exclude: the list of path to exclude
|
|
|
|
|
"""
|
2010-11-20 02:25:42 +00:00
|
|
|
if not extensions:
|
|
|
|
|
extensions = self.markup
|
|
|
|
|
|
2010-10-30 20:17:23 +01:00
|
|
|
files = []
|
|
|
|
|
for root, dirs, temp_files in os.walk(path, followlinks=True):
|
|
|
|
|
for e in exclude:
|
|
|
|
|
if e in dirs:
|
|
|
|
|
dirs.remove(e)
|
|
|
|
|
files.extend([os.sep.join((root, f)) for f in temp_files
|
2010-11-20 02:25:42 +00:00
|
|
|
if True in [f.endswith(ext) for ext in extensions]])
|
2010-10-30 20:17:23 +01:00
|
|
|
return files
|
2010-11-21 01:49:37 +00:00
|
|
|
|
2010-11-20 02:26:49 +00:00
|
|
|
def _get_relative_siteurl(self, filename):
|
|
|
|
|
"""Return the siteurl relative to the given filename"""
|
|
|
|
|
return '../' * filename.count('/') + '.'
|