Skip to content

Instantly share code, notes, and snippets.

@fbs
Last active December 17, 2015 11:09
Show Gist options
  • Save fbs/5600400 to your computer and use it in GitHub Desktop.
Save fbs/5600400 to your computer and use it in GitHub Desktop.
from .yaml_reader import *
Howto:
- Clone the original pelican-plugins repo if you dont already have it
https://github.com/getpelican/pelican-plugins
- Create a new yaml directory inside pelican-plugins and add the 2 .py files.
- Update your pelicanconfig.py. Don't forget the MARKUP line!
PLUGIN_PATH = 'path/to/pelican-plugins'
PLUGINS = ['yaml-reader']
MARKUP = ('rst', 'md', 'yml')
# -*- coding: utf-8 -*-
"""
Yaml plugin for pelican
=======================
Based on
http://www.ian-barton.com/posts/2013/Apr/06/blogging-with-emacs-org-mode-and-pelican/
https://github.com/getpelican/pelican/issues/607
"""
from __future__ import unicode_literals
import os
import logging
from pelican import signals
from pelican.readers import EXTENSIONS, Reader
try:
import yaml
Yaml = True # NOQA
except ImportError:
Yaml = False # NOQA
class YamlReader(Reader):
enabled = bool(Yaml)
file_extensions = ['yml']
def read(self, filename):
"""Parse content and metadata of YAML files"""
raw = open(filename).read()
docs = []
metadata = {}
raw_doc = raw.split('---')
docs.append(yaml.load(raw_doc[1]))
md = docs[0]
# yaml returns date as a datetime.datetime object.
# We need to turn this back into a string.
md['date'] = md['date'].strftime('%Y-%m-%d')
for key, value in md.items():
name = key.lower()
# Process article tags.
if name == "tags":
tags = ''
for item in value.split(','):
tags = tags + ", " + unicode(item)
metadata[name] = self.process_metadata(name, tags)
else:
metadata[name] = self.process_metadata(name, unicode(value))
udata = raw_doc[2].decode("utf-8")
# asciidata = udata.encode("ascii", "ignore")
return udata, metadata
def get_generators(generators):
# define a new generator here if you need to
return generators
def add_reader(arg):
EXTENSIONS['yml'] = YamlReader
def register():
signals.get_generators.connect(get_generators)
signals.initialized.connect(add_reader)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment