refactored conversions.py

This commit is contained in:
specCon18 2025-02-01 01:56:00 -05:00
parent 8d709fdc0e
commit 590f8548b0
2 changed files with 33 additions and 18 deletions

View file

@ -2,6 +2,7 @@ from textnode import TextType, TextNode
from htmlnode import LeafNode
import re
# ====Markdown Parsing and Block Handling====
def block_to_block_type(markdown):
markdown = markdown.strip() # Remove leading/trailing whitespace
@ -68,27 +69,33 @@ def markdown_to_blocks(markdown):
current_block = []
return blocks
# ====Markdown Node Conversion====
def text_node_to_html_node(text_node):
match text_node.text_type:
case TextType.NORMAL_TEXT:
return LeafNode(value=text_node.text)
case TextType.BOLD_TEXT:
return LeafNode(value=text_node.text,tag="b")
return LeafNode(value=text_node.text, tag="b")
case TextType.ITALIC_TEXT:
return LeafNode(value=text_node.text,tag="i")
return LeafNode(value=text_node.text, tag="i")
case TextType.CODE_TEXT:
return LeafNode(value=text_node.text,tag="code")
return LeafNode(value=text_node.text, tag="code")
case TextType.LINK_TEXT:
node = LeafNode(value=text_node.text,tag="a")
node.props = {"href":text_node.url}
node = LeafNode(value=text_node.text, tag="a")
node.props = {"href": text_node.url}
return node
case TextType.IMAGE_TEXT:
node = LeafNode(value="",tag="img")
node.props = {"src":text_node.url ,"alt":text_node.text}
node = LeafNode(value="", tag="img")
node.props = {"src": text_node.url, "alt": text_node.text}
return node
case _:
raise Exception("NOT_A_VALID_TEXT_TYPE")
# ====Node Splitting and Text Processing====
def split_nodes_delimiter(old_nodes, delimiter, text_type):
new_nodes = []
@ -133,15 +140,6 @@ def split_nodes_delimiter(old_nodes, delimiter, text_type):
return new_nodes
def extract_markdown_images(text):
pattern = r"!\[(.*?)\]\((.*?\.(?:png|jpg|jpeg|gif|svg|webp|bmp|tiff|ico)[^)]*)\)"
matches = re.findall(pattern, text)
return matches
def extract_markdown_links(text):
pattern = r"\[(.*?)\]\((.*?)\)"
matches = re.findall(pattern,text)
return matches
def split_nodes_image(old_nodes):
new_nodes = []
@ -178,6 +176,7 @@ def split_nodes_image(old_nodes):
return new_nodes
def split_nodes_link(old_nodes):
new_nodes = []
for node in old_nodes:
@ -209,6 +208,9 @@ def split_nodes_link(old_nodes):
return new_nodes
# ====Text-to-Node Conversion====
def text_to_textnodes(text):
nodes = [TextNode(text, TextType.NORMAL_TEXT)]
@ -222,3 +224,17 @@ def text_to_textnodes(text):
nodes = [node for node in nodes if node.text.strip()]
return nodes
# ====Markdown Extraction Functions====
def extract_markdown_images(text):
pattern = r"!\[(.*?)\]\((.*?\.(?:png|jpg|jpeg|gif|svg|webp|bmp|tiff|ico)[^)]*)\)"
matches = re.findall(pattern, text)
return matches
def extract_markdown_links(text):
pattern = r"\[(.*?)\]\((.*?)\)"
matches = re.findall(pattern, text)
return matches

View file

@ -1,9 +1,8 @@
import unittest
from htmlnode import LeafNode
from conversions import text_node_to_html_node,split_nodes_delimiter,extract_markdown_images,extract_markdown_links,split_nodes_image,split_nodes_link,text_to_textnodes,markdown_to_blocks,block_to_block_type
from textnode import TextType,TextNode
import re
class TestConversions(unittest.TestCase):
def test_text_node_to_html_node(self):