From 843849e81ab70f4b1387f3d7e4ed56acf4ac34aa Mon Sep 17 00:00:00 2001 From: Mike Shoup Date: Sat, 22 Dec 2018 19:56:04 -0700 Subject: [PATCH] Add setext heading --- src/synthale/markdown.py | 19 +++++++++++++++++++ src/synthale/recipes.py | 4 +++- tests/test_markdown.py | 14 ++++++++++++++ tests/test_recipes.py | 6 ++++-- 4 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 src/synthale/markdown.py create mode 100644 tests/test_markdown.py diff --git a/src/synthale/markdown.py b/src/synthale/markdown.py new file mode 100644 index 0000000..db4d373 --- /dev/null +++ b/src/synthale/markdown.py @@ -0,0 +1,19 @@ +"""This module contains functions to generate markdown elements.""" + + +def setext_heading(text, level): + """Return an setext heading. + + `text` is the text to include in the heading. Leading and trailing + whitespace is trimmed from `text`. If `level` is the number `1`, a first + level (h1) heading is returned. If `level` is the number `2` (or anything + else), a second level (h2) heading is returned. + + See https://github.github.com/gfm/#setext-heading for more information. + """ + if level == 1: + hchar = '=' + else: + hchar = '-' + + return '{}\n{}'.format(text.strip(), hchar * len(text.strip())) diff --git a/src/synthale/recipes.py b/src/synthale/recipes.py index b2d3731..250b4ed 100644 --- a/src/synthale/recipes.py +++ b/src/synthale/recipes.py @@ -6,6 +6,8 @@ import sys import pybeerxml +from synthale import markdown + class MarkdownRecipe: """A recipe in markdown form.""" @@ -36,7 +38,7 @@ class MarkdownRecipe: @property def markdown(self): """Return generated markdown for the recipe.""" - return '' + return markdown.setext_heading(self.recipe.name, 1) def load_file(path): diff --git a/tests/test_markdown.py b/tests/test_markdown.py new file mode 100644 index 0000000..e7823c0 --- /dev/null +++ b/tests/test_markdown.py @@ -0,0 +1,14 @@ +"""Contains tests for the synthale.markdown module.""" +import pytest + +from synthale import markdown + + +@pytest.mark.parametrize('text,level,expected', [ + ('test', 1, 'test\n===='), + ('foobar', 2, 'foobar\n------'), + (' test ', None, 'test\n----') +]) +def test_setext_heading(text, level, expected): + """Test for valid setext headings from setext_heading function.""" + assert markdown.setext_heading(text, level) == expected diff --git a/tests/test_recipes.py b/tests/test_recipes.py index 44ba5ee..689a1b0 100644 --- a/tests/test_recipes.py +++ b/tests/test_recipes.py @@ -36,5 +36,7 @@ def test_markdown_recipe_filename(): def test_markdown_recipe_markdown(): """Validate the generated markdown.""" - recipe = MarkdownRecipe(None) - assert recipe.markdown == '' + xml_recipe = pybeerxml.Recipe() + xml_recipe.name = 'Foobar' + recipe = MarkdownRecipe(xml_recipe) + assert recipe.markdown == 'Foobar\n======'