Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix new line inside emphasis #368

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 31 additions & 6 deletions html2text/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
self.tag_callback = None
self.open_quote = config.OPEN_QUOTE # covered in cli
self.close_quote = config.CLOSE_QUOTE # covered in cli

if out is None:
self.out = self.outtextf
else:
Expand Down Expand Up @@ -120,6 +120,8 @@
self.tag_stack = (
[]
) # type: List[Tuple[str, Dict[str, Optional[str]], Dict[str, str]]]
self.emphasis_tag_stack = {}
self.remove_space = False
self.emphasis = 0
self.drop_white_space = 0
self.inheader = False
Expand Down Expand Up @@ -303,10 +305,19 @@
) -> None:
self.current_tag = tag

if tag in ["b","em","i","u"]:
if start:
if tag in self.emphasis_tag_stack:
self.emphasis_tag_stack[tag] += 1

Check warning on line 311 in html2text/__init__.py

View check run for this annotation

Codecov / codecov/patch

html2text/__init__.py#L311

Added line #L311 was not covered by tests
else:
self.emphasis_tag_stack[tag] = 1
elif list(self.emphasis_tag_stack.keys()):
self.emphasis_tag_stack.popitem()

if self.tag_callback is not None:
if self.tag_callback(self, tag, attrs, start) is True:
return

# first thing inside the anchor tag is another tag
# that produces some output
if (
Expand Down Expand Up @@ -373,10 +384,24 @@
self.p()

if tag == "br" and start:
for key in list(self.emphasis_tag_stack.keys())[::-1]:
if(key == "b"):
self.o(self.strong_mark)
elif key in ["em","i","u"]:
self.o(self.emphasis_mark)

Check warning on line 391 in html2text/__init__.py

View check run for this annotation

Codecov / codecov/patch

html2text/__init__.py#L390-L391

Added lines #L390 - L391 were not covered by tests

if self.blockquote > 0:
self.o(" \n> ")
else:
self.o(" \n")

for key in list(self.emphasis_tag_stack.keys()):
if(key == "b"):
self.o(self.strong_mark)
elif key in ["em","i","u"]:
self.o(self.emphasis_mark)

Check warning on line 402 in html2text/__init__.py

View check run for this annotation

Codecov / codecov/patch

html2text/__init__.py#L401-L402

Added lines #L401 - L402 were not covered by tests
self.remove_space = True
self.drop_white_space = 1

if tag == "hr" and start:
self.p()
Expand Down Expand Up @@ -641,11 +666,11 @@
# https://spec.commonmark.org/0.28/#motivation
# TODO: line up <ol><li>s > 9 correctly.
parent_list = None
for list in self.list:
for item in self.list:
self.o(
" " if parent_list == "ol" and list.name == "ul" else " "
" " if parent_list == "ol" and item.name == "ul" else " "
)
parent_list = list.name
parent_list = item.name

if li.name == "ul":
self.o(self.ul_item_mark + " ")
Expand Down Expand Up @@ -744,7 +769,7 @@
self.abbr_data += data

if not self.quiet:
if self.google_doc:
if self.google_doc or self.remove_space:
# prevent white space immediately after 'begin emphasis'
# marks ('**' and '_')
lstripped_data = data.lstrip()
Expand Down
1 change: 1 addition & 0 deletions test/new_line_in_emphasis.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<b>Our multiline<br />bold text</b>
3 changes: 3 additions & 0 deletions test/new_line_in_emphasis.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
**Our multiline**
**bold text**

8 changes: 8 additions & 0 deletions test/test_new_line_inside_emphasis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import html2text

def test_emphasis_with_new_line():
h = html2text.HTML2Text()
html = "<b>Our multiline<br />bold text</b>"
result = h.handle(html)
assert result == '**Our multiline** \n**bold text**\n\n'

Loading