def reformat_html(html_str: str, max_length: Optional[int] = None) -> str: """ Reformats an HTML string, removing all opening and closing tags. Adds a line break element between each set of text content. Optionally truncates contents that exceeds the given max length. returns: A reformatted HTML string """ parser = HTMLTagRemover() # Must have a root node, otherwise the parser will fail parser.feed(f'<div>{html_str}</div>') content = [web.websafe(s) for s in parser.data if s] if max_length: return truncate(''.join(content), max_length).strip().replace('\n', '<br>') else: return ''.join(content).strip().replace('\n', '<br>')
def test_truncate(): assert h.truncate("hello", 6) == "hello" assert h.truncate("hello", 5) == "hello" assert h.truncate("hello", 4) == "hell..."