2024-05-11 05:10:41 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# This file is part of the Calibre-Web (https://github.com/janeczku/calibre-web)
|
|
|
|
# Copyright (C) 2018-2019 OzzieIsaacs
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
from . import logger
|
|
|
|
from lxml.etree import ParserError
|
|
|
|
|
2024-06-15 06:18:27 +00:00
|
|
|
log = logger.create()
|
|
|
|
|
2024-05-11 05:10:41 +00:00
|
|
|
try:
|
|
|
|
# at least bleach 6.0 is needed -> incomplatible change from list arguments to set arguments
|
2024-06-15 06:18:27 +00:00
|
|
|
from bleach import clean as clean_html
|
2024-06-18 18:13:26 +00:00
|
|
|
from bleach.sanitizer import ALLOWED_TAGS
|
|
|
|
bleach = True
|
2024-05-11 05:10:41 +00:00
|
|
|
except ImportError:
|
2024-06-15 06:18:27 +00:00
|
|
|
from nh3 import clean as clean_html
|
2024-06-18 18:13:26 +00:00
|
|
|
bleach = False
|
2024-05-11 05:10:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
def clean_string(unsafe_text, book_id=0):
|
|
|
|
try:
|
2024-06-18 18:13:26 +00:00
|
|
|
if bleach:
|
|
|
|
allowed_tags = list(ALLOWED_TAGS)
|
2024-07-08 12:09:53 +00:00
|
|
|
allowed_tags.extend(["p", "span", "div", "pre", "br", "h1", "h2", "h3", "h4", "h5", "h6"])
|
2024-06-18 18:13:26 +00:00
|
|
|
safe_text = clean_html(unsafe_text, tags=set(allowed_tags))
|
|
|
|
else:
|
|
|
|
safe_text = clean_html(unsafe_text)
|
2024-05-11 05:10:41 +00:00
|
|
|
except ParserError as e:
|
|
|
|
log.error("Comments of book {} are corrupted: {}".format(book_id, e))
|
|
|
|
safe_text = ""
|
|
|
|
except TypeError as e:
|
|
|
|
log.error("Comments can't be parsed, maybe 'lxml' is too new, try installing 'bleach': {}".format(e))
|
|
|
|
safe_text = ""
|
|
|
|
return safe_text
|