lektor-website/packages/blog-archive/lektor_blog_archive.py

240 lines
7.8 KiB
Python
Raw Normal View History

2015-12-19 14:52:17 +01:00
# -*- coding: utf-8 -*-
import posixpath
from datetime import date
2016-01-09 10:24:39 +01:00
from itertools import chain
2015-12-19 14:52:17 +01:00
from werkzeug.utils import cached_property
from lektor.pluginsystem import Plugin
from lektor.sourceobj import VirtualSourceObject
from lektor.build_programs import BuildProgram
2016-01-09 10:24:39 +01:00
from lektor.utils import build_url, parse_path
2015-12-19 14:52:17 +01:00
class BlogArchive(VirtualSourceObject):
2016-01-09 10:24:39 +01:00
def __init__(self, record, plugin, items=None):
VirtualSourceObject.__init__(self, record)
2015-12-19 14:52:17 +01:00
self.plugin = plugin
self._items = items
@property
2016-01-09 10:24:39 +01:00
def path(self):
return self.record.path + '@blog-archive'
2015-12-19 14:52:17 +01:00
2016-01-09 10:24:39 +01:00
template_name = 'blog-archive/index.html'
2015-12-19 14:52:17 +01:00
@property
2016-01-09 10:24:39 +01:00
def parent(self):
return self.record
2015-12-19 14:52:17 +01:00
@cached_property
def year_archives(self):
years = set()
2016-01-09 10:24:39 +01:00
for item in self.record.children:
2015-12-19 14:52:17 +01:00
pub_date = self.plugin.get_pub_date(item)
if pub_date:
years.add(pub_date.year)
2016-01-09 10:24:39 +01:00
return [BlogYearArchive(self.record, self.plugin,
year=year) for year in sorted(years)]
2015-12-19 14:52:17 +01:00
@property
def items(self):
if self._items is not None:
return self._items
rv = list(self._iter_items())
self._items = rv
return rv
def _iter_items(self):
2016-01-09 10:24:39 +01:00
return iter(())
2015-12-19 14:52:17 +01:00
@property
def has_any_items(self):
if self._items is not None:
return bool(self._items)
for item in self._iter_items():
return True
return False
@property
def items_by_months(self):
months = {}
for item in self.items:
pub_date = self.plugin.get_pub_date(item)
months.setdefault(date(pub_date.year, pub_date.month, 1),
[]).append(item)
2016-01-09 10:24:39 +01:00
return [(BlogMonthArchive(self.record, self.plugin,
year=d.year, month=d.month), i)
2015-12-19 14:52:17 +01:00
for d, i in sorted(months.items())]
2016-01-09 10:24:39 +01:00
def get_archive_url_path(self):
return self.plugin.get_url_path('archive_path')
2015-12-19 14:52:17 +01:00
@property
def url_path(self):
2016-01-09 10:24:39 +01:00
return build_url(chain([self.record.url_path.strip('/')],
self.get_archive_url_path() or ()))
class BlogYearArchive(BlogArchive):
template_name = 'blog-archive/year.html'
def __init__(self, record, plugin, items=None, year=None):
BlogArchive.__init__(self, record, plugin, items)
self.year = year
def _iter_items(self):
for item in self.record.children:
pub_date = self.plugin.get_pub_date(item)
if pub_date is not None and \
pub_date.year == self.year:
yield item
@property
def path(self):
return '%s@blog-archive/%s' % (
self.record.path,
self.year,
)
@property
def parent(self):
return BlogArchive(self.record, self.plugin)
@property
def date(self):
return date(self.year, 1, 1)
def get_archive_url_path(self):
return self.plugin.get_url_path('year_archive_prefix') + [self.year]
class BlogMonthArchive(BlogArchive):
template_name = 'blog-archive/year.html'
def __init__(self, record, plugin, items=None, year=None, month=None):
BlogArchive.__init__(self, record, plugin, items)
self.year = year
self.month = month
def _iter_items(self):
for item in self.record.children:
pub_date = self.plugin.get_pub_date(item)
if pub_date is not None and \
pub_date.year == self.year and \
pub_date.month == self.month:
yield item
2015-12-19 14:52:17 +01:00
@property
2016-01-09 10:24:39 +01:00
def path(self):
return '%s@blog-archive/%s/%s' % (
self.record.path,
self.year,
self.month
)
@property
def parent(self):
return BlogYearArchive(self.record, self.plugin, year=self.year)
@property
def date(self):
return date(self.year, self.month, 1)
def get_archive_url_path(self):
return self.plugin.get_url_path('month_archive_prefix') + [
self.year, self.month]
2015-12-19 14:52:17 +01:00
class BlogArchiveBuildProgram(BuildProgram):
def produce_artifacts(self):
self.declare_artifact(
posixpath.join(self.source.url_path, 'index.html'),
sources=list(self.source.iter_source_filenames()))
def build_artifact(self, artifact):
artifact.render_template_into(self.source.template_name,
this=self.source)
class BlogArchivePlugin(Plugin):
name = u'Blog Archive'
description = u'Adds archives to a blog.'
def get_pub_date(self, post):
key = self.get_config().get('pub_date_field', 'pub_date')
return post[key]
def get_blog_path(self):
return self.get_config().get('blog_path', '/blog')
2016-01-09 10:24:39 +01:00
def get_url_path(self, name, default='archive'):
return parse_path(self.get_config().get(name, default))
2015-12-19 14:52:17 +01:00
def on_setup_env(self, **extra):
self.env.add_build_program(BlogArchive, BlogArchiveBuildProgram)
2016-01-09 10:24:39 +01:00
@self.env.virtualpathresolver('blog-archive')
def blog_archive_resolver(node, pieces):
if node.path == self.get_blog_path():
if not pieces:
return BlogArchive(node, self)
elif len(pieces) == 1 and pieces[0].isdigit():
return BlogYearArchive(node, self, year=int(pieces[0]))
elif len(pieces) == 2 and pieces[0].isdigit() \
and pieces[1].isdigit():
return BlogMonthArchive(node, self, year=int(pieces[0]),
month=int(pieces[1]))
2015-12-19 14:52:17 +01:00
@self.env.urlresolver
2016-01-09 10:24:39 +01:00
def archive_urlresolver(node, url_path):
if node.path != self.get_blog_path():
2015-12-19 14:52:17 +01:00
return
2016-01-09 10:24:39 +01:00
archive_index = self.get_url_path('archive_path')
2015-12-19 14:52:17 +01:00
if url_path == archive_index:
return BlogArchive(node, self)
2016-01-09 10:24:39 +01:00
year_prefix = self.get_url_path('year_archive_prefix')
2015-12-19 14:52:17 +01:00
if url_path[:len(year_prefix)] == year_prefix and \
url_path[len(year_prefix)].isdigit() and \
len(url_path) == len(year_prefix) + 1:
year = int(url_path[len(year_prefix)])
2016-01-09 10:24:39 +01:00
rv = BlogYearArchive(node, self, year=year)
if rv.has_any_items:
return rv
month_prefix = self.get_url_path('month_archive_prefix')
if url_path[:len(month_prefix)] == month_prefix and \
len(url_path) == len(month_prefix) + 2 and \
url_path[len(month_prefix)].isdigit() and \
url_path[len(month_prefix) + 1].isdigit():
2015-12-19 14:52:17 +01:00
year = int(url_path[len(month_prefix)])
month = int(url_path[len(month_prefix) + 1])
2016-01-09 10:24:39 +01:00
rv = BlogMonthArchive(node, self, year=year, month=month)
if rv.has_any_items:
return rv
2015-12-19 14:52:17 +01:00
@self.env.generator
def genererate_blog_archive_pages(source):
2016-01-09 10:24:39 +01:00
if source.path != self.get_blog_path():
2015-12-19 14:52:17 +01:00
return
years = {}
months = {}
2016-01-09 10:24:39 +01:00
for post in source.children:
2015-12-19 14:52:17 +01:00
pub_date = self.get_pub_date(post)
if pub_date:
years.setdefault(pub_date.year, []).append(post)
months.setdefault((pub_date.year,
pub_date.month), []).append(post)
2016-01-09 10:24:39 +01:00
yield BlogArchive(source, self)
2015-12-19 14:52:17 +01:00
for year, items in sorted(years.items()):
2016-01-09 10:24:39 +01:00
yield BlogYearArchive(source, self, year=year, items=items)
2015-12-19 14:52:17 +01:00
for (year, month), items in sorted(months.items()):
2016-01-09 10:24:39 +01:00
yield BlogMonthArchive(source, self, year=year, month=month,
items=items)