From b631c100b1ce7f68b00f9e334f4ba1fe054eda17 Mon Sep 17 00:00:00 2001 From: Aroy-Art Date: Wed, 27 Sep 2023 19:08:27 +0200 Subject: [PATCH] Add: function to check if string is html --- .../furaffinity/templatetags/custom_filters.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/archivist/sites/furaffinity/templatetags/custom_filters.py b/archivist/sites/furaffinity/templatetags/custom_filters.py index cb9d70c..de7bb5c 100644 --- a/archivist/sites/furaffinity/templatetags/custom_filters.py +++ b/archivist/sites/furaffinity/templatetags/custom_filters.py @@ -7,6 +7,23 @@ from bs4 import BeautifulSoup register = template.Library() + +def is_html(string): + ''' + Check if string is HTML + ''' + soup = BeautifulSoup(string, "html.parser") + # Remove leading and trailing white space + stripped_string = string.strip() + stripped_soup = str(soup).strip() + # If the string remained the same after parsing with BeautifulSoup, it's probably not HTML + if stripped_string == stripped_soup: + return False + # If the string changed when parsed by BeautifulSoup, it's probably HTML + else: + return True + + @register.filter def is_image(file_url): image_extensions = ['.png', '.jpg', '.jpeg', '.gif', '.webp']