added config for ignore-list (html parser)

This commit is contained in:
likho 2024-01-13 19:29:00 -08:00
parent af589847e7
commit 3607295b9e
2 changed files with 10 additions and 5 deletions

View File

@ -9,6 +9,8 @@ relative_css=["./style.css", "./timeline.css"]
accepted_images= ["jpg", "JPG", "png", "PNG"] accepted_images= ["jpg", "JPG", "png", "PNG"]
# true = add <p></p> tags to each line. # true = add <p></p> tags to each line.
tag_paragraphs=true tag_paragraphs=true
# apply <p> tags even if a line contains the following
inline_tags = ["i", "em", "b", "strong","u", "s", "a", "span"]
# adds <br> or user defined string between each line # adds <br> or user defined string between each line
# line_separator="<br>" # line_separator="<br>"
format=""" format="""

View File

@ -56,18 +56,18 @@ def make_gallery(indices, w, conf=None):
# apply basic HTML formatting - only div class here is gallery # apply basic HTML formatting - only div class here is gallery
from html.parser import HTMLParser from html.parser import HTMLParser
class My_Html_Parser(HTMLParser): class My_Html_Parser(HTMLParser):
def __init__(self): def __init__(self, ignore_list):
super().__init__() super().__init__()
self.stack = [] self.stack = []
self.completed_by = "" self.completed_by = ""
# ignore common inline tags
self.ignore = ignore_list
def handle_starttag(self, tag, attrs): def handle_starttag(self, tag, attrs):
self.stack.append(tag) self.stack.append(tag)
self.is_completed_by = "" self.is_completed_by = ""
def handle_endtag(self, tag): def handle_endtag(self, tag):
# ignore common inline tags
ignore = ["i", "em", "b", "strong","u", "s", "a", "span"]
# remove an item == tag from the end of the list # remove an item == tag from the end of the list
i = len(self.stack) - 1 i = len(self.stack) - 1
last = self.stack[i] last = self.stack[i]
@ -77,7 +77,7 @@ class My_Html_Parser(HTMLParser):
break break
i -= 1 i -= 1
last = self.stack[i] last = self.stack[i]
if self.stack == [] and tag not in ignore: if self.stack == [] and tag not in self.ignore:
self.completed_by = "</%s>" % tag self.completed_by = "</%s>" % tag
from html import escape from html import escape
@ -127,8 +127,11 @@ def markup(message, config):
output = [] output = []
gallery = [] gallery = []
ptags = config["tag_paragraphs"] ptags = config["tag_paragraphs"]
ignore = []
if "inline_tags" in config:
ignore = config["inline_tags"]
parser = My_Html_Parser(ignore)
sep = "" sep = ""
parser = My_Html_Parser()
if "line_separator" in config: if "line_separator" in config:
sep = config["line_separator"] sep = config["line_separator"]
for line in message: for line in message: