From a6684d75e3d62e752b16aaf878ca1ce723b838e9 Mon Sep 17 00:00:00 2001 From: Bastian Venthur Date: Mon, 18 Jan 2021 21:52:03 +0100 Subject: [PATCH] added markdown factory for testing --- sg/sg.py | 44 ++++++++++++++++++++++++-------------------- tests/test_sg.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 20 deletions(-) create mode 100644 tests/test_sg.py diff --git a/sg/sg.py b/sg/sg.py index 4406bd2..48ead61 100644 --- a/sg/sg.py +++ b/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)) diff --git a/tests/test_sg.py b/tests/test_sg.py new file mode 100644 index 0000000..c287ec7 --- /dev/null +++ b/tests/test_sg.py @@ -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)