mirror of
https://github.com/getpelican/pelican.git
synced 2025-10-15 20:28:56 +02:00
Metadata, not metadatas.
This commit is contained in:
parent
56effc24cb
commit
52f2a8383a
4 changed files with 29 additions and 29 deletions
|
|
@ -49,14 +49,14 @@ Take a look to the Markdown reader::
|
||||||
md = Markdown(extensions = ['meta', 'codehilite'])
|
md = Markdown(extensions = ['meta', 'codehilite'])
|
||||||
content = md.convert(text)
|
content = md.convert(text)
|
||||||
|
|
||||||
metadatas = {}
|
metadata = {}
|
||||||
for name, value in md.Meta.items():
|
for name, value in md.Meta.items():
|
||||||
if name in _METADATAS_FIELDS:
|
if name in _METADATA_FIELDS:
|
||||||
meta = _METADATAS_FIELDS[name](value[0])
|
meta = _METADATA_FIELDS[name](value[0])
|
||||||
else:
|
else:
|
||||||
meta = value[0]
|
meta = value[0]
|
||||||
metadatas[name.lower()] = meta
|
metadata[name.lower()] = meta
|
||||||
return content, metadatas
|
return content, metadata
|
||||||
|
|
||||||
Simple isn't it ?
|
Simple isn't it ?
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,21 +4,21 @@ from pelican.log import *
|
||||||
|
|
||||||
class Page(object):
|
class Page(object):
|
||||||
"""Represents a page
|
"""Represents a page
|
||||||
Given a content, and metadatas, create an adequate object.
|
Given a content, and metadata, create an adequate object.
|
||||||
|
|
||||||
:param string: the string to parse, containing the original content.
|
:param string: the string to parse, containing the original content.
|
||||||
:param markup: the markup language to use while parsing.
|
:param markup: the markup language to use while parsing.
|
||||||
"""
|
"""
|
||||||
mandatory_properties = ('title',)
|
mandatory_properties = ('title',)
|
||||||
|
|
||||||
def __init__(self, content, metadatas={}, settings={}, filename=None):
|
def __init__(self, content, metadata={}, settings={}, filename=None):
|
||||||
self._content = content
|
self._content = content
|
||||||
self.translations = []
|
self.translations = []
|
||||||
|
|
||||||
self.status = "published" # default value
|
self.status = "published" # default value
|
||||||
|
|
||||||
local_metadata = dict(settings['DEFAULT_METADATA'])
|
local_metadata = dict(settings['DEFAULT_METADATA'])
|
||||||
local_metadata.update(metadatas)
|
local_metadata.update(metadata)
|
||||||
for key, value in local_metadata.items():
|
for key, value in local_metadata.items():
|
||||||
setattr(self, key.lower(), value)
|
setattr(self, key.lower(), value)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -183,10 +183,10 @@ class ArticlesGenerator(Generator):
|
||||||
files = self.get_files(self.path, exclude=['pages',])
|
files = self.get_files(self.path, exclude=['pages',])
|
||||||
all_articles = []
|
all_articles = []
|
||||||
for f in files:
|
for f in files:
|
||||||
content, metadatas = read_file(f)
|
content, metadata = read_file(f)
|
||||||
|
|
||||||
# if no category is set, use the name of the path as a category
|
# if no category is set, use the name of the path as a category
|
||||||
if 'category' not in metadatas.keys():
|
if 'category' not in metadata.keys():
|
||||||
|
|
||||||
if os.path.dirname(f) == self.path:
|
if os.path.dirname(f) == self.path:
|
||||||
category = self.settings['DEFAULT_CATEGORY']
|
category = self.settings['DEFAULT_CATEGORY']
|
||||||
|
|
@ -194,13 +194,13 @@ class ArticlesGenerator(Generator):
|
||||||
category = os.path.basename(os.path.dirname(f))
|
category = os.path.basename(os.path.dirname(f))
|
||||||
|
|
||||||
if category != '':
|
if category != '':
|
||||||
metadatas['category'] = unicode(category)
|
metadata['category'] = unicode(category)
|
||||||
|
|
||||||
if 'date' not in metadatas.keys()\
|
if 'date' not in metadata.keys()\
|
||||||
and self.settings['FALLBACK_ON_FS_DATE']:
|
and self.settings['FALLBACK_ON_FS_DATE']:
|
||||||
metadatas['date'] = datetime.fromtimestamp(os.stat(f).st_ctime)
|
metadata['date'] = datetime.fromtimestamp(os.stat(f).st_ctime)
|
||||||
|
|
||||||
article = Article(content, metadatas, settings=self.settings,
|
article = Article(content, metadata, settings=self.settings,
|
||||||
filename=f)
|
filename=f)
|
||||||
if not is_valid_content(article, f):
|
if not is_valid_content(article, f):
|
||||||
continue
|
continue
|
||||||
|
|
@ -273,8 +273,8 @@ class PagesGenerator(Generator):
|
||||||
def generate_context(self):
|
def generate_context(self):
|
||||||
all_pages = []
|
all_pages = []
|
||||||
for f in self.get_files(os.sep.join((self.path, 'pages'))):
|
for f in self.get_files(os.sep.join((self.path, 'pages'))):
|
||||||
content, metadatas = read_file(f)
|
content, metadata = read_file(f)
|
||||||
page = Page(content, metadatas, settings=self.settings,
|
page = Page(content, metadata, settings=self.settings,
|
||||||
filename=f)
|
filename=f)
|
||||||
if not is_valid_content(page, f):
|
if not is_valid_content(page, f):
|
||||||
continue
|
continue
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@ import re
|
||||||
from pelican.utils import get_date, open
|
from pelican.utils import get_date, open
|
||||||
|
|
||||||
|
|
||||||
_METADATAS_PROCESSORS = {
|
_METADATA_PROCESSORS = {
|
||||||
'tags': lambda x: map(unicode.strip, x.split(',')),
|
'tags': lambda x: map(unicode.strip, x.split(',')),
|
||||||
'date': lambda x: get_date(x),
|
'date': lambda x: get_date(x),
|
||||||
'status': unicode.strip,
|
'status': unicode.strip,
|
||||||
|
|
@ -30,11 +30,11 @@ class RstReader(Reader):
|
||||||
extension = "rst"
|
extension = "rst"
|
||||||
|
|
||||||
def _parse_metadata(self, content):
|
def _parse_metadata(self, content):
|
||||||
"""Return the dict containing metadatas"""
|
"""Return the dict containing metadata"""
|
||||||
output = {}
|
output = {}
|
||||||
for m in re.compile('^:([a-z]+): (.*)\s', re.M).finditer(content):
|
for m in re.compile('^:([a-z]+): (.*)\s', re.M).finditer(content):
|
||||||
name, value = m.group(1).lower(), m.group(2)
|
name, value = m.group(1).lower(), m.group(2)
|
||||||
output[name] = _METADATAS_PROCESSORS.get(
|
output[name] = _METADATA_PROCESSORS.get(
|
||||||
name, lambda x:x
|
name, lambda x:x
|
||||||
)(value)
|
)(value)
|
||||||
return output
|
return output
|
||||||
|
|
@ -42,7 +42,7 @@ class RstReader(Reader):
|
||||||
def read(self, filename):
|
def read(self, filename):
|
||||||
"""Parse restructured text"""
|
"""Parse restructured text"""
|
||||||
text = open(filename)
|
text = open(filename)
|
||||||
metadatas = self._parse_metadata(text)
|
metadata = self._parse_metadata(text)
|
||||||
extra_params = {'input_encoding': 'unicode',
|
extra_params = {'input_encoding': 'unicode',
|
||||||
'initial_header_level': '2'}
|
'initial_header_level': '2'}
|
||||||
rendered_content = core.publish_parts(text,
|
rendered_content = core.publish_parts(text,
|
||||||
|
|
@ -51,9 +51,9 @@ class RstReader(Reader):
|
||||||
settings_overrides=extra_params)
|
settings_overrides=extra_params)
|
||||||
title = rendered_content.get('title')
|
title = rendered_content.get('title')
|
||||||
content = rendered_content.get('body')
|
content = rendered_content.get('body')
|
||||||
if not metadatas.has_key('title'):
|
if not metadata.has_key('title'):
|
||||||
metadatas['title'] = title
|
metadata['title'] = title
|
||||||
return content, metadatas
|
return content, metadata
|
||||||
|
|
||||||
class MarkdownReader(Reader):
|
class MarkdownReader(Reader):
|
||||||
enabled = bool(Markdown)
|
enabled = bool(Markdown)
|
||||||
|
|
@ -65,13 +65,13 @@ class MarkdownReader(Reader):
|
||||||
md = Markdown(extensions = ['meta', 'codehilite'])
|
md = Markdown(extensions = ['meta', 'codehilite'])
|
||||||
content = md.convert(text)
|
content = md.convert(text)
|
||||||
|
|
||||||
metadatas = {}
|
metadata = {}
|
||||||
for name, value in md.Meta.items():
|
for name, value in md.Meta.items():
|
||||||
name = name.lower()
|
name = name.lower()
|
||||||
metadatas[name] = _METADATAS_PROCESSORS.get(
|
metadata[name] = _METADATA_PROCESSORS.get(
|
||||||
name, lambda x:x
|
name, lambda x:x
|
||||||
)(value[0])
|
)(value[0])
|
||||||
return content, metadatas
|
return content, metadata
|
||||||
|
|
||||||
|
|
||||||
class HtmlReader(Reader):
|
class HtmlReader(Reader):
|
||||||
|
|
@ -81,13 +81,13 @@ class HtmlReader(Reader):
|
||||||
def read(self, filename):
|
def read(self, filename):
|
||||||
"""Parse content and metadata of (x)HTML files"""
|
"""Parse content and metadata of (x)HTML files"""
|
||||||
content = open(filename)
|
content = open(filename)
|
||||||
metadatas = {'title':'unnamed'}
|
metadata = {'title':'unnamed'}
|
||||||
for i in self._re.findall(content):
|
for i in self._re.findall(content):
|
||||||
key = i.split(':')[0][5:].strip()
|
key = i.split(':')[0][5:].strip()
|
||||||
value = i.split(':')[-1][:-3].strip()
|
value = i.split(':')[-1][:-3].strip()
|
||||||
metadatas[key.lower()] = value
|
metadata[key.lower()] = value
|
||||||
|
|
||||||
return content, metadatas
|
return content, metadata
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue