2010-10-30 00:56:40 +01:00
|
|
|
from docutils import core
|
2010-10-31 00:08:16 +01:00
|
|
|
from markdown import Markdown
|
2010-10-30 00:56:40 +01:00
|
|
|
import re
|
2010-12-22 01:08:23 +03:00
|
|
|
import string
|
2010-10-30 00:56:40 +01:00
|
|
|
|
|
|
|
|
# import the directives to have pygments support
|
|
|
|
|
import rstdirectives
|
|
|
|
|
|
|
|
|
|
from pelican.utils import get_date, open
|
|
|
|
|
|
|
|
|
|
|
2010-12-22 01:08:23 +03:00
|
|
|
_METADATAS_PROCESSORS = {
|
|
|
|
|
'tags': lambda x: map(string.strip, x.split(',')),
|
|
|
|
|
'date': lambda x: get_date(x),
|
|
|
|
|
'status': string.strip,
|
|
|
|
|
}
|
2010-10-30 00:56:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class RstReader(object):
|
|
|
|
|
|
|
|
|
|
def _parse_metadata(self, content):
|
|
|
|
|
"""Return the dict containing metadatas"""
|
|
|
|
|
output = {}
|
2010-12-22 01:08:23 +03:00
|
|
|
for m in re.compile('^:([a-z]+): (.*)\s', re.M).finditer(content):
|
2010-10-30 00:56:40 +01:00
|
|
|
name, value = m.group(1).lower(), m.group(2)
|
2010-12-22 01:08:23 +03:00
|
|
|
output[name] = _METADATAS_PROCESSORS.get(
|
|
|
|
|
name, lambda x:x
|
|
|
|
|
)(value)
|
2010-10-30 00:56:40 +01:00
|
|
|
return output
|
|
|
|
|
|
|
|
|
|
def read(self, filename):
|
|
|
|
|
"""Parse restructured text"""
|
|
|
|
|
text = open(filename)
|
|
|
|
|
metadatas = self._parse_metadata(text)
|
|
|
|
|
extra_params = {'input_encoding': 'unicode',
|
|
|
|
|
'initial_header_level': '2'}
|
|
|
|
|
rendered_content = core.publish_parts(text, writer_name='html',
|
|
|
|
|
settings_overrides=extra_params)
|
|
|
|
|
title = rendered_content.get('title')
|
|
|
|
|
content = rendered_content.get('body')
|
|
|
|
|
if not metadatas.has_key('title'):
|
|
|
|
|
metadatas['title'] = title
|
|
|
|
|
return content, metadatas
|
|
|
|
|
|
2010-10-31 00:08:16 +01:00
|
|
|
class MarkdownReader(object):
|
|
|
|
|
|
|
|
|
|
def read(self, filename):
|
|
|
|
|
"""Parse content and metadata of markdown files"""
|
|
|
|
|
text = open(filename)
|
2010-11-24 15:49:10 +01:00
|
|
|
md = Markdown(extensions = ['meta', 'codehilite'])
|
2010-10-31 00:08:16 +01:00
|
|
|
content = md.convert(text)
|
|
|
|
|
|
|
|
|
|
metadatas = {}
|
|
|
|
|
for name, value in md.Meta.items():
|
2010-12-17 00:04:45 +03:00
|
|
|
name = name.lower()
|
2010-12-22 01:08:23 +03:00
|
|
|
metadatas[name] = _METADATAS_PROCESSORS.get(
|
2010-12-17 00:07:55 +03:00
|
|
|
name, lambda x:x
|
|
|
|
|
)(value[0])
|
2010-10-31 00:08:16 +01:00
|
|
|
return content, metadatas
|
|
|
|
|
|
|
|
|
|
_EXTENSIONS = {'rst': RstReader, 'md': MarkdownReader} # supported formats
|
2010-10-30 00:56:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def read_file(filename, fmt=None):
|
|
|
|
|
"""Return a reader object using the given format."""
|
|
|
|
|
if not fmt:
|
2010-10-31 00:08:16 +01:00
|
|
|
fmt = filename.split('.')[-1]
|
2010-10-30 00:56:40 +01:00
|
|
|
if fmt not in _EXTENSIONS.keys():
|
2010-10-31 00:08:16 +01:00
|
|
|
raise TypeError('Pelican does not know how to parse %s' % filename)
|
2010-10-30 00:56:40 +01:00
|
|
|
reader = _EXTENSIONS[fmt]()
|
|
|
|
|
return reader.read(filename)
|