From c16c5688d508afddb9875ef2d57642210a3b549b Mon Sep 17 00:00:00 2001 From: Jose Alberto Rodriguez Salinas Date: Wed, 17 Jun 2020 09:19:47 -0700 Subject: [PATCH 1/3] [feature] Support unordered lists --- htmlslacker/htmlslacker.py | 9 +++++++++ test_general.py | 12 ++++++++++++ 2 files changed, 21 insertions(+) diff --git a/htmlslacker/htmlslacker.py b/htmlslacker/htmlslacker.py index a81c5de..ab78772 100644 --- a/htmlslacker/htmlslacker.py +++ b/htmlslacker/htmlslacker.py @@ -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 = '' @@ -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): """ @@ -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 +='\n' def handle_data(self, data): """ diff --git a/test_general.py b/test_general.py index 3bd2a2f..61d6f8b 100644 --- a/test_general.py +++ b/test_general.py @@ -35,3 +35,15 @@ def test_link_with_target(): expected = "Please click " output = HTMLSlacker(html).get_output() assert(output == expected) + +def test_unordered_list(): + html = 'Here is my cool list ' + expected = 'Here is my cool list • The Shining\n• Memento\n• Blade Runner' + output = HTMLSlacker(html).get_output() + assert(output == expected) + +def test_unordered_list_with_text_modifications(): + html = 'Here is my cool list ' + expected = 'Here is my cool list • The Shining\n• Me_me_nto\n• Blade *Runner*' + output = HTMLSlacker(html).get_output() + assert(output == expected) From 37580e9cb22cfdf28705cd87545e3baedc972ff6 Mon Sep 17 00:00:00 2001 From: Jose Alberto Rodriguez Salinas Date: Wed, 17 Jun 2020 09:28:33 -0700 Subject: [PATCH 2/3] fix unit tests --- htmlslacker/htmlslacker.py | 2 +- test_general.py | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/htmlslacker/htmlslacker.py b/htmlslacker/htmlslacker.py index ab78772..cbe63e6 100644 --- a/htmlslacker/htmlslacker.py +++ b/htmlslacker/htmlslacker.py @@ -80,7 +80,7 @@ def handle_endtag(self, tag): if tag == 'ul': self.isProcessingList = False if tag == 'li' and self.isProcessingList: - self.output +='\n' + self.output += LINEBR def handle_data(self, data): """ diff --git a/test_general.py b/test_general.py index 61d6f8b..a430d6c 100644 --- a/test_general.py +++ b/test_general.py @@ -37,13 +37,15 @@ def test_link_with_target(): assert(output == expected) def test_unordered_list(): - html = 'Here is my cool list
  • The Shining>
  • Memento
  • Blade Runner
' - expected = 'Here is my cool list • The Shining\n• Memento\n• Blade Runner' + html = 'Here is my cool list
  • The Shining
  • Memento
  • Blade Runner
' + 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
  • The Shining>
  • Memento
  • Blade Runner
' - expected = 'Here is my cool list • The Shining\n• Me_me_nto\n• Blade *Runner*' + html = 'Here is my cool list
  • The Shining
  • Memento
  • Blade Runner
' + expected = 'Here is my cool list • The Shining\n• Memento\n• Blade *Runner*\n' + print(expected) output = HTMLSlacker(html).get_output() + print(output) assert(output == expected) From df21d447393709b30300cd2c420f2e2dae55e54b Mon Sep 17 00:00:00 2001 From: Jose Alberto Rodriguez Salinas Date: Wed, 17 Jun 2020 09:29:24 -0700 Subject: [PATCH 3/3] remove debug print --- test_general.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/test_general.py b/test_general.py index a430d6c..6cccf01 100644 --- a/test_general.py +++ b/test_general.py @@ -45,7 +45,5 @@ def test_unordered_list(): def test_unordered_list_with_text_modifications(): html = 'Here is my cool list
  • The Shining
  • Memento
  • Blade Runner
' expected = 'Here is my cool list • The Shining\n• Memento\n• Blade *Runner*\n' - print(expected) output = HTMLSlacker(html).get_output() - print(output) assert(output == expected)