1 6e3768be 2024-09-05 continue from unittest import TestCase, main
2 57410cb2 2024-09-04 continue from lonk import HtmlToGmi
5 4e7950e1 2024-09-30 continue def fn_media_url(mime, img_url):
6 4e7950e1 2024-09-30 continue return img_url
9 6e3768be 2024-09-05 continue class TestHtmlToGmi(TestCase):
10 5c749e57 2024-09-05 continue def test_br(self):
11 5c749e57 2024-09-05 continue html = '<p>head<br>tail</p>'
12 4e7950e1 2024-09-30 continue self.assertEqual(HtmlToGmi("https://localhost/api", fn_media_url).feed(html), """\
18 1f653a98 2024-09-05 continue def test_img_realtive(self):
19 1f653a98 2024-09-05 continue html = '<img src="/d/xxxx.jpg" title="yyy">'
20 4e7950e1 2024-09-30 continue self.assertEqual(HtmlToGmi("https://localhost/api", fn_media_url).feed(html), "=> https://localhost/d/xxxx.jpg yyy")
22 1f653a98 2024-09-05 continue def test_link_realtive(self):
23 1f653a98 2024-09-05 continue html = '<p>head <a href="/sub1/1">https link</a> tail</p>'
24 4e7950e1 2024-09-30 continue self.assertEqual(HtmlToGmi("https://localhost/api", fn_media_url).feed(html), """\
25 1f653a98 2024-09-05 continue head ↳ https link tail
26 1f653a98 2024-09-05 continue => https://localhost/sub1/1 https link
29 1be7bb9a 2024-09-05 continue def test_links_in_paragraph(self):
30 1be7bb9a 2024-09-05 continue 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 4e7950e1 2024-09-30 continue self.assertEqual(HtmlToGmi("", fn_media_url).feed(html), """\
32 1f653a98 2024-09-05 continue head ↳ https link ↳ gemini link tail
33 1be7bb9a 2024-09-05 continue => https://127.0.0.1 https link
34 1be7bb9a 2024-09-05 continue => gemini://127.0.0.1 gemini link
37 6e3768be 2024-09-05 continue def test_in_text_tag(self):
38 6e3768be 2024-09-05 continue html = "<p><b>bold</b> text</p>"
39 4e7950e1 2024-09-30 continue self.assertEqual(HtmlToGmi("", fn_media_url).feed(html), "bold text\n")
41 6e3768be 2024-09-05 continue def test_img_emu(self):
42 6e3768be 2024-09-05 continue html = "aa <img class=\"emu\" title=\":blobcatgooglyshrug:\" src=\"/d/6ytBYw515CvqFJZ8N2.png\"> bb</p>"
43 4e7950e1 2024-09-30 continue self.assertEqual(HtmlToGmi("", fn_media_url).feed(html), "aa :blobcatgooglyshrug: bb\n")
45 6e3768be 2024-09-05 continue def test_html2gmi_header(self):
46 6e3768be 2024-09-05 continue html = """\
47 6e3768be 2024-09-05 continue <h1>Header 1</h1>
48 6e3768be 2024-09-05 continue <p>Paragraph 1</p>
49 6e3768be 2024-09-05 continue <h2>Header 1.1</h2>
50 6e3768be 2024-09-05 continue <p>Paragraph 1.1</p>
52 4e7950e1 2024-09-30 continue self.assertEqual(HtmlToGmi("", fn_media_url).feed(html), """\
53 57410cb2 2024-09-04 continue # Header 1
55 57410cb2 2024-09-04 continue Paragraph 1
57 57410cb2 2024-09-04 continue ## Header 1.1
59 57410cb2 2024-09-04 continue Paragraph 1.1
62 6e3768be 2024-09-05 continue def test_html2gmi_pre(self):
63 6e3768be 2024-09-05 continue html = """\
65 57410cb2 2024-09-04 continue def fib(n):
66 57410cb2 2024-09-04 continue if n == 1 or n == 2:
67 57410cb2 2024-09-04 continue return 1
68 57410cb2 2024-09-04 continue return fib(n - 1) + fib(n - 1)
71 4e7950e1 2024-09-30 continue self.assertEqual(HtmlToGmi("", fn_media_url).feed(html), """\
74 57410cb2 2024-09-04 continue def fib(n):
75 57410cb2 2024-09-04 continue if n == 1 or n == 2:
76 57410cb2 2024-09-04 continue return 1
77 57410cb2 2024-09-04 continue return fib(n - 1) + fib(n - 1)
82 6e3768be 2024-09-05 continue def test_html2gmi_description_list(self):
83 6e3768be 2024-09-05 continue html = """
85 6e3768be 2024-09-05 continue <dt>Coffee</dt>
86 6e3768be 2024-09-05 continue <dd>Black hot drink</dd>
87 6e3768be 2024-09-05 continue <dt>Milk</dt>
88 6e3768be 2024-09-05 continue <dd>White cold drink</dd>
91 4e7950e1 2024-09-30 continue self.assertEqual(HtmlToGmi("", fn_media_url).feed(html), """\
92 57410cb2 2024-09-04 continue * Coffee
94 57410cb2 2024-09-04 continue Black hot drink
98 57410cb2 2024-09-04 continue White cold drink
102 57410cb2 2024-09-04 continue if __name__ == '__main__':