Skip to content

Commit 70b2c1d

Browse files
committed
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!
2 parents 1dceb54 + 40d1578 commit 70b2c1d

File tree

2 files changed

+36
-26
lines changed

2 files changed

+36
-26
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ A pandoc [markdown] reader plugin for [pelican]
77
Requirements
88
------------
99

10-
- [pypandoc]
1110
- [pandoc] in $PATH
1211

1312

@@ -30,6 +29,13 @@ Additional command line parameters can be passed to pandoc via the PANDOC_ARGS p
3029
'--number-sections',
3130
]
3231

32+
Pandoc's markdown extensions can be enabled or disabled via the
33+
PANDOC_EXTENSIONS parameter.
34+
35+
PANDOC_EXTENSIONS = [
36+
'+hard_line_breaks',
37+
'-citations'
38+
]
3339

3440
Contributing
3541
------------
@@ -44,4 +50,3 @@ Contributing
4450
[markdown]: http://daringfireball.net/projects/markdown/
4551
[pandoc]: http://johnmacfarlane.net/pandoc/
4652
[pelican]: http://getpelican.com
47-
[pypandoc]: https://github.com/bebraw/pypandoc

pandoc_reader.py

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,48 @@
1-
import os
1+
import subprocess
2+
23
from pelican import signals
34
from pelican.readers import BaseReader
45
from pelican.utils import pelican_open
5-
import pypandoc
6-
76

87
class PandocReader(BaseReader):
98
enabled = True
109
file_extensions = ['md', 'markdown', 'mkd', 'mdown']
1110

1211
def read(self, filename):
13-
with pelican_open(filename) as text:
14-
metadata_items = []
15-
in_content = False
16-
MD = ''
17-
for line in text.splitlines():
18-
splitted = line.split(':', 1)
19-
if len(splitted) == 2 and not in_content:
20-
metadata_items.append(splitted)
21-
else:
22-
in_content = True
23-
MD += line + '\n'
24-
25-
metadata = {}
26-
for name, value in metadata_items:
27-
name = name.lower()
28-
value = value.strip()
12+
with pelican_open(filename) as fp:
13+
text = list(fp.splitlines())
14+
15+
metadata = {}
16+
for i, line in enumerate(text):
17+
kv = line.split(':', 1)
18+
if len(kv) == 2:
19+
name, value = kv[0].lower(), kv[1].strip()
2920
metadata[name] = self.process_metadata(name, value)
21+
else:
22+
content = "\n".join(text[i:])
23+
break
3024

31-
os.chdir(self.settings['PATH']) # change the cwd to the content dir
32-
if not 'PANDOC_ARGS' in self.settings: self.settings['PANDOC_ARGS'] = []
33-
output = pypandoc.convert(MD, 'html5', format='md', extra_args=self.settings['PANDOC_ARGS'])
25+
extra_args = self.settings.get('PANDOC_ARGS', [])
26+
extensions = self.settings.get('PANDOC_EXTENSIONS', '')
27+
if isinstance(extensions, list):
28+
extensions = ''.join(extensions)
3429

35-
return output, metadata
30+
pandoc_cmd = ["pandoc", "--from=markdown" + extensions, "--to=html5"]
31+
pandoc_cmd.extend(extra_args)
32+
33+
proc = subprocess.Popen(pandoc_cmd,
34+
stdin = subprocess.PIPE,
35+
stdout = subprocess.PIPE)
3636

37+
output = proc.communicate(content.encode('utf-8'))[0].decode('utf-8')
38+
status = proc.wait()
39+
if status:
40+
raise subprocess.CalledProcessError(status, pandoc_cmd)
41+
42+
return output, metadata
3743

3844
def add_reader(readers):
3945
readers.reader_classes['md'] = PandocReader
4046

41-
4247
def register():
4348
signals.readers_init.connect(add_reader)

0 commit comments

Comments
 (0)