1
0
Fork 0
mirror of https://github.com/shouptech/synthale.git synced 2026-02-03 15:39:45 +00:00

Add setext heading

This commit is contained in:
Emma 2018-12-22 19:56:04 -07:00
parent f71a2044d2
commit 843849e81a
4 changed files with 40 additions and 3 deletions

19
src/synthale/markdown.py Normal file
View file

@ -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()))

View file

@ -6,6 +6,8 @@ import sys
import pybeerxml import pybeerxml
from synthale import markdown
class MarkdownRecipe: class MarkdownRecipe:
"""A recipe in markdown form.""" """A recipe in markdown form."""
@ -36,7 +38,7 @@ class MarkdownRecipe:
@property @property
def markdown(self): def markdown(self):
"""Return generated markdown for the recipe.""" """Return generated markdown for the recipe."""
return '' return markdown.setext_heading(self.recipe.name, 1)
def load_file(path): def load_file(path):

14
tests/test_markdown.py Normal file
View file

@ -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

View file

@ -36,5 +36,7 @@ def test_markdown_recipe_filename():
def test_markdown_recipe_markdown(): def test_markdown_recipe_markdown():
"""Validate the generated markdown.""" """Validate the generated markdown."""
recipe = MarkdownRecipe(None) xml_recipe = pybeerxml.Recipe()
assert recipe.markdown == '' xml_recipe.name = 'Foobar'
recipe = MarkdownRecipe(xml_recipe)
assert recipe.markdown == 'Foobar\n======'