Skip to content

Commit a204f2e

Browse files
committed
initial commit via new name
0 parents  commit a204f2e

File tree

7 files changed

+190
-0
lines changed

7 files changed

+190
-0
lines changed

.gitignore

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# general things to ignore
2+
build/
3+
dist/
4+
*.egg-info/
5+
*.egg
6+
*.py[cod]
7+
__pycache__/
8+
*.so
9+
*~

LICENSE.txt

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 CodeX
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.rst

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
html-slacker
2+
========
3+
4+
Converts HTML to Slack formatted markdown
5+
6+
Usage
7+
-----
8+
9+
Install HTMLSlacker from pip.
10+
11+
.. code:: bash
12+
13+
$ pip install html-slacker
14+
15+
Import library.
16+
17+
.. code:: python
18+
19+
>>> from htmlslacker import HTMLSlacker
20+
21+
Use it.
22+
23+
.. code:: python
24+
25+
>>> HTMLSlacker('<b>Hello</b>, <i>Slack</i>!').get_output()
26+
'*Hello*,_Slack_!'
27+
28+
Requirements
29+
------------
30+
31+
- Python >= 2.5
32+
- html
33+
34+
Links
35+
-----
36+
37+
Repository: https://github.com/codex-bot/html-slacker.git
38+
39+
Report a bug: https://github.com/codex-bot/html-slacker/issues
40+
41+
PyPI Package: https://pypi.python.org/pypi/slackify
42+
43+
CodeX Team: https://ifmo.su

htmlslacker/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .htmlslacker import HTMLSlacker

htmlslacker/htmlslacker.py

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
from html.parser import HTMLParser
2+
from html.entities import name2codepoint
3+
4+
5+
class HTMLSlacker(HTMLParser):
6+
7+
"""
8+
>>> from htmlslacker import HTMLSlacker
9+
>>> HTMLSlacker('<b>Hello</b>, <i>Slack</i>!').get_output()
10+
'*Hello*,_Slack_!'
11+
"""
12+
def __init__(self, html, *args, **kwargs):
13+
14+
# call parent constructor __init__
15+
super().__init__(*args, **kwargs)
16+
17+
# slackified string
18+
self.output = ''
19+
20+
# send to HTMLParser feed function to parse HTML string
21+
self.feed(html)
22+
23+
def handle_starttag(self, tag, attrs):
24+
"""
25+
Create slack markdown
26+
27+
https://api.slack.com/docs/message-formatting
28+
29+
:param tag: income tag, that will be switched into slack supported markdown
30+
:param attrs: we need to recover attributes of anchor
31+
:return:
32+
"""
33+
34+
if tag == 'b' or tag == 'strong':
35+
self.output += '*'
36+
if tag == 'i' or tag == 'em':
37+
self.output += '_'
38+
if tag == 'code':
39+
self.output += '`'
40+
if tag == 'a':
41+
self.output += '<'
42+
for attr in attrs:
43+
self.output += attr[1] + '|'
44+
45+
def handle_endtag(self, tag):
46+
"""
47+
https://api.slack.com/docs/message-formatting
48+
:param tag: endtag. Close tag via markdown
49+
:return:
50+
"""
51+
if tag == 'b' or tag == 'strong':
52+
self.output += '*'
53+
if tag == 'i' or tag == 'em':
54+
self.output += '_'
55+
if tag == 'a':
56+
self.output += '>'
57+
if tag == 'code':
58+
self.output += '`'
59+
60+
def handle_data(self, data):
61+
"""
62+
concatenate TEXT nodes into output
63+
:param data:
64+
:return:
65+
"""
66+
self.output += data
67+
68+
def handle_comment(self, data):
69+
pass
70+
71+
def handle_entityref(self, name):
72+
c = chr(name2codepoint[name])
73+
pass
74+
75+
def handle_charref(self, name):
76+
if name.startswith('x'):
77+
c = chr(int(name[1:], 16))
78+
else:
79+
c = chr(int(name))
80+
81+
def handle_decl(self, data):
82+
pass
83+
84+
def get_output(self):
85+
"""
86+
substitute multiple whitespace with single whitespace
87+
88+
link: https://stackoverflow.com/questions/2077897/substitute-multiple-whitespace-with-single-whitespace-in-python
89+
:return:
90+
"""
91+
return ' '.join(self.output.split())

setup.cfg

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[metadata]
2+
description-file = README.rst

setup.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from setuptools import setup, find_packages
2+
from os.path import join, dirname
3+
4+
setup(
5+
name='html-slacker',
6+
packages=find_packages(),
7+
version='0.1.0',
8+
description='Converts HTML to Slack formatted markdown',
9+
long_description=open(join(dirname(__file__), 'README.rst')).read(),
10+
keywords='slack bot html convert markdown slackbot pythonbot slack html markdown',
11+
url='https://github.com/codex-bot/html-slacker',
12+
author='CodeX Team',
13+
author_email='[email protected]',
14+
license='MIT',
15+
classifiers=[
16+
'Intended Audience :: Developers',
17+
'Programming Language :: Python',
18+
'License :: OSI Approved :: MIT License',
19+
'Topic :: Software Development :: Libraries',
20+
'Environment :: Console',
21+
],
22+
python_requires='>=2.5'
23+
)

0 commit comments

Comments
 (0)