1
0
Fork 0
forked from github/pelican
pelican-theme/pelican/contents.py

100 lines
3.1 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
from pelican.utils import slugify, truncate_html_words
from pelican.log import *
class Page(object):
"""Represents a page
2011-05-07 20:00:30 +01:00
Given a content, and metadata, create an adequate object.
:param content: the string to parse, containing the original content.
"""
2010-10-30 20:17:23 +01:00
mandatory_properties = ('title',)
2011-05-07 20:00:30 +01:00
def __init__(self, content, metadata={}, settings={}, filename=None):
self._content = content
self.translations = []
2010-11-15 18:58:54 +00:00
self.status = "published" # default value
local_metadata = dict(settings['DEFAULT_METADATA'])
2011-05-07 20:00:30 +01:00
local_metadata.update(metadata)
for key, value in local_metadata.items():
setattr(self, key.lower(), value)
if not hasattr(self, 'author'):
if 'AUTHOR' in settings:
self.author = settings['AUTHOR']
default_lang = settings.get('DEFAULT_LANG').lower()
if not hasattr(self, 'lang'):
self.lang = default_lang
self.in_default_lang = (self.lang == default_lang)
if not hasattr(self, 'slug'):
self.slug = slugify(self.title)
if not hasattr(self, 'save_as'):
if self.in_default_lang:
self.save_as = '%s.html' % self.slug
clean_url = '%s/' % self.slug
else:
self.save_as = '%s-%s.html' % (self.slug, self.lang)
clean_url = '%s-%s/' % (self.slug, self.lang)
if settings.get('CLEAN_URLS', False):
self.url = clean_url
else:
self.url = self.save_as
2010-11-06 02:03:32 +00:00
if filename:
self.filename = filename
if not hasattr(self, 'date_format'):
if self.lang in settings['DATE_FORMATS']:
self.date_format = settings['DATE_FORMATS'][self.lang]
else:
self.date_format = settings['DEFAULT_DATE_FORMAT']
if hasattr(self, 'date'):
self.locale_date = self.date.strftime(self.date_format.encode('ascii','xmlcharrefreplace')).decode('utf')
if not hasattr(self, 'summary'):
self.summary = property(lambda self: truncate_html_words(self.content, 50)).__get__(self, Page)
if not hasattr(self, 'status'):
self.status = settings['DEFAULT_STATUS']
# store the settings ref.
self._settings = settings
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)
@property
def content(self):
if hasattr(self, "_get_content"):
content = self._get_content()
else:
content = self._content
return content
class Article(Page):
Put the notmyidea theme by default. --HG-- rename : samples/themes/notmyidea/css/main.css => pelican/themes/notmyidea/css/main.css rename : samples/themes/notmyidea/css/pygment.css => pelican/themes/notmyidea/css/pygment.css rename : samples/themes/notmyidea/css/reset.css => pelican/themes/notmyidea/css/reset.css rename : samples/themes/notmyidea/images/icons/delicious.png => pelican/themes/notmyidea/images/icons/delicious.png rename : samples/themes/notmyidea/images/icons/lastfm.png => pelican/themes/notmyidea/images/icons/lastfm.png rename : samples/themes/notmyidea/images/icons/rss.png => pelican/themes/notmyidea/images/icons/rss.png rename : samples/themes/notmyidea/images/icons/twitter.png => pelican/themes/notmyidea/images/icons/twitter.png rename : samples/themes/notmyidea/templates/archives.html => pelican/themes/notmyidea/templates/archives.html rename : samples/themes/notmyidea/templates/article.html => pelican/themes/notmyidea/templates/article.html rename : samples/themes/notmyidea/templates/base.html => pelican/themes/notmyidea/templates/base.html rename : samples/themes/notmyidea/templates/categories.html => pelican/themes/notmyidea/templates/categories.html rename : samples/themes/notmyidea/templates/category.html => pelican/themes/notmyidea/templates/category.html rename : samples/themes/notmyidea/templates/index.html => pelican/themes/notmyidea/templates/index.html rename : samples/themes/notmyidea/templates/tag.html => pelican/themes/notmyidea/templates/tag.html rename : samples/themes/notmyidea/templates/tags.html => pelican/themes/notmyidea/templates/tags.html rename : pelican/themes/templates/archives.html => pelican/themes/simple/templates/archives.html rename : pelican/themes/templates/article.html => pelican/themes/simple/templates/article.html rename : pelican/themes/templates/base.html => pelican/themes/simple/templates/base.html rename : pelican/themes/templates/categories.html => pelican/themes/simple/templates/categories.html rename : pelican/themes/templates/category.html => pelican/themes/simple/templates/category.html rename : pelican/themes/templates/index.html => pelican/themes/simple/templates/index.html rename : pelican/themes/templates/tag.html => pelican/themes/simple/templates/tag.html rename : pelican/themes/templates/tags.html => pelican/themes/simple/templates/tags.html
2010-10-30 16:47:59 +01:00
mandatory_properties = ('title', 'date', 'category')
class Quote(Page):
base_properties = ('author', 'date')
def is_valid_content(content, f):
try:
content.check_properties()
return True
2011-05-06 16:45:27 +06:00
except NameError, e:
error(u"Skipping %s: impossible to find informations about '%s'" % (f, e))
return False