Skip to content

Commit

Permalink
Merge pull request #12 from sivakov512/bump-major-version
Browse files Browse the repository at this point in the history
Bump major version
  • Loading branch information
sivakov512 committed Mar 4, 2019
2 parents c477b04 + e130334 commit 4f32530
Show file tree
Hide file tree
Showing 12 changed files with 162 additions and 96 deletions.
2 changes: 2 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[flake8]
inline-quotes = "
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
*#
*.egg-info
*.py[cod]
*~
*[~#]
.cache
.dir-locals.el
.tox
.vimrc
__pycache__
dist
dist
4 changes: 3 additions & 1 deletion .isort.cfg
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[settings]
line-length=79
multi_line_output=4
multi_line_output=3
combine_as_imports=true
include_trailing_comma=true
12 changes: 6 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
dist: xenial
sudo: false

language: python
cache: pip

python:
- "3.4"
- "3.5"
- "3.6"

before_install:
- sudo apt-get -qq update
- sudo apt-get install -y pandoc
- "3.7"

install:
- pip install coveralls tox-travis -r requirements.txt

script:
- flake8
- black --check ./
- tox

after_success:
Expand All @@ -26,3 +25,4 @@ deploy:
password: $PyPiPassword
on:
tags: true
python: 3.7
1 change: 0 additions & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
include requirements.txt
include README.md
include README.rst
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

[![Build Status](https://travis-ci.org/sivakov512/python-markdown-full-yaml-metadata.svg?branch=master)](https://travis-ci.org/sivakov512/python-markdown-full-yaml-metadata)
[![Coverage Status](https://coveralls.io/repos/github/sivakov512/python-markdown-full-yaml-metadata/badge.svg)](https://coveralls.io/github/sivakov512/python-markdown-full-yaml-metadata)
![Python versions](https://img.shields.io/badge/python-3.4,%203.5,%203.6-blue.svg)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/ambv/black)
![Python versions](https://img.shields.io/badge/python-3.6,%203.7-blue.svg)
[![PyPi](https://img.shields.io/pypi/v/markdown-full-yaml-metadata.svg)](https://pypi.python.org/pypi/markdown-full-yaml-metadata)

This extension adds YAML meta data handling to markdown with all YAML features.
Expand Down Expand Up @@ -35,6 +36,4 @@ md.Meta == {'title': 'What is Lorem Ipsum?', 'categories': ['Lorem Ipsum', 'Stup

## Python versions compatibility

This plugin tested with python versions 3.4, 3.5 and 3.6.

For python 3.4 you must install [typing](https://pypi.python.org/pypi/typing)
This plugin tested with python versions 3.6 and 3.7.
15 changes: 9 additions & 6 deletions full_yaml_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@

class FullYamlMetadataExtension(markdown.Extension):
"""Extension for parsing YAML metadata part with Python-Markdown."""
def extendMarkdown(self, md: markdown.Markdown, md_globals: dict):

def extendMarkdown(self, md: markdown.Markdown, *args, **kwargs):
md.Meta = None
md.preprocessors.add(
'full_yaml_metadata', FullYamlMetadataPreprocessor(md), '_begin')
md.preprocessors.register(
FullYamlMetadataPreprocessor(md), "full_yaml_metadata", 1
)


class FullYamlMetadataPreprocessor(markdown.preprocessors.Preprocessor):
Expand All @@ -18,20 +20,21 @@ class FullYamlMetadataPreprocessor(markdown.preprocessors.Preprocessor):
YAML block is delimited by '---' at start and '...' or '---' at end.
"""

def run(self, lines: list) -> list:
meta_lines, lines = self.split_by_meta_and_content(lines)

self.markdown.Meta = yaml.load('\n'.join(meta_lines))
self.md.Meta = yaml.load("\n".join(meta_lines))
return lines

def split_by_meta_and_content(self, lines: list) -> typing.Tuple[list]:
meta_lines = []
if lines[0] != '---':
if lines[0] != "---":
return meta_lines, lines

lines.pop(0)
for line in lines: # type: str
if line in ('---', '...'):
if line in ("---", "..."):
content_starts_at = lines.index(line) + 1
lines = lines[content_starts_at:]
break
Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tool.black]
line-length = 79
31 changes: 23 additions & 8 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
appdirs==1.4.3
atomicwrites==1.3.0
attrs==19.1.0
black==18.9b0
Click==7.0
entrypoints==0.3
flake8==3.7.7
flake8-debugger==3.1.0
flake8-isort==2.6.0
flake8-print==3.1.0
flake8-quotes==1.0.0
isort==4.3.10
Markdown==3.0.1
PyYAML>=3.13
mccabe==0.6.1
more-itertools==6.0.0
pluggy==0.9.0
py==1.7.0
pycodestyle==2.5.0
pyflakes==2.1.1
Pygments==2.2.0
flake8-commas==0.4.3
flake8-debugger==1.4.0
flake8-isort==2.2.2
flake8-print==2.0.2
flake8==3.4.1
isort==4.2.15
pytest==3.2.3
pytest==4.3.0
PyYAML==3.13
six==1.12.0
testfixtures==6.4.3
toml==0.10.0
34 changes: 20 additions & 14 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
import os

from setuptools import setup

url = 'https://github.com/cryptomaniac512/python-markdown-full-yaml-metadata'

def read(fname):
with open(os.path.join(os.path.dirname(__file__), fname)) as _f:
return _f.read()


setup(
author='Nikita Sivakov',
author_email='cryptomaniac.512@gmail.com',
description='YAML metadata extension for Python-Markdown',
install_requires=['Markdown==3.0.1', 'PyYAML>=3.13'],
keywords='markdown yaml meta metadata',
license='MIT',
long_description_markdown_filename='README.md',
name='markdown-full-yaml-metadata',
py_modules=['full_yaml_metadata'],
python_requires='>=3.4',
setup_requires=['setuptools-markdown'],
url=url,
version='0.0.4',
author="Nikita Sivakov",
author_email="sivakov512@gmail.com",
description="YAML metadata extension for Python-Markdown",
install_requires=["Markdown==3.0.1", "PyYAML>=3.13"],
keywords="markdown yaml meta metadata",
license="MIT",
long_description=read("README.md"),
long_description_content_type="text/markdown",
name="markdown-full-yaml-metadata",
py_modules=["full_yaml_metadata"],
python_requires=">=3.6",
setup_requires=["setuptools-markdown"],
version="1.0.0",
url="https://github.com/sivakov512/python-markdown-full-yaml-metadata",
)
136 changes: 90 additions & 46 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,45 @@
import pytest


@pytest.mark.parametrize('source, expected_meta, expected_body', [
("""---
@pytest.mark.parametrize(
"source, expected_meta, expected_body",
(
[
"""---
title: What is Lorem Ipsum?
category: Lorem Ipsum
...
Lorem Ipsum is simply dummy text.
""",
{'title': 'What is Lorem Ipsum?', 'category': 'Lorem Ipsum'},
'<p>Lorem Ipsum is simply dummy text.</p>'),
("""---
{"title": "What is Lorem Ipsum?", "category": "Lorem Ipsum"},
"<p>Lorem Ipsum is simply dummy text.</p>",
],
[
"""---
TITLE: Where does it come from?
Author: CryptoManiac
Author: Sivakov Nikita
---
Contrary to popular belief, Lorem Ipsum is not simply random text.
Contrary to popular belief, Lorem Ipsum is...
""",
{'TITLE': 'Where does it come from?', 'Author': 'CryptoManiac'},
'<p>Contrary to popular belief, Lorem Ipsum is not simply random text.</p>'), # noqa
])
{"TITLE": "Where does it come from?", "Author": "Sivakov Nikita"},
"<p>Contrary to popular belief, Lorem Ipsum is...</p>",
],
),
)
def test_plain_metadata(source, expected_meta, expected_body):
md = markdown.Markdown(extensions=['full_yaml_metadata'])
md = markdown.Markdown(extensions=["full_yaml_metadata"])

assert md.convert(source) == expected_body
assert md.Meta == expected_meta


@pytest.mark.parametrize('source, expected_meta, expected_body', [
("""---
@pytest.mark.parametrize(
"source, expected_meta, expected_body",
(
[
"""---
title: What is Lorem Ipsum?
categories:
- Lorem Ipsum
Expand All @@ -38,30 +49,42 @@ def test_plain_metadata(source, expected_meta, expected_body):
Lorem Ipsum is simply dummy text.
""",
{'title': 'What is Lorem Ipsum?', 'categories': [
'Lorem Ipsum', 'Stupid posts']},
'<p>Lorem Ipsum is simply dummy text.</p>'),
("""---
{
"title": "What is Lorem Ipsum?",
"categories": ["Lorem Ipsum", "Stupid posts"],
},
"<p>Lorem Ipsum is simply dummy text.</p>",
],
[
"""---
TITLE: Where does it come from?
Authors:
- CryptoManiac
- Sivakov Nikita
- Another Guy
---
Contrary to popular belief, Lorem Ipsum is not simply random text.
Contrary to popular belief, Lorem Ipsum is...
""",
{'TITLE': 'Where does it come from?', 'Authors': [
'CryptoManiac', 'Another Guy']},
'<p>Contrary to popular belief, Lorem Ipsum is not simply random text.</p>'), # noqa
])
{
"TITLE": "Where does it come from?",
"Authors": ["Sivakov Nikita", "Another Guy"],
},
"<p>Contrary to popular belief, Lorem Ipsum is...</p>",
],
),
)
def test_metadata_with_lists(source, expected_meta, expected_body):
md = markdown.Markdown(extensions=['full_yaml_metadata'])
md = markdown.Markdown(extensions=["full_yaml_metadata"])

assert md.convert(source) == expected_body
assert md.Meta == expected_meta


@pytest.mark.parametrize('source, expected_meta, expected_body', [
("""---
@pytest.mark.parametrize(
"source, expected_meta, expected_body",
(
[
"""---
title: What is Lorem Ipsum?
categories:
first: Lorem Ipsum
Expand All @@ -70,40 +93,61 @@ def test_metadata_with_lists(source, expected_meta, expected_body):
Lorem Ipsum is simply dummy text.
""",
{'title': 'What is Lorem Ipsum?', 'categories': {
'first': 'Lorem Ipsum', 'second': 'Stupid posts'}},
'<p>Lorem Ipsum is simply dummy text.</p>'),
("""---
{
"title": "What is Lorem Ipsum?",
"categories": {
"first": "Lorem Ipsum",
"second": "Stupid posts",
},
},
"<p>Lorem Ipsum is simply dummy text.</p>",
],
[
"""---
TITLE: Where does it come from?
Authors:
first: CryptoManiac
second: Another Guy
---
Contrary to popular belief, Lorem Ipsum is not simply random text.
Contrary to popular belief, Lorem Ipsum is...
""",
{'TITLE': 'Where does it come from?', 'Authors': {
'first': 'CryptoManiac', 'second': 'Another Guy'}},
'<p>Contrary to popular belief, Lorem Ipsum is not simply random text.</p>'), # noqa
])
{
"TITLE": "Where does it come from?",
"Authors": {"first": "CryptoManiac", "second": "Another Guy"},
},
"<p>Contrary to popular belief, Lorem Ipsum is...</p>",
],
),
)
def test_metadata_with_dicts(source, expected_meta, expected_body):
md = markdown.Markdown(extensions=['full_yaml_metadata'])
md = markdown.Markdown(extensions=["full_yaml_metadata"])

assert md.convert(source) == expected_body
assert md.Meta == expected_meta


@pytest.mark.parametrize('source, expected_body', [
('Lorem Ipsum is simply dummy text.',
'<p>Lorem Ipsum is simply dummy text.</p>'),
('Contrary to popular belief, Lorem Ipsum is not simply random text.',
'<p>Contrary to popular belief, Lorem Ipsum is not simply random text.</p>'), # noqa
])
@pytest.mark.parametrize(
"source, expected_body",
(
[
"Lorem Ipsum is simply dummy text.",
"<p>Lorem Ipsum is simply dummy text.</p>",
],
[
"Contrary to popular belief, Lorem Ipsum is...",
"<p>Contrary to popular belief, Lorem Ipsum is...</p>",
],
),
)
def test_without_metadata(source, expected_body):
md = markdown.Markdown(extensions=['full_yaml_metadata'])
md = markdown.Markdown(extensions=["full_yaml_metadata"])

assert md.convert(source) == expected_body
assert not md.Meta
assert md.Meta is None


def test_meta_is_acceccable_before_parsing():
md = markdown.Markdown(extensions=['full_yaml_metadata'])
assert not md.Meta
md = markdown.Markdown(extensions=["full_yaml_metadata"])

assert md.Meta is None

0 comments on commit 4f32530

Please sign in to comment.