def _sanitize_post(inp, bbcode_uid: str): smls = re.compile(r'<!-- s.*? --><img src=\\?"\{SMILIES_PATH\}/.*?\\?" ' 'alt=\\?"(.*?)\\?" title=\\?".*?" /><!-- s.*? -->') inp = re.sub(smls, r"\1", inp) inp = html.unescape(inp) inp = inp.replace(":" + bbcode_uid, "") img_tags = re.compile(r"\[/?img\]") inp = re.sub(img_tags, "", inp) youtube_embeds = re.compile( r'\[html\]<iframe width="\d+" height="\d+" ' 'src="//www.youtube.com/embed/([^"]+)" frameborder=' r'"0" allowfullscreen></iframe>\[/html\]') inp = re.sub(youtube_embeds, r"https://www.youtube.com/watch?v=\1", inp) inp = bbcode.render_html(inp, drop_unrecognized=True, escape_html=False, replace_links=False) inp = inp.replace('rel="nofollow"', "") inp = HTMLSlacker(inp).get_output() return inp
def convert_to_slack_markdown(gh_text): html = markdown.markdown(gh_text) # later convert back to \n html = html.replace("\n", "<br>") # slack treat header as bold html = re.sub(r"<h[1-6]{1}>", "<br><strong>", html) html = re.sub(r"</h[1-6]{1}>", "</strong>", html) # task list html = html.replace("[ ] ", "☐ ") html = html.replace("[x] ", "☑︎ ") # convert to slack markdown slack_markdown = HTMLSlacker(html).get_output() return slack_markdown
def test_example_1(): html = """ <b>Hello</b><br> There is <i>something</i> interesting about <code>this doc</code> <p> And <a href="http://example.com/">here is a link in a paragraph!</a> </p> """ expected = "*Hello*\n There is _something_ interesting about `this doc` \n And <http://example.com/|here is a link in a paragraph!>" output = HTMLSlacker(html).get_output() assert (output == expected)
def test_rss_feed(feed_url): feed_dump = feedparser.parse(feed_url).feed if "title" in feed_dump: # pprint.pprint(feedparser.parse(feed_url).feed) d = feedparser.parse(feed_url) dumpout = { "status": True, "title": feed_dump["title"], "feed_subtext": feed_dump["subtitle"], "feed_summary": HTMLSlacker(d.entries[0]["summary"]).get_output(), "feed_entry_link": d.entries[0]["link"], } return dumpout else: return {"status": False}
def get_messages(**options): for message_data in fetch_messages(**options): channel = options['channel'] email_message = email.message_from_bytes(message_data) subject = decoded_header(email_message.get('Subject')) from_header = email_message.get('From') to_header = email_message.get('To') from_name = email.utils.getaddresses([from_header])[0][0] msgid = email_message.get('Message-ID').strip() should_notify = True try: if not options.get('text', ''): should_notify = not MailMessage.objects.get(pk=msgid) except MailMessage.DoesNotExist: pass if should_notify: # Create a nicely-formatted version of the message body, mimetype = get_body(email_message) reply = quotations.extract_from(body, mimetype) text, sig = signature.extract(reply, sender=from_header) if mimetype == "text/html": text = HTMLSlacker(text).get_output() msg = "_{}_ to _{}_\n*{}*\n\n{}".format(from_name, to_header, subject, text) msg = truncatewords_html(msg, 400) opts = {'channel': channel, 'text': msg} # Attempt to thread any email conversation as a Slack thread references = email_message.get('References', '').split() thread = MailMessage.objects.filter(msgid__in=references) sc = SlackClient(os.environ['SLACK_TOKEN']) # Send the message response = sc.api_call("chat.postMessage", **opts) if thread: # Also add it as a thread opts['thread_ts'] = thread.first().slackthread_ts response = sc.api_call("chat.postMessage", **opts) if response['ok']: ts = response['ts'] msg, created = MailMessage.objects.get_or_create( subject=subject, msgid=msgid) msg.slackthread_ts = ts msg.save()
def test_pipedrive_test(): # html = "Test <div><br></div><div>This is after newline</div><div><br></div><div><b><i><u>This is bold italic underlined</u></i></b></div><div><b><i><u><br></u></i></b></div><div><ul><li>this is unordered list</li><ul><li>nested</li></ul><li>unnested</li></ul><ol><li>ordered</li></ol></div>" html = "Test <div><br></div><div>This is after newline</div><div><br></div><div><b><i><u>This is bold italic underlined</u></i></b></div><div><b><i><u><br></u></i></b></div>" expected = "Test \nThis is after newline\n*_This is bold italic underlined_*" output = HTMLSlacker(html).get_output() assert (output == expected)
def test_link_with_target(): html = 'Please click <a href="http://xxx.com/t.html" target="_blank">here</a>' expected = "Please click <http://xxx.com/t.html|here>" output = HTMLSlacker(html).get_output() assert (output == expected)
def test_script_not_rendered(): html = '''<script><!-- /* stuff */ --></script>Dear Prudence''' expected = "Dear Prudence" output = HTMLSlacker(html).get_output() assert (output == expected)
def test_task_list_rendered(): html = '''[] Grocery<br>[x] Laundary''' expected = "☐ Grocery\n☑︎ Laundary" output = HTMLSlacker(html).get_output() assert (output == expected)
def test_headers_rendered_no_spaces(): html = '''<h2>Hello</h2><h7>new</h7><h2><b>world</b></h2>''' expected = "*Hello*\nnew *world*" output = HTMLSlacker(html).get_output() assert (output == expected)
def test_ordered_list(): html = 'Here is my cool list <ol><li>The Shining</li><li>Memento</li><li>Blade Runner</li></ol>' expected = 'Here is my cool list 1. The Shining\n2. Memento\n3. Blade Runner' output = HTMLSlacker(html).get_output() assert (output == expected)