-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmmd_reader.py
42 lines (33 loc) · 1.25 KB
/
mmd_reader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import subprocess
from pelican import signals
from pelican.readers import BaseReader
from pelican.utils import pelican_open
class MmdReader(BaseReader):
enabled = True
file_extensions = ['md', 'markdown', 'mkd', 'mdown']
def read(self, filename):
with pelican_open(filename) as fp:
text = list(fp.splitlines())
metadata = {}
for i, line in enumerate(text):
kv = line.split(':', 1)
if len(kv) == 2:
name, value = kv[0].lower(), kv[1].strip()
metadata[name] = self.process_metadata(name, value)
else:
content = "\n".join(text[i:])
break
mmd_cmd = ["multimarkdown"]
proc = subprocess.Popen(mmd_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, mmd_cmd)
return output, metadata
def add_reader(readers):
for ext in MmdReader.file_extensions:
readers.reader_classes[ext] = MmdReader
def register():
signals.readers_init.connect(add_reader)