created split images and split nodes
This commit is contained in:
		@@ -4,25 +4,89 @@ from textnode import TextNode
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def split_nodes_delimiter(old_nodes, delimiter, text_type):
 | 
					def split_nodes_delimiter(old_nodes, delimiter, text_type):
 | 
				
			||||||
    final_node_list = []
 | 
					    new_nodes = []
 | 
				
			||||||
    for old_node in old_nodes:
 | 
					    for old_node in old_nodes:
 | 
				
			||||||
        node_list = []
 | 
					        if old_node.text_type != "text":
 | 
				
			||||||
        if not isinstance(old_node, TextNode):
 | 
					            new_nodes.append(old_node)
 | 
				
			||||||
            node_list.append(old_node)
 | 
					            continue
 | 
				
			||||||
            return node_list
 | 
					        split_nodes = []
 | 
				
			||||||
        split_list = old_node.text.split(delimiter)
 | 
					        sections = old_node.text.split(delimiter)
 | 
				
			||||||
        if old_node.text.count(delimiter) % 2 != 0:
 | 
					        if len(sections) % 2 == 0:
 | 
				
			||||||
            raise ValueError("No closing delimiter found")
 | 
					            raise ValueError("Invalid markdown, formatted section not closed")
 | 
				
			||||||
        for i in range(len(split_list)):
 | 
					        for i in range(len(sections)):
 | 
				
			||||||
            if i % 2 != 0 and len(split_list[i]) > 0:
 | 
					            if sections[i] == "":
 | 
				
			||||||
                part = TextNode(split_list[i], text_type)
 | 
					                continue
 | 
				
			||||||
                node_list.append(part)
 | 
					            if i % 2 == 0:
 | 
				
			||||||
            elif len(split_list[i]) > 0:
 | 
					                split_nodes.append(TextNode(sections[i], "text"))
 | 
				
			||||||
                part = TextNode(split_list[i], "text")
 | 
					            else:
 | 
				
			||||||
                node_list.append(part)
 | 
					                split_nodes.append(TextNode(sections[i], text_type))
 | 
				
			||||||
        final_node_list.extend(node_list)
 | 
					        new_nodes.extend(split_nodes)
 | 
				
			||||||
 | 
					    return new_nodes
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return final_node_list
 | 
					
 | 
				
			||||||
 | 
					def split_nodes_image(old_nodes):
 | 
				
			||||||
 | 
					    new_nodes = []
 | 
				
			||||||
 | 
					    for old_node in old_nodes:
 | 
				
			||||||
 | 
					        images = extract_markdown_images(old_node.text)
 | 
				
			||||||
 | 
					        if old_node.text_type != "text" or len(images) == 0:
 | 
				
			||||||
 | 
					            new_nodes.append(old_node)
 | 
				
			||||||
 | 
					            continue
 | 
				
			||||||
 | 
					        if len(old_node.text) == 0:
 | 
				
			||||||
 | 
					            continue
 | 
				
			||||||
 | 
					        split_nodes = []
 | 
				
			||||||
 | 
					        original_text = old_node.text
 | 
				
			||||||
 | 
					        sections = []
 | 
				
			||||||
 | 
					        for i in range(len(images)):
 | 
				
			||||||
 | 
					            section = original_text.split(f"![{images[i][0]}]({images[i][1]})", 1)
 | 
				
			||||||
 | 
					            if len(section) != 2:
 | 
				
			||||||
 | 
					                raise ValueError("Invalid markdown, image section not closed")
 | 
				
			||||||
 | 
					            sections.append(section[0])
 | 
				
			||||||
 | 
					            sections.append(images[i])
 | 
				
			||||||
 | 
					            if i+1 is len(images):
 | 
				
			||||||
 | 
					                sections.append(section[1])
 | 
				
			||||||
 | 
					            else:
 | 
				
			||||||
 | 
					                original_text = section[1]
 | 
				
			||||||
 | 
					        for i in range(len(sections)):
 | 
				
			||||||
 | 
					            if sections[i] == "":
 | 
				
			||||||
 | 
					                continue
 | 
				
			||||||
 | 
					            if isinstance(sections[i], str):
 | 
				
			||||||
 | 
					                split_nodes.append(TextNode(sections[i], "text"))
 | 
				
			||||||
 | 
					            else:
 | 
				
			||||||
 | 
					                split_nodes.append(TextNode(sections[i][0], "image", sections[i][1]))
 | 
				
			||||||
 | 
					        new_nodes.extend(split_nodes)
 | 
				
			||||||
 | 
					    return new_nodes
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def split_nodes_link(old_nodes):
 | 
				
			||||||
 | 
					    new_nodes = []
 | 
				
			||||||
 | 
					    for old_node in old_nodes:
 | 
				
			||||||
 | 
					        links = extract_markdown_links(old_node.text)
 | 
				
			||||||
 | 
					        if old_node.text_type != "text" or len(links) == 0:
 | 
				
			||||||
 | 
					            new_nodes.append(old_node)
 | 
				
			||||||
 | 
					            continue
 | 
				
			||||||
 | 
					        if len(old_node.text) == 0:
 | 
				
			||||||
 | 
					            continue
 | 
				
			||||||
 | 
					        split_nodes = []
 | 
				
			||||||
 | 
					        original_text = old_node.text
 | 
				
			||||||
 | 
					        sections = []
 | 
				
			||||||
 | 
					        for i in range(len(links)):
 | 
				
			||||||
 | 
					            section = original_text.split(f"[{links[i][0]}]({links[i][1]})", 1)
 | 
				
			||||||
 | 
					            if len(section) != 2:
 | 
				
			||||||
 | 
					                raise ValueError("Invalid markdown, link section not closed")
 | 
				
			||||||
 | 
					            sections.append(section[0])
 | 
				
			||||||
 | 
					            sections.append(links[i])
 | 
				
			||||||
 | 
					            if i+1 is len(links):
 | 
				
			||||||
 | 
					                sections.append(section[1])
 | 
				
			||||||
 | 
					            else:
 | 
				
			||||||
 | 
					                original_text = section[1]
 | 
				
			||||||
 | 
					        for i in range(len(sections)):
 | 
				
			||||||
 | 
					            if sections[i] == "":
 | 
				
			||||||
 | 
					                continue
 | 
				
			||||||
 | 
					            if isinstance(sections[i], str):
 | 
				
			||||||
 | 
					                split_nodes.append(TextNode(sections[i], "text"))
 | 
				
			||||||
 | 
					            else:
 | 
				
			||||||
 | 
					                split_nodes.append(TextNode(sections[i][0], "link", sections[i][1]))
 | 
				
			||||||
 | 
					        new_nodes.extend(split_nodes)
 | 
				
			||||||
 | 
					    return new_nodes
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def extract_markdown_images(text):
 | 
					def extract_markdown_images(text):
 | 
				
			||||||
@@ -31,3 +95,12 @@ def extract_markdown_images(text):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
def extract_markdown_links(text):
 | 
					def extract_markdown_links(text):
 | 
				
			||||||
    return re.findall(r"\[(.*?)\]\((.*?)\)", text)
 | 
					    return re.findall(r"\[(.*?)\]\((.*?)\)", text)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					node_image_first = TextNode(
 | 
				
			||||||
 | 
					            " and another . Isn't this awesome!",
 | 
				
			||||||
 | 
					            "text"
 | 
				
			||||||
 | 
					        ),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					new_node = split_nodes_image(node_image_first)
 | 
				
			||||||
 | 
					print(new_node)
 | 
				
			||||||
@@ -1,9 +1,9 @@
 | 
				
			|||||||
import unittest
 | 
					import unittest
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from inline_markdown import (extract_markdown_images, extract_markdown_links,
 | 
					from inline_markdown import (extract_markdown_images, extract_markdown_links,
 | 
				
			||||||
                             split_nodes_delimiter)
 | 
					                             split_nodes_delimiter, split_nodes_image, split_nodes_link)
 | 
				
			||||||
from textnode import (TextNode, text_type_bold, text_type_code,
 | 
					from textnode import (TextNode, text_type_bold, text_type_code,
 | 
				
			||||||
                      text_type_italic, text_type_text)
 | 
					                      text_type_italic, text_type_text, text_type_image, text_type_link)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class TestInlineMarkdown(unittest.TestCase):
 | 
					class TestInlineMarkdown(unittest.TestCase):
 | 
				
			||||||
@@ -73,6 +73,103 @@ class TestInlineMarkdown(unittest.TestCase):
 | 
				
			|||||||
            new_nodes,
 | 
					            new_nodes,
 | 
				
			||||||
        )
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def test_delim_images(self):
 | 
				
			||||||
 | 
					        no_images = TextNode("There are no images in this text", text_type_text)
 | 
				
			||||||
 | 
					        no_images_answer = split_nodes_image([no_images])
 | 
				
			||||||
 | 
					        self.assertEqual(
 | 
				
			||||||
 | 
					            [
 | 
				
			||||||
 | 
					                TextNode("There are no images in this text", text_type_text)
 | 
				
			||||||
 | 
					            ],
 | 
				
			||||||
 | 
					            no_images_answer
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        node = TextNode(
 | 
				
			||||||
 | 
					            "This is text with an  and another ",
 | 
				
			||||||
 | 
					            text_type_text,
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					        new_nodes = split_nodes_image([node])
 | 
				
			||||||
 | 
					        self.assertEqual(
 | 
				
			||||||
 | 
					            [
 | 
				
			||||||
 | 
					                TextNode("This is text with an ", text_type_text),
 | 
				
			||||||
 | 
					                TextNode("image", text_type_image, "https://storage.googleapis.com/qvault-webapp-dynamic-assets/course_assets/zjjcJKZ.png"),
 | 
				
			||||||
 | 
					                TextNode(" and another ", text_type_text),
 | 
				
			||||||
 | 
					                TextNode(
 | 
				
			||||||
 | 
					                    "second image", text_type_image, "https://storage.googleapis.com/qvault-webapp-dynamic-assets/course_assets/3elNhQu.png"
 | 
				
			||||||
 | 
					                ),
 | 
				
			||||||
 | 
					            ],
 | 
				
			||||||
 | 
					            new_nodes,
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        node_with_extra_content = TextNode(
 | 
				
			||||||
 | 
					            "This is text with an  and another . Isn't this awesome!",
 | 
				
			||||||
 | 
					            text_type_text,
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        new_nodes_extra = split_nodes_image([node_with_extra_content])
 | 
				
			||||||
 | 
					        self.assertEqual(
 | 
				
			||||||
 | 
					            [
 | 
				
			||||||
 | 
					                TextNode("This is text with an ", text_type_text),
 | 
				
			||||||
 | 
					                TextNode("image", text_type_image, "https://storage.googleapis.com/qvault-webapp-dynamic-assets/course_assets/zjjcJKZ.png"),
 | 
				
			||||||
 | 
					                TextNode(" and another ", text_type_text),
 | 
				
			||||||
 | 
					                TextNode(
 | 
				
			||||||
 | 
					                    "second image", text_type_image, "https://storage.googleapis.com/qvault-webapp-dynamic-assets/course_assets/3elNhQu.png"
 | 
				
			||||||
 | 
					                ),
 | 
				
			||||||
 | 
					                TextNode(". Isn't this awesome!", text_type_text),
 | 
				
			||||||
 | 
					            ],
 | 
				
			||||||
 | 
					            new_nodes_extra,
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        node_image_first = TextNode(
 | 
				
			||||||
 | 
					            " and another . Isn't this awesome!",
 | 
				
			||||||
 | 
					            text_type_text
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					        new_node_image_first = split_nodes_image([node_image_first])
 | 
				
			||||||
 | 
					        self.assertEqual(
 | 
				
			||||||
 | 
					            [
 | 
				
			||||||
 | 
					                TextNode("image", text_type_image, "https://storage.googleapis.com/qvault-webapp-dynamic-assets/course_assets/zjjcJKZ.png"),
 | 
				
			||||||
 | 
					                TextNode(" and another ", text_type_text),
 | 
				
			||||||
 | 
					                TextNode(
 | 
				
			||||||
 | 
					                    "second image", text_type_image, "https://storage.googleapis.com/qvault-webapp-dynamic-assets/course_assets/3elNhQu.png"
 | 
				
			||||||
 | 
					                ),
 | 
				
			||||||
 | 
					                TextNode(". Isn't this awesome!", text_type_text),
 | 
				
			||||||
 | 
					            ],
 | 
				
			||||||
 | 
					            new_node_image_first,
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def test_delim_links(self):
 | 
				
			||||||
 | 
					        node = TextNode(
 | 
				
			||||||
 | 
					            "This is a website with [Google](https://www.google.com) and another [Games Database](https://games.linuxhg.com)",
 | 
				
			||||||
 | 
					            text_type_text,
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					        new_nodes = split_nodes_link([node])
 | 
				
			||||||
 | 
					        self.assertEqual(
 | 
				
			||||||
 | 
					            [
 | 
				
			||||||
 | 
					                TextNode("This is a website with ", text_type_text),
 | 
				
			||||||
 | 
					                TextNode("Google", text_type_link, "https://www.google.com"),
 | 
				
			||||||
 | 
					                TextNode(" and another ", text_type_text),
 | 
				
			||||||
 | 
					                TextNode("Games Database", text_type_link, "https://games.linuxhg.com"),
 | 
				
			||||||
 | 
					            ],
 | 
				
			||||||
 | 
					            new_nodes,
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        node_with_extra_content = TextNode(
 | 
				
			||||||
 | 
					            "This is a website with [Google](https://www.google.com) and another [Games Database](https://games.linuxhg.com). Isn't this awesome!",
 | 
				
			||||||
 | 
					            text_type_text,
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        new_nodes_extra = split_nodes_link([node_with_extra_content])
 | 
				
			||||||
 | 
					        self.assertEqual(
 | 
				
			||||||
 | 
					            [
 | 
				
			||||||
 | 
					                TextNode("This is a website with ", text_type_text),
 | 
				
			||||||
 | 
					                TextNode("Google", text_type_link, "https://www.google.com"),
 | 
				
			||||||
 | 
					                TextNode(" and another ", text_type_text),
 | 
				
			||||||
 | 
					                TextNode("Games Database", text_type_link, "https://games.linuxhg.com"),
 | 
				
			||||||
 | 
					                TextNode(". Isn't this awesome!", text_type_text),
 | 
				
			||||||
 | 
					            ],
 | 
				
			||||||
 | 
					            new_nodes_extra,
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def test_extract_markdown_images(self):
 | 
					def test_extract_markdown_images(self):
 | 
				
			||||||
    matches = extract_markdown_images(
 | 
					    matches = extract_markdown_images(
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user