mirror of
https://github.com/ihabunek/toot.git
synced 2024-09-29 04:35:54 -04:00
Extract render method
This commit is contained in:
parent
2aba3f93f9
commit
ce6faccb99
@ -44,10 +44,7 @@ class ContentParser:
|
|||||||
if (first_tag and not recovery_attempt and name not in BLOCK_TAGS):
|
if (first_tag and not recovery_attempt and name not in BLOCK_TAGS):
|
||||||
return self.html_to_widgets(f"<p>{html}</p>", recovery_attempt=True)
|
return self.html_to_widgets(f"<p>{html}</p>", recovery_attempt=True)
|
||||||
|
|
||||||
# First, look for a custom tag handler method in this class
|
markup = self.render(name, e)
|
||||||
# If that fails, fall back to inline_tag_to_text handler
|
|
||||||
method = getattr(self, "_" + name, self.inline_tag_to_text)
|
|
||||||
markup = method(e) # either returns a Widget, or plain text
|
|
||||||
first_tag = False
|
first_tag = False
|
||||||
|
|
||||||
if not isinstance(markup, urwid.Widget):
|
if not isinstance(markup, urwid.Widget):
|
||||||
@ -77,8 +74,7 @@ class ContentParser:
|
|||||||
markups = []
|
markups = []
|
||||||
for child in tag.children:
|
for child in tag.children:
|
||||||
if isinstance(child, Tag):
|
if isinstance(child, Tag):
|
||||||
method = getattr(self, "_" + child.name, self.inline_tag_to_text)
|
markup = self.render(child.name, child)
|
||||||
markup = method(child)
|
|
||||||
markups.append(markup)
|
markups.append(markup)
|
||||||
else:
|
else:
|
||||||
markups.append(child)
|
markups.append(child)
|
||||||
@ -133,8 +129,7 @@ class ContentParser:
|
|||||||
if isinstance(child, Tag):
|
if isinstance(child, Tag):
|
||||||
# child is a nested tag; process using custom method
|
# child is a nested tag; process using custom method
|
||||||
# or default to inline_tag_to_text
|
# or default to inline_tag_to_text
|
||||||
method = getattr(self, "_" + child.name, self.inline_tag_to_text)
|
result = self.render(child.name, child)
|
||||||
result = method(child)
|
|
||||||
if isinstance(result, urwid.Widget):
|
if isinstance(result, urwid.Widget):
|
||||||
found_nested_widget = True
|
found_nested_widget = True
|
||||||
child_widgets.append(result)
|
child_widgets.append(result)
|
||||||
@ -207,6 +202,12 @@ class ContentParser:
|
|||||||
|
|
||||||
return "a"
|
return "a"
|
||||||
|
|
||||||
|
def render(self, attr, content):
|
||||||
|
# First, look for a custom tag handler method in this class
|
||||||
|
# If that fails, fall back to inline_tag_to_text handler
|
||||||
|
method = getattr(self, f"_{attr}", self.inline_tag_to_text)
|
||||||
|
return method(content)
|
||||||
|
|
||||||
def _a(self, tag) -> Tuple:
|
def _a(self, tag) -> Tuple:
|
||||||
"""anchor tag handler"""
|
"""anchor tag handler"""
|
||||||
|
|
||||||
@ -298,8 +299,7 @@ class ContentParser:
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
for li in tag.find_all("li", recursive=False):
|
for li in tag.find_all("li", recursive=False):
|
||||||
method = getattr(self, "_li", self.inline_tag_to_text)
|
markup = self.render("li", li)
|
||||||
markup = method(li)
|
|
||||||
|
|
||||||
# li value= attribute will change the item number
|
# li value= attribute will change the item number
|
||||||
# it also overrides any ol start= attribute
|
# it also overrides any ol start= attribute
|
||||||
@ -396,8 +396,7 @@ class ContentParser:
|
|||||||
widgets = []
|
widgets = []
|
||||||
|
|
||||||
for li in tag.find_all("li", recursive=False):
|
for li in tag.find_all("li", recursive=False):
|
||||||
method = getattr(self, "_li", self.inline_tag_to_text)
|
markup = self.render("li", li)
|
||||||
markup = method(li)
|
|
||||||
|
|
||||||
if not isinstance(markup, urwid.Widget):
|
if not isinstance(markup, urwid.Widget):
|
||||||
txt = self.text_to_widget("li", ["\N{bullet} ", markup])
|
txt = self.text_to_widget("li", ["\N{bullet} ", markup])
|
||||||
|
Loading…
Reference in New Issue
Block a user