2014-03-26 10:59:49 +00:00
|
|
|
import os
|
2014-03-26 10:35:27 +00:00
|
|
|
from pelican import signals
|
|
|
|
from pelican.readers import BaseReader
|
2014-03-30 14:02:53 +00:00
|
|
|
from pelican.utils import pelican_open
|
2014-03-26 10:35:27 +00:00
|
|
|
import pypandoc
|
|
|
|
|
2014-08-27 07:41:47 +00:00
|
|
|
|
|
|
|
class PandocReader(BaseReader):
|
2014-03-26 10:35:27 +00:00
|
|
|
enabled = True
|
|
|
|
file_extensions = ['md', 'markdown', 'mkd', 'mdown']
|
|
|
|
|
|
|
|
def read(self, filename):
|
2014-03-30 14:02:53 +00:00
|
|
|
with pelican_open(filename) as text:
|
2014-03-26 10:35:27 +00:00
|
|
|
metadata_items = []
|
|
|
|
in_content = False
|
|
|
|
MD = ''
|
2014-03-30 14:02:53 +00:00
|
|
|
for line in text.splitlines():
|
2014-03-26 10:35:27 +00:00
|
|
|
splitted = line.split(':', 1)
|
|
|
|
if len(splitted) == 2 and not in_content:
|
|
|
|
metadata_items.append(splitted)
|
|
|
|
else:
|
|
|
|
in_content = True
|
2014-03-30 14:02:53 +00:00
|
|
|
MD += line + '\n'
|
2014-03-26 10:35:27 +00:00
|
|
|
|
|
|
|
metadata = {}
|
2014-08-27 07:41:47 +00:00
|
|
|
for name, value in metadata_items:
|
2014-03-26 10:35:27 +00:00
|
|
|
name = name.lower()
|
|
|
|
value = value.strip()
|
2014-08-27 07:41:47 +00:00
|
|
|
metadata[name] = self.process_metadata(name, value)
|
2014-03-26 10:35:27 +00:00
|
|
|
|
2014-03-26 10:59:49 +00:00
|
|
|
os.chdir(self.settings['PATH']) # change the cwd to the content dir
|
2014-08-27 07:41:47 +00:00
|
|
|
if not 'PANDOC_ARGS' in self.settings: self.settings['PANDOC_ARGS'] = []
|
|
|
|
output = pypandoc.convert(MD, 'html5', format='md', extra_args=self.settings['PANDOC_ARGS'])
|
2014-03-26 10:35:27 +00:00
|
|
|
|
|
|
|
return output, metadata
|
|
|
|
|
2014-08-27 07:41:47 +00:00
|
|
|
|
2014-03-26 10:35:27 +00:00
|
|
|
def add_reader(readers):
|
2014-08-27 07:41:47 +00:00
|
|
|
readers.reader_classes['md'] = PandocReader
|
|
|
|
|
2014-03-26 10:35:27 +00:00
|
|
|
|
|
|
|
def register():
|
|
|
|
signals.readers_init.connect(add_reader)
|