Exemple #1
0
 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))
Exemple #2
0
 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))
Exemple #3
0
 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))
Exemple #4
0
 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))
Exemple #5
0
 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>")
Exemple #6
0
 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))
Exemple #7
0
 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>")
Exemple #8
0
 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))
Exemple #10
0
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)])
Exemple #11
0
 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))
Exemple #12
0
 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>")
Exemple #13
0
 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))
Exemple #14
0
 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))
Exemple #15
0
 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))
Exemple #16
0
 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>"
         )
Exemple #17
0
 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(".")
         ])
Exemple #18
0
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
Exemple #19
0
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
Exemple #20
0
 def test_parse1(self):
     parser = Parser()
     node1 = parser.parse_string("newline  \n")
     node2 = ParagraphNode(nodes=[
         TextNode("newline"),
         NewlineNode()
     ])
     self.assertEqual(str(node1), str(node2))
Exemple #21
0
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
Exemple #22
0
 def test_parse1(self):
     parser = Parser()
     node1 = parser.parse_string("*emphasis*")
     node2 = ParagraphNode(nodes=[
         EmphasisNode(nodes=[
             TextNode("emphasis")
         ])
     ])
     self.assertEqual(str(node1), str(node2))
Exemple #23
0
 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))
Exemple #24
0
 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))
Exemple #25
0
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())
Exemple #26
0
 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))
Exemple #27
0
 def test_parse1(self):
     parser = Parser()
     node1 = parser.parse_string("# h1")
     node2 = HeadingNode(1, nodes=[TextNode("h1")])
     self.assertEqual(str(node1), str(node2))
Exemple #28
0
 def write_test1(self):
     node = TextNode("moco chibi nanashi")
     with StringIO() as stream:
         node.write(stream)
         self.assertEqual(stream.getvalue(), "moco chibi nanashi")
Exemple #29
0
 def write_test2(self):
     node = TextNode("<moco> chibi nanashi")
     with StringIO() as stream:
         node.write(stream)
         self.assertEqual(stream.getvalue(), "&lt;moco&gt; chibi nanashi")
Exemple #30
0
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