Created
September 25, 2014 23:58
-
-
Save hamaluik/594aed8aa5fc09d9bdc5 to your computer and use it in GitHub Desktop.
A Pelican plugin to enable third party Markdown extensions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
Markdown Third Party | |
============================ | |
This plugin allows you to use various third-party | |
Markdown extensions to make writing posts in Markdown | |
easier and better. | |
""" | |
from pelican import signals, readers | |
import os, sys, inspect | |
class MarkdownThirdParty(readers.MarkdownReader): | |
def __init__(self, *args, **kwargs): | |
try: | |
super(MarkdownThirdParty, self).__init__(*args, **kwargs) | |
self.extensions = list(self.settings['MD_EXTENSIONS']) | |
# always make sure we have the 'meta' extension | |
if 'meta' not in self.extensions: | |
self.extensions.append('meta') | |
# add our current folder to the path so Markdown can find the extension | |
cmdFolder = os.path.realpath(os.path.abspath(os.path.split(inspect.getfile(inspect.currentframe()))[0])) | |
if cmdFolder not in sys.path: | |
sys.path.insert(0, cmdFolder) | |
except Exception as inst: | |
print("MarkdownThirdParty initialization error: %s" % inst) | |
def addReader(readers): | |
# re-route all markdown extensions to be our reader | |
try: | |
extensions = ['md', 'markdown', 'mkd', 'mdown'] | |
for ext in extensions: | |
readers.reader_classes[ext] = MarkdownThirdParty | |
except Exception as inst: | |
print("MarkdownThirdParty reader error: %s" % inst) | |
def register(): | |
signals.readers_init.connect(addReader) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment