Compatibility with Markdown's meta extension
This commit is contained in:
parent
8073847dc2
commit
c81e7a999f
30
meta_yaml.py
30
meta_yaml.py
@ -47,29 +47,34 @@ License: BSD (see LICENSE.md for details)
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
from __future__ import absolute_import
|
from __future__ import absolute_import
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
from markdown import Extension
|
from markdown import Extension
|
||||||
from markdown.preprocessors import Preprocessor
|
from markdown.preprocessors import Preprocessor
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from yaml import CBaseLoader as Loader
|
from yaml import CBaseLoader as Loader
|
||||||
except ImportError:
|
except ImportError:
|
||||||
from yaml import BaseLoader
|
from yaml import BaseLoader as Loader
|
||||||
|
|
||||||
|
|
||||||
# Override the default string handling function to always return unicode objects
|
# Override the default string handling function to always return unicode objects
|
||||||
def construct_yaml_str(self, node):
|
def construct_yaml_str(self, node):
|
||||||
return self.construct_scalar(node)
|
return self.construct_scalar(node)
|
||||||
|
|
||||||
Loader.add_constructor(u'tag:yaml.org,2002:str', construct_yaml_str)
|
Loader.add_constructor(u'tag:yaml.org,2002:str', construct_yaml_str)
|
||||||
|
|
||||||
|
|
||||||
class MetaYamlExtension (Extension):
|
class MetaYamlExtension(Extension):
|
||||||
"""Extension for parsing YAML-Metadata with Python-Markdown."""
|
"""Extension for parsing YAML-Metadata with Python-Markdown."""
|
||||||
|
|
||||||
def extendMarkdown(self, md, md_globals):
|
def extendMarkdown(self, md, md_globals):
|
||||||
"""Add MetaYamlPreprocessor to Markdown instance."""
|
"""Add MetaYamlPreprocessor to Markdown instance."""
|
||||||
md.preprocessors.add("meta_yaml", MetaYamlPreprocessor(md), ">meta")
|
|
||||||
|
md.preprocessors.add("meta_yaml", MetaYamlPreprocessor(md), "<meta")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class MetaYamlPreprocessor(Preprocessor):
|
class MetaYamlPreprocessor(Preprocessor):
|
||||||
@ -84,26 +89,45 @@ class MetaYamlPreprocessor(Preprocessor):
|
|||||||
|
|
||||||
def run(self, lines):
|
def run(self, lines):
|
||||||
""" Parse Meta-Data and store in Markdown.Meta. """
|
""" Parse Meta-Data and store in Markdown.Meta. """
|
||||||
|
|
||||||
yaml_block = []
|
yaml_block = []
|
||||||
line = lines.pop(0)
|
line = lines.pop(0)
|
||||||
|
|
||||||
if line == "---":
|
if line == "---":
|
||||||
while lines:
|
while lines:
|
||||||
line = lines.pop(0)
|
line = lines.pop(0)
|
||||||
if line in ("---", "..."):
|
if line in ("---", "..."):
|
||||||
break
|
break
|
||||||
yaml_block.append(line)
|
yaml_block.append(line)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
lines.insert(0, line)
|
lines.insert(0, line)
|
||||||
|
|
||||||
if yaml_block:
|
if yaml_block:
|
||||||
meta = yaml.load("\n".join(yaml_block), Loader)
|
meta = yaml.load("\n".join(yaml_block), Loader)
|
||||||
|
|
||||||
# Compat with PyMarkdown's meta: Keys are lowercase, values are lists
|
# Compat with PyMarkdown's meta: Keys are lowercase, values are lists
|
||||||
meta = {k.lower(): v if isinstance(v, list) else [v] for k, v in meta.items()}
|
meta = {k.lower(): v if isinstance(v, list) else [v] for k, v in meta.items()}
|
||||||
|
|
||||||
|
# # this is what the code should look like, if the Markdown "meta"
|
||||||
|
# # extension would tolerate other extensions writing to markdown.Meta
|
||||||
|
# if hasattr(self.markdown, 'Meta'):
|
||||||
|
# self.markdown.update(meta)
|
||||||
|
# else:
|
||||||
|
# self.markdown.Meta = meta
|
||||||
|
|
||||||
|
# hacky quick fix for the moment (see above)
|
||||||
|
if hasattr(self.markdown, 'Meta'):
|
||||||
|
self.markdown._Meta_data = self.markdown.Meta
|
||||||
|
else:
|
||||||
|
self.markdown._Meta_data = {}
|
||||||
|
self.markdown.__class__.Meta = property(lambda self: self._Meta_data, lambda self, value: self._Meta_data.update(value))
|
||||||
self.markdown.Meta = meta
|
self.markdown.Meta = meta
|
||||||
|
|
||||||
return lines
|
return lines
|
||||||
|
|
||||||
|
|
||||||
def makeExtension(configs={}):
|
def makeExtension(configs={}):
|
||||||
"""set up extension."""
|
"""set up extension."""
|
||||||
|
|
||||||
return MetaYamlExtension(configs=configs)
|
return MetaYamlExtension(configs=configs)
|
||||||
|
Loading…
Reference in New Issue
Block a user