Blame


1 6e3768be 2024-09-05 continue from unittest import TestCase, main
2 57410cb2 2024-09-04 continue from lonk import HtmlToGmi
3 57410cb2 2024-09-04 continue
4 57410cb2 2024-09-04 continue
5 4e7950e1 2024-09-30 continue def fn_media_url(mime, img_url):
6 4e7950e1 2024-09-30 continue return img_url
7 4e7950e1 2024-09-30 continue
8 4e7950e1 2024-09-30 continue
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), """\
13 5c749e57 2024-09-05 continue head
14 5c749e57 2024-09-05 continue
15 5c749e57 2024-09-05 continue tail
16 5c749e57 2024-09-05 continue """)
17 5c749e57 2024-09-05 continue
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")
21 1f653a98 2024-09-05 continue
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
27 1f653a98 2024-09-05 continue """)
28 1f653a98 2024-09-05 continue
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
35 1be7bb9a 2024-09-05 continue """)
36 1be7bb9a 2024-09-05 continue
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")
40 6e3768be 2024-09-05 continue
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")
44 6e3768be 2024-09-05 continue
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>
51 6e3768be 2024-09-05 continue """
52 4e7950e1 2024-09-30 continue self.assertEqual(HtmlToGmi("", fn_media_url).feed(html), """\
53 57410cb2 2024-09-04 continue # Header 1
54 57410cb2 2024-09-04 continue
55 57410cb2 2024-09-04 continue Paragraph 1
56 57410cb2 2024-09-04 continue
57 57410cb2 2024-09-04 continue ## Header 1.1
58 57410cb2 2024-09-04 continue
59 57410cb2 2024-09-04 continue Paragraph 1.1
60 6e3768be 2024-09-05 continue """)
61 57410cb2 2024-09-04 continue
62 6e3768be 2024-09-05 continue def test_html2gmi_pre(self):
63 6e3768be 2024-09-05 continue html = """\
64 57410cb2 2024-09-04 continue <pre>
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)
69 57410cb2 2024-09-04 continue </pre>
70 6e3768be 2024-09-05 continue """
71 4e7950e1 2024-09-30 continue self.assertEqual(HtmlToGmi("", fn_media_url).feed(html), """\
72 57410cb2 2024-09-04 continue ```
73 57410cb2 2024-09-04 continue
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)
78 57410cb2 2024-09-04 continue
79 57410cb2 2024-09-04 continue ```
80 6e3768be 2024-09-05 continue """)
81 57410cb2 2024-09-04 continue
82 6e3768be 2024-09-05 continue def test_html2gmi_description_list(self):
83 6e3768be 2024-09-05 continue html = """
84 6e3768be 2024-09-05 continue <dl>
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>
89 6e3768be 2024-09-05 continue </dl>
90 6e3768be 2024-09-05 continue """
91 4e7950e1 2024-09-30 continue self.assertEqual(HtmlToGmi("", fn_media_url).feed(html), """\
92 57410cb2 2024-09-04 continue * Coffee
93 57410cb2 2024-09-04 continue
94 57410cb2 2024-09-04 continue Black hot drink
95 57410cb2 2024-09-04 continue
96 57410cb2 2024-09-04 continue * Milk
97 57410cb2 2024-09-04 continue
98 57410cb2 2024-09-04 continue White cold drink
99 6e3768be 2024-09-05 continue """)
100 57410cb2 2024-09-04 continue
101 57410cb2 2024-09-04 continue
102 57410cb2 2024-09-04 continue if __name__ == '__main__':
103 6e3768be 2024-09-05 continue main()