Skip to content

Commit c87bff6

Browse files
authored
Added Support for Line Breaks in Inline Elements (#168)
* Added linebreak conceptually * Doesn't work when extended to Paragraph * Tried the alternative of embedding the line break * Updated docs and version * Converted to self-closing tag to mirror markdown python library * Updated testing to match self-closing tag * Removed tags
1 parent 0fa30cd commit c87bff6

File tree

5 files changed

+33
-4
lines changed

5 files changed

+33
-4
lines changed

docs/version-history.rst

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,23 @@ as follows:
1313
v2.x
1414
----
1515

16-
* v2.2.1 [:pr:`#164`]
16+
* v2.3.0 [:pr:`168`]
17+
18+
* Added a line break option to Inline text to grant a little more control over formatting
19+
20+
* v2.2.1 [:pr:`164`]
1721

1822
* Fixed a bug where headings with special characters would not link properly in the table of contents
1923
* Dropped support for Python 3.8 due to deprecation
2024
* Added support for Python 3.12 and 3.13
2125

22-
* v2.2.0 [:pr:`#152`, :pr:`153`, :pr:`#159`]
26+
* v2.2.0 [:pr:`152`, :pr:`153`, :pr:`159`]
2327

2428
* Added documentation throughout the repo
2529
* Expanded testing
2630
* Added CSVTable Template and accompanying documentation
2731

28-
* v2.2.0b1 [:pr:`#140`, :pr:`#142`, :pr:`#143`, :pr:`#144`, :pr:`#145`, :pr:`#146`, :pr:`#149`]
32+
* v2.2.0b1 [:pr:`140`, :pr:`142`, :pr:`143`, :pr:`144`, :pr:`145`, :pr:`146`, :pr:`149`]
2933

3034
* Expanded the Element requirements to include :code:`__repr__()` for developer friendly strings
3135
* Reworked logging system to take advantage of lazy loading and new :code:`__repr__()` methods

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
[tool.poetry]
33
name = "SnakeMD"
44
description = "A markdown generation library for Python."
5-
version = "2.2.1"
5+
version = "2.3.0"
66
license = "MIT"
77

88
authors = [

snakemd/elements.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ class Inline(Element):
104104
105105
- defaults to :code:`False`
106106
- set to :code:`True` to render text as code (i.e., ```text```)
107+
:param bool linebreak:
108+
the line break state of the inline text
109+
110+
- defaults to :code:`False`
111+
- set to :code:`True` to add a line break to the end of the element (i.e., `<br>`)
107112
"""
108113

109114
def __init__(
@@ -115,6 +120,7 @@ def __init__(
115120
italics: bool = False,
116121
strikethrough: bool = False,
117122
code: bool = False,
123+
linebreak: bool = False,
118124
) -> None:
119125
self._text = text
120126
self._image = image
@@ -123,6 +129,7 @@ def __init__(
123129
self._italics = italics
124130
self._strikethrough = strikethrough
125131
self._code = code
132+
self._linebreak = linebreak
126133

127134
def __str__(self) -> str:
128135
"""
@@ -152,6 +159,10 @@ def __str__(self) -> str:
152159
text = f"~~{text}~~"
153160
if self._code:
154161
text = f"`{text}`"
162+
if self._linebreak:
163+
# Note: doing this the markdown way (i.e., space-space-newline)
164+
# does not work with the current implementation of Paragraph
165+
text = f"{text}<br />"
155166
logger.debug("Rendered inline text: %r", text)
156167
return text
157168

tests/elements/test_inline.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,12 @@ def test_inline_code():
200200
")"
201201
)
202202
assert markdown.markdown(str(text)) == "<p><code>x = 7</code></p>"
203+
204+
205+
def test_inline_linebreak():
206+
text = Inline("Test", linebreak=True)
207+
assert str(text) == "Test<br />"
208+
assert markdown.markdown(str(text)) == "<p>Test<br /></p>"
203209

204210

205211
# Constructor tests (2-combos)

tests/elements/test_paragraph.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,11 @@ def test_repr_can_create_object():
145145
paragraph = Paragraph("")
146146
obj = eval(repr(paragraph))
147147
assert isinstance(obj, Paragraph)
148+
149+
150+
def test_paragraph_with_linebreak():
151+
paragraph = Paragraph([
152+
Inline("First Line", linebreak=True),
153+
Inline("Second Line")
154+
])
155+
assert str(paragraph) == "First Line<br />Second Line"

0 commit comments

Comments
 (0)