mirror of
https://github.com/getpelican/pelican.git
synced 2025-10-15 20:28:56 +02:00
Factorize some code about URL wrapping.
This commit is contained in:
parent
8499ce3340
commit
3bdc769134
1 changed files with 22 additions and 47 deletions
|
|
@ -2,6 +2,7 @@
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from os import getenv
|
from os import getenv
|
||||||
from sys import platform, stdin
|
from sys import platform, stdin
|
||||||
|
import functools
|
||||||
import locale
|
import locale
|
||||||
|
|
||||||
from pelican.log import warning, error
|
from pelican.log import warning, error
|
||||||
|
|
@ -108,17 +109,13 @@ class Page(object):
|
||||||
'category': getattr(self, 'category', 'misc'),
|
'category': getattr(self, 'category', 'misc'),
|
||||||
}
|
}
|
||||||
|
|
||||||
@property
|
def _expand_settings(self, key):
|
||||||
def url(self):
|
fq_key = ('%s_%s' % (self.__class__.__name__, key)).upper()
|
||||||
if self.in_default_lang:
|
return self.settings[fq_key].format(**self.url_format)
|
||||||
return self.settings['PAGE_URL'].format(**self.url_format)
|
|
||||||
return self.settings['PAGE_LANG_URL'].format(**self.url_format)
|
|
||||||
|
|
||||||
@property
|
def get_url_setting(self, key):
|
||||||
def save_as(self):
|
key = key if self.in_default_lang else 'lang_%s' % key
|
||||||
if self.in_default_lang:
|
return self._expand_settings(key)
|
||||||
return self.settings['PAGE_SAVE_AS'].format(**self.url_format)
|
|
||||||
return self.settings['PAGE_LANG_SAVE_AS'].format(**self.url_format)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def content(self):
|
def content(self):
|
||||||
|
|
@ -139,22 +136,13 @@ class Page(object):
|
||||||
summary = property(_get_summary, _set_summary, "Summary of the article."
|
summary = property(_get_summary, _set_summary, "Summary of the article."
|
||||||
"Based on the content. Can't be set")
|
"Based on the content. Can't be set")
|
||||||
|
|
||||||
|
url = property(functools.partial(get_url_setting, key='url'))
|
||||||
|
save_as = property(functools.partial(get_url_setting, key='save_as'))
|
||||||
|
|
||||||
|
|
||||||
class Article(Page):
|
class Article(Page):
|
||||||
mandatory_properties = ('title', 'date', 'category')
|
mandatory_properties = ('title', 'date', 'category')
|
||||||
|
|
||||||
@property
|
|
||||||
def url(self):
|
|
||||||
if self.in_default_lang:
|
|
||||||
return self.settings['ARTICLE_URL'].format(**self.url_format)
|
|
||||||
return self.settings['ARTICLE_LANG_URL'].format(**self.url_format)
|
|
||||||
|
|
||||||
@property
|
|
||||||
def save_as(self):
|
|
||||||
if self.in_default_lang:
|
|
||||||
return self.settings['ARTICLE_SAVE_AS'].format(**self.url_format)
|
|
||||||
return self.settings['ARTICLE_LANG_SAVE_AS'].format(**self.url_format)
|
|
||||||
|
|
||||||
|
|
||||||
class Quote(Page):
|
class Quote(Page):
|
||||||
base_properties = ('author', 'date')
|
base_properties = ('author', 'date')
|
||||||
|
|
@ -163,8 +151,12 @@ class Quote(Page):
|
||||||
class URLWrapper(object):
|
class URLWrapper(object):
|
||||||
def __init__(self, name, settings):
|
def __init__(self, name, settings):
|
||||||
self.name = unicode(name)
|
self.name = unicode(name)
|
||||||
|
self.slug = slugify(self.name)
|
||||||
self.settings = settings
|
self.settings = settings
|
||||||
|
|
||||||
|
def as_dict(self):
|
||||||
|
return self.__dict__
|
||||||
|
|
||||||
def __hash__(self):
|
def __hash__(self):
|
||||||
return hash(self.name)
|
return hash(self.name)
|
||||||
|
|
||||||
|
|
@ -177,42 +169,25 @@ class URLWrapper(object):
|
||||||
def __unicode__(self):
|
def __unicode__(self):
|
||||||
return self.name
|
return self.name
|
||||||
|
|
||||||
@property
|
def _from_settings(self, key):
|
||||||
def url(self):
|
setting = "%s_%s" % (self.__class__.__name__.upper(), key)
|
||||||
return '%s.html' % self.name
|
return self.settings[setting].format(**self.as_dict())
|
||||||
|
|
||||||
|
url = property(functools.partial(_from_settings, key='URL'))
|
||||||
|
save_as = property(functools.partial(_from_settings, key='SAVE_AS'))
|
||||||
|
|
||||||
|
|
||||||
class Category(URLWrapper):
|
class Category(URLWrapper):
|
||||||
@property
|
pass
|
||||||
def url(self):
|
|
||||||
return self.settings['CATEGORY_URL'].format(name=self.name)
|
|
||||||
|
|
||||||
@property
|
|
||||||
def save_as(self):
|
|
||||||
return self.settings['CATEGORY_SAVE_AS'].format(name=self.name)
|
|
||||||
|
|
||||||
|
|
||||||
class Tag(URLWrapper):
|
class Tag(URLWrapper):
|
||||||
def __init__(self, name, *args, **kwargs):
|
def __init__(self, name, *args, **kwargs):
|
||||||
super(Tag, self).__init__(unicode.strip(name), *args, **kwargs)
|
super(Tag, self).__init__(unicode.strip(name), *args, **kwargs)
|
||||||
|
|
||||||
@property
|
|
||||||
def url(self):
|
|
||||||
return self.settings['TAG_URL'].format(name=self.name)
|
|
||||||
|
|
||||||
@property
|
|
||||||
def save_as(self):
|
|
||||||
return self.settings['TAG_SAVE_AS'].format(name=self.name)
|
|
||||||
|
|
||||||
|
|
||||||
class Author(URLWrapper):
|
class Author(URLWrapper):
|
||||||
@property
|
pass
|
||||||
def url(self):
|
|
||||||
return self.settings['AUTHOR_URL'].format(name=self.name)
|
|
||||||
|
|
||||||
@property
|
|
||||||
def save_as(self):
|
|
||||||
return self.settings['AUTHOR_SAVE_AS'].format(name=self.name)
|
|
||||||
|
|
||||||
|
|
||||||
def is_valid_content(content, f):
|
def is_valid_content(content, f):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue