added split nodes delimiter
This commit is contained in:
parent
591be4b275
commit
39c94f7a71
23
src/inline_markdown.py
Normal file
23
src/inline_markdown.py
Normal file
@ -0,0 +1,23 @@
|
||||
from textnode import TextNode
|
||||
|
||||
|
||||
def split_nodes_delimiter(old_nodes, delimiter, text_type):
|
||||
final_node_list = []
|
||||
for old_node in old_nodes:
|
||||
node_list = []
|
||||
if not isinstance(old_node, TextNode):
|
||||
node_list.append(old_node)
|
||||
return node_list
|
||||
split_list = old_node.text.split(delimiter)
|
||||
if old_node.text.count(delimiter) % 2 != 0:
|
||||
raise ValueError("No closing delimiter found")
|
||||
for i in range(len(split_list)):
|
||||
if i % 2 != 0 and len(split_list[i]) > 0:
|
||||
part = TextNode(split_list[i], text_type)
|
||||
node_list.append(part)
|
||||
elif len(split_list[i]) > 0:
|
||||
part = TextNode(split_list[i], "text")
|
||||
node_list.append(part)
|
||||
final_node_list.extend(node_list)
|
||||
|
||||
return final_node_list
|
7
src/test.py
Normal file
7
src/test.py
Normal file
@ -0,0 +1,7 @@
|
||||
from inline_markdown import split_nodes_delimiter
|
||||
from textnode import (TextNode, text_type_bold, text_type_code,
|
||||
text_type_italic, text_type_text)
|
||||
|
||||
node = TextNode("This is text with a `code block` word", text_type_text)
|
||||
new_nodes = split_nodes_delimiter([node], "`", text_type_code)
|
||||
print(new_nodes)
|
83
src/test_inline_markdown.py
Normal file
83
src/test_inline_markdown.py
Normal file
@ -0,0 +1,83 @@
|
||||
import unittest
|
||||
|
||||
from inline_markdown import split_nodes_delimiter
|
||||
|
||||
from textnode import (
|
||||
TextNode,
|
||||
text_type_bold,
|
||||
text_type_code,
|
||||
text_type_italic,
|
||||
text_type_text,
|
||||
)
|
||||
|
||||
|
||||
class TestInlineMarkdown(unittest.TestCase):
|
||||
def test_delim_bold(self):
|
||||
node = TextNode("This is text with a **bolded** word", text_type_text)
|
||||
new_nodes = split_nodes_delimiter([node], "**", text_type_bold)
|
||||
self.assertListEqual(
|
||||
[
|
||||
TextNode("This is text with a ", text_type_text),
|
||||
TextNode("bolded", text_type_bold),
|
||||
TextNode(" word", text_type_text),
|
||||
],
|
||||
new_nodes,
|
||||
)
|
||||
|
||||
def test_delim_bold_double(self):
|
||||
node = TextNode(
|
||||
"This is text with a **bolded** word and **another**", text_type_text
|
||||
)
|
||||
new_nodes = split_nodes_delimiter([node], "**", text_type_bold)
|
||||
self.assertListEqual(
|
||||
[
|
||||
TextNode("This is text with a ", text_type_text),
|
||||
TextNode("bolded", text_type_bold),
|
||||
TextNode(" word and ", text_type_text),
|
||||
TextNode("another", text_type_bold),
|
||||
],
|
||||
new_nodes,
|
||||
)
|
||||
|
||||
def test_delim_bold_multiword(self):
|
||||
node = TextNode(
|
||||
"This is text with a **bolded word** and **another**", text_type_text
|
||||
)
|
||||
new_nodes = split_nodes_delimiter([node], "**", text_type_bold)
|
||||
self.assertListEqual(
|
||||
[
|
||||
TextNode("This is text with a ", text_type_text),
|
||||
TextNode("bolded word", text_type_bold),
|
||||
TextNode(" and ", text_type_text),
|
||||
TextNode("another", text_type_bold),
|
||||
],
|
||||
new_nodes,
|
||||
)
|
||||
|
||||
def test_delim_italic(self):
|
||||
node = TextNode("This is text with an *italic* word", text_type_text)
|
||||
new_nodes = split_nodes_delimiter([node], "*", text_type_italic)
|
||||
self.assertListEqual(
|
||||
[
|
||||
TextNode("This is text with an ", text_type_text),
|
||||
TextNode("italic", text_type_italic),
|
||||
TextNode(" word", text_type_text),
|
||||
],
|
||||
new_nodes,
|
||||
)
|
||||
|
||||
def test_delim_code(self):
|
||||
node = TextNode("This is text with a `code block` word", text_type_text)
|
||||
new_nodes = split_nodes_delimiter([node], "`", text_type_code)
|
||||
self.assertListEqual(
|
||||
[
|
||||
TextNode("This is text with a ", text_type_text),
|
||||
TextNode("code block", text_type_code),
|
||||
TextNode(" word", text_type_text),
|
||||
],
|
||||
new_nodes,
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
@ -1,15 +1,17 @@
|
||||
import unittest
|
||||
|
||||
from inline_markdown import split_nodes_delimiter
|
||||
from textnode import (
|
||||
TextNode,
|
||||
text_type_text,
|
||||
text_type_bold,
|
||||
text_type_italic,
|
||||
text_type_code,
|
||||
text_type_image,
|
||||
text_type_italic,
|
||||
text_type_link,
|
||||
text_type_text,
|
||||
)
|
||||
|
||||
|
||||
class TestTextNode(unittest.TestCase):
|
||||
def test_eq(self):
|
||||
node = TextNode("This is a text node", "bold")
|
||||
@ -39,5 +41,18 @@ class TestTextNode(unittest.TestCase):
|
||||
"TextNode(This is a text node, text, https://www.boot.dev)", repr(node)
|
||||
)
|
||||
|
||||
def test_split_nodes_delimiter(self):
|
||||
node = TextNode("This is text with a `code block` word", text_type_text)
|
||||
new_nodes = split_nodes_delimiter([node], "`", text_type_code)
|
||||
self.assertEqual(
|
||||
[
|
||||
TextNode("This is text with a ", text_type_text),
|
||||
TextNode("code block", text_type_code),
|
||||
TextNode(" word", text_type_text),
|
||||
],
|
||||
new_nodes,
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
unittest.main()
|
||||
|
Loading…
Reference in New Issue
Block a user