Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Dirty hack] to parse pandoc-style metadata correctly #15

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Requirements
------------

- [pandoc] in $PATH

- PyYaml for metadata loading

Installation
------------
Expand Down
38 changes: 28 additions & 10 deletions pandoc_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,38 @@
from pelican import signals
from pelican.readers import BaseReader
from pelican.utils import pelican_open
from yaml import load
try:
from yaml import CLoader as Loader
except ImportError:
from yaml import Loader

from datetime import datetime

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

def read(self, filename):
with pelican_open(filename) as fp:
text = list(fp.splitlines())
text = tuple(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
init = text.index("...")
end = text[init:].index("---") + init

metatext = "\n".join(text[init+1:end])
metadata = load(metatext, Loader=Loader)

if "Date" in metadata:
# Back to string because PyYaml is way too clever
metadata["Date"] = metadata["Date"].isoformat()

finalmeta = {}
for k,v in metadata.items():
finalmeta[k.lower()] = self.process_metadata(k.lower(),v)

content = "\n".join(text[:init] + text[end+1:])

extra_args = self.settings.get('PANDOC_ARGS', [])
extensions = self.settings.get('PANDOC_EXTENSIONS', '')
Expand All @@ -38,7 +52,11 @@ def read(self, filename):
if status:
raise subprocess.CalledProcessError(status, pandoc_cmd)

return output, metadata
# Need that to make {static} -like tags be available
output = output.replace("%7B", "{")
output = output.replace("%7D", "}")

return output, finalmeta

def add_reader(readers):
for ext in PandocReader.file_extensions:
Expand Down