From 9efac322287755f0735933bc086e5b4d96149b92 Mon Sep 17 00:00:00 2001 From: Ivan Habunek Date: Thu, 20 Apr 2023 11:34:32 +0200 Subject: [PATCH] Extract palette names to a constant to avoid calculating it each time the class is constructed. --- toot/tui/richtext.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/toot/tui/richtext.py b/toot/tui/richtext.py index d311d1f..486bad3 100644 --- a/toot/tui/richtext.py +++ b/toot/tui/richtext.py @@ -10,19 +10,18 @@ from .constants import PALETTE from bs4.element import NavigableString, Tag -class ContentParser: - def __init__(self): - self.palette_names = [] - for p in PALETTE: - self.palette_names.append(p[0]) +PALETTE_NAMES = [name for name, _, _ in PALETTE] - """Parse a limited subset of HTML and create urwid widgets.""" + +class ContentParser: + """Parse a limited subset of HTML and create urwid widgets.""" def html_to_widgets(self, html) -> List[urwid.Widget]: """Convert html to urwid widgets""" widgets: List[urwid.Widget] = [] html = unicodedata.normalize("NFKC", html) soup = bs4_parse(html) + for e in soup.body or soup: if isinstance(e, NavigableString): continue @@ -121,7 +120,7 @@ class ContentParser: style_name = "class_" + "_".join(clss) # return the class name, only if we # find it as a defined palette name - if style_name in self.palette_names: + if style_name in PALETTE_NAMES: return style_name # fallback to returning the tag name