summaryrefslogtreecommitdiff
path: root/creole/tests/test_html2creole.py
blob: 2895b789def40d201f056a51a2638b695317afe2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
#!/usr/bin/env python
# coding: utf-8


"""
    html2creole tests
    ~~~~~~~~~~~~~~~~~

    special html to creole convert tests, witch can't tests in "cross compare"


    :copyleft: 2008-2011 by python-creole team, see AUTHORS for more details.
    :license: GNU GPL v3 or above, see LICENSE for more details.
"""


import unittest

from creole import html2creole
from creole.shared.unknown_tags import (
    escape_unknown_nodes,
    raise_unknown_node,
    transparent_unknown_nodes,
    use_html_macro,
)
from creole.tests.utils.base_unittest import BaseCreoleTest


class TestHtml2Creole(unittest.TestCase):
    """
    Tests around html2creole API.
    """
    pass


class TestHtml2CreoleMarkup(BaseCreoleTest):

    #    def assertCreole(self, raw_markup, raw_html, debug=False, **kwargs):
    #        self.assert_html2creole(raw_markup, raw_html, debug=debug, **kwargs)

    # --------------------------------------------------------------------------

    def test_not_used(self):
        """
        Some other html tags -> convert.
        """
        self.assert_html2creole(r"""
            **Bold text**
            **Big text**
            //em tag//
            //italic//
        """, """
            <p><b>Bold text</b><br />
            <big>Big text</big><br />
            <em>em tag</em><br />
            <i>italic</i></p>
        """)

    # --------------------------------------------------------------------------

    def test_raise_unknown_node(self):
        """
        Test creole.html2creole.raise_unknown_node callable:
        Raise NotImplementedError on unknown tags.
        """
        self.assertRaises(NotImplementedError,
                          html2creole,
                          html_string="<unknwon>",
                          unknown_emit=raise_unknown_node
                          )

    def test_use_html_macro(self):
        """
        Test creole.html2creole.use_html_macro callable:
        Use the <<html>> macro to mask unknown tags.
        """
        self.assert_html2creole(r"""
            111 <<html>><unknown><</html>>foo<<html>></unknown><</html>> 222
            333<<html>><unknown foo1="bar1" foo2="bar2"><</html>>foobar<<html>></unknown><</html>>444

            555<<html>><unknown /><</html>>666
        """, """
            <p>111 <unknown>foo</unknown> 222<br />
            333<unknown foo1="bar1" foo2="bar2">foobar</unknown>444</p>

            <p>555<unknown />666</p>
        """,
                                unknown_emit=use_html_macro
                                )

    def test_escape_unknown_nodes(self):
        """
        Test creole.html2creole.escape_unknown_nodes callable:
        All unknown tags should be escaped.
        """
        self.assert_html2creole(r"""
            111 &lt;unknown&gt;foo&lt;/unknown&gt; 222
            333&lt;unknown foo1="bar1" foo2="bar2"&gt;foobar&lt;/unknown&gt;444

            555&lt;unknown /&gt;666
        """, """
            <p>111 <unknown>foo</unknown> 222<br />
            333<unknown foo1="bar1" foo2="bar2">foobar</unknown>444</p>

            <p>555<unknown />666</p>
        """,
                                unknown_emit=escape_unknown_nodes
                                )

    def test_escape_unknown_nodes2(self):
        """
        HTMLParser has problems with <script> tags.
        See: http://bugs.python.org/issue670664
        """
        self.assert_html2creole(r"""
            &lt;script&gt;var js_sha_link='<p>***</p>';&lt;/script&gt;
        """, """
            <script>
            var js_sha_link='<p>***</p>';
            </script>
        """,
                                unknown_emit=escape_unknown_nodes
                                )

    def test_transparent_unknown_nodes(self):
        """
        Test creole.html2creole.transparent_unknown_nodes callable:
        All unknown tags should be "transparent" and show only
        their child nodes' content.
        """
        self.assert_html2creole(r"""
            //baz//, **quux**
        """, """
            <form class="foo" id="bar"><label><em>baz</em></label>, <strong>quux</strong></form>
        """, unknown_emit=transparent_unknown_nodes
                                )

    def test_transparent_unknown_nodes2(self):
        """
        HTMLParser has problems with <script> tags.
        See: http://bugs.python.org/issue670664
        """
        self.assert_html2creole(r"""
            FOO var a='<em>STRONG</em>'; BAR
        """, """
            <p>FOO <script>var a='<em>STRONG</em>';</script> BAR</p>
        """, unknown_emit=transparent_unknown_nodes
                                )

    def test_transparent_unknown_nodes_block_elements(self):
        """
        Test that block elements insert linefeeds into the stream.
        """
        self.assert_html2creole(r"""
            //baz//,

            **quux**

            spam, ham, and eggs
        """, """
            <div><em>baz</em>,</div> <fieldset><strong>quux</strong></fieldset>
            <span>spam, </span><label>ham, </label>and eggs
        """, unknown_emit=transparent_unknown_nodes
                                )

    # --------------------------------------------------------------------------

    def test_entities(self):
        """
        Test html entities.

        copyright sign is in Latin-1 Supplement:
            http://pylucid.org/_command/144/DecodeUnicode/display/1/
        Box Drawing:
            http://pylucid.org/_command/144/DecodeUnicode/display/66/
        """
        self.assert_html2creole("""
            * less-than sign: < < <
            * greater-than sign: > > >
            * copyright sign: © ©
            * box drawing: ╬ ╬
            * german umlauts: ä ö ü
        """, """
            <ul>
            <li>less-than sign: &lt; &#60; &#x3C;</li>
            <li>greater-than sign: &gt; &#62; &#x3E;</li>
            <li>copyright sign: &#169; &#xA9;</li>
            <li>box drawing: &#9580; &#x256C;</li>
            <li>german umlauts: &auml; &ouml; &uuml;</li>
            </ul>
        """)

    def test_html_entity_nbsp(self):
        """ Non breaking spaces is not in htmlentitydefs """
        self.assert_html2creole(r"""
            a non braking space: [ ] !
        """, """
            <p>a non braking space: [&nbsp;] !</p>
        """)

    def test_html_entity_in_pre(self):
        self.assert_html2creole(r"""
            {{{<code>{% lucidTag RSS url="http url" %}</code>}}}
        """, """
            <pre><code>&#x7B;% lucidTag RSS url="http url" %&#x7D;</code></pre>
        """)

    def test_unknown_entity(self):
        """
        Test a unknown html entity.
        FIXME: What sould happend?
        """
        self.assert_html2creole(r"""
            copy&paste
        """, """
            <p>copy&paste</p>
        """)
        self.assert_html2creole(r"""
            [[/url/|Search & Destroy]]
        """, """
            <a href="/url/">Search & Destroy</a>
        """)

    def test_tbody_table(self):
        self.assert_html2creole(r"""
            Ignore 'tbody' tag in tables:

            |= Headline 1 |= Headline 2 |
            | cell one    | cell two    |
            end
        """, """
            <p>Ignore 'tbody' tag in tables:</p>
            <table>
            <tbody>
            <tr>
                <th>Headline 1</th>
                <th>Headline 2</th>
            </tr>
            <tr>
                <td>cell one</td>
                <td>cell two</td>
            </tr>
            </tbody>
            </table>
            <p>end</p>
        """)

    def test_p_table(self):
        """ strip <p> tags in table cells """
        self.assert_html2creole(r"""
            | cell one | cell two\\new line |
        """, """
            <table>
            <tr>
                <td><p>cell one</p></td>
                <td><p>cell two</p><p>new line</p><p></p></td>
            </tr>
            </table>
        """)

    def test_image(self):
        """ test image tag with different alt/title attribute """
        self.assert_html2creole(r"""
            {{foobar1.jpg|foobar1.jpg}}
            {{/foobar2.jpg|foobar2.jpg}}
            {{/path1/path2/foobar3.jpg|foobar3.jpg}}
            {{/foobar4.jpg|It's foobar 4}}
            {{/foobar5.jpg|It's foobar 5}}
            {{/foobar6.jpg|a long picture title}}
        """, """
            <p><img src="foobar1.jpg" /><br />
            <img src="/foobar2.jpg" /><br />
            <img src="/path1/path2/foobar3.jpg" /><br />
            <img src="/foobar4.jpg" alt="It's foobar 4" /><br />
            <img src="/foobar5.jpg" title="It's foobar 5" /><br />
            <img src="/foobar6.jpg" alt="short name" title="a long picture title" /><br />
            <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA
            AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
            9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="data uri should be disallowed" /></p>
        """)

    def test_image_with_size(self):
        """ test image tag with sizes """
        self.assert_html2creole(r"""
            {{foobar1.jpg|foobar1.jpg}}
            {{foobar2.jpg|foobar2.jpg|90x160}}
            {{foobar3.jpg|foobar3.jpg}}
        """, """
            <p><img src="foobar1.jpg" /><br />
            <img src="foobar2.jpg" width="160" height="90" /><br />
            <img src="foobar3.jpg" width="160" /></p>
        """)

    def test_image_with_size_strict(self):
        """ test image tag with sizes """
        self.assert_html2creole(r"""
            {{foobar1.jpg|foobar1.jpg}}
            {{foobar2.jpg|foobar2.jpg}}
            {{foobar3.jpg|foobar3.jpg}}
        """, """
            <p><img src="foobar1.jpg" /><br />
            <img src="foobar2.jpg" width="160" height="90" /><br />
            <img src="foobar3.jpg" width="160" /></p>
        """, strict=True)

    def test_non_closed_br(self):
        self.assert_html2creole(r"""
            one
            two
        """, """
            <p>one<br>
            two</p>
        """)

    def test_explicit_closed_br(self):
        self.assert_html2creole(r"""
            one
            two
        """, """
            <p>one<br></br>
            two</p>
        """)

    def test_newline_before_list(self):
        """
        http://code.google.com/p/python-creole/issues/detail?id=16
        """
        self.assert_html2creole(r"""
            **foo**

            * one
        """, """
            <b>foo</b><ul><li>one</li></ul>
        """)

    def test_empty_tags_are_not_escaped(self):
        self.assert_html2creole(r"""
            //baz//, **quux**
        """, """
            <div class="foo" id="bar"><span><em>baz</em></span>, <strong>quux</strong></div>
        """)

    def test_nested_listsitems_with_paragraph(self):
        self.assert_html2creole("""
            * item 1
            ** subitem 1.1
            *** subsubitem 1.1.1
            *** subsubitem 1.1.2
            ** subitem 1.2
            * item 2
            ** subitem 2.1
            """, """
            <ul>
                <li><p>item 1</p>
                    <ul>
                        <li><p>subitem 1.1</p>
                            <ul>
                                <li>subsubitem 1.1.1</li>
                                <li>subsubitem 1.1.2</li>
                            </ul>
                        </li>
                            <li><p>subitem 1.2</p></li>
                    </ul>
                </li>
                <li><p>item 2</p>
                    <ul>
                        <li>subitem 2.1</li>
                    </ul>
                </li>
            </ul>
        """)

    def test_class_in_list(self):
        """https://code.google.com/p/python-creole/issues/detail?id=19#c4"""
        self.assert_html2creole(r"""
            # foo
        """, """
            <ol class=gbtc><li>foo</li></ol>
        """)  # , debug=True)

    def test_ignore_links_without_href(self):
        """https://code.google.com/p/python-creole/issues/detail?id=19#c4"""
        self.assert_html2creole(r"""
            bar
        """, """
            <a class="foo">bar</a>
        """)  # , debug=True)

    def test_newlines_after_headlines(self):
        self.assert_html2creole(r"""
            = Headline news

            [[http://google.com|The googlezor]] is a big bad mother.
        """, """
            <h1>Headline news</h1>

            <p><a href="http://google.com">The googlezor</a> is a big bad mother.</p>
        """)

    def test_links(self):
        self.assert_html2creole(r"""
            test link: '[[internal links|link A]]' 1 and
            test link: '[[http://domain.tld|link B]]' 2.
        """, """
            <p>test link: '<a href="internal links">link A</a>' 1 and<br />
            test link: '<a href="http://domain.tld">link B</a>' 2.</p>
        """)

    def test_horizontal_rule(self):
        self.assert_html2creole(r"""
            one

            ----

            two
        """, """
            <p>one</p>
            <hr />
            <p>two</p>
        """)

    def test_nested_empty_tags(self):
        self.assert_html2creole2("TEST", "<p>TEST</p>")
        self.assert_html2creole2("TEST", "<bar><p>TEST</p></bar>")
        self.assert_html2creole2("TEST", "<foo><bar><p>TEST</p></bar></foo>")


#    def test_nowiki1(self):
#        self.assert_html2creole(r"""
#            this:
#            {{{
#            //This// does **not** get [[formatted]]
#            }}}
#            and this: {{{** <i>this</i> ** }}} not, too.
#
#            === Closing braces in nowiki:
#            {{{
#            if (x != NULL) {
#              for (i = 0; i < size; i++) {
#                if (x[i] > 0) {
#                  x[i]--;
#              }}}
#            }}}
#        """, """
#            <p>this:</p>
#            <pre>
#            //This// does **not** get [[formatted]]
#            </pre>
#            <p>and this: <tt>** &lt;i&gt;this&lt;/i&gt; ** </tt> not, too.</p>
#
#            <h3>Closing braces in nowiki:</h3>
#            <pre>
#            if (x != NULL) {
#              for (i = 0; i &lt; size; i++) {
#                if (x[i] &gt; 0) {
#                  x[i]--;
#              }}}
#            </pre>
#        """)
#
#    def test_list1(self):
#        """
#        FIXME: Two newlines between a list and the next paragraph :(
#        """
#        self.assert_html2creole(r"""
#            ==== List a:
#            * a1 item
#            ** a1.1 Force\\linebreak
#            ** a1.2 item
#            *** a1.2.1 item
#            *** a1.2.2 item
#            * a2 item
#
#
#            list 'a' end
#
#            ==== List b:
#            # b1 item
#            ## b1.2 item
#            ### b1.2.1 item
#            ### b1.2.2 Force\\linebreak1\\linebreak2
#            ## b1.3 item
#            # b2 item
#
#
#            list 'b' end
#        """, """
#            <h4>List a:</h4>
#            <ul>
#            <li>a1 item</li>
#            <ul>
#                <li>a1.1 Force
#                linebreak</li>
#                <li>a1.2 item</li>
#                <ul>
#                    <li>a1.2.1 item</li>
#                    <li>a1.2.2 item</li>
#                </ul>
#            </ul>
#            <li>a2 item</li>
#            </ul>
#            <p>list 'a' end</p>
#
#            <h4>List b:</h4>
#            <ol>
#            <li>b1 item</li>
#            <ol>
#                <li>b1.2 item</li>
#                <ol>
#                    <li>b1.2.1 item</li>
#                    <li>b1.2.2 Force
#                    linebreak1
#                    linebreak2</li>
#                </ol>
#                <li>b1.3 item</li>
#            </ol>
#            <li>b2 item</li>
#            </ol>
#            <p>list 'b' end</p>
#        """,
# debug=True
#        )
#
#    def test_list2(self):
#        """ Bold, Italics, Links, Pre in Lists """
#        self.assert_html2creole(r"""
#            * **bold** item
#            * //italic// item
#
#            # item about a [[domain.tld|page link]]
#            # {{{ //this// is **not** [[processed]] }}}
#        """, """
#            <ul>
#                <li><strong>bold</strong> item</li>
#                <li><i>italic</i> item</li>
#            </ul>
#            <ol>
#                <li>item about a <a href="domain.tld">page link</a></li>
#                <li><tt>//this// is **not** [[processed]]</tt></li>
#            </ol>
#        """,
# debug=True
#        )


if __name__ == '__main__':
    unittest.main(
        #        defaultTest="TestHtml2CreoleMarkup.test_nested_listsitems_with_paragraph"
    )