From 2cabbba4457e137dea31d1a07dffb4f30bc6abdf Mon Sep 17 00:00:00 2001 From: zcchen Date: Thu, 24 Nov 2016 10:31:58 +0800 Subject: [PATCH 1/3] Fixed a bug which would be caused when 'inlinestyles=None' or 'linenos=None'. --- mistune_contrib/highlight.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mistune_contrib/highlight.py b/mistune_contrib/highlight.py index 377d428..a450a90 100644 --- a/mistune_contrib/highlight.py +++ b/mistune_contrib/highlight.py @@ -38,6 +38,6 @@ def block_code(text, lang, inlinestyles=False, linenos=False): class HighlightMixin(object): def block_code(self, text, lang): # renderer has an options - inlinestyles = self.options.get('inlinestyles') - linenos = self.options.get('linenos') + inlinestyles = True if self.options.get('inlinestyles') else False + linenos = True if self.options.get('linenos') else False return block_code(text, lang, inlinestyles, linenos) From 183478e7809c8674a7628dda3f8911cbe2c4ad86 Mon Sep 17 00:00:00 2001 From: zcchen Date: Mon, 28 Nov 2016 23:22:57 +0800 Subject: [PATCH 2/3] Better with self.options.get('inlinestyles', False) as lepture said. --- mistune_contrib/highlight.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mistune_contrib/highlight.py b/mistune_contrib/highlight.py index a450a90..5d95456 100644 --- a/mistune_contrib/highlight.py +++ b/mistune_contrib/highlight.py @@ -38,6 +38,6 @@ def block_code(text, lang, inlinestyles=False, linenos=False): class HighlightMixin(object): def block_code(self, text, lang): # renderer has an options - inlinestyles = True if self.options.get('inlinestyles') else False - linenos = True if self.options.get('linenos') else False + inlinestyles = self.options.get('inlinestyles', False) + linenos = self.options.get('linenos', False) return block_code(text, lang, inlinestyles, linenos) From bf7a558744c2fd8fc80280c9052807832d80aacf Mon Sep 17 00:00:00 2001 From: zcchen Date: Thu, 19 Jan 2017 13:38:02 +0800 Subject: [PATCH 3/3] Fixed the old bug of meta, and added the a new feature about how to split the meta data elements, as well as the test code. --- mistune_contrib/meta.py | 9 +++++---- tests/test_meta.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 tests/test_meta.py diff --git a/mistune_contrib/meta.py b/mistune_contrib/meta.py index 9791ecd..7463821 100644 --- a/mistune_contrib/meta.py +++ b/mistune_contrib/meta.py @@ -18,22 +18,23 @@ INDENTATION = re.compile(r'\n\s{2,}') -META = re.compile(r'^(\w+):\s*(.*(?:\n\s{2,}.*)*)\n') +META = re.compile(r'^\s*(\w+)\s*:\s*(\S.*(?:\n\s{2,}.*)*)\n') -def parse(text): +def parse(text, split_str=r'[\n]'): """Parse the given text into metadata and strip it for a Markdown parser. :param text: text to be parsed """ rv = {} m = META.match(text) + s_re = re.compile(split_str) while m: key = m.group(1) value = m.group(2) - value = INDENTATION.sub('\n', value.strip()) - rv[key] = value + value = s_re.split(INDENTATION.sub('\n', value.strip())) + rv[key] = [i for i in value if i] text = text[len(m.group(0)):] m = META.match(text) diff --git a/tests/test_meta.py b/tests/test_meta.py new file mode 100644 index 0000000..b45ae87 --- /dev/null +++ b/tests/test_meta.py @@ -0,0 +1,30 @@ +# -*- coding: utf-8 -*- + +import sys +sys.path.append('../') + +import mistune +import mistune_contrib.meta as m_meta + +COMMON_CASE = ''' +Title: A Metadata DEMO +Author: Hsiaoming Yang + Zhechuan Chen, +Tags: a, b, c + +The Text are starting here... +''' + +def test_common(): + a = m_meta.parse(COMMON_CASE, split_str=r'[\n,]') + if a: + meta, text = a + print(">>-----The meta data ---------<<") + print(meta) + print("--------------------------------") + print(">>-----The text data ---------<<") + print(text) + print("--------------------------------") + +if __name__ == "__main__": + test_common()