refactored conversions.py
This commit is contained in:
parent
8d709fdc0e
commit
590f8548b0
2 changed files with 33 additions and 18 deletions
|
|
@ -2,6 +2,7 @@ from textnode import TextType, TextNode
|
||||||
from htmlnode import LeafNode
|
from htmlnode import LeafNode
|
||||||
import re
|
import re
|
||||||
|
|
||||||
|
# ====Markdown Parsing and Block Handling====
|
||||||
|
|
||||||
def block_to_block_type(markdown):
|
def block_to_block_type(markdown):
|
||||||
markdown = markdown.strip() # Remove leading/trailing whitespace
|
markdown = markdown.strip() # Remove leading/trailing whitespace
|
||||||
|
|
@ -68,27 +69,33 @@ def markdown_to_blocks(markdown):
|
||||||
current_block = []
|
current_block = []
|
||||||
return blocks
|
return blocks
|
||||||
|
|
||||||
|
|
||||||
|
# ====Markdown Node Conversion====
|
||||||
|
|
||||||
def text_node_to_html_node(text_node):
|
def text_node_to_html_node(text_node):
|
||||||
match text_node.text_type:
|
match text_node.text_type:
|
||||||
case TextType.NORMAL_TEXT:
|
case TextType.NORMAL_TEXT:
|
||||||
return LeafNode(value=text_node.text)
|
return LeafNode(value=text_node.text)
|
||||||
case TextType.BOLD_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:
|
case TextType.ITALIC_TEXT:
|
||||||
return LeafNode(value=text_node.text,tag="i")
|
return LeafNode(value=text_node.text, tag="i")
|
||||||
case TextType.CODE_TEXT:
|
case TextType.CODE_TEXT:
|
||||||
return LeafNode(value=text_node.text,tag="code")
|
return LeafNode(value=text_node.text, tag="code")
|
||||||
case TextType.LINK_TEXT:
|
case TextType.LINK_TEXT:
|
||||||
node = LeafNode(value=text_node.text,tag="a")
|
node = LeafNode(value=text_node.text, tag="a")
|
||||||
node.props = {"href":text_node.url}
|
node.props = {"href": text_node.url}
|
||||||
return node
|
return node
|
||||||
case TextType.IMAGE_TEXT:
|
case TextType.IMAGE_TEXT:
|
||||||
node = LeafNode(value="",tag="img")
|
node = LeafNode(value="", tag="img")
|
||||||
node.props = {"src":text_node.url ,"alt":text_node.text}
|
node.props = {"src": text_node.url, "alt": text_node.text}
|
||||||
return node
|
return node
|
||||||
case _:
|
case _:
|
||||||
raise Exception("NOT_A_VALID_TEXT_TYPE")
|
raise Exception("NOT_A_VALID_TEXT_TYPE")
|
||||||
|
|
||||||
|
|
||||||
|
# ====Node Splitting and Text Processing====
|
||||||
|
|
||||||
def split_nodes_delimiter(old_nodes, delimiter, text_type):
|
def split_nodes_delimiter(old_nodes, delimiter, text_type):
|
||||||
new_nodes = []
|
new_nodes = []
|
||||||
|
|
||||||
|
|
@ -133,15 +140,6 @@ def split_nodes_delimiter(old_nodes, delimiter, text_type):
|
||||||
|
|
||||||
return new_nodes
|
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):
|
def split_nodes_image(old_nodes):
|
||||||
new_nodes = []
|
new_nodes = []
|
||||||
|
|
@ -178,6 +176,7 @@ def split_nodes_image(old_nodes):
|
||||||
|
|
||||||
return new_nodes
|
return new_nodes
|
||||||
|
|
||||||
|
|
||||||
def split_nodes_link(old_nodes):
|
def split_nodes_link(old_nodes):
|
||||||
new_nodes = []
|
new_nodes = []
|
||||||
for node in old_nodes:
|
for node in old_nodes:
|
||||||
|
|
@ -209,6 +208,9 @@ def split_nodes_link(old_nodes):
|
||||||
|
|
||||||
return new_nodes
|
return new_nodes
|
||||||
|
|
||||||
|
|
||||||
|
# ====Text-to-Node Conversion====
|
||||||
|
|
||||||
def text_to_textnodes(text):
|
def text_to_textnodes(text):
|
||||||
nodes = [TextNode(text, TextType.NORMAL_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()]
|
nodes = [node for node in nodes if node.text.strip()]
|
||||||
return nodes
|
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
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,8 @@
|
||||||
import unittest
|
import unittest
|
||||||
from htmlnode import LeafNode
|
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 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
|
from textnode import TextType,TextNode
|
||||||
|
import re
|
||||||
class TestConversions(unittest.TestCase):
|
class TestConversions(unittest.TestCase):
|
||||||
|
|
||||||
def test_text_node_to_html_node(self):
|
def test_text_node_to_html_node(self):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue