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

Support unordered list #12

Open
wants to merge 3 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
9 changes: 9 additions & 0 deletions htmlslacker/htmlslacker.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def __init__(self, html, *args, **kwargs):
except TypeError:
HTMLParser.__init__(self, *args, **kwargs)
self.skip = False
self.isProcessingList = False

# slackified string
self.output = ''
Expand Down Expand Up @@ -55,6 +56,10 @@ def handle_starttag(self, tag, attrs):
self.output += attr[1] + '|'
if tag == 'style' or tag == 'script':
self.skip = True
if tag == 'ul':
self.isProcessingList = True
if tag == 'li' and self.isProcessingList:
self.output += '• '

def handle_endtag(self, tag):
"""
Expand All @@ -72,6 +77,10 @@ def handle_endtag(self, tag):
self.output += '`'
if tag == 'style' or tag == 'script':
self.skip = False
if tag == 'ul':
self.isProcessingList = False
if tag == 'li' and self.isProcessingList:
self.output += LINEBR

def handle_data(self, data):
"""
Expand Down
12 changes: 12 additions & 0 deletions test_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,15 @@ def test_link_with_target():
expected = "Please click <http://xxx.com/t.html|here>"
output = HTMLSlacker(html).get_output()
assert(output == expected)

def test_unordered_list():
html = 'Here is my cool list <ul><li>The Shining</li><li>Memento</li><li>Blade Runner</li></ul>'
expected = 'Here is my cool list • The Shining\n• Memento\n• Blade Runner\n'
output = HTMLSlacker(html).get_output()
assert(output == expected)

def test_unordered_list_with_text_modifications():
html = 'Here is my cool list <ul><li>The Shining</li><li>Memento</li><li>Blade <b>Runner</b></li></ul>'
expected = 'Here is my cool list • The Shining\n• Memento\n• Blade *Runner*\n'
output = HTMLSlacker(html).get_output()
assert(output == expected)