From 3607295b9eb4ef9b4dc390616ff7248d5bbb5277 Mon Sep 17 00:00:00 2001 From: likho Date: Sat, 13 Jan 2024 19:29:00 -0800 Subject: [PATCH] added config for ignore-list (html parser) --- example/settings.toml | 2 ++ microblog.py | 13 ++++++++----- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/example/settings.toml b/example/settings.toml index 2e2c77f..8d3ad6d 100644 --- a/example/settings.toml +++ b/example/settings.toml @@ -9,6 +9,8 @@ relative_css=["./style.css", "./timeline.css"] accepted_images= ["jpg", "JPG", "png", "PNG"] # true = add

tags to each line. tag_paragraphs=true +# apply

tags even if a line contains the following +inline_tags = ["i", "em", "b", "strong","u", "s", "a", "span"] # adds
or user defined string between each line # line_separator="
" format=""" diff --git a/microblog.py b/microblog.py index 3b51bd7..bb1ba53 100644 --- a/microblog.py +++ b/microblog.py @@ -56,18 +56,18 @@ def make_gallery(indices, w, conf=None): # apply basic HTML formatting - only div class here is gallery from html.parser import HTMLParser class My_Html_Parser(HTMLParser): - def __init__(self): + def __init__(self, ignore_list): super().__init__() self.stack = [] self.completed_by = "" + # ignore common inline tags + self.ignore = ignore_list def handle_starttag(self, tag, attrs): self.stack.append(tag) self.is_completed_by = "" 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 i = len(self.stack) - 1 last = self.stack[i] @@ -77,7 +77,7 @@ class My_Html_Parser(HTMLParser): break i -= 1 last = self.stack[i] - if self.stack == [] and tag not in ignore: + if self.stack == [] and tag not in self.ignore: self.completed_by = "" % tag from html import escape @@ -127,8 +127,11 @@ def markup(message, config): output = [] gallery = [] ptags = config["tag_paragraphs"] + ignore = [] + if "inline_tags" in config: + ignore = config["inline_tags"] + parser = My_Html_Parser(ignore) sep = "" - parser = My_Html_Parser() if "line_separator" in config: sep = config["line_separator"] for line in message: