added text node to html node conversion function

This commit is contained in:
John O'Keefe 2024-03-26 21:16:33 -04:00
parent ffb01d835c
commit 591be4b275

View File

@ -1,3 +1,5 @@
from htmlnode import LeafNode
text_type_text = "text"
text_type_bold = "bold"
text_type_italic = "italic"
@ -5,6 +7,7 @@ text_type_code = "code"
text_type_link = "link"
text_type_image = "image"
class TextNode:
def __init__(self, text, text_type, url=None) -> None:
self.text = text
@ -17,6 +20,25 @@ class TextNode:
and self.text_type == other_node.text_type
and self.url == other_node.url
)
def __repr__(self) -> str:
return f"TextNode({self.text}, {self.text_type}, {self.url})"
def text_node_to_html_node(text_node):
text = text_node.text
match text_node.text_type:
case "text":
return LeafNode(None, text)
case "bold":
return LeafNode("b", text)
case "italic":
return LeafNode("i", text)
case "code":
return LeafNode("code", text)
case "link":
return LeafNode("a", text, {"href": text_node.url})
case "image":
return LeafNode("img", "", {"src": text_node.url, "alt": text})
case _:
raise ValueError(f"Invalid text type: {text_node.text_type}")