From 591be4b275b3113205867b727cb868fc4f81558e Mon Sep 17 00:00:00 2001 From: John O'Keefe Date: Tue, 26 Mar 2024 21:16:33 -0400 Subject: [PATCH] added text node to html node conversion function --- src/textnode.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/textnode.py b/src/textnode.py index 9ab19c9..900c1ab 100644 --- a/src/textnode.py +++ b/src/textnode.py @@ -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}")