2011-02-01 22:49:33 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2012-03-20 13:01:21 +00:00
|
|
|
import locale
|
|
|
|
|
import logging
|
|
|
|
|
import functools
|
|
|
|
|
|
2012-03-09 16:21:38 +01:00
|
|
|
from datetime import datetime
|
2011-07-26 17:45:15 +02:00
|
|
|
from os import getenv
|
2011-08-19 02:19:44 +08:00
|
|
|
from sys import platform, stdin
|
2010-10-30 00:56:40 +01:00
|
|
|
|
2012-03-20 13:01:21 +00:00
|
|
|
|
2012-02-21 17:53:53 +01:00
|
|
|
from pelican.settings import _DEFAULT_CONFIG
|
|
|
|
|
from pelican.utils import slugify, truncate_html_words
|
|
|
|
|
|
2012-03-09 16:21:38 +01:00
|
|
|
|
2012-03-20 13:01:21 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
2010-10-30 00:56:40 +01:00
|
|
|
class Page(object):
|
2010-12-02 03:22:24 +00:00
|
|
|
"""Represents a page
|
2011-05-07 20:00:30 +01:00
|
|
|
Given a content, and metadata, create an adequate object.
|
2010-10-30 00:56:40 +01:00
|
|
|
|
2011-05-06 16:44:12 +06:00
|
|
|
:param content: the string to parse, containing the original content.
|
2010-10-30 00:56:40 +01:00
|
|
|
"""
|
2010-10-30 20:17:23 +01:00
|
|
|
mandatory_properties = ('title',)
|
2010-10-30 00:56:40 +01:00
|
|
|
|
2012-02-20 17:58:23 +01:00
|
|
|
def __init__(self, content, metadata=None, settings=None,
|
2012-02-20 17:39:47 +01:00
|
|
|
filename=None):
|
2011-05-10 23:18:11 +01:00
|
|
|
# init parameters
|
|
|
|
|
if not metadata:
|
|
|
|
|
metadata = {}
|
|
|
|
|
if not settings:
|
|
|
|
|
settings = _DEFAULT_CONFIG
|
|
|
|
|
|
2011-12-23 22:01:32 +00:00
|
|
|
self.settings = settings
|
2010-12-30 14:11:37 +00:00
|
|
|
self._content = content
|
2010-12-19 00:32:43 +03:00
|
|
|
self.translations = []
|
|
|
|
|
|
2011-05-10 23:18:11 +01:00
|
|
|
local_metadata = dict(settings.get('DEFAULT_METADATA', ()))
|
2011-05-07 20:00:30 +01:00
|
|
|
local_metadata.update(metadata)
|
2011-05-10 23:18:11 +01:00
|
|
|
|
|
|
|
|
# set metadata as attributes
|
2011-05-07 19:56:55 +01:00
|
|
|
for key, value in local_metadata.items():
|
2011-01-05 16:22:52 +01:00
|
|
|
setattr(self, key.lower(), value)
|
2012-02-20 17:39:47 +01:00
|
|
|
|
2012-04-03 11:58:31 +02:00
|
|
|
# also keep track of the metadata attributes available
|
|
|
|
|
self.metadata = local_metadata
|
|
|
|
|
|
2011-05-10 23:18:11 +01:00
|
|
|
# default author to the one in settings if not defined
|
2010-10-30 00:56:40 +01:00
|
|
|
if not hasattr(self, 'author'):
|
|
|
|
|
if 'AUTHOR' in settings:
|
2011-12-23 23:43:32 +00:00
|
|
|
self.author = Author(settings['AUTHOR'], settings)
|
2011-07-26 17:45:15 +02:00
|
|
|
else:
|
2012-03-16 21:13:24 +01:00
|
|
|
title = filename.decode('utf-8') if filename else self.title
|
2011-12-23 23:43:32 +00:00
|
|
|
self.author = Author(getenv('USER', 'John Doe'), settings)
|
2012-03-20 13:01:21 +00:00
|
|
|
logger.warning(u"Author of `{0}' unknown, assuming that his name is "
|
2012-03-16 21:13:24 +01:00
|
|
|
"`{1}'".format(title, self.author))
|
2010-10-30 00:56:40 +01:00
|
|
|
|
2011-05-10 23:18:11 +01:00
|
|
|
# manage languages
|
|
|
|
|
self.in_default_lang = True
|
|
|
|
|
if 'DEFAULT_LANG' in settings:
|
|
|
|
|
default_lang = settings['DEFAULT_LANG'].lower()
|
|
|
|
|
if not hasattr(self, 'lang'):
|
|
|
|
|
self.lang = default_lang
|
2010-12-19 00:32:43 +03:00
|
|
|
|
2011-05-10 23:18:11 +01:00
|
|
|
self.in_default_lang = (self.lang == default_lang)
|
2010-12-19 00:32:43 +03:00
|
|
|
|
2011-05-10 23:18:11 +01:00
|
|
|
# create the slug if not existing, fro mthe title
|
2011-01-13 00:32:37 +01:00
|
|
|
if not hasattr(self, 'slug') and hasattr(self, 'title'):
|
2010-12-17 00:32:12 +03:00
|
|
|
self.slug = slugify(self.title)
|
|
|
|
|
|
2010-11-06 02:03:32 +00:00
|
|
|
if filename:
|
|
|
|
|
self.filename = filename
|
|
|
|
|
|
2011-05-10 23:18:11 +01:00
|
|
|
# manage the date format
|
2011-02-01 21:44:50 +00:00
|
|
|
if not hasattr(self, 'date_format'):
|
2011-05-10 23:18:11 +01:00
|
|
|
if hasattr(self, 'lang') and self.lang in settings['DATE_FORMATS']:
|
2011-02-01 21:44:50 +00:00
|
|
|
self.date_format = settings['DATE_FORMATS'][self.lang]
|
|
|
|
|
else:
|
|
|
|
|
self.date_format = settings['DEFAULT_DATE_FORMAT']
|
|
|
|
|
|
2012-02-28 01:43:36 +09:00
|
|
|
if isinstance(self.date_format, tuple):
|
|
|
|
|
locale.setlocale(locale.LC_ALL, self.date_format[0])
|
|
|
|
|
self.date_format = self.date_format[1]
|
2012-02-21 17:53:53 +01:00
|
|
|
|
2011-02-01 22:49:33 +00:00
|
|
|
if hasattr(self, 'date'):
|
2012-03-09 16:21:38 +01:00
|
|
|
encoded_date = self.date.strftime(
|
|
|
|
|
self.date_format.encode('ascii', 'xmlcharrefreplace'))
|
|
|
|
|
|
2011-08-19 02:19:44 +08:00
|
|
|
if platform == 'win32':
|
2012-03-09 16:21:38 +01:00
|
|
|
self.locale_date = encoded_date.decode(stdin.encoding)
|
2011-08-19 02:19:44 +08:00
|
|
|
else:
|
2012-03-09 16:21:38 +01:00
|
|
|
self.locale_date = encoded_date.decode('utf')
|
2012-02-20 17:39:47 +01:00
|
|
|
|
2011-05-10 23:18:11 +01:00
|
|
|
# manage status
|
2011-05-08 14:58:57 +01:00
|
|
|
if not hasattr(self, 'status'):
|
|
|
|
|
self.status = settings['DEFAULT_STATUS']
|
2011-11-26 23:23:19 +00:00
|
|
|
if not settings['WITH_FUTURE_DATES']:
|
2012-03-09 16:21:38 +01:00
|
|
|
if hasattr(self, 'date') and self.date > datetime.now():
|
2011-11-26 23:23:19 +00:00
|
|
|
self.status = 'draft'
|
2012-02-20 17:39:47 +01:00
|
|
|
|
2012-03-16 19:59:03 +01:00
|
|
|
# store the summary metadata if it is set
|
2012-03-13 17:10:20 +01:00
|
|
|
if 'summary' in metadata:
|
|
|
|
|
self._summary = metadata['summary']
|
2011-10-25 18:31:17 +02:00
|
|
|
|
2010-10-30 00:56:40 +01:00
|
|
|
def check_properties(self):
|
|
|
|
|
"""test that each mandatory property is set."""
|
|
|
|
|
for prop in self.mandatory_properties:
|
|
|
|
|
if not hasattr(self, prop):
|
|
|
|
|
raise NameError(prop)
|
|
|
|
|
|
2011-12-23 22:01:32 +00:00
|
|
|
@property
|
|
|
|
|
def url_format(self):
|
|
|
|
|
return {
|
|
|
|
|
'slug': getattr(self, 'slug', ''),
|
|
|
|
|
'lang': getattr(self, 'lang', 'en'),
|
2012-03-09 16:21:38 +01:00
|
|
|
'date': getattr(self, 'date', datetime.now()),
|
2011-12-23 22:01:32 +00:00
|
|
|
'author': self.author,
|
|
|
|
|
'category': getattr(self, 'category', 'misc'),
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-11 01:14:22 +01:00
|
|
|
def _expand_settings(self, key):
|
|
|
|
|
fq_key = ('%s_%s' % (self.__class__.__name__, key)).upper()
|
|
|
|
|
return self.settings[fq_key].format(**self.url_format)
|
2011-12-23 22:01:32 +00:00
|
|
|
|
2012-03-11 01:14:22 +01:00
|
|
|
def get_url_setting(self, key):
|
|
|
|
|
key = key if self.in_default_lang else 'lang_%s' % key
|
|
|
|
|
return self._expand_settings(key)
|
2011-12-23 22:01:32 +00:00
|
|
|
|
2010-12-30 14:11:37 +00:00
|
|
|
@property
|
|
|
|
|
def content(self):
|
|
|
|
|
if hasattr(self, "_get_content"):
|
|
|
|
|
content = self._get_content()
|
|
|
|
|
else:
|
|
|
|
|
content = self._content
|
|
|
|
|
return content
|
|
|
|
|
|
2011-10-29 12:57:15 +02:00
|
|
|
def _get_summary(self):
|
2012-03-16 19:59:03 +01:00
|
|
|
"""Returns the summary of an article, based on the summary metadata
|
2012-03-13 17:10:20 +01:00
|
|
|
if it is set, else troncate the content."""
|
|
|
|
|
if hasattr(self, '_summary'):
|
|
|
|
|
return self._summary
|
|
|
|
|
else:
|
|
|
|
|
return truncate_html_words(self.content, 50)
|
2011-10-29 12:57:15 +02:00
|
|
|
|
|
|
|
|
def _set_summary(self, summary):
|
|
|
|
|
"""Dummy function"""
|
|
|
|
|
pass
|
|
|
|
|
|
2012-03-09 16:21:38 +01:00
|
|
|
summary = property(_get_summary, _set_summary, "Summary of the article."
|
|
|
|
|
"Based on the content. Can't be set")
|
2011-10-29 12:57:15 +02:00
|
|
|
|
2012-03-11 01:14:22 +01:00
|
|
|
url = property(functools.partial(get_url_setting, key='url'))
|
|
|
|
|
save_as = property(functools.partial(get_url_setting, key='save_as'))
|
|
|
|
|
|
2011-10-29 12:57:15 +02:00
|
|
|
|
2010-10-30 00:56:40 +01:00
|
|
|
class Article(Page):
|
2010-10-30 16:47:59 +01:00
|
|
|
mandatory_properties = ('title', 'date', 'category')
|
2010-10-30 00:56:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class Quote(Page):
|
|
|
|
|
base_properties = ('author', 'date')
|
2010-11-05 00:22:03 +00:00
|
|
|
|
2012-03-09 16:22:28 +01:00
|
|
|
|
2011-12-22 16:22:34 +00:00
|
|
|
class URLWrapper(object):
|
2011-12-23 23:43:32 +00:00
|
|
|
def __init__(self, name, settings):
|
2011-12-22 16:22:34 +00:00
|
|
|
self.name = unicode(name)
|
2012-03-11 01:14:22 +01:00
|
|
|
self.slug = slugify(self.name)
|
2011-12-23 23:43:32 +00:00
|
|
|
self.settings = settings
|
2011-12-22 15:13:12 +00:00
|
|
|
|
2012-03-11 01:14:22 +01:00
|
|
|
def as_dict(self):
|
|
|
|
|
return self.__dict__
|
|
|
|
|
|
2011-12-22 15:13:12 +00:00
|
|
|
def __hash__(self):
|
2011-12-22 16:22:34 +00:00
|
|
|
return hash(self.name)
|
2011-12-22 15:13:12 +00:00
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
2011-12-22 16:22:34 +00:00
|
|
|
return self.name == unicode(other)
|
2011-12-22 15:13:12 +00:00
|
|
|
|
|
|
|
|
def __str__(self):
|
2012-03-23 19:31:44 +01:00
|
|
|
return str(self.name.encode('utf-8', 'replace'))
|
2011-12-22 15:13:12 +00:00
|
|
|
|
|
|
|
|
def __unicode__(self):
|
2011-12-22 16:22:34 +00:00
|
|
|
return self.name
|
2011-12-22 15:13:12 +00:00
|
|
|
|
2012-03-11 01:14:22 +01:00
|
|
|
def _from_settings(self, key):
|
|
|
|
|
setting = "%s_%s" % (self.__class__.__name__.upper(), key)
|
2012-03-23 19:31:44 +01:00
|
|
|
return unicode(self.settings[setting]).format(**self.as_dict())
|
2011-12-22 15:43:44 +00:00
|
|
|
|
2012-03-11 01:14:22 +01:00
|
|
|
url = property(functools.partial(_from_settings, key='URL'))
|
|
|
|
|
save_as = property(functools.partial(_from_settings, key='SAVE_AS'))
|
2012-03-09 16:22:28 +01:00
|
|
|
|
2011-12-23 23:43:32 +00:00
|
|
|
|
2012-03-11 01:14:22 +01:00
|
|
|
class Category(URLWrapper):
|
|
|
|
|
pass
|
2012-03-09 16:22:28 +01:00
|
|
|
|
2011-12-22 15:43:44 +00:00
|
|
|
|
2011-12-22 16:22:34 +00:00
|
|
|
class Tag(URLWrapper):
|
2011-12-23 23:43:32 +00:00
|
|
|
def __init__(self, name, *args, **kwargs):
|
|
|
|
|
super(Tag, self).__init__(unicode.strip(name), *args, **kwargs)
|
2011-12-22 15:43:44 +00:00
|
|
|
|
|
|
|
|
|
2011-12-22 16:22:34 +00:00
|
|
|
class Author(URLWrapper):
|
2012-03-11 01:14:22 +01:00
|
|
|
pass
|
2012-03-09 16:22:28 +01:00
|
|
|
|
2011-12-22 16:22:34 +00:00
|
|
|
|
2010-11-05 00:22:03 +00:00
|
|
|
def is_valid_content(content, f):
|
|
|
|
|
try:
|
|
|
|
|
content.check_properties()
|
|
|
|
|
return True
|
2011-05-06 16:45:27 +06:00
|
|
|
except NameError, e:
|
2012-03-20 13:01:21 +00:00
|
|
|
logger.error(u"Skipping %s: impossible to find informations about '%s'"\
|
2012-03-09 16:21:38 +01:00
|
|
|
% (f, e))
|
2010-11-05 00:22:03 +00:00
|
|
|
return False
|