commit 1ee78f5f627029811229757675f76b30a4b08506 Author: Bernhard Fisseni Date: Sun Jul 20 11:15:27 2014 +0200 Initial checkin diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..4cd8b14 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,30 @@ +Copyright 2007, 2008 The Python Markdown Project (v. 1.7 and later) +Copyright 2004, 2005, 2006 Yuri Takhteyev (v. 0.2-1.6b) +Copyright 2004 Manfred Stienstra (the original version) + +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +* Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +* Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. +* Neither the name of the nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE PYTHON MARKDOWN PROJECT ''AS IS'' AND ANY +EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL ANY CONTRIBUTORS TO THE PYTHON MARKDOWN PROJECT +BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + diff --git a/README.md b/README.md new file mode 100644 index 0000000..5b88905 --- /dev/null +++ b/README.md @@ -0,0 +1,47 @@ +# 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. + ... ''' + >>> md = markdown.Markdown(['meta']) + >>> print(md.convert(text)) +

The body. This is paragraph one.

+ >>> print(md.Meta) # doctest: +SKIP + {'blank_data': [''], 'author': ['Waylan Limberg'], 'title': ['Test Doc.']} + +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) + + + +## Use in Sublime Text 3 with [OmniMarkupPreviewer](https://github.com/timonwong/OmniMarkupPreviewer) + +- copy python code e.g. to `Packages/User` +- enable as extension in User Preferences + + "extensions": [ + ... + "User.meta_yaml", // if installed in Packages/User + ... + ] + diff --git a/meta_yaml.py b/meta_yaml.py new file mode 100644 index 0000000..0a31620 --- /dev/null +++ b/meta_yaml.py @@ -0,0 +1,106 @@ +""" +# 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. + ... ''' + >>> md = markdown.Markdown(['meta']) + >>> print(md.convert(text)) +

The body. This is paragraph one.

+ >>> 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

). + + >>> text = ' Some Code - not extra lines of meta data.' + >>> md = markdown.Markdown(['meta']) + >>> print(md.convert(text)) +

Some Code - not extra lines of meta data.
+    
+ >>> 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 re +import yaml +# from yaml.scanner import ScannerError + + +class MetaYamlExtension (Extension): + + """Extension for parsing YAML-Metadata with Python-Markdown.""" + + def extendMarkdown(self, md, md_globals): + """Add MetaYamlPreprocessor to Markdown instance.""" + md.preprocessors.add("meta", MetaYamlPreprocessor(md), "_begin") + + +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. """ + in_yaml = False + yaml_block = [] + line = lines.pop(0) + if re.match(r'-{3}', line): + in_yaml = True + else: + 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: + yaml_block = "\n".join(yaml_block) + meta = yaml.load(yaml_block) + # case-insensitize meta data keys: + meta = { + dkey.lower(): meta[dkey] for dkey in meta + } + self.markdown.Meta = meta + return lines + + +def makeExtension(configs={}): + """set up extension.""" + return MetaYamlExtension(configs=configs)