def test_parse_list1(self): parser = Parser() node1 = parser.parse_string( "- 0\n" + " * 1\n" + " - 2\n" + " * 1") node2 = OrderedListNode(nodes=[ ListItemNode(nodes=[ TextNode("0"), UnOrderedListNode(nodes=[ ListItemNode(nodes=[ TextNode("1"), OrderedListNode(nodes=[ ListItemNode(nodes=[ TextNode("2") ]) ]) ]), ListItemNode(nodes=[ TextNode("1") ]) ]) ]) ]) self.assertEqual(str(node1), str(node2))
def test_parse1(self): parser = Parser() node1 = parser.parse_string("> a\n> b\n> c") node2 = QuotationNode( nodes=[TextNode("a"), TextNode("b"), TextNode("c")]) self.assertEqual(str(node1), str(node2))
def test_parse2(self): parser = Parser() node1 = parser.parse_string( "@audio example.mp3\n@audio @src example.mp3\n@audio @src example.wav audio/wav") node2 = AudioNode( preload="none", controls=True, nodes=[ SourceNode( src="example.mp3", type="audio/mpeg"), SourceNode( src="example.mp3", type="audio/mpeg"), SourceNode( src="example.wav", type="audio/wav"), ParagraphNode(nodes=[ TextNode( "Your browser has not supported playing audio with HTML5."), TextNode("You can download audio from "), LinkNode( href="example.mp3", target="_blank", nodes=[ TextNode("here") ]), TextNode(".") ]) ]) self.assertEqual(str(node1), str(node2))
def test_parse2(self): parser = Parser() node1 = parser.parse_string("> a\n> b\n> c\n>> https://example.com") node2 = QuotationNode( cite="https://example.com", nodes=[TextNode("a"), TextNode("b"), TextNode("c")]) self.assertEqual(str(node1), str(node2))
def test_write(self): pnode = ParagraphNode() pnode.add_node(TextNode("moco")) pnode.add_node(TextNode("chibi")) pnode.add_node(TextNode("nisechibi")) with StringIO() as stream: pnode.write(stream) self.assertEqual(stream.getvalue(), "<p>mocochibinisechibi</p>")
def test_parse3(self): parser = Parser() node1 = parser.parse_string("# h1\n## h2") node2 = RootNode(nodes=[ HeadingNode(1, nodes=[TextNode("h1")]), HeadingNode(2, nodes=[TextNode("h2")]) ]) self.assertEqual(str(node1), str(node2))
def test_write(self): hnode = HeadingNode(1) hnode.add_node(TextNode("moco")) hnode.add_node(TextNode("chibi")) hnode.add_node(TextNode("nanashi")) with StringIO() as stream: hnode.write(stream) self.assertEqual(stream.getvalue(), "<h1>mocochibinanashi</h1>")
def test_parse3(self): parser = Parser() node1 = parser.parse_string("text then **strong**!") node2 = ParagraphNode(nodes=[ TextNode("text then "), StrongNode(nodes=[TextNode("strong")]), TextNode("!") ]) self.assertEqual(str(node1), str(node2))
def test_parse2(self): parser = Parser() node1 = parser.parse_string("text then `<b>example</b>`!") node2 = ParagraphNode(nodes=[ TextNode("text then "), InlineCodeNode("<b>example</b>"), TextNode("!") ]) self.assertEqual(str(node1), str(node2))
def make_link(text, href): parsed = urlparse(href) if (not parsed.scheme and not parsed.netloc and not parsed.path and not parsed.params and not parsed.query and not parsed.username and not parsed.password and not parsed.hostname and not parsed.port and parsed.fragment): return LinkNode(href=href, nodes=[TextNode(text)]) else: return LinkNode(href=href, target="_blank", nodes=[TextNode(text)])
def test_parse3(self): parser = Parser() node1 = parser.parse_string("text then <b>bold</b>!") node2 = ParagraphNode(nodes=[ TextNode("text then "), HTMLNode("<b>bold</b>"), TextNode("!") ]) self.assertEqual(str(node1), str(node2))
def test_write1(self): qnode = QuotationNode() qnode.add_node(TextNode("moco")) qnode.add_node(TextNode("chibi")) qnode.add_node(TextNode("nanashi")) with StringIO() as stream: qnode.write(stream) self.assertEqual(stream.getvalue(), "<blockquote>mocochibinanashi</blockquote>")
def test_parse3(self): parser = Parser() node1 = parser.parse_string("text then ~~delete~~!") node2 = ParagraphNode(nodes=[ TextNode("text then "), DeleteNode(nodes=[TextNode("delete")]), TextNode("!") ]) self.assertEqual(str(node1), str(node2))
def test_parse4(self): parser = Parser() node1 = parser.parse_string("text then <img src=\"example.png\"/>!") node2 = ParagraphNode(nodes=[ TextNode("text then "), HTMLNode("<img src=\"example.png\"/>"), TextNode("!") ]) self.assertEqual(str(node1), str(node2))
def test_parse3(self): parser = Parser() node1 = parser.parse_string("text then *emphasis*!") node2 = ParagraphNode(nodes=[ TextNode("text then "), EmphasisNode(nodes=[ TextNode("emphasis") ]), TextNode("!") ]) self.assertEqual(str(node1), str(node2))
def test_write2(self): qnode = QuotationNode(cite="https://example.com") qnode.add_node(TextNode("moco")) qnode.add_node(TextNode("chibi")) qnode.add_node(TextNode("nanashi")) with StringIO() as stream: qnode.write(stream) self.assertEqual( stream.getvalue(), "<blockquote cite=\"https://example.com\">mocochibinanashi</blockquote>" )
def build_alternative_node(self): if len(self.sources) == 0: raise Exception() else: source = self.sources[0] return ParagraphNode(nodes=[ TextNode( "Your browser has not supported playing audio with HTML5." ), TextNode("You can download audio from "), LinkNode(href=source.get_src(), target="_blank", nodes=[TextNode("here")]), TextNode(".") ])
def read_strong(preread, stream, parser, options): read1 = read_until_string(stream, "**", use_escape=True) stream.get() stream.get() snode = StrongNode() snode.add_node(TextNode(read1)) return snode
def read_delete(preread, stream, parser, options): read1 = read_until_string(stream, "~~", use_escape=True) stream.get() stream.get() dnode = DeleteNode() dnode.add_node(TextNode(read1)) return dnode
def test_parse1(self): parser = Parser() node1 = parser.parse_string("newline \n") node2 = ParagraphNode(nodes=[ TextNode("newline"), NewlineNode() ]) self.assertEqual(str(node1), str(node2))
def read_url(preread, stream, parser, options): with StringIO() as readstr: readstr.write(preread) read = read_while(stream, URL_CHARS, eof_is_error=False) readstr.write(read) linknode = LinkNode(href=readstr.getvalue(), target="_blank") linknode.add_node(TextNode(readstr.getvalue())) return linknode
def test_parse1(self): parser = Parser() node1 = parser.parse_string("*emphasis*") node2 = ParagraphNode(nodes=[ EmphasisNode(nodes=[ TextNode("emphasis") ]) ]) self.assertEqual(str(node1), str(node2))
def test_parse_ordered_list1(self): parser = Parser() node1 = parser.parse_string( "- a\n" + "- b\n" + "- c") node2 = OrderedListNode(nodes=[ ListItemNode(nodes=[ TextNode("a") ]), ListItemNode(nodes=[ TextNode("b") ]), ListItemNode(nodes=[ TextNode("c") ]) ]) self.assertEqual(str(node1), str(node2))
def test_parse1(self): parser = Parser() node1 = parser.parse_string("[https://example.com]") node2 = ParagraphNode(nodes=[ LinkNode(href="https://example.com", target="_blank", nodes=[TextNode("https://example.com")]) ]) self.assertEqual(str(node1), str(node2))
def read_text(preread, stream, parser, options): with StringIO() as readstr: readstr.write(preread) while True: character = stream.peek() if not character: break if parser.is_inline_syntax_character(character): break stream.get() readstr.write(character) return TextNode(readstr.getvalue())
def test_parse_unordered_list2(self): parser = Parser() node1 = parser.parse_string( "* *a*\n" + "* *b*\n" + "* *c*") node2 = UnOrderedListNode(nodes=[ ListItemNode(nodes=[ EmphasisNode(nodes=[ TextNode("a") ]) ]), ListItemNode(nodes=[ EmphasisNode(nodes=[ TextNode("b") ]) ]), ListItemNode(nodes=[ EmphasisNode(nodes=[ TextNode("c") ]) ]) ]) self.assertEqual(str(node1), str(node2))
def test_parse1(self): parser = Parser() node1 = parser.parse_string("# h1") node2 = HeadingNode(1, nodes=[TextNode("h1")]) self.assertEqual(str(node1), str(node2))
def write_test1(self): node = TextNode("moco chibi nanashi") with StringIO() as stream: node.write(stream) self.assertEqual(stream.getvalue(), "moco chibi nanashi")
def write_test2(self): node = TextNode("<moco> chibi nanashi") with StringIO() as stream: node.write(stream) self.assertEqual(stream.getvalue(), "<moco> chibi nanashi")
def read_emphasis(preread, stream, parser, options): read = read_until(stream, "*", use_escape=True) stream.get() enode = EmphasisNode() enode.add_node(TextNode(read)) return enode