From 39c94f7a715384869d59d8d03a30f8e4d33f5a7d Mon Sep 17 00:00:00 2001 From: John O'Keefe Date: Thu, 28 Mar 2024 19:34:12 -0400 Subject: [PATCH] added split nodes delimiter --- src/inline_markdown.py | 23 ++++++++++ src/test.py | 7 ++++ src/test_inline_markdown.py | 83 +++++++++++++++++++++++++++++++++++++ src/test_textnode.py | 21 ++++++++-- 4 files changed, 131 insertions(+), 3 deletions(-) create mode 100644 src/inline_markdown.py create mode 100644 src/test.py create mode 100644 src/test_inline_markdown.py diff --git a/src/inline_markdown.py b/src/inline_markdown.py new file mode 100644 index 0000000..afe208d --- /dev/null +++ b/src/inline_markdown.py @@ -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 diff --git a/src/test.py b/src/test.py new file mode 100644 index 0000000..f046cf5 --- /dev/null +++ b/src/test.py @@ -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) diff --git a/src/test_inline_markdown.py b/src/test_inline_markdown.py new file mode 100644 index 0000000..55f48d5 --- /dev/null +++ b/src/test_inline_markdown.py @@ -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() diff --git a/src/test_textnode.py b/src/test_textnode.py index b23235b..ebd5106 100644 --- a/src/test_textnode.py +++ b/src/test_textnode.py @@ -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() \ No newline at end of file + unittest.main()