forked from github.com/blag
added markdown factory for testing
This commit is contained in:
44
sg/sg.py
44
sg/sg.py
@@ -90,6 +90,27 @@ def build(args):
|
||||
convert_to_html(convertibles)
|
||||
|
||||
|
||||
def markdown_factory():
|
||||
"""Create a Markdown instance.
|
||||
|
||||
This method exists only to ensure we use the same Markdown instance
|
||||
for tests as for the actual thing.
|
||||
|
||||
Returns
|
||||
-------
|
||||
markdown.Markdown
|
||||
|
||||
"""
|
||||
md = Markdown(
|
||||
extensions=[
|
||||
'meta', 'fenced_code', 'codehilite',
|
||||
MarkdownLinkExtension()
|
||||
],
|
||||
output_format='html5',
|
||||
)
|
||||
return md
|
||||
|
||||
|
||||
def convert_to_html(convertibles):
|
||||
|
||||
env = Environment(
|
||||
@@ -99,13 +120,7 @@ def convert_to_html(convertibles):
|
||||
])
|
||||
)
|
||||
|
||||
md = Markdown(
|
||||
extensions=[
|
||||
'meta', 'fenced_code', 'codehilite',
|
||||
MarkdownLinkExtension()
|
||||
],
|
||||
output_format='html5',
|
||||
)
|
||||
md = markdown_factory()
|
||||
|
||||
pages = []
|
||||
articles = []
|
||||
@@ -114,23 +129,12 @@ def convert_to_html(convertibles):
|
||||
logger.debug(f'Processing {src}')
|
||||
with open(src, 'r') as fh:
|
||||
body = fh.read()
|
||||
md.reset()
|
||||
content = md.convert(body)
|
||||
meta = md.Meta
|
||||
# convert markdown's weird format to str or list[str]
|
||||
for key, value in meta.items():
|
||||
value = '\n'.join(value).split(',')
|
||||
value = [v.strip() for v in value]
|
||||
if len(value) == 1:
|
||||
value = value[0]
|
||||
meta[key] = value
|
||||
|
||||
# convert known metadata
|
||||
if 'date' in meta:
|
||||
meta['date'] = datetime.fromisoformat(meta['date'])
|
||||
content, meta = convert_markdown(md, body)
|
||||
|
||||
context = dict(content=content)
|
||||
context.update(meta)
|
||||
|
||||
# for now, treat all pages as articles
|
||||
if not meta:
|
||||
pages.append((dst, context))
|
||||
|
||||
48
tests/test_sg.py
Normal file
48
tests/test_sg.py
Normal file
@@ -0,0 +1,48 @@
|
||||
from datetime import datetime
|
||||
|
||||
import markdown
|
||||
import pytest
|
||||
|
||||
from sg import sg
|
||||
|
||||
|
||||
@pytest.mark.parametrize("input_, expected", [
|
||||
# inline
|
||||
('[test](test.md)', 'test.html'),
|
||||
('[test](test.md "test")', 'test.html'),
|
||||
('[test](a/test.md)', 'a/test.html'),
|
||||
('[test](a/test.md "test")', 'a/test.html'),
|
||||
('[test](/test.md)', '/test.html'),
|
||||
('[test](/test.md "test")', '/test.html'),
|
||||
('[test](/a/test.md)', '/a/test.html'),
|
||||
('[test](/a/test.md "test")', '/a/test.html'),
|
||||
# reference
|
||||
('[test][]\n[test]: test.md ''', 'test.html'),
|
||||
('[test][]\n[test]: test.md "test"', 'test.html'),
|
||||
('[test][]\n[test]: a/test.md', 'a/test.html'),
|
||||
('[test][]\n[test]: a/test.md "test"', 'a/test.html'),
|
||||
('[test][]\n[test]: /test.md', '/test.html'),
|
||||
('[test][]\n[test]: /test.md "test"', '/test.html'),
|
||||
('[test][]\n[test]: /a/test.md', '/a/test.html'),
|
||||
('[test][]\n[test]: /a/test.md "test"', '/a/test.html'),
|
||||
])
|
||||
def test_convert_markdown_links(input_, expected):
|
||||
md = sg.markdown_factory()
|
||||
html, _ = sg.convert_markdown(md, input_)
|
||||
assert expected in html
|
||||
|
||||
|
||||
@pytest.mark.parametrize("input_, expected", [
|
||||
('foo: bar', {'foo': 'bar'}),
|
||||
('tags: this, is, a, test\n', {'tags': ['this', 'is', 'a', 'test']}),
|
||||
('date: 2020-01-01 12:10', {'date': datetime(2020, 1, 1, 12, 10)}),
|
||||
])
|
||||
def test_convert_metadata(input_, expected):
|
||||
md = sg.markdown_factory()
|
||||
_, meta = sg.convert_markdown(md, input_)
|
||||
assert expected == meta
|
||||
|
||||
|
||||
def test_markdown_factory():
|
||||
md = sg.markdown_factory()
|
||||
assert isinstance(md, markdown.Markdown)
|
||||
Reference in New Issue
Block a user