Blob


1 from unittest import TestCase, main
2 from lonk import HtmlToGmi
5 def fn_media_url(mime, img_url):
6 return img_url
9 class TestHtmlToGmi(TestCase):
10 def test_br(self):
11 html = '<p>head<br>tail</p>'
12 self.assertEqual(HtmlToGmi("https://localhost/api", fn_media_url).feed(html), """\
13 head
15 tail
16 """)
18 def test_img_realtive(self):
19 html = '<img src="/d/xxxx.jpg" title="yyy">'
20 self.assertEqual(HtmlToGmi("https://localhost/api", fn_media_url).feed(html), "=> https://localhost/d/xxxx.jpg yyy")
22 def test_link_realtive(self):
23 html = '<p>head <a href="/sub1/1">https link</a> tail</p>'
24 self.assertEqual(HtmlToGmi("https://localhost/api", fn_media_url).feed(html), """\
25 head ↳ https link tail
26 => https://localhost/sub1/1 https link
27 """)
29 def test_links_in_paragraph(self):
30 html = '<p>head <a href="https://127.0.0.1">https link</a> <a href="gemini://127.0.0.1">gemini link</a> tail</p>'
31 self.assertEqual(HtmlToGmi("", fn_media_url).feed(html), """\
32 head ↳ https link ↳ gemini link tail
33 => https://127.0.0.1 https link
34 => gemini://127.0.0.1 gemini link
35 """)
37 def test_in_text_tag(self):
38 html = "<p><b>bold</b> text</p>"
39 self.assertEqual(HtmlToGmi("", fn_media_url).feed(html), "bold text\n")
41 def test_img_emu(self):
42 html = "aa <img class=\"emu\" title=\":blobcatgooglyshrug:\" src=\"/d/6ytBYw515CvqFJZ8N2.png\"> bb</p>"
43 self.assertEqual(HtmlToGmi("", fn_media_url).feed(html), "aa :blobcatgooglyshrug: bb\n")
45 def test_html2gmi_header(self):
46 html = """\
47 <h1>Header 1</h1>
48 <p>Paragraph 1</p>
49 <h2>Header 1.1</h2>
50 <p>Paragraph 1.1</p>
51 """
52 self.assertEqual(HtmlToGmi("", fn_media_url).feed(html), """\
53 # Header 1
55 Paragraph 1
57 ## Header 1.1
59 Paragraph 1.1
60 """)
62 def test_html2gmi_pre(self):
63 html = """\
64 <pre>
65 def fib(n):
66 if n == 1 or n == 2:
67 return 1
68 return fib(n - 1) + fib(n - 1)
69 </pre>
70 """
71 self.assertEqual(HtmlToGmi("", fn_media_url).feed(html), """\
72 ```
74 def fib(n):
75 if n == 1 or n == 2:
76 return 1
77 return fib(n - 1) + fib(n - 1)
79 ```
80 """)
82 def test_html2gmi_description_list(self):
83 html = """
84 <dl>
85 <dt>Coffee</dt>
86 <dd>Black hot drink</dd>
87 <dt>Milk</dt>
88 <dd>White cold drink</dd>
89 </dl>
90 """
91 self.assertEqual(HtmlToGmi("", fn_media_url).feed(html), """\
92 * Coffee
94 Black hot drink
96 * Milk
98 White cold drink
99 """)
102 if __name__ == '__main__':
103 main()