From 667260e3242650e74db7847d3152fab8a7461f2e Mon Sep 17 00:00:00 2001 From: pR0Ps Date: Wed, 24 Dec 2014 23:37:35 -0500 Subject: [PATCH 1/5] Force unicode for all strings --- meta_yaml.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/meta_yaml.py b/meta_yaml.py index 2a6e3f9..27909ad 100644 --- a/meta_yaml.py +++ b/meta_yaml.py @@ -58,6 +58,10 @@ try: except ImportError: from yaml import Loader +# 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) class MetaYamlExtension (Extension): From 201a20e1c3cf6f89cb7d38fbb2a7da3f81923091 Mon Sep 17 00:00:00 2001 From: pR0Ps Date: Wed, 24 Dec 2014 23:56:13 -0500 Subject: [PATCH 2/5] Add gitignore --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..43ae0e2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +__pycache__/ +*.py[cod] From e7db190fca71762df459f353c6d6e8f9ed667aac Mon Sep 17 00:00:00 2001 From: pR0Ps Date: Fri, 26 Dec 2014 13:49:51 -0500 Subject: [PATCH 3/5] No need to use regexp to compare equality --- meta_yaml.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/meta_yaml.py b/meta_yaml.py index 27909ad..d1dfde7 100644 --- a/meta_yaml.py +++ b/meta_yaml.py @@ -51,7 +51,6 @@ from __future__ import absolute_import from __future__ import unicode_literals from markdown import Extension from markdown.preprocessors import Preprocessor -import re import yaml try: from yaml import CSafeLoader as Loader @@ -87,10 +86,10 @@ class MetaYamlPreprocessor(Preprocessor): """ Parse Meta-Data and store in Markdown.Meta. """ yaml_block = [] line = lines.pop(0) - if re.match(r'-{3}', line): + if line == "---": while lines: line = lines.pop(0) - if re.match(r'(\.{3}|-{3})', line): + if line in ("---", "..."): break yaml_block.append(line) else: From 7a7e135f2bb97f7cd15019a2ce401a50d9cdc78d Mon Sep 17 00:00:00 2001 From: pR0Ps Date: Fri, 26 Dec 2014 13:57:53 -0500 Subject: [PATCH 4/5] Slight optimization --- meta_yaml.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/meta_yaml.py b/meta_yaml.py index d1dfde7..2fb536f 100644 --- a/meta_yaml.py +++ b/meta_yaml.py @@ -96,14 +96,10 @@ class MetaYamlPreprocessor(Preprocessor): lines.insert(0, line) if yaml_block: meta = yaml.load("\n".join(yaml_block), Loader) - # case-insensitize meta data keys: - 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() - } + + # 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()} + self.markdown.Meta = meta return lines From 27433419bdc7311f2d34c7316207603a02f03f52 Mon Sep 17 00:00:00 2001 From: pR0Ps Date: Fri, 26 Dec 2014 13:58:45 -0500 Subject: [PATCH 5/5] Formatting cleanup --- meta_yaml.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/meta_yaml.py b/meta_yaml.py index 2fb536f..b75fe7b 100644 --- a/meta_yaml.py +++ b/meta_yaml.py @@ -57,13 +57,14 @@ try: except ImportError: from yaml import Loader + # 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) -class MetaYamlExtension (Extension): +class MetaYamlExtension (Extension): """Extension for parsing YAML-Metadata with Python-Markdown.""" def extendMarkdown(self, md, md_globals): @@ -72,7 +73,6 @@ class MetaYamlExtension (Extension): class MetaYamlPreprocessor(Preprocessor): - """ Get Meta-Data.