forked from qingfeng/misaka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
misaka_test.py
351 lines (260 loc) · 12.6 KB
/
misaka_test.py
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
# -*- coding: utf-8 -*-
# Most unit tests are based on the tests from Redcarpet.
import re
import codecs
from os import path
from glob import glob
from subprocess import Popen, PIPE, STDOUT
import misaka
from misaka import Markdown, BaseRenderer, HtmlRenderer, SmartyPants, \
EXT_NO_INTRA_EMPHASIS, EXT_TABLES, EXT_FENCED_CODE, EXT_AUTOLINK, \
EXT_STRIKETHROUGH, EXT_LAX_SPACING, EXT_SPACE_HEADERS, \
EXT_SUPERSCRIPT, \
HTML_SKIP_HTML, HTML_SKIP_STYLE, HTML_SKIP_IMAGES, HTML_SKIP_LINKS, \
HTML_SKIP_SCRIPT, \
HTML_EXPAND_TABS, HTML_SAFELINK, HTML_TOC, HTML_HARD_WRAP, \
HTML_USE_XHTML, HTML_ESCAPE, \
HTML_SMARTYPANTS
from minitest import TestCase, ok, runner
def clean_html(dirty_html):
input_html = dirty_html.encode('utf-8')
p = Popen(['tidy', '--show-body-only', '1', '--quiet', '1', '--show-warnings', '0', '-utf8'],
stdout=PIPE, stdin=PIPE, stderr=STDOUT)
stdout, stderr = p.communicate(input=input_html)
return stdout.decode('utf-8')
class SmartyPantsTest(TestCase):
name = 'SmartyPants'
def setup(self):
self.r = lambda html: misaka.html(html, render_flags=HTML_SMARTYPANTS)
def test_single_quotes_re(self):
html = self.r('<p>They\'re not for sale.</p>\n')
ok(html).diff('<p>They’re not for sale.</p>\n')
def test_single_quotes_ll(self):
html = self.r('<p>Well that\'ll be the day</p>\n')
ok(html).diff('<p>Well that’ll be the day</p>\n')
def test_double_quotes_to_curly_quotes(self):
html = self.r('<p>"Quoted text"</p>\n')
ok(html).diff('<p>“Quoted text”</p>\n')
def test_single_quotes_ve(self):
html = self.r('<p>I\'ve been meaning to tell you ..</p>\n')
ok(html).diff('<p>I’ve been meaning to tell you ..</p>\n')
def test_single_quotes_m(self):
html = self.r('<p>I\'m not kidding</p>\n')
ok(html).diff('<p>I’m not kidding</p>\n')
def test_single_quotes_d(self):
html = self.r('<p>what\'d you say?</p>\n')
ok(html).diff('<p>what’d you say?</p>\n')
class HtmlRenderTest(TestCase):
name = 'Html Renderer'
def setup(self):
pants = SmartyPants()
self.r = {
HTML_SKIP_HTML: HtmlRenderer(HTML_SKIP_HTML),
HTML_SKIP_IMAGES: HtmlRenderer(HTML_SKIP_IMAGES),
HTML_SKIP_LINKS: HtmlRenderer(HTML_SKIP_LINKS),
HTML_SAFELINK: HtmlRenderer(HTML_SAFELINK),
HTML_ESCAPE: HtmlRenderer(HTML_ESCAPE),
HTML_HARD_WRAP: HtmlRenderer(HTML_HARD_WRAP)
}
def render_with(self, flag, text):
return Markdown(self.r[flag]).render(text)
# Hint: overrides HTML_SKIP_HTML, HTML_SKIP_IMAGES and HTML_SKIP_LINKS
def test_escape_html(self):
source = '''
Through <em>NO</em> <script>DOUBLE NO</script>
<script>BAD</script>
<img src="/favicon.ico" />
'''
expected = clean_html('''
<p>Through <em>NO</em> <script>DOUBLE NO</script></p>
<p><script>BAD</script></p>
<p><img src="/favicon.ico" /></p>
''')
markdown = clean_html(self.render_with(HTML_ESCAPE, source))
ok(markdown).diff(expected)
def test_skip_html(self):
markdown = self.render_with(HTML_SKIP_HTML, 'Through <em>NO</em> <script>DOUBLE NO</script>')
ok(markdown).diff('<p>Through NO DOUBLE NO</p>\n')
def test_skip_script(self):
markdown = self.render_with(HTML_SKIP_SCRIPT, 'Through <em>NO</em> <script>DOUBLE NO</script>')
ok(markdown).diff('<p>Through <em>NO</em> DOUBLE NO</p>\n')
def test_skip_html_two_space_hard_break(self):
markdown = self.render_with(HTML_SKIP_HTML, 'Lorem, \nipsum\n')
ok(markdown).diff('<p>Lorem,<br>\nipsum</p>\n')
def test_skip_image(self):
markdown = self.render_with(HTML_SKIP_IMAGES, '![dust mite](https://dust.mite/image.png) <img src="image.png" />')
ok(markdown).not_contains('<img')
def test_skip_links(self):
markdown = self.render_with(HTML_SKIP_LINKS, '[This link](https://example.net/) <a href="links.html">links</a>')
ok(markdown).not_contains('<a ')
def test_safelink(self):
markdown = self.render_with(HTML_SAFELINK, '[IRC](irc:https://chat.freenode.org/#freenode)')
ok(markdown).diff('<p>[IRC](irc:https://chat.freenode.org/#freenode)</p>\n')
def test_hard_wrap(self):
markdown = self.render_with(HTML_HARD_WRAP, '''
Hello world,
this is just a simple test
With hard wraps
and other *things*.''')
ok(markdown).contains('<br>')
class MarkdownParserTest(TestCase):
name = 'Markdown Parser'
def setup(self):
self.r = Markdown(HtmlRenderer()).render
def render_with(self, text, flags=0, extensions=0):
return Markdown(HtmlRenderer(), extensions).render(text)
def test_one_liner_to_html(self):
markdown = self.r('Hello World.')
ok(markdown).diff('<p>Hello World.</p>\n')
def test_inline_markdown_to_html(self):
markdown = self.r('_Hello World_!')
ok(markdown).diff('<p><em>Hello World</em>!</p>\n')
def test_inline_markdown_start_end(self):
markdown = self.render_with('_start _ foo_bar bar_baz _ end_ *italic* **bold** <a>_blah_</a>',
extensions=EXT_NO_INTRA_EMPHASIS)
ok(markdown).diff('<p><em>start _ foo_bar bar_baz _ end</em> <em>italic</em> <strong>bold</strong> <a><em>blah</em></a></p>\n')
markdown = self.r('Run \'rake radiant:extensions:rbac_base:migrate\'')
ok(markdown).diff('<p>Run 'rake radiant:extensions:rbac_base:migrate'</p>\n')
def test_urls_not_doubly_escaped(self):
markdown = self.r('[Page 2](/search?query=Markdown+Test&page=2)')
ok(markdown).diff('<p><a href="/search?query=Markdown+Test&page=2">Page 2</a></p>\n')
def test_inline_html(self):
markdown = self.r('before\n\n<div>\n foo\n</div>\n\nafter')
ok(markdown).diff('<p>before</p>\n\n<div>\n foo\n</div>\n\n<p>after</p>\n')
def test_html_block_end_tag_on_same_line(self):
markdown = self.r('Para 1\n\n<div><pre>HTML block\n</pre></div>\n\nPara 2 [Link](#anchor)')
ok(markdown).diff('<p>Para 1</p>\n\n<div><pre>HTML block\n</pre></div>\n\n<p>Para 2 <a href=\"#anchor\">Link</a></p>\n')
# This isn't in the spec but is Markdown.pl behavior.
def test_block_quotes_preceded_by_spaces(self):
markdown = self.r(
'A wise man once said:\n\n' \
' > Isn\'t it wonderful just to be alive.\n')
ok(markdown).diff(
'<p>A wise man once said:</p>\n\n' \
'<blockquote>\n<p>Isn't it wonderful just to be alive.</p>\n</blockquote>\n')
def test_html_block_not_wrapped_in_p(self):
markdown = self.render_with(
'Things to watch out for\n\n' \
'<ul>\n<li>Blah</li>\n</ul>\n',
extensions=EXT_LAX_SPACING)
ok(markdown).diff(
'<p>Things to watch out for</p>\n\n' \
'<ul>\n<li>Blah</li>\n</ul>\n')
# https://github.com/rtomayko/rdiscount/issues/#issue/13
def test_headings_with_trailing_space(self):
markdown = self.render_with(
'The Ant-Sugar Tales \n' \
'=================== \n\n' \
'By Candice Yellowflower \n')
ok(markdown).diff('<h1>The Ant-Sugar Tales </h1>\n\n<p>By Candice Yellowflower </p>\n')
def test_intra_emphasis(self):
markdown = self.r('foo_bar_baz')
ok(markdown).diff('<p>foo<em>bar</em>baz</p>\n')
markdown = self.render_with('foo_bar_baz', extensions=EXT_NO_INTRA_EMPHASIS)
ok(markdown).diff('<p>foo_bar_baz</p>\n')
def test_autolink(self):
markdown = self.render_with('https://axr.vg/', extensions=EXT_AUTOLINK)
ok(markdown).diff('<p><a href=\"https://axr.vg/\">https://axr.vg/</a></p>\n')
def test_tags_with_dashes_and_underscored(self):
markdown = self.r('foo <asdf-qwerty>bar</asdf-qwerty> and <a_b>baz</a_b>')
ok(markdown).diff('<p>foo <asdf-qwerty>bar</asdf-qwerty> and <a_b>baz</a_b></p>\n')
def test_no_link_in_code_blocks(self):
markdown = self.r(' This is a code block\n This is a link [[1]] inside\n')
ok(markdown).diff('<pre><code>This is a code block\nThis is a link [[1]] inside\n</code></pre>\n')
def test_whitespace_after_urls(self):
markdown = self.render_with('Japan: https://www.abc.net.au/news/events/japan-quake-2011/beforeafter.htm (yes, japan)',
extensions=EXT_AUTOLINK)
ok(markdown).diff('<p>Japan: <a href="https://www.abc.net.au/news/events/japan-quake-2011/beforeafter.htm">https://www.abc.net.au/news/events/japan-quake-2011/beforeafter.htm</a> (yes, japan)</p>\n')
def test_infinite_loop_in_header(self):
markdown = self.render_with(
'######\n' \
'#Body#\n' \
'######\n')
ok(markdown).diff('<h1>Body</h1>\n')
def test_tables(self):
text = ' aaa | bbbb\n' \
'-----|------\n' \
'hello|sailor\n'
ok(self.render_with(text)).not_contains('<table')
ok(self.render_with(text, extensions=EXT_TABLES)).contains('<table')
def test_strikethrough(self):
text = 'this is ~some~ striked ~~text~~'
ok(self.render_with(text)).not_contains('<del')
ok(self.render_with(text, extensions=EXT_STRIKETHROUGH)).contains('<del')
def test_fenced_code_blocks(self):
text = '''
This is a simple test
~~~~~
This is some awesome code
with tabs and shit
~~~
'''
ok(self.render_with(text)).not_contains('<code')
ok(self.render_with(text, extensions=EXT_FENCED_CODE)).contains('<code')
def test_fenced_code_blocks_without_space(self):
text = 'foo\nbar\n```\nsome\ncode\n```\nbaz'
ok(self.render_with(text)).not_contains('<pre><code>')
ok(self.render_with(text, extensions=EXT_FENCED_CODE | EXT_LAX_SPACING)).contains('<pre><code>')
def test_linkable_headers(self):
markdown = self.r('### Hello [GitHub](https://github.com)')
ok(markdown).diff('<h3>Hello <a href=\"https://github.com\">GitHub</a></h3>\n')
def test_autolinking_with_ent_chars(self):
markdown = self.render_with('This a stupid link: https://github.com/rtomayko/tilt/issues?milestone=1&state=open',
extensions=EXT_AUTOLINK)
ok(markdown).diff('<p>This a stupid link: <a href=\"https://github.com/rtomayko/tilt/issues?milestone=1&state=open\">https://github.com/rtomayko/tilt/issues?milestone=1&state=open</a></p>\n')
def test_spaced_headers(self):
text = '#123 a header yes\n'
ok(self.render_with(text, extensions=EXT_SPACE_HEADERS)).not_contains('<h1>')
class MarkdownConformanceTest_10(TestCase):
name = 'Markdown Conformance 1.0'
suite = 'MarkdownTest_1.0'
def setup(self):
self.r = Markdown(HtmlRenderer()).render
tests_dir = path.dirname(__file__)
for text_path in glob(path.join(tests_dir, self.suite, '*.text')):
html_path = '%s.html' % path.splitext(text_path)[0]
self._create_test(text_path, html_path)
def _create_test(self, text_path, html_path):
def test():
with codecs.open(text_path, 'r', encoding='utf-8') as fd:
text = fd.read()
with codecs.open(html_path, 'r', encoding='utf-8') as fd:
expected_html = fd.read()
actual_html = self.r(text)
expected_result = clean_html(expected_html)
actual_result = clean_html(actual_html)
ok(actual_result).diff(expected_result)
test.__name__ = self._test_name(text_path)
self.add_test(test)
def _test_name(self, text_path):
name = path.splitext(path.basename(text_path))[0]
name = name.replace(' - ', '_')
name = name.replace(' ', '_')
name = re.sub('[(),]', '', name)
return 'test_%s' % name.lower()
class MarkdownConformanceTest_103(MarkdownConformanceTest_10):
name = 'Markdown Conformance 1.0.3'
suite = 'MarkdownTest_1.0.3'
class UnicodeTest(TestCase):
name = 'Unicode'
def setup(self):
self.r = Markdown(HtmlRenderer()).render
def test_unicode(self):
tests_dir = path.dirname(__file__)
with codecs.open(path.join(tests_dir, 'unicode.txt'), 'r', encoding='utf-8') as fd:
text = fd.read()
with codecs.open(path.join(tests_dir, 'unicode.html'), 'r', encoding='utf-8') as fd:
html = fd.read()
markdown = self.r(text)
ok(markdown).diff(html)
def run_tests():
runner([
SmartyPantsTest,
HtmlRenderTest,
MarkdownParserTest,
MarkdownConformanceTest_10,
MarkdownConformanceTest_103,
UnicodeTest
])
if __name__ == '__main__':
run_tests()