Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

About the Meta parse #11

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions mistune_contrib/highlight.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = self.options.get('inlinestyles', False)
linenos = self.options.get('linenos', False)
return block_code(text, lang, inlinestyles, linenos)
9 changes: 5 additions & 4 deletions mistune_contrib/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
30 changes: 30 additions & 0 deletions tests/test_meta.py
Original file line number Diff line number Diff line change
@@ -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:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test case should use assert so that it can be run by CI.

meta, text = a
print(">>-----The meta data ---------<<")
print(meta)
print("--------------------------------")
print(">>-----The text data ---------<<")
print(text)
print("--------------------------------")

if __name__ == "__main__":
test_common()