Merge pull request #2 from zackw/master
I very much like, what you have done with the code. I agree, that it is nice to be able to declare pandoc extensions. On the other hand, I disapproved at taking out pypandoc at first. However, after taking a closer look at the source of pypandoc, I came to realize, that in this case we do not hugely benefit by using an abstraction layer. Your code works nicely with my setup with the exception that you removed the change of the cwd. However, as pandoc_reader is still a very young project, I think we can afford to break compatibility. Thank you very much for your contribution!
This commit is contained in:
commit
70b2c1d1cc
@ -7,7 +7,6 @@ A pandoc [markdown] reader plugin for [pelican]
|
|||||||
Requirements
|
Requirements
|
||||||
------------
|
------------
|
||||||
|
|
||||||
- [pypandoc]
|
|
||||||
- [pandoc] in $PATH
|
- [pandoc] in $PATH
|
||||||
|
|
||||||
|
|
||||||
@ -30,6 +29,13 @@ Additional command line parameters can be passed to pandoc via the PANDOC_ARGS p
|
|||||||
'--number-sections',
|
'--number-sections',
|
||||||
]
|
]
|
||||||
|
|
||||||
|
Pandoc's markdown extensions can be enabled or disabled via the
|
||||||
|
PANDOC_EXTENSIONS parameter.
|
||||||
|
|
||||||
|
PANDOC_EXTENSIONS = [
|
||||||
|
'+hard_line_breaks',
|
||||||
|
'-citations'
|
||||||
|
]
|
||||||
|
|
||||||
Contributing
|
Contributing
|
||||||
------------
|
------------
|
||||||
@ -44,4 +50,3 @@ Contributing
|
|||||||
[markdown]: http://daringfireball.net/projects/markdown/
|
[markdown]: http://daringfireball.net/projects/markdown/
|
||||||
[pandoc]: http://johnmacfarlane.net/pandoc/
|
[pandoc]: http://johnmacfarlane.net/pandoc/
|
||||||
[pelican]: http://getpelican.com
|
[pelican]: http://getpelican.com
|
||||||
[pypandoc]: https://github.com/bebraw/pypandoc
|
|
||||||
|
@ -1,43 +1,48 @@
|
|||||||
import os
|
import subprocess
|
||||||
|
|
||||||
from pelican import signals
|
from pelican import signals
|
||||||
from pelican.readers import BaseReader
|
from pelican.readers import BaseReader
|
||||||
from pelican.utils import pelican_open
|
from pelican.utils import pelican_open
|
||||||
import pypandoc
|
|
||||||
|
|
||||||
|
|
||||||
class PandocReader(BaseReader):
|
class PandocReader(BaseReader):
|
||||||
enabled = True
|
enabled = True
|
||||||
file_extensions = ['md', 'markdown', 'mkd', 'mdown']
|
file_extensions = ['md', 'markdown', 'mkd', 'mdown']
|
||||||
|
|
||||||
def read(self, filename):
|
def read(self, filename):
|
||||||
with pelican_open(filename) as text:
|
with pelican_open(filename) as fp:
|
||||||
metadata_items = []
|
text = list(fp.splitlines())
|
||||||
in_content = False
|
|
||||||
MD = ''
|
|
||||||
for line in text.splitlines():
|
|
||||||
splitted = line.split(':', 1)
|
|
||||||
if len(splitted) == 2 and not in_content:
|
|
||||||
metadata_items.append(splitted)
|
|
||||||
else:
|
|
||||||
in_content = True
|
|
||||||
MD += line + '\n'
|
|
||||||
|
|
||||||
metadata = {}
|
metadata = {}
|
||||||
for name, value in metadata_items:
|
for i, line in enumerate(text):
|
||||||
name = name.lower()
|
kv = line.split(':', 1)
|
||||||
value = value.strip()
|
if len(kv) == 2:
|
||||||
|
name, value = kv[0].lower(), kv[1].strip()
|
||||||
metadata[name] = self.process_metadata(name, value)
|
metadata[name] = self.process_metadata(name, value)
|
||||||
|
else:
|
||||||
|
content = "\n".join(text[i:])
|
||||||
|
break
|
||||||
|
|
||||||
os.chdir(self.settings['PATH']) # change the cwd to the content dir
|
extra_args = self.settings.get('PANDOC_ARGS', [])
|
||||||
if not 'PANDOC_ARGS' in self.settings: self.settings['PANDOC_ARGS'] = []
|
extensions = self.settings.get('PANDOC_EXTENSIONS', '')
|
||||||
output = pypandoc.convert(MD, 'html5', format='md', extra_args=self.settings['PANDOC_ARGS'])
|
if isinstance(extensions, list):
|
||||||
|
extensions = ''.join(extensions)
|
||||||
|
|
||||||
|
pandoc_cmd = ["pandoc", "--from=markdown" + extensions, "--to=html5"]
|
||||||
|
pandoc_cmd.extend(extra_args)
|
||||||
|
|
||||||
|
proc = subprocess.Popen(pandoc_cmd,
|
||||||
|
stdin = subprocess.PIPE,
|
||||||
|
stdout = subprocess.PIPE)
|
||||||
|
|
||||||
|
output = proc.communicate(content.encode('utf-8'))[0].decode('utf-8')
|
||||||
|
status = proc.wait()
|
||||||
|
if status:
|
||||||
|
raise subprocess.CalledProcessError(status, pandoc_cmd)
|
||||||
|
|
||||||
return output, metadata
|
return output, metadata
|
||||||
|
|
||||||
|
|
||||||
def add_reader(readers):
|
def add_reader(readers):
|
||||||
readers.reader_classes['md'] = PandocReader
|
readers.reader_classes['md'] = PandocReader
|
||||||
|
|
||||||
|
|
||||||
def register():
|
def register():
|
||||||
signals.readers_init.connect(add_reader)
|
signals.readers_init.connect(add_reader)
|
||||||
|
Loading…
Reference in New Issue
Block a user