2014-07-20 09:15:27 +00:00
|
|
|
"""
|
|
|
|
# YAML Meta Data Extension for [Python-Markdown](https://github.com/waylan/Python-Markdown)
|
|
|
|
|
|
|
|
This extension adds YAML meta data handling to markdown.
|
|
|
|
|
|
|
|
As in the original, meta data is parsed but not used in processing.
|
|
|
|
|
|
|
|
(YAML meta data is used e.g. by [pandoc](http://johnmacfarlane.net/pandoc/))
|
|
|
|
|
|
|
|
Dependencies: [PyYAML](http://pyyaml.org/)
|
|
|
|
|
|
|
|
|
|
|
|
Basic Usage:
|
|
|
|
|
|
|
|
>>> import markdown
|
|
|
|
>>> text = '''---
|
|
|
|
... Title: Test Doc.
|
|
|
|
... Author: Waylan Limberg
|
|
|
|
... Blank_Data:
|
|
|
|
... ...
|
|
|
|
...
|
|
|
|
... The body. This is paragraph one.
|
|
|
|
... '''
|
2014-07-20 09:27:20 +00:00
|
|
|
>>> md = markdown.Markdown(['meta_yaml'])
|
2014-07-20 09:15:27 +00:00
|
|
|
>>> print(md.convert(text))
|
|
|
|
<p>The body. This is paragraph one.</p>
|
|
|
|
>>> print(md.Meta) # doctest: +SKIP
|
|
|
|
{'blank_data': [''], 'author': ['Waylan Limberg'], 'title': ['Test Doc.']}
|
|
|
|
|
|
|
|
Make sure text without Meta Data still works (markdown < 1.6b returns a <p>).
|
|
|
|
|
|
|
|
>>> text = ' Some Code - not extra lines of meta data.'
|
2014-07-20 09:27:20 +00:00
|
|
|
>>> md = markdown.Markdown(['meta_yaml'])
|
2014-07-20 09:15:27 +00:00
|
|
|
>>> print(md.convert(text))
|
|
|
|
<pre><code>Some Code - not extra lines of meta data.
|
|
|
|
</code></pre>
|
|
|
|
>>> md.Meta
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
Copyright 2014 Bernhard Fisseni
|
|
|
|
|
|
|
|
Based on the meta data extension included with Python-Markdown,
|
|
|
|
Copyright 2007-2008 [Waylan Limberg](http://achinghead.com).
|
|
|
|
|
|
|
|
License: BSD (see LICENSE.md for details)
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
from __future__ import absolute_import
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
from markdown import Extension
|
|
|
|
from markdown.preprocessors import Preprocessor
|
|
|
|
import yaml
|
2014-11-25 00:28:49 +00:00
|
|
|
try:
|
2015-04-24 23:19:12 +00:00
|
|
|
from yaml import CBaseLoader as Loader
|
2014-11-25 00:28:49 +00:00
|
|
|
except ImportError:
|
2015-04-24 23:19:12 +00:00
|
|
|
from yaml import BaseLoader
|
2014-07-20 09:15:27 +00:00
|
|
|
|
2014-12-26 18:58:45 +00:00
|
|
|
|
2014-12-25 04:37:35 +00:00
|
|
|
# Override the default string handling function to always return unicode objects
|
|
|
|
def construct_yaml_str(self, node):
|
|
|
|
return self.construct_scalar(node)
|
|
|
|
Loader.add_constructor(u'tag:yaml.org,2002:str', construct_yaml_str)
|
2014-07-20 09:15:27 +00:00
|
|
|
|
|
|
|
|
2014-12-26 18:58:45 +00:00
|
|
|
class MetaYamlExtension (Extension):
|
2014-07-20 09:15:27 +00:00
|
|
|
"""Extension for parsing YAML-Metadata with Python-Markdown."""
|
|
|
|
|
|
|
|
def extendMarkdown(self, md, md_globals):
|
|
|
|
"""Add MetaYamlPreprocessor to Markdown instance."""
|
2014-11-25 00:28:49 +00:00
|
|
|
md.preprocessors.add("meta_yaml", MetaYamlPreprocessor(md), ">meta")
|
2014-07-20 09:15:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
class MetaYamlPreprocessor(Preprocessor):
|
|
|
|
"""
|
|
|
|
Get Meta-Data.
|
|
|
|
|
|
|
|
A YAML block is delimited by
|
|
|
|
- a line '---' at the start
|
|
|
|
- and a '...' or '---' line
|
|
|
|
at the end.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def run(self, lines):
|
|
|
|
""" Parse Meta-Data and store in Markdown.Meta. """
|
|
|
|
yaml_block = []
|
|
|
|
line = lines.pop(0)
|
2014-12-26 18:49:51 +00:00
|
|
|
if line == "---":
|
2014-11-25 00:28:49 +00:00
|
|
|
while lines:
|
|
|
|
line = lines.pop(0)
|
2014-12-26 18:49:51 +00:00
|
|
|
if line in ("---", "..."):
|
2014-11-25 00:28:49 +00:00
|
|
|
break
|
|
|
|
yaml_block.append(line)
|
2014-07-20 09:15:27 +00:00
|
|
|
else:
|
|
|
|
lines.insert(0, line)
|
|
|
|
if yaml_block:
|
2014-11-25 00:28:49 +00:00
|
|
|
meta = yaml.load("\n".join(yaml_block), Loader)
|
2014-12-26 18:57:53 +00:00
|
|
|
|
|
|
|
# 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()}
|
|
|
|
|
2014-07-20 09:15:27 +00:00
|
|
|
self.markdown.Meta = meta
|
|
|
|
return lines
|
|
|
|
|
|
|
|
|
|
|
|
def makeExtension(configs={}):
|
|
|
|
"""set up extension."""
|
|
|
|
return MetaYamlExtension(configs=configs)
|