2017-04-24 16:25:34 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import re
|
2017-12-29 14:26:40 +01:00
|
|
|
import socket
|
2017-04-24 16:25:34 +02:00
|
|
|
|
|
|
|
from bs4 import BeautifulSoup
|
|
|
|
|
2017-12-30 16:30:35 +01:00
|
|
|
from toot.exceptions import ConsoleError
|
|
|
|
|
2017-04-24 16:25:34 +02:00
|
|
|
|
|
|
|
def get_text(html):
|
|
|
|
"""Converts html to text, strips all tags."""
|
|
|
|
return BeautifulSoup(html, "html.parser").get_text().replace(''', "'")
|
|
|
|
|
|
|
|
|
|
|
|
def parse_html(html):
|
|
|
|
"""Attempt to convert html to plain text while keeping line breaks.
|
|
|
|
Returns a list of paragraphs, each being a list of lines.
|
|
|
|
"""
|
|
|
|
paragraphs = re.split("</?p[^>]*>", html)
|
|
|
|
|
|
|
|
# Convert <br>s to line breaks and remove empty paragraphs
|
|
|
|
paragraphs = [re.split("<br */?>", p) for p in paragraphs if p]
|
|
|
|
|
|
|
|
# Convert each line in each paragraph to plain text:
|
|
|
|
return [[get_text(l) for l in p] for p in paragraphs]
|
|
|
|
|
|
|
|
|
|
|
|
def format_content(content):
|
|
|
|
"""Given a Status contents in HTML, converts it into lines of plain text.
|
|
|
|
|
|
|
|
Returns a generator yielding lines of content.
|
|
|
|
"""
|
|
|
|
|
|
|
|
paragraphs = parse_html(content)
|
|
|
|
|
|
|
|
first = True
|
|
|
|
|
|
|
|
for paragraph in paragraphs:
|
|
|
|
if not first:
|
|
|
|
yield ""
|
|
|
|
|
|
|
|
for line in paragraph:
|
|
|
|
yield line
|
|
|
|
|
|
|
|
first = False
|
2017-12-29 14:26:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
def domain_exists(name):
|
|
|
|
try:
|
|
|
|
socket.gethostbyname(name)
|
|
|
|
return True
|
|
|
|
except OSError:
|
|
|
|
return False
|
2017-12-30 16:30:35 +01:00
|
|
|
|
|
|
|
|
|
|
|
def assert_domain_exists(domain):
|
|
|
|
if not domain_exists(domain):
|
|
|
|
raise ConsoleError("Domain {} not found".format(domain))
|
2018-01-04 12:36:14 +01:00
|
|
|
|
|
|
|
|
|
|
|
def trunc(text, length):
|
|
|
|
"""Trims text to given length, if trimmed appends ellipsis."""
|
|
|
|
if len(text) <= length:
|
|
|
|
return text
|
|
|
|
|
|
|
|
return text[:length - 1] + '…'
|