64 lines
2.2 KiB
Python
64 lines
2.2 KiB
Python
|
# Copyright 2017-present Michael Hall
|
||
|
#
|
||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
# you may not use this file except in compliance with the License.
|
||
|
# You may obtain a copy of the License at
|
||
|
#
|
||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||
|
#
|
||
|
# Unless required by applicable law or agreed to in writing, software
|
||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
# See the License for the specific language governing permissions and
|
||
|
# limitations under the License.
|
||
|
|
||
|
import html.parser
|
||
|
import re
|
||
|
|
||
|
__all__ = ["html_to_text"]
|
||
|
|
||
|
WhitespaceHandler = re.compile("<p>|<br>|<br />|<li>", flags=re.MULTILINE)
|
||
|
|
||
|
# The below is semi-safe for use here,
|
||
|
# wouldn't reccomend copying this without understanding it.
|
||
|
# html cleanup is taken from https://stackoverflow.com/a/7778368
|
||
|
# with some modifications made to better suit the needs of this.
|
||
|
|
||
|
|
||
|
class HTMLTextExtractor(html.parser.HTMLParser): # https://stackoverflow.com/a/7778368
|
||
|
def __init__(self):
|
||
|
super().__init__()
|
||
|
self.result = []
|
||
|
|
||
|
def handle_data(self, d):
|
||
|
self.result.append(d)
|
||
|
|
||
|
def get_text(self):
|
||
|
return "".join(self.result)
|
||
|
|
||
|
|
||
|
def html_to_text(html_data): # https://stackoverflow.com/a/7778368
|
||
|
"""Converts HTML to plain text (stripping tags and converting entities).
|
||
|
>>> html_to_text('<a href="#">Demo<!--...--> <em>(¬ \u0394ημώ)</em></a>')
|
||
|
'Demo (\xac \u0394\u03b7\u03bc\u03ce)'
|
||
|
|
||
|
"Plain text" doesn't mean result can safely be used as-is in HTML.
|
||
|
>>> html_to_text('<script>alert("Hello");</script>')
|
||
|
'<script>alert("Hello");</script>'
|
||
|
|
||
|
Always use html.escape to sanitize text before using in an HTML context!
|
||
|
|
||
|
HTMLParser will do its best to make sense of invalid HTML.
|
||
|
>>> html_to_text('x < y < z <!--b')
|
||
|
'x < y < z '
|
||
|
|
||
|
Unrecognized named entities are included as-is. ''' is recognized,
|
||
|
despite being XML only.
|
||
|
>>> html_to_text('&nosuchentity; ' ')
|
||
|
"&nosuchentity; ' "
|
||
|
"""
|
||
|
html_data = WhitespaceHandler.sub("\n", html_data)
|
||
|
s = HTMLTextExtractor()
|
||
|
s.feed(html_data)
|
||
|
return s.get_text()
|