Skip to content

Commit 17a5edc

Browse files
authored
Merge pull request codex-team#9 from sebbacon/handle-script-and-styl
Don't output some unsupported tags or attributes
2 parents ab56793 + c1677e9 commit 17a5edc

File tree

4 files changed

+32
-3
lines changed

4 files changed

+32
-3
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
build/
33
dist/
44
venv/
5+
.idea/
56
*.egg-info/
67
*.egg
78
*.py[cod]

htmlslacker/htmlslacker.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ def __init__(self, html, *args, **kwargs):
2222
super().__init__(*args, **kwargs)
2323
except TypeError:
2424
HTMLParser.__init__(self, *args, **kwargs)
25+
self.skip = False
2526

2627
# slackified string
2728
self.output = ''
@@ -50,7 +51,10 @@ def handle_starttag(self, tag, attrs):
5051
if tag == 'a':
5152
self.output += '<'
5253
for attr in attrs:
53-
self.output += attr[1] + '|'
54+
if attr[0] == 'href':
55+
self.output += attr[1] + '|'
56+
if tag == 'style' or tag == 'script':
57+
self.skip = True
5458

5559
def handle_endtag(self, tag):
5660
"""
@@ -66,14 +70,17 @@ def handle_endtag(self, tag):
6670
self.output += '>'
6771
if tag == 'code':
6872
self.output += '`'
73+
if tag == 'style' or tag == 'script':
74+
self.skip = False
6975

7076
def handle_data(self, data):
7177
"""
7278
concatenate TEXT nodes into output
7379
:param data:
7480
:return:
7581
"""
76-
self.output += data
82+
if not self.skip:
83+
self.output += data
7784

7885
def handle_comment(self, data):
7986
pass

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
setup(
99
name='html-slacker',
1010
packages=find_packages(),
11-
version='0.1.5',
11+
version='0.1.6',
1212
description='Converts HTML to Slack formatted markdown',
1313
long_description=open(join(dirname(__file__), 'README.rst')).read(),
1414
keywords='slack bot html convert markdown slackbot pythonbot slack html markdown',

test_general.py

+21
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,24 @@ def test_example_1():
1414
expected = "*Hello*\n There is _something_ interesting about `this doc` \n And <http://example.com/|here is a link in a paragraph!>"
1515
output = HTMLSlacker(html).get_output()
1616
assert(output == expected)
17+
18+
19+
def test_style_not_rendered():
20+
html = '''<style><!-- /* stuff */ --></style>Dear Prudence'''
21+
expected = "Dear Prudence"
22+
output = HTMLSlacker(html).get_output()
23+
assert(output == expected)
24+
25+
26+
def test_script_not_rendered():
27+
html = '''<script><!-- /* stuff */ --></script>Dear Prudence'''
28+
expected = "Dear Prudence"
29+
output = HTMLSlacker(html).get_output()
30+
assert(output == expected)
31+
32+
33+
def test_link_with_target():
34+
html = 'Please click <a href="http://xxx.com/t.html" target="_blank">here</a>'
35+
expected = "Please click <http://xxx.com/t.html|here>"
36+
output = HTMLSlacker(html).get_output()
37+
assert(output == expected)

0 commit comments

Comments
 (0)