├── .gitignore ├── LICENSE.md ├── README.md ├── __init__.py └── meta_yaml.py /.gitignore: -------------------------------------------------------------------------------- 1 | __pycache__/ 2 | *.py[cod] 3 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright 2007, 2008 The Python Markdown Project (v. 1.7 and later) 2 | Copyright 2004, 2005, 2006 Yuri Takhteyev (v. 0.2-1.6b) 3 | Copyright 2004 Manfred Stienstra (the original version) 4 | 5 | All rights reserved. 6 | 7 | Redistribution and use in source and binary forms, with or without 8 | modification, are permitted provided that the following conditions are met: 9 | 10 | * Redistributions of source code must retain the above copyright 11 | notice, this list of conditions and the following disclaimer. 12 | * Redistributions in binary form must reproduce the above copyright 13 | notice, this list of conditions and the following disclaimer in the 14 | documentation and/or other materials provided with the distribution. 15 | * Neither the name of the nor the 16 | names of its contributors may be used to endorse or promote products 17 | derived from this software without specific prior written permission. 18 | 19 | THIS SOFTWARE IS PROVIDED BY THE PYTHON MARKDOWN PROJECT ''AS IS'' AND ANY 20 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 21 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 22 | DISCLAIMED. IN NO EVENT SHALL ANY CONTRIBUTORS TO THE PYTHON MARKDOWN PROJECT 23 | BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | POSSIBILITY OF SUCH DAMAGE. 30 | 31 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # YAML Meta Data Extension for [Python-Markdown](https://github.com/waylan/Python-Markdown) 2 | 3 | This extension adds YAML meta data handling to markdown. 4 | 5 | As in the original, meta data is parsed but not used in processing. 6 | 7 | (YAML meta data is used e.g. by [pandoc](http://johnmacfarlane.net/pandoc/)) 8 | 9 | Dependencies: [PyYAML](http://pyyaml.org/) 10 | 11 | Copyright 2014 Bernhard Fisseni 12 | 13 | Based on the meta data extension included with Python-Markdown, 14 | Copyright 2007-2008 [Waylan Limberg](http://achinghead.com). 15 | 16 | License: BSD (see LICENSE.md for details) 17 | 18 | 19 | ## Basic Usage 20 | 21 | >>> import markdown 22 | >>> text = '''--- 23 | ... Title: Test Doc. 24 | ... Author: Waylan Limberg 25 | ... Blank_Data: 26 | ... ... 27 | ... 28 | ... The body. This is paragraph one. 29 | ... ''' 30 | >>> md = markdown.Markdown(['meta_yaml']) 31 | >>> print(md.convert(text)) 32 |

The body. This is paragraph one.

33 | >>> print(md.Meta) # doctest: +SKIP 34 | {'blank_data': [''], 'author': ['Waylan Limberg'], 'title': ['Test Doc.']} 35 | 36 | 37 | 38 | ## Use in Sublime Text 3 with [OmniMarkupPreviewer](https://github.com/timonwong/OmniMarkupPreviewer) 39 | 40 | - copy python code e.g. to `Packages/User` 41 | - enable as extension in User Preferences 42 | 43 | "extensions": [ 44 | ... 45 | "User.meta_yaml", // if installed in Packages/User 46 | ... 47 | ] 48 | 49 | -------------------------------------------------------------------------------- /__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teoric/python-markdown-yaml-meta-data/48ba1d2cd6205e0ea737728abc58dc1d538cc4eb/__init__.py -------------------------------------------------------------------------------- /meta_yaml.py: -------------------------------------------------------------------------------- 1 | """ 2 | # YAML Meta Data Extension for [Python-Markdown](https://github.com/waylan/Python-Markdown) 3 | 4 | This extension adds YAML meta data handling to markdown. 5 | 6 | As in the original, meta data is parsed but not used in processing. 7 | 8 | (YAML meta data is used e.g. by [pandoc](http://johnmacfarlane.net/pandoc/)) 9 | 10 | Dependencies: [PyYAML](http://pyyaml.org/) 11 | 12 | 13 | Basic Usage: 14 | 15 | >>> import markdown 16 | >>> text = '''--- 17 | ... Title: Test Doc. 18 | ... Author: Waylan Limberg 19 | ... Blank_Data: 20 | ... ... 21 | ... 22 | ... The body. This is paragraph one. 23 | ... ''' 24 | >>> md = markdown.Markdown(['meta_yaml']) 25 | >>> print(md.convert(text)) 26 |

The body. This is paragraph one.

27 | >>> print(md.Meta) # doctest: +SKIP 28 | {'blank_data': [''], 'author': ['Waylan Limberg'], 'title': ['Test Doc.']} 29 | 30 | Make sure text without Meta Data still works (markdown < 1.6b returns a

). 31 | 32 | >>> text = ' Some Code - not extra lines of meta data.' 33 | >>> md = markdown.Markdown(['meta_yaml']) 34 | >>> print(md.convert(text)) 35 |

Some Code - not extra lines of meta data.
 36 |     
37 | >>> md.Meta 38 | {} 39 | 40 | 41 | Copyright 2014 Bernhard Fisseni 42 | 43 | Based on the meta data extension included with Python-Markdown, 44 | Copyright 2007-2008 [Waylan Limberg](http://achinghead.com). 45 | 46 | License: BSD (see LICENSE.md for details) 47 | 48 | """ 49 | 50 | from __future__ import absolute_import 51 | from __future__ import unicode_literals 52 | from markdown import Extension 53 | from markdown.preprocessors import Preprocessor 54 | import yaml 55 | try: 56 | from yaml import CBaseLoader as Loader 57 | except ImportError: 58 | from yaml import BaseLoader as Loader 59 | 60 | 61 | # Override the default string handling function to always return unicode objects 62 | def construct_yaml_str(self, node): 63 | return self.construct_scalar(node) 64 | Loader.add_constructor(u'tag:yaml.org,2002:str', construct_yaml_str) 65 | 66 | 67 | class MetaYamlExtension (Extension): 68 | """Extension for parsing YAML-Metadata with Python-Markdown.""" 69 | 70 | def extendMarkdown(self, md, md_globals): 71 | """Add MetaYamlPreprocessor to Markdown instance.""" 72 | md.preprocessors.add("meta_yaml", MetaYamlPreprocessor(md), ">meta") 73 | 74 | 75 | class MetaYamlPreprocessor(Preprocessor): 76 | """ 77 | Get Meta-Data. 78 | 79 | A YAML block is delimited by 80 | - a line '---' at the start 81 | - and a '...' or '---' line 82 | at the end. 83 | """ 84 | 85 | def run(self, lines): 86 | """ Parse Meta-Data and store in Markdown.Meta. """ 87 | yaml_block = [] 88 | line = lines.pop(0) 89 | if line == "---": 90 | while lines: 91 | line = lines.pop(0) 92 | if line in ("---", "..."): 93 | break 94 | yaml_block.append(line) 95 | else: 96 | lines.insert(0, line) 97 | if yaml_block: 98 | meta = yaml.load("\n".join(yaml_block), Loader) 99 | 100 | # Compat with PyMarkdown's meta: Keys are lowercase, values are lists 101 | meta = {k.lower(): v if isinstance(v, list) else [v] for k, v in meta.items()} 102 | 103 | self.markdown.Meta = meta 104 | return lines 105 | 106 | 107 | def makeExtension(configs={}): 108 | """set up extension.""" 109 | return MetaYamlExtension(configs=configs) 110 | --------------------------------------------------------------------------------