Skip to content
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 @@ def __init__(
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 @@ def __init__(
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 @@ def handle_tag(
) -> 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
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 @@ def handle_tag(
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)

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)
self.remove_space = True
self.drop_white_space = 1

if tag == "hr" and start:
self.p()
Expand Down Expand Up @@ -641,11 +666,11 @@ def link_url(self: HTML2Text, link: str, title: str = "") -> None:
# 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 @@ def o(
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'