Skip to content

Instantly share code, notes, and snippets.

@juanAFernandez
Created November 28, 2018 17:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juanAFernandez/f67f36cf64698cb24306470af146d118 to your computer and use it in GitHub Desktop.
Save juanAFernandez/f67f36cf64698cb24306470af146d118 to your computer and use it in GitHub Desktop.
A json reader plugin to do Pelican able to read json files to render in html instead of Markdown or RST files.
from pelican import signals
from pelican.readers import BaseReader
import json
class JsonReader(BaseReader):
enabled = True
def __init__(self, settings):
super(JsonReader, self).__init__(settings)
file_extensions = ['json']
def read(self, filename):
with open(filename, 'rt', encoding='UTF-8') as f:
json_data = json.loads(f.read())
parsed = {}
for key, value in json_data.items():
parsed[key] = self.process_metadata(key, value)
# All is processed as metadata, content is ''.
return '', parsed
def add_reader(readers):
readers.reader_classes['json'] = JsonReader
def register():
signals.readers_init.connect(add_reader)
# ...
# Adding a plugin
PLUGIN_PATHS = ['pineapple_pelican_plugins']
PLUGINS = [
'json_reader'
]
# ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment