Play along nicely with Markdown's Meta extension

This commit is contained in:
Kernc 2014-11-25 01:28:49 +01:00
parent b9a79ab484
commit 2aabeb4aae

View File

@ -53,7 +53,10 @@ from markdown import Extension
from markdown.preprocessors import Preprocessor from markdown.preprocessors import Preprocessor
import re import re
import yaml import yaml
# from yaml.scanner import ScannerError try:
from yaml import CSafeLoader as Loader
except ImportError:
from yaml import Loader
class MetaYamlExtension (Extension): class MetaYamlExtension (Extension):
@ -62,7 +65,7 @@ class MetaYamlExtension (Extension):
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", MetaYamlPreprocessor(md), "_begin") md.preprocessors.add("meta_yaml", MetaYamlPreprocessor(md), ">meta")
class MetaYamlPreprocessor(Preprocessor): class MetaYamlPreprocessor(Preprocessor):
@ -78,24 +81,25 @@ 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. """
in_yaml = False
yaml_block = [] yaml_block = []
line = lines.pop(0) line = lines.pop(0)
if re.match(r'-{3}', line): if re.match(r'-{3}', line):
in_yaml = True while lines:
line = lines.pop(0)
if re.match(r'(\.{3}|-{3})', line):
break
yaml_block.append(line)
else: else:
lines.insert(0, line) lines.insert(0, line)
while in_yaml and lines:
line = lines.pop(0)
if re.match(r'(?:\.{3}|-{3})', line):
break
yaml_block.append(line)
if yaml_block: if yaml_block:
yaml_block = "\n".join(yaml_block) meta = yaml.load("\n".join(yaml_block), Loader)
meta = yaml.load(yaml_block)
# case-insensitize meta data keys: # case-insensitize meta data keys:
meta = { meta = {
dkey.lower(): meta[dkey] for dkey in meta k.lower(): meta[k] for k in meta
}
# PyMarkdown's Meta compat: ensure everything's a list
meta = {
k: v if isinstance(v, list) else [v] for k, v in meta.items()
} }
self.markdown.Meta = meta self.markdown.Meta = meta
return lines return lines