2011-02-01 22:49:33 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2013-01-11 02:57:43 +01:00
|
|
|
from __future__ import unicode_literals, print_function
|
|
|
|
|
|
2010-10-30 00:56:40 +01:00
|
|
|
import os
|
2011-01-01 23:08:29 +03:00
|
|
|
import math
|
|
|
|
|
import random
|
2012-03-20 13:01:21 +00:00
|
|
|
import logging
|
|
|
|
|
import datetime
|
2012-11-30 10:46:32 +01:00
|
|
|
import shutil
|
2010-10-30 00:56:40 +01:00
|
|
|
|
2012-08-18 20:32:43 +03:00
|
|
|
from codecs import open
|
2012-02-21 17:53:53 +01:00
|
|
|
from collections import defaultdict
|
|
|
|
|
from functools import partial
|
|
|
|
|
from itertools import chain
|
|
|
|
|
from operator import attrgetter, itemgetter
|
|
|
|
|
|
2013-03-03 20:12:31 -08:00
|
|
|
from jinja2 import (
|
|
|
|
|
Environment, FileSystemLoader, PrefixLoader, ChoiceLoader, BaseLoader,
|
|
|
|
|
TemplateNotFound
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
from pelican.contents import (
|
2013-01-04 13:54:08 -05:00
|
|
|
Article, Page, Category, Static, is_valid_content
|
2013-03-03 20:12:31 -08:00
|
|
|
)
|
2012-02-21 17:53:53 +01:00
|
|
|
from pelican.readers import read_file
|
2012-11-30 10:46:32 +01:00
|
|
|
from pelican.utils import copy, process_translations, mkdir_p
|
2011-06-18 01:03:53 +02:00
|
|
|
from pelican import signals
|
2013-03-06 10:06:42 -05:00
|
|
|
import pelican.utils
|
2010-10-30 00:56:40 +01:00
|
|
|
|
|
|
|
|
|
2012-03-20 13:01:21 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
2010-10-30 00:56:40 +01:00
|
|
|
class Generator(object):
|
2010-12-02 03:22:24 +00:00
|
|
|
"""Baseclass generator"""
|
2010-11-20 02:26:49 +00:00
|
|
|
|
2010-12-02 03:22:24 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
for idx, item in enumerate(('context', 'settings', 'path', 'theme',
|
|
|
|
|
'output_path', 'markup')):
|
|
|
|
|
setattr(self, item, args[idx])
|
|
|
|
|
|
|
|
|
|
for arg, value in kwargs.items():
|
|
|
|
|
setattr(self, arg, value)
|
2010-10-30 00:56:40 +01:00
|
|
|
|
2011-01-02 02:50:08 +03:00
|
|
|
# templates cache
|
|
|
|
|
self._templates = {}
|
2012-09-03 00:57:23 +01:00
|
|
|
self._templates_path = []
|
|
|
|
|
self._templates_path.append(os.path.expanduser(
|
|
|
|
|
os.path.join(self.theme, 'templates')))
|
|
|
|
|
self._templates_path += self.settings.get('EXTRA_TEMPLATES_PATHS', [])
|
|
|
|
|
|
2012-03-10 12:21:54 +01:00
|
|
|
theme_path = os.path.dirname(os.path.abspath(__file__))
|
2012-03-09 16:21:38 +01:00
|
|
|
|
2012-03-10 11:32:22 +01:00
|
|
|
simple_loader = FileSystemLoader(os.path.join(theme_path,
|
|
|
|
|
"themes", "simple", "templates"))
|
2012-05-07 17:11:57 +02:00
|
|
|
self.env = Environment(
|
2012-11-21 16:38:27 +01:00
|
|
|
trim_blocks=True,
|
2011-07-19 12:31:18 +02:00
|
|
|
loader=ChoiceLoader([
|
|
|
|
|
FileSystemLoader(self._templates_path),
|
2012-03-09 16:21:38 +01:00
|
|
|
simple_loader, # implicit inheritance
|
|
|
|
|
PrefixLoader({'!simple': simple_loader}) # explicit one
|
2011-07-19 12:31:18 +02:00
|
|
|
]),
|
2011-03-23 10:25:38 +03:00
|
|
|
extensions=self.settings.get('JINJA_EXTENSIONS', []),
|
|
|
|
|
)
|
2012-03-09 16:21:38 +01:00
|
|
|
|
2012-05-07 17:11:57 +02:00
|
|
|
logger.debug('template list: {0}'.format(self.env.list_templates()))
|
2011-05-06 17:01:34 +06:00
|
|
|
|
2011-04-12 02:17:42 +08:00
|
|
|
# get custom Jinja filters from user settings
|
|
|
|
|
custom_filters = self.settings.get('JINJA_FILTERS', {})
|
2012-05-07 17:11:57 +02:00
|
|
|
self.env.filters.update(custom_filters)
|
2011-01-02 02:50:08 +03:00
|
|
|
|
2012-11-20 00:07:44 +01:00
|
|
|
signals.generator_init.send(self)
|
|
|
|
|
|
2011-01-02 02:50:08 +03:00
|
|
|
def get_template(self, name):
|
|
|
|
|
"""Return the template by name.
|
2010-11-20 02:25:42 +00:00
|
|
|
Use self.theme to get the templates to use, and return a list of
|
|
|
|
|
templates ready to use with Jinja2.
|
|
|
|
|
"""
|
2011-01-02 02:50:08 +03:00
|
|
|
if name not in self._templates:
|
2010-10-30 00:56:40 +01:00
|
|
|
try:
|
2012-05-07 17:11:57 +02:00
|
|
|
self._templates[name] = self.env.get_template(name + '.html')
|
2010-10-30 00:56:40 +01:00
|
|
|
except TemplateNotFound:
|
2013-03-03 20:12:31 -08:00
|
|
|
raise Exception(
|
|
|
|
|
('[templates] unable to load %s.html from %s'
|
|
|
|
|
% (name, self._templates_path)))
|
2011-01-02 02:50:08 +03:00
|
|
|
return self._templates[name]
|
2010-10-30 00:56:40 +01:00
|
|
|
|
2013-01-04 09:20:40 -05:00
|
|
|
def _include_path(self, path, extensions=None):
|
|
|
|
|
"""Inclusion logic for .get_files(), returns True/False
|
|
|
|
|
|
|
|
|
|
:param path: the path which might be including
|
|
|
|
|
:param extensions: the list of allowed extensions (if False, all
|
|
|
|
|
extensions are allowed)
|
|
|
|
|
"""
|
|
|
|
|
if extensions is None:
|
|
|
|
|
extensions = self.markup
|
|
|
|
|
basename = os.path.basename(path)
|
2013-03-06 11:46:17 -08:00
|
|
|
if extensions is False or basename.endswith(extensions):
|
2013-01-04 09:20:40 -05:00
|
|
|
return True
|
|
|
|
|
return False
|
|
|
|
|
|
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
|
2012-11-30 10:46:32 +01:00
|
|
|
:param extensions: the list of allowed extensions (if False, all
|
|
|
|
|
extensions are allowed)
|
2010-10-30 20:17:23 +01:00
|
|
|
"""
|
|
|
|
|
files = []
|
2011-05-06 17:01:34 +06:00
|
|
|
|
2013-01-04 09:20:40 -05:00
|
|
|
if os.path.isdir(path):
|
|
|
|
|
for root, dirs, temp_files in os.walk(path, followlinks=True):
|
|
|
|
|
for e in exclude:
|
|
|
|
|
if e in dirs:
|
|
|
|
|
dirs.remove(e)
|
|
|
|
|
for f in temp_files:
|
|
|
|
|
fp = os.path.join(root, f)
|
|
|
|
|
if self._include_path(fp, extensions):
|
|
|
|
|
files.append(fp)
|
|
|
|
|
elif os.path.exists(path) and self._include_path(path, extensions):
|
|
|
|
|
files.append(path) # can't walk non-directories
|
2010-10-30 20:17:23 +01:00
|
|
|
return files
|
2010-12-02 03:22:24 +00:00
|
|
|
|
2013-01-04 10:50:09 -05:00
|
|
|
def add_source_path(self, content):
|
2013-01-04 13:54:08 -05:00
|
|
|
location = content.get_relative_source_path()
|
2012-11-30 10:46:32 +01:00
|
|
|
self.context['filenames'][location] = content
|
|
|
|
|
|
2010-12-02 03:22:24 +00:00
|
|
|
def _update_context(self, items):
|
|
|
|
|
"""Update the context with the given items from the currrent
|
|
|
|
|
processor.
|
|
|
|
|
"""
|
|
|
|
|
for item in items:
|
|
|
|
|
value = getattr(self, item)
|
|
|
|
|
if hasattr(value, 'items'):
|
2013-01-11 18:55:04 +01:00
|
|
|
value = list(value.items()) # py3k safeguard for iterators
|
2010-12-02 03:22:24 +00:00
|
|
|
self.context[item] = value
|
|
|
|
|
|
|
|
|
|
|
2012-03-05 15:52:14 +01:00
|
|
|
class _FileLoader(BaseLoader):
|
|
|
|
|
|
2012-10-30 02:33:01 +01:00
|
|
|
def __init__(self, path, basedir):
|
2012-03-05 15:52:14 +01:00
|
|
|
self.path = path
|
2012-10-30 02:33:01 +01:00
|
|
|
self.fullpath = os.path.join(basedir, path)
|
2012-03-05 15:52:14 +01:00
|
|
|
|
|
|
|
|
def get_source(self, environment, template):
|
2012-10-30 02:33:01 +01:00
|
|
|
if template != self.path or not os.path.exists(self.fullpath):
|
2012-03-05 15:52:14 +01:00
|
|
|
raise TemplateNotFound(template)
|
2012-10-30 02:33:01 +01:00
|
|
|
mtime = os.path.getmtime(self.fullpath)
|
2013-01-11 02:57:43 +01:00
|
|
|
with open(self.fullpath, 'r', encoding='utf-8') as f:
|
|
|
|
|
source = f.read()
|
2013-03-06 11:46:17 -08:00
|
|
|
return (source, self.fullpath,
|
|
|
|
|
lambda: mtime == os.path.getmtime(self.fullpath))
|
2012-03-05 15:52:14 +01:00
|
|
|
|
|
|
|
|
|
2012-10-30 00:27:18 +01:00
|
|
|
class TemplatePagesGenerator(Generator):
|
2012-03-05 15:52:14 +01:00
|
|
|
|
|
|
|
|
def generate_output(self, writer):
|
2012-10-30 02:33:01 +01:00
|
|
|
for source, dest in self.settings['TEMPLATE_PAGES'].items():
|
|
|
|
|
self.env.loader.loaders.insert(0, _FileLoader(source, self.path))
|
2012-03-05 15:52:14 +01:00
|
|
|
try:
|
|
|
|
|
template = self.env.get_template(source)
|
|
|
|
|
rurls = self.settings.get('RELATIVE_URLS')
|
2012-10-30 02:33:01 +01:00
|
|
|
writer.write_file(dest, template, self.context, rurls)
|
2012-03-05 15:52:14 +01:00
|
|
|
finally:
|
|
|
|
|
del self.env.loader.loaders[0]
|
|
|
|
|
|
|
|
|
|
|
2010-12-02 03:22:24 +00:00
|
|
|
class ArticlesGenerator(Generator):
|
|
|
|
|
"""Generate blog articles"""
|
|
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
"""initialize properties"""
|
2012-03-09 16:21:38 +01:00
|
|
|
self.articles = [] # only articles in default language
|
2010-12-19 00:32:43 +03:00
|
|
|
self.translations = []
|
2010-12-02 03:22:24 +00:00
|
|
|
self.dates = {}
|
2010-12-26 23:59:30 +03:00
|
|
|
self.tags = defaultdict(list)
|
|
|
|
|
self.categories = defaultdict(list)
|
2012-07-10 11:06:07 +05:45
|
|
|
self.related_posts = []
|
2011-06-30 23:49:09 +02:00
|
|
|
self.authors = defaultdict(list)
|
2011-05-08 14:58:57 +01:00
|
|
|
self.drafts = []
|
2013-03-03 20:12:31 -08:00
|
|
|
super(ArticlesGenerator, self).__init__(*args, **kwargs)
|
2011-09-07 17:08:28 +02:00
|
|
|
signals.article_generator_init.send(self)
|
2010-12-02 03:22:24 +00:00
|
|
|
|
|
|
|
|
def generate_feeds(self, writer):
|
|
|
|
|
"""Generate the feeds from the current context, and output files."""
|
|
|
|
|
|
2012-07-16 09:35:05 -07:00
|
|
|
if self.settings.get('FEED_ATOM'):
|
2012-10-22 23:37:52 +02:00
|
|
|
writer.write_feed(self.articles, self.context,
|
2012-07-16 09:35:05 -07:00
|
|
|
self.settings['FEED_ATOM'])
|
2010-12-02 03:22:24 +00:00
|
|
|
|
2012-03-10 21:18:01 +09:00
|
|
|
if self.settings.get('FEED_RSS'):
|
2012-10-22 23:37:52 +02:00
|
|
|
writer.write_feed(self.articles, self.context,
|
2012-03-10 21:18:01 +09:00
|
|
|
self.settings['FEED_RSS'], feed_type='rss')
|
2010-12-02 03:22:24 +00:00
|
|
|
|
2013-03-03 20:12:31 -08:00
|
|
|
if (self.settings.get('FEED_ALL_ATOM')
|
|
|
|
|
or self.settings.get('FEED_ALL_RSS')):
|
2012-10-22 23:37:52 +02:00
|
|
|
all_articles = list(self.articles)
|
|
|
|
|
for article in self.articles:
|
|
|
|
|
all_articles.extend(article.translations)
|
|
|
|
|
all_articles.sort(key=attrgetter('date'), reverse=True)
|
|
|
|
|
|
|
|
|
|
if self.settings.get('FEED_ALL_ATOM'):
|
|
|
|
|
writer.write_feed(all_articles, self.context,
|
|
|
|
|
self.settings['FEED_ALL_ATOM'])
|
|
|
|
|
|
|
|
|
|
if self.settings.get('FEED_ALL_RSS'):
|
|
|
|
|
writer.write_feed(all_articles, self.context,
|
2013-03-03 20:12:31 -08:00
|
|
|
self.settings['FEED_ALL_RSS'],
|
|
|
|
|
feed_type='rss')
|
2012-10-22 23:37:52 +02:00
|
|
|
|
2011-02-01 01:57:39 +00:00
|
|
|
for cat, arts in self.categories:
|
2010-12-02 03:22:24 +00:00
|
|
|
arts.sort(key=attrgetter('date'), reverse=True)
|
2012-07-16 09:35:05 -07:00
|
|
|
if self.settings.get('CATEGORY_FEED_ATOM'):
|
2012-03-10 21:18:01 +09:00
|
|
|
writer.write_feed(arts, self.context,
|
2012-11-27 16:06:55 +04:00
|
|
|
self.settings['CATEGORY_FEED_ATOM'] % cat.slug)
|
2010-12-02 03:22:24 +00:00
|
|
|
|
2012-03-10 21:18:01 +09:00
|
|
|
if self.settings.get('CATEGORY_FEED_RSS'):
|
2010-12-02 03:22:24 +00:00
|
|
|
writer.write_feed(arts, self.context,
|
2012-11-27 16:06:55 +04:00
|
|
|
self.settings['CATEGORY_FEED_RSS'] % cat.slug,
|
2012-03-10 21:18:01 +09:00
|
|
|
feed_type='rss')
|
2010-12-02 03:22:24 +00:00
|
|
|
|
2013-03-03 20:12:31 -08:00
|
|
|
if (self.settings.get('TAG_FEED_ATOM')
|
|
|
|
|
or self.settings.get('TAG_FEED_RSS')):
|
2010-12-17 09:25:19 +01:00
|
|
|
for tag, arts in self.tags.items():
|
|
|
|
|
arts.sort(key=attrgetter('date'), reverse=True)
|
2012-07-16 09:35:05 -07:00
|
|
|
if self.settings.get('TAG_FEED_ATOM'):
|
2012-03-10 21:18:01 +09:00
|
|
|
writer.write_feed(arts, self.context,
|
2012-11-27 16:06:55 +04:00
|
|
|
self.settings['TAG_FEED_ATOM'] % tag.slug)
|
2010-12-17 09:25:19 +01:00
|
|
|
|
2012-03-10 21:18:01 +09:00
|
|
|
if self.settings.get('TAG_FEED_RSS'):
|
2011-05-06 17:01:34 +06:00
|
|
|
writer.write_feed(arts, self.context,
|
2012-11-27 16:06:55 +04:00
|
|
|
self.settings['TAG_FEED_RSS'] % tag.slug,
|
2012-03-09 16:21:38 +01:00
|
|
|
feed_type='rss')
|
2010-12-20 22:42:29 +00:00
|
|
|
|
2013-03-03 20:12:31 -08:00
|
|
|
if (self.settings.get('TRANSLATION_FEED_ATOM')
|
|
|
|
|
or self.settings.get('TRANSLATION_FEED_RSS')):
|
2012-03-10 21:18:01 +09:00
|
|
|
translations_feeds = defaultdict(list)
|
|
|
|
|
for article in chain(self.articles, self.translations):
|
|
|
|
|
translations_feeds[article.lang].append(article)
|
2010-12-19 00:32:43 +03:00
|
|
|
|
2012-03-10 21:18:01 +09:00
|
|
|
for lang, items in translations_feeds.items():
|
|
|
|
|
items.sort(key=attrgetter('date'), reverse=True)
|
2012-10-22 16:34:55 +02:00
|
|
|
if self.settings.get('TRANSLATION_FEED_ATOM'):
|
|
|
|
|
writer.write_feed(items, self.context,
|
2012-10-22 23:05:18 +02:00
|
|
|
self.settings['TRANSLATION_FEED_ATOM'] % lang)
|
2012-10-22 16:34:55 +02:00
|
|
|
if self.settings.get('TRANSLATION_FEED_RSS'):
|
|
|
|
|
writer.write_feed(items, self.context,
|
2012-10-22 23:05:18 +02:00
|
|
|
self.settings['TRANSLATION_FEED_RSS'] % lang,
|
|
|
|
|
feed_type='rss')
|
2010-12-17 09:25:19 +01:00
|
|
|
|
2012-05-02 09:26:33 +01:00
|
|
|
def generate_articles(self, write):
|
2012-04-29 10:34:20 +01:00
|
|
|
"""Generate the articles."""
|
2010-12-30 14:11:37 +00:00
|
|
|
for article in chain(self.translations, self.articles):
|
2012-07-07 12:15:35 -07:00
|
|
|
write(article.save_as, self.get_template(article.template),
|
|
|
|
|
self.context, article=article, category=article.category)
|
2010-12-30 14:11:37 +00:00
|
|
|
|
2012-05-02 09:26:33 +01:00
|
|
|
def generate_direct_templates(self, write):
|
2012-04-29 10:34:20 +01:00
|
|
|
"""Generate direct templates pages"""
|
2011-03-23 10:25:38 +03:00
|
|
|
PAGINATED_TEMPLATES = self.settings.get('PAGINATED_DIRECT_TEMPLATES')
|
2011-01-02 02:50:08 +03:00
|
|
|
for template in self.settings.get('DIRECT_TEMPLATES'):
|
2011-02-15 14:36:55 +01:00
|
|
|
paginated = {}
|
2011-03-23 10:25:38 +03:00
|
|
|
if template in PAGINATED_TEMPLATES:
|
2011-02-15 14:36:55 +01:00
|
|
|
paginated = {'articles': self.articles, 'dates': self.dates}
|
2012-05-02 09:26:33 +01:00
|
|
|
save_as = self.settings.get("%s_SAVE_AS" % template.upper(),
|
|
|
|
|
'%s.html' % template)
|
|
|
|
|
if not save_as:
|
2012-09-08 18:24:15 +03:00
|
|
|
continue
|
2012-03-09 16:21:38 +01:00
|
|
|
|
2012-05-02 09:26:33 +01:00
|
|
|
write(save_as, self.get_template(template),
|
2013-02-09 17:23:59 -07:00
|
|
|
self.context, blog=True, paginated=paginated,
|
|
|
|
|
page_name=os.path.splitext(save_as)[0])
|
2011-01-02 02:50:08 +03:00
|
|
|
|
2012-05-02 09:26:33 +01:00
|
|
|
def generate_tags(self, write):
|
2012-04-29 10:34:20 +01:00
|
|
|
"""Generate Tags pages."""
|
2011-01-02 02:50:08 +03:00
|
|
|
tag_template = self.get_template('tag')
|
2010-12-05 19:15:02 +00:00
|
|
|
for tag, articles in self.tags.items():
|
2011-06-12 22:18:50 +02:00
|
|
|
articles.sort(key=attrgetter('date'), reverse=True)
|
2011-02-15 13:44:36 +01:00
|
|
|
dates = [article for article in self.dates if article in articles]
|
2011-12-23 23:43:32 +00:00
|
|
|
write(tag.save_as, tag_template, self.context, tag=tag,
|
2011-03-23 10:25:38 +03:00
|
|
|
articles=articles, dates=dates,
|
2011-02-15 14:36:55 +01:00
|
|
|
paginated={'articles': articles, 'dates': dates},
|
2012-06-23 16:39:02 -05:00
|
|
|
page_name=tag.page_name)
|
2011-01-02 02:50:08 +03:00
|
|
|
|
2012-05-02 09:26:33 +01:00
|
|
|
def generate_categories(self, write):
|
2012-04-29 10:34:20 +01:00
|
|
|
"""Generate category pages."""
|
2011-01-02 02:50:08 +03:00
|
|
|
category_template = self.get_template('category')
|
2011-02-01 01:57:39 +00:00
|
|
|
for cat, articles in self.categories:
|
2011-02-15 13:44:36 +01:00
|
|
|
dates = [article for article in self.dates if article in articles]
|
2011-12-23 23:43:32 +00:00
|
|
|
write(cat.save_as, category_template, self.context,
|
2011-02-15 14:36:55 +01:00
|
|
|
category=cat, articles=articles, dates=dates,
|
|
|
|
|
paginated={'articles': articles, 'dates': dates},
|
2012-06-23 16:39:02 -05:00
|
|
|
page_name=cat.page_name)
|
2011-01-02 02:50:08 +03:00
|
|
|
|
2012-05-02 09:26:33 +01:00
|
|
|
def generate_authors(self, write):
|
2012-04-29 10:34:20 +01:00
|
|
|
"""Generate Author pages."""
|
2011-06-30 23:49:09 +02:00
|
|
|
author_template = self.get_template('author')
|
|
|
|
|
for aut, articles in self.authors:
|
|
|
|
|
dates = [article for article in self.dates if article in articles]
|
2011-12-23 23:43:32 +00:00
|
|
|
write(aut.save_as, author_template, self.context,
|
2011-06-30 23:49:09 +02:00
|
|
|
author=aut, articles=articles, dates=dates,
|
|
|
|
|
paginated={'articles': articles, 'dates': dates},
|
2012-06-23 16:39:02 -05:00
|
|
|
page_name=aut.page_name)
|
2011-06-30 23:49:09 +02:00
|
|
|
|
2012-05-02 09:26:33 +01:00
|
|
|
def generate_drafts(self, write):
|
2012-04-29 10:34:20 +01:00
|
|
|
"""Generate drafts pages."""
|
2011-05-08 14:58:57 +01:00
|
|
|
for article in self.drafts:
|
2012-07-07 12:15:35 -07:00
|
|
|
write('drafts/%s.html' % article.slug,
|
|
|
|
|
self.get_template(article.template), self.context,
|
|
|
|
|
article=article, category=article.category)
|
2010-12-02 03:22:24 +00:00
|
|
|
|
2012-04-29 10:34:20 +01:00
|
|
|
def generate_pages(self, writer):
|
|
|
|
|
"""Generate the pages on the disk"""
|
2012-05-02 09:26:33 +01:00
|
|
|
write = partial(writer.write_file,
|
|
|
|
|
relative_urls=self.settings.get('RELATIVE_URLS'))
|
2012-04-29 10:34:20 +01:00
|
|
|
|
|
|
|
|
# to minimize the number of relative path stuff modification
|
|
|
|
|
# in writer, articles pass first
|
2012-05-02 09:26:33 +01:00
|
|
|
self.generate_articles(write)
|
|
|
|
|
self.generate_direct_templates(write)
|
|
|
|
|
|
2012-04-29 10:34:20 +01:00
|
|
|
# and subfolders after that
|
2012-05-02 09:26:33 +01:00
|
|
|
self.generate_tags(write)
|
|
|
|
|
self.generate_categories(write)
|
|
|
|
|
self.generate_authors(write)
|
|
|
|
|
self.generate_drafts(write)
|
2012-04-29 10:34:20 +01:00
|
|
|
|
2010-12-02 03:22:24 +00:00
|
|
|
def generate_context(self):
|
2012-11-30 10:46:32 +01:00
|
|
|
"""Add the articles into the shared context"""
|
2010-12-02 03:22:24 +00:00
|
|
|
|
2012-05-12 11:49:49 +02:00
|
|
|
article_path = os.path.normpath( # we have to remove trailing slashes
|
|
|
|
|
os.path.join(self.path, self.settings['ARTICLE_DIR'])
|
|
|
|
|
)
|
2010-12-19 00:32:43 +03:00
|
|
|
all_articles = []
|
2012-03-06 00:29:56 +01:00
|
|
|
for f in self.get_files(
|
2012-03-16 15:04:06 +09:00
|
|
|
article_path,
|
2012-03-06 00:29:56 +01:00
|
|
|
exclude=self.settings['ARTICLE_EXCLUDES']):
|
2011-11-23 20:35:45 +00:00
|
|
|
try:
|
2012-11-14 22:12:46 -08:00
|
|
|
signals.article_generate_preread.send(self)
|
2011-11-23 20:35:45 +00:00
|
|
|
content, metadata = read_file(f, settings=self.settings)
|
2013-01-11 02:57:43 +01:00
|
|
|
except Exception as e:
|
|
|
|
|
logger.warning('Could not process %s\n%s' % (f, str(e)))
|
2011-11-23 20:35:45 +00:00
|
|
|
continue
|
2010-12-02 03:22:24 +00:00
|
|
|
|
|
|
|
|
# if no category is set, use the name of the path as a category
|
2012-03-14 09:38:36 +01:00
|
|
|
if 'category' not in metadata:
|
2010-12-02 03:22:24 +00:00
|
|
|
|
2012-09-23 10:04:51 +02:00
|
|
|
if (self.settings['USE_FOLDER_AS_CATEGORY']
|
|
|
|
|
and os.path.dirname(f) != article_path):
|
|
|
|
|
# if the article is in a subdirectory
|
2013-01-11 02:57:43 +01:00
|
|
|
category = os.path.basename(os.path.dirname(f))
|
2012-09-23 10:04:51 +02:00
|
|
|
else:
|
|
|
|
|
# if the article is not in a subdirectory
|
|
|
|
|
category = self.settings['DEFAULT_CATEGORY']
|
2010-12-02 03:22:24 +00:00
|
|
|
|
|
|
|
|
if category != '':
|
2011-12-23 23:43:32 +00:00
|
|
|
metadata['category'] = Category(category, self.settings)
|
2010-12-02 03:22:24 +00:00
|
|
|
|
2012-11-24 00:26:46 +01:00
|
|
|
if 'date' not in metadata and self.settings.get('DEFAULT_DATE'):
|
2012-07-05 00:07:01 +02:00
|
|
|
if self.settings['DEFAULT_DATE'] == 'fs':
|
2012-03-09 16:21:38 +01:00
|
|
|
metadata['date'] = datetime.datetime.fromtimestamp(
|
2012-07-05 00:07:01 +02:00
|
|
|
os.stat(f).st_ctime)
|
|
|
|
|
else:
|
|
|
|
|
metadata['date'] = datetime.datetime(
|
|
|
|
|
*self.settings['DEFAULT_DATE'])
|
2010-12-02 03:22:24 +00:00
|
|
|
|
2011-06-18 01:03:53 +02:00
|
|
|
signals.article_generate_context.send(self, metadata=metadata)
|
2011-05-07 20:00:30 +01:00
|
|
|
article = Article(content, metadata, settings=self.settings,
|
2013-01-04 10:50:09 -05:00
|
|
|
source_path=f, context=self.context)
|
2010-12-02 03:22:24 +00:00
|
|
|
if not is_valid_content(article, f):
|
|
|
|
|
continue
|
|
|
|
|
|
2013-01-04 10:50:09 -05:00
|
|
|
self.add_source_path(article)
|
2012-11-30 10:46:32 +01:00
|
|
|
|
2011-05-08 14:58:57 +01:00
|
|
|
if article.status == "published":
|
|
|
|
|
if hasattr(article, 'tags'):
|
|
|
|
|
for tag in article.tags:
|
|
|
|
|
self.tags[tag].append(article)
|
|
|
|
|
all_articles.append(article)
|
|
|
|
|
elif article.status == "draft":
|
|
|
|
|
self.drafts.append(article)
|
2012-05-15 17:48:07 -04:00
|
|
|
else:
|
2013-01-11 02:57:43 +01:00
|
|
|
logger.warning("Unknown status %s for file %s, skipping it." %
|
|
|
|
|
(repr(article.status),
|
2012-05-15 17:48:07 -04:00
|
|
|
repr(f)))
|
2010-12-19 00:32:43 +03:00
|
|
|
|
|
|
|
|
self.articles, self.translations = process_translations(all_articles)
|
|
|
|
|
|
|
|
|
|
for article in self.articles:
|
|
|
|
|
# only main articles are listed in categories, not translations
|
2010-12-26 23:59:30 +03:00
|
|
|
self.categories[article.category].append(article)
|
2012-10-26 18:20:05 +01:00
|
|
|
# ignore blank authors as well as undefined
|
2013-03-03 20:12:31 -08:00
|
|
|
if hasattr(article, 'author') and article.author.name != '':
|
2012-10-26 18:20:05 +01:00
|
|
|
self.authors[article.author].append(article)
|
2010-12-02 03:22:24 +00:00
|
|
|
|
|
|
|
|
# sort the articles by date
|
|
|
|
|
self.articles.sort(key=attrgetter('date'), reverse=True)
|
|
|
|
|
self.dates = list(self.articles)
|
2011-05-06 17:01:34 +06:00
|
|
|
self.dates.sort(key=attrgetter('date'),
|
2012-07-18 11:18:48 -07:00
|
|
|
reverse=self.context['NEWEST_FIRST_ARCHIVES'])
|
2011-01-01 23:08:29 +03:00
|
|
|
|
|
|
|
|
# create tag cloud
|
|
|
|
|
tag_cloud = defaultdict(int)
|
|
|
|
|
for article in self.articles:
|
2011-03-23 16:41:24 +00:00
|
|
|
for tag in getattr(article, 'tags', []):
|
2011-01-01 23:08:29 +03:00
|
|
|
tag_cloud[tag] += 1
|
|
|
|
|
|
2012-03-09 16:21:38 +01:00
|
|
|
tag_cloud = sorted(tag_cloud.items(), key=itemgetter(1), reverse=True)
|
2011-01-01 23:08:29 +03:00
|
|
|
tag_cloud = tag_cloud[:self.settings.get('TAG_CLOUD_MAX_ITEMS')]
|
|
|
|
|
|
2013-01-11 02:57:43 +01:00
|
|
|
tags = list(map(itemgetter(1), tag_cloud))
|
2011-03-27 12:49:28 +02:00
|
|
|
if tags:
|
2011-08-22 19:42:42 +02:00
|
|
|
max_count = max(tags)
|
2011-01-01 23:08:29 +03:00
|
|
|
steps = self.settings.get('TAG_CLOUD_STEPS')
|
2011-05-06 17:01:34 +06:00
|
|
|
|
2011-01-01 23:08:29 +03:00
|
|
|
# calculate word sizes
|
|
|
|
|
self.tag_cloud = [
|
|
|
|
|
(
|
|
|
|
|
tag,
|
2012-03-09 16:21:38 +01:00
|
|
|
int(math.floor(steps - (steps - 1) * math.log(count)
|
|
|
|
|
/ (math.log(max_count)or 1)))
|
2011-01-01 23:08:29 +03:00
|
|
|
)
|
|
|
|
|
for tag, count in tag_cloud
|
|
|
|
|
]
|
|
|
|
|
# put words in chaos
|
|
|
|
|
random.shuffle(self.tag_cloud)
|
|
|
|
|
|
2010-12-02 03:22:24 +00:00
|
|
|
# and generate the output :)
|
2011-02-01 01:57:39 +00:00
|
|
|
|
|
|
|
|
# order the categories per name
|
|
|
|
|
self.categories = list(self.categories.items())
|
2012-05-06 02:43:13 +02:00
|
|
|
self.categories.sort(
|
|
|
|
|
reverse=self.settings['REVERSE_CATEGORY_ORDER'])
|
2011-06-30 23:49:09 +02:00
|
|
|
|
|
|
|
|
self.authors = list(self.authors.items())
|
2013-01-03 13:54:56 -05:00
|
|
|
self.authors.sort()
|
2011-06-30 23:49:09 +02:00
|
|
|
|
2012-03-09 16:21:38 +01:00
|
|
|
self._update_context(('articles', 'dates', 'tags', 'categories',
|
2012-07-10 11:06:07 +05:45
|
|
|
'tag_cloud', 'authors', 'related_posts'))
|
2010-12-02 03:22:24 +00:00
|
|
|
|
2012-10-13 19:17:16 +02:00
|
|
|
signals.article_generator_finalized.send(self)
|
2010-12-02 03:22:24 +00:00
|
|
|
|
|
|
|
|
def generate_output(self, writer):
|
|
|
|
|
self.generate_feeds(writer)
|
|
|
|
|
self.generate_pages(writer)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class PagesGenerator(Generator):
|
|
|
|
|
"""Generate pages"""
|
|
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
self.pages = []
|
2012-06-26 19:26:43 -07:00
|
|
|
self.hidden_pages = []
|
|
|
|
|
self.hidden_translations = []
|
2010-12-02 03:22:24 +00:00
|
|
|
super(PagesGenerator, self).__init__(*args, **kwargs)
|
2012-07-19 20:59:48 -04:00
|
|
|
signals.pages_generator_init.send(self)
|
2010-12-02 03:22:24 +00:00
|
|
|
|
|
|
|
|
def generate_context(self):
|
2010-12-19 00:32:43 +03:00
|
|
|
all_pages = []
|
2012-06-26 19:26:43 -07:00
|
|
|
hidden_pages = []
|
2012-03-06 00:29:56 +01:00
|
|
|
for f in self.get_files(
|
|
|
|
|
os.path.join(self.path, self.settings['PAGE_DIR']),
|
|
|
|
|
exclude=self.settings['PAGE_EXCLUDES']):
|
2011-11-23 20:35:45 +00:00
|
|
|
try:
|
2012-09-08 21:39:10 -07:00
|
|
|
content, metadata = read_file(f, settings=self.settings)
|
2013-01-11 02:57:43 +01:00
|
|
|
except Exception as e:
|
|
|
|
|
logger.warning('Could not process %s\n%s' % (f, str(e)))
|
2011-11-23 20:35:45 +00:00
|
|
|
continue
|
2012-11-30 10:46:32 +01:00
|
|
|
signals.pages_generate_context.send(self, metadata=metadata)
|
2011-05-07 20:00:30 +01:00
|
|
|
page = Page(content, metadata, settings=self.settings,
|
2013-01-04 10:50:09 -05:00
|
|
|
source_path=f, context=self.context)
|
2010-12-02 03:22:24 +00:00
|
|
|
if not is_valid_content(page, f):
|
|
|
|
|
continue
|
2012-11-30 10:46:32 +01:00
|
|
|
|
2013-01-04 10:50:09 -05:00
|
|
|
self.add_source_path(page)
|
2012-11-30 10:46:32 +01:00
|
|
|
|
2012-06-26 19:26:43 -07:00
|
|
|
if page.status == "published":
|
|
|
|
|
all_pages.append(page)
|
|
|
|
|
elif page.status == "hidden":
|
|
|
|
|
hidden_pages.append(page)
|
|
|
|
|
else:
|
2013-01-11 02:57:43 +01:00
|
|
|
logger.warning("Unknown status %s for file %s, skipping it." %
|
|
|
|
|
(repr(page.status),
|
2012-06-26 19:26:43 -07:00
|
|
|
repr(f)))
|
2010-12-19 00:32:43 +03:00
|
|
|
|
|
|
|
|
self.pages, self.translations = process_translations(all_pages)
|
2013-03-03 20:12:31 -08:00
|
|
|
self.hidden_pages, self.hidden_translations = (
|
|
|
|
|
process_translations(hidden_pages))
|
2010-12-02 03:22:24 +00:00
|
|
|
|
|
|
|
|
self._update_context(('pages', ))
|
2010-12-14 15:48:35 +00:00
|
|
|
self.context['PAGES'] = self.pages
|
2010-12-02 03:22:24 +00:00
|
|
|
|
2013-02-10 12:42:54 -08:00
|
|
|
signals.pages_generator_finalized.send(self)
|
|
|
|
|
|
2010-12-02 03:22:24 +00:00
|
|
|
def generate_output(self, writer):
|
2012-06-26 19:26:43 -07:00
|
|
|
for page in chain(self.translations, self.pages,
|
|
|
|
|
self.hidden_translations, self.hidden_pages):
|
2012-07-07 12:15:35 -07:00
|
|
|
writer.write_file(page.save_as, self.get_template(page.template),
|
2010-12-22 03:19:35 +03:00
|
|
|
self.context, page=page,
|
2012-03-09 16:21:38 +01:00
|
|
|
relative_urls=self.settings.get('RELATIVE_URLS'))
|
2010-12-02 03:22:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class StaticGenerator(Generator):
|
2012-04-07 18:02:40 -06:00
|
|
|
"""copy static paths (what you want to copy, like images, medias etc.
|
2010-12-05 19:15:02 +00:00
|
|
|
to output"""
|
2010-12-02 03:22:24 +00:00
|
|
|
|
|
|
|
|
def _copy_paths(self, paths, source, destination, output_path,
|
|
|
|
|
final_path=None):
|
2011-05-07 22:46:56 +01:00
|
|
|
"""Copy all the paths from source to destination"""
|
2010-12-02 03:22:24 +00:00
|
|
|
for path in paths:
|
2012-03-09 16:21:38 +01:00
|
|
|
copy(path, source, os.path.join(output_path, destination),
|
|
|
|
|
final_path, overwrite=True)
|
2010-12-02 03:22:24 +00:00
|
|
|
|
2012-11-30 10:46:32 +01:00
|
|
|
def generate_context(self):
|
|
|
|
|
self.staticfiles = []
|
|
|
|
|
|
|
|
|
|
# walk static paths
|
|
|
|
|
for static_path in self.settings['STATIC_PATHS']:
|
|
|
|
|
for f in self.get_files(
|
|
|
|
|
os.path.join(self.path, static_path), extensions=False):
|
|
|
|
|
f_rel = os.path.relpath(f, self.path)
|
|
|
|
|
# TODO remove this hardcoded 'static' subdirectory
|
2013-03-06 10:06:42 -05:00
|
|
|
dest = os.path.join('static', f_rel)
|
|
|
|
|
url = '/'.join(pelican.utils.split_all(dest))
|
2013-01-04 13:54:08 -05:00
|
|
|
sc = Static(
|
|
|
|
|
content=None,
|
2013-03-06 10:06:42 -05:00
|
|
|
metadata={
|
|
|
|
|
'save_as': dest,
|
|
|
|
|
'url': url,
|
|
|
|
|
},
|
2013-01-04 13:54:08 -05:00
|
|
|
settings=self.settings,
|
|
|
|
|
source_path=f_rel)
|
2012-11-30 10:46:32 +01:00
|
|
|
self.staticfiles.append(sc)
|
2013-01-04 13:54:08 -05:00
|
|
|
self.add_source_path(sc)
|
2012-11-30 10:46:32 +01:00
|
|
|
# same thing for FILES_TO_COPY
|
|
|
|
|
for src, dest in self.settings['FILES_TO_COPY']:
|
2013-01-04 13:54:08 -05:00
|
|
|
sc = Static(
|
|
|
|
|
content=None,
|
|
|
|
|
metadata={'save_as': dest},
|
|
|
|
|
settings=self.settings,
|
|
|
|
|
source_path=src)
|
2012-11-30 10:46:32 +01:00
|
|
|
self.staticfiles.append(sc)
|
2013-01-04 13:54:08 -05:00
|
|
|
self.add_source_path(sc)
|
2012-05-07 17:11:57 +02:00
|
|
|
|
2012-11-30 10:46:32 +01:00
|
|
|
def generate_output(self, writer):
|
2010-12-05 19:15:02 +00:00
|
|
|
self._copy_paths(self.settings['THEME_STATIC_PATHS'], self.theme,
|
2010-12-02 03:22:24 +00:00
|
|
|
'theme', self.output_path, '.')
|
2013-01-04 13:54:08 -05:00
|
|
|
# copy all Static files
|
2012-11-30 10:46:32 +01:00
|
|
|
for sc in self.staticfiles:
|
2013-01-04 13:54:08 -05:00
|
|
|
source_path = os.path.join(self.path, sc.source_path)
|
|
|
|
|
save_as = os.path.join(self.output_path, sc.save_as)
|
|
|
|
|
mkdir_p(os.path.dirname(save_as))
|
|
|
|
|
shutil.copy(source_path, save_as)
|
2013-01-04 10:50:09 -05:00
|
|
|
logger.info('copying {} to {}'.format(sc.source_path, sc.save_as))
|
2011-05-07 22:46:56 +01:00
|
|
|
|
2010-12-02 03:22:24 +00:00
|
|
|
|
|
|
|
|
class PdfGenerator(Generator):
|
|
|
|
|
"""Generate PDFs on the output dir, for all articles and pages coming from
|
|
|
|
|
rst"""
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
2012-09-08 18:24:15 +03:00
|
|
|
super(PdfGenerator, self).__init__(*args, **kwargs)
|
2010-12-02 03:22:24 +00:00
|
|
|
try:
|
|
|
|
|
from rst2pdf.createpdf import RstToPdf
|
2013-03-06 11:46:17 -08:00
|
|
|
if 'PDF_STYLE_PATH' in self.settings.keys():
|
|
|
|
|
pdf_style_path = os.path.join(self.settings['PDF_STYLE_PATH'])
|
|
|
|
|
else:
|
|
|
|
|
pdf_style_path = ''
|
|
|
|
|
|
|
|
|
|
if 'PDF_STYLE' in self.settings.keys():
|
|
|
|
|
pdf_style = self.settings.get('PDF_STYLE', 'twelvepoint')
|
|
|
|
|
|
2012-03-09 16:21:38 +01:00
|
|
|
self.pdfcreator = RstToPdf(breakside=0,
|
2012-09-08 18:24:15 +03:00
|
|
|
stylesheets=[pdf_style],
|
|
|
|
|
style_path=[pdf_style_path])
|
2010-12-02 03:22:24 +00:00
|
|
|
except ImportError:
|
|
|
|
|
raise Exception("unable to find rst2pdf")
|
|
|
|
|
|
|
|
|
|
def _create_pdf(self, obj, output_path):
|
2013-01-04 10:50:09 -05:00
|
|
|
if obj.source_path.endswith('.rst'):
|
2010-12-22 04:46:11 +08:00
|
|
|
filename = obj.slug + ".pdf"
|
2012-03-09 16:21:38 +01:00
|
|
|
output_pdf = os.path.join(output_path, filename)
|
2013-01-04 10:50:09 -05:00
|
|
|
# print('Generating pdf for', obj.source_path, 'in', output_pdf)
|
|
|
|
|
with open(obj.source_path) as f:
|
2012-08-18 20:32:43 +03:00
|
|
|
self.pdfcreator.createPdf(text=f.read(), output=output_pdf)
|
2013-01-11 02:57:43 +01:00
|
|
|
logger.info(' [ok] writing %s' % output_pdf)
|
2011-05-06 17:01:34 +06:00
|
|
|
|
2010-12-02 03:22:24 +00:00
|
|
|
def generate_context(self):
|
2010-12-22 04:46:11 +08:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def generate_output(self, writer=None):
|
2011-02-15 13:48:57 +00:00
|
|
|
# we don't use the writer passed as argument here
|
|
|
|
|
# since we write our own files
|
2013-01-11 02:57:43 +01:00
|
|
|
logger.info(' Generating PDF files...')
|
2010-12-02 03:22:24 +00:00
|
|
|
pdf_path = os.path.join(self.output_path, 'pdf')
|
2011-08-20 09:51:18 -04:00
|
|
|
if not os.path.exists(pdf_path):
|
|
|
|
|
try:
|
|
|
|
|
os.mkdir(pdf_path)
|
|
|
|
|
except OSError:
|
2012-11-30 10:46:32 +01:00
|
|
|
logger.error("Couldn't create the pdf output folder in " +
|
|
|
|
|
pdf_path)
|
2010-12-02 03:22:24 +00:00
|
|
|
|
|
|
|
|
for article in self.context['articles']:
|
|
|
|
|
self._create_pdf(article, pdf_path)
|
|
|
|
|
|
|
|
|
|
for page in self.context['pages']:
|
|
|
|
|
self._create_pdf(page, pdf_path)
|
2012-04-15 02:20:20 +03:00
|
|
|
|
2013-03-03 20:12:31 -08:00
|
|
|
|
2012-09-07 08:46:38 +02:00
|
|
|
class SourceFileGenerator(Generator):
|
2012-09-28 14:59:05 +02:00
|
|
|
def generate_context(self):
|
|
|
|
|
self.output_extension = self.settings['OUTPUT_SOURCES_EXTENSION']
|
|
|
|
|
|
2013-03-03 21:00:52 -08:00
|
|
|
def _create_source(self, obj):
|
|
|
|
|
output_path, _ = os.path.splitext(obj.save_as)
|
|
|
|
|
dest = os.path.join(self.output_path,
|
|
|
|
|
output_path + self.output_extension)
|
2013-01-04 10:50:09 -05:00
|
|
|
copy('', obj.source_path, dest)
|
2012-09-07 08:46:38 +02:00
|
|
|
|
|
|
|
|
def generate_output(self, writer=None):
|
2013-01-11 02:57:43 +01:00
|
|
|
logger.info(' Generating source files...')
|
2013-03-03 21:00:52 -08:00
|
|
|
for obj in chain(self.context['articles'], self.context['pages']):
|
|
|
|
|
self._create_source(obj)
|