f3c125235d773892f7521268cd42c125211d5f79
[nit.git] / lib / markdown / test_markdown.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Test suites for module `markdown`
16 module test_markdown is test_suite
17
18 import test_suite
19 intrude import markdown
20
21 class TestMarkdownProcessor
22 super TestSuite
23
24 fun test_process_empty do
25 var test = ""
26 var exp = ""
27 var res = test.md_to_html.write_to_string
28 assert res == exp
29 end
30
31 fun test_process_tabs do
32 var test = """
33 some code
34 """
35 var exp = """<pre><code>some code
36 </code></pre>
37 """
38 var res = test.md_to_html.write_to_string
39 assert res == exp
40 end
41
42
43 fun test_process_par1 do
44 var test = "test"
45 var exp = "<p>test</p>\n"
46 var res = test.md_to_html.write_to_string
47 assert res == exp
48 end
49
50 fun test_process_par2 do
51 var test = """
52 line1
53 line2
54
55 line3 line4
56
57 line5"""
58 var exp = """
59 <p>line1
60 line2</p>
61 <p>line3 line4</p>
62 <p>line5</p>
63 """
64 var res = test.md_to_html.write_to_string
65 assert res == exp
66 end
67
68 fun test_process_par3 do
69 var test = """
70 Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
71 Aliquam hendrerit mi posuere lectus.
72 Vestibulum enim wisi, viverra nec, fringilla in, laoreet vitae, risus.
73
74 Donec sit amet nisl. Aliquam semper ipsum sit amet velit. Suspendisse
75 id sem consectetuer libero luctus adipiscing.
76 """
77 var exp = """
78 <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
79 Aliquam hendrerit mi posuere lectus.
80 Vestibulum enim wisi, viverra nec, fringilla in, laoreet vitae, risus.</p>
81 <p>Donec sit amet nisl. Aliquam semper ipsum sit amet velit. Suspendisse
82 id sem consectetuer libero luctus adipiscing.</p>
83 """
84 var res = test.md_to_html.write_to_string
85 assert res == exp
86 end
87
88 fun test_process_headings_1 do
89 var test = """
90 This is a H1
91 =============
92
93 This is a H2
94 -------------
95 """
96 var exp = """
97 <h1 id="This_is_a_H1">This is a H1</h1>
98 <h2 id="This_is_a_H2">This is a H2</h2>
99 """
100 var res = test.md_to_html.write_to_string
101 assert res == exp
102 end
103
104 fun test_process_headings_2 do
105 var test = """
106 # This is a H1
107
108 ## This is a H2
109 ###### This is a H6
110 """
111 var exp = """
112 <h1 id="This_is_a_H1">This is a H1</h1>
113 <h2 id="This_is_a_H2">This is a H2</h2>
114 <h6 id="This_is_a_H6">This is a H6</h6>
115 """
116 var res = test.md_to_html.write_to_string
117 assert res == exp
118 end
119
120 fun test_process_headings_3 do
121 var test = """
122 # This is a H1 #
123
124 ## This is a H2 ##
125
126 ### This is a H3 ######
127 """
128 var exp = """
129 <h1 id="This_is_a_H1">This is a H1</h1>
130 <h2 id="This_is_a_H2">This is a H2</h2>
131 <h3 id="This_is_a_H3">This is a H3</h3>
132 """
133 var res = test.md_to_html.write_to_string
134 assert res == exp
135 end
136
137 fun test_process_hr do
138 var test = """
139 * * *
140
141 ***
142
143 *****
144
145 - - -
146
147 ---------------------------------------
148 """
149 var exp = "<hr/>\n<hr/>\n<hr/>\n<hr/>\n<hr/>\n"
150 var res = test.md_to_html.write_to_string
151 assert res == exp
152 end
153
154 fun test_process_bquote1 do
155 var test = """
156 > This is a blockquote with two paragraphs. Lorem ipsum dolor sit amet,
157 > consectetuer adipiscing elit. Aliquam hendrerit mi posuere lectus.
158 > Vestibulum enim wisi, viverra nec, fringilla in, laoreet vitae, risus.
159 >
160 > Donec sit amet nisl. Aliquam semper ipsum sit amet velit. Suspendisse
161 > id sem consectetuer libero luctus adipiscing.
162 """
163 var exp = """<blockquote>
164 <p>This is a blockquote with two paragraphs. Lorem ipsum dolor sit amet,
165 consectetuer adipiscing elit. Aliquam hendrerit mi posuere lectus.
166 Vestibulum enim wisi, viverra nec, fringilla in, laoreet vitae, risus.</p>
167 <p>Donec sit amet nisl. Aliquam semper ipsum sit amet velit. Suspendisse
168 id sem consectetuer libero luctus adipiscing.</p>
169 </blockquote>
170 """
171 var res = test.md_to_html.write_to_string
172 assert res == exp
173 end
174
175 fun test_process_bquote2 do
176 var test = """
177 > This is a blockquote with two paragraphs. Lorem ipsum dolor sit amet,
178 consectetuer adipiscing elit. Aliquam hendrerit mi posuere lectus.
179 Vestibulum enim wisi, viverra nec, fringilla in, laoreet vitae, risus.
180
181 > Donec sit amet nisl. Aliquam semper ipsum sit amet velit. Suspendisse
182 id sem consectetuer libero luctus adipiscing.
183 """
184 var exp = """<blockquote>
185 <p>This is a blockquote with two paragraphs. Lorem ipsum dolor sit amet,
186 consectetuer adipiscing elit. Aliquam hendrerit mi posuere lectus.
187 Vestibulum enim wisi, viverra nec, fringilla in, laoreet vitae, risus.</p>
188 <p>Donec sit amet nisl. Aliquam semper ipsum sit amet velit. Suspendisse
189 id sem consectetuer libero luctus adipiscing.</p>
190 </blockquote>
191 """
192 var res = test.md_to_html.write_to_string
193 assert res == exp
194 end
195
196 fun test_process_bquote3 do
197 var test = """
198 > This is the first level of quoting.
199 >
200 > > This is nested blockquote.
201 >
202 > Back to the first level.
203 """
204 var exp = """<blockquote>
205 <p>This is the first level of quoting.</p>
206 <blockquote>
207 <p>This is nested blockquote.</p>
208 </blockquote>
209 <p>Back to the first level.</p>
210 </blockquote>
211 """
212 var res = test.md_to_html.write_to_string
213 assert res == exp
214 end
215
216 fun test_process_list1 do
217 var test = """
218 * Red
219 * Green
220 * Blue
221 """
222 var exp = """<ul>
223 <li>Red</li>
224 <li>Green</li>
225 <li>Blue</li>
226 </ul>
227 """
228 var res = test.md_to_html.write_to_string
229 assert res == exp
230 end
231
232 fun test_process_list2 do
233 var test = """
234 + Red
235 + Green
236 + Blue
237 """
238 var exp = """<ul>
239 <li>Red</li>
240 <li>Green</li>
241 <li>Blue</li>
242 </ul>
243 """
244 var res = test.md_to_html.write_to_string
245 assert res == exp
246 end
247
248 fun test_process_list3 do
249 var test = """
250 - Red
251 - Green
252 - Blue
253 """
254 var exp = """<ul>
255 <li>Red</li>
256 <li>Green</li>
257 <li>Blue</li>
258 </ul>
259 """
260 var res = test.md_to_html.write_to_string
261 assert res == exp
262 end
263
264 fun test_process_list4 do
265 var test = """
266 1. Bird
267 2. McHale
268 3. Parish
269 """
270 var exp = """<ol>
271 <li>Bird</li>
272 <li>McHale</li>
273 <li>Parish</li>
274 </ol>
275 """
276 var res = test.md_to_html.write_to_string
277 assert res == exp
278 end
279
280 fun test_process_list5 do
281 var test = """
282 3. Bird
283 1. McHale
284 8. Parish
285 """
286 var exp = """<ol>
287 <li>Bird</li>
288 <li>McHale</li>
289 <li>Parish</li>
290 </ol>
291 """
292 var res = test.md_to_html.write_to_string
293 assert res == exp
294 end
295
296 fun test_process_list6 do
297 var test = """
298 * Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
299 Aliquam hendrerit mi posuere lectus. Vestibulum enim wisi,
300 viverra nec, fringilla in, laoreet vitae, risus.
301 * Donec sit amet nisl. Aliquam semper ipsum sit amet velit.
302 Suspendisse id sem consectetuer libero luctus adipiscing.
303 """
304 var exp = """
305 <ul>
306 <li>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
307 Aliquam hendrerit mi posuere lectus. Vestibulum enim wisi,
308 viverra nec, fringilla in, laoreet vitae, risus.</li>
309 <li>Donec sit amet nisl. Aliquam semper ipsum sit amet velit.
310 Suspendisse id sem consectetuer libero luctus adipiscing.</li>
311 </ul>
312 """
313 var res = test.md_to_html.write_to_string
314 assert res == exp
315 end
316
317 fun test_process_list7 do
318 var test = """
319 * Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
320 Aliquam hendrerit mi posuere lectus. Vestibulum enim wisi,
321 viverra nec, fringilla in, laoreet vitae, risus.
322 * Donec sit amet nisl. Aliquam semper ipsum sit amet velit.
323 Suspendisse id sem consectetuer libero luctus adipiscing.
324 """
325 var exp = """
326 <ul>
327 <li>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
328 Aliquam hendrerit mi posuere lectus. Vestibulum enim wisi,
329 viverra nec, fringilla in, laoreet vitae, risus.</li>
330 <li>Donec sit amet nisl. Aliquam semper ipsum sit amet velit.
331 Suspendisse id sem consectetuer libero luctus adipiscing.</li>
332 </ul>
333 """
334 var res = test.md_to_html.write_to_string
335 assert res == exp
336 end
337
338 fun test_process_list8 do
339 var test = """
340 * Bird
341
342 * Magic
343 """
344 var exp = """
345 <ul>
346 <li><p>Bird</p>
347 </li>
348 <li><p>Magic</p>
349 </li>
350 </ul>
351 """
352 var res = test.md_to_html.write_to_string
353 assert res == exp
354 end
355
356 fun test_process_list9 do
357 var test = """
358 1. This is a list item with two paragraphs. Lorem ipsum dolor
359 sit amet, consectetuer adipiscing elit. Aliquam hendrerit
360 mi posuere lectus.
361
362 Vestibulum enim wisi, viverra nec, fringilla in, laoreet
363 vitae, risus. Donec sit amet nisl. Aliquam semper ipsum
364 sit amet velit.
365
366 2. Suspendisse id sem consectetuer libero luctus adipiscing.
367 """
368 var exp = """
369 <ol>
370 <li><p>This is a list item with two paragraphs. Lorem ipsum dolor
371 sit amet, consectetuer adipiscing elit. Aliquam hendrerit
372 mi posuere lectus.</p>
373 <p>Vestibulum enim wisi, viverra nec, fringilla in, laoreet
374 vitae, risus. Donec sit amet nisl. Aliquam semper ipsum
375 sit amet velit.</p>
376 </li>
377 <li><p>Suspendisse id sem consectetuer libero luctus adipiscing.</p>
378 </li>
379 </ol>
380 """
381 var res = test.md_to_html.write_to_string
382 assert res == exp
383 end
384
385 fun test_process_list10 do
386 var test = """
387 * This is a list item with two paragraphs.
388
389 This is the second paragraph in the list item. You're
390 only required to indent the first line. Lorem ipsum dolor
391 sit amet, consectetuer adipiscing elit.
392
393 * Another item in the same list.
394 """
395 var exp = """
396 <ul>
397 <li><p>This is a list item with two paragraphs.</p>
398 <p>This is the second paragraph in the list item. You're
399 only required to indent the first line. Lorem ipsum dolor
400 sit amet, consectetuer adipiscing elit.</p>
401 </li>
402 <li><p>Another item in the same list.</p>
403 </li>
404 </ul>
405 """
406 var res = test.md_to_html.write_to_string
407 assert res == exp
408 end
409
410 fun test_process_code1 do
411 var test = """
412 This is a normal paragraph:
413
414 This is a code block.
415 """
416 var exp = """<p>This is a normal paragraph:</p>
417 <pre><code>This is a code block.
418 </code></pre>
419 """
420 var res = test.md_to_html.write_to_string
421 assert res == exp
422 end
423
424 fun test_process_code2 do
425 var test = """
426 Here is an example of AppleScript:
427
428 tell application "Foo"
429 beep
430 end tell
431
432 <div class="footer">
433 &copy; 2004 Foo Corporation
434 </div>
435 """
436 var exp = """
437 <p>Here is an example of AppleScript:</p>
438 <pre><code>tell application "Foo"
439 beep
440 end tell
441
442 &lt;div class="footer"&gt;
443 &amp;copy; 2004 Foo Corporation
444 &lt;/div&gt;
445 </code></pre>
446 """
447 var res = test.md_to_html.write_to_string
448 assert res == exp
449 end
450
451 fun test_process_nesting1 do
452 var test = """
453 > ## This is a header.
454 >
455 > 1. This is the first list item.
456 > 2. This is the second list item.
457 >
458 > Here's some example code:
459 >
460 > return shell_exec("echo $input | $markdown_script");
461 """
462 var exp = """
463 <blockquote>
464 <h2 id="This_is_a_header.">This is a header.</h2>
465 <ol>
466 <li>This is the first list item.</li>
467 <li>This is the second list item.</li>
468 </ol>
469 <p>Here's some example code:</p>
470 <pre><code>return shell_exec("echo $input | $markdown_script");
471 </code></pre>
472 </blockquote>
473 """
474 var res = test.md_to_html.write_to_string
475 assert res == exp
476 end
477
478 fun test_process_nesting2 do
479 var test = """
480 * A list item with a blockquote:
481
482 > This is a blockquote
483 > inside a list item.
484 """
485 var exp = """
486 <ul>
487 <li><p>A list item with a blockquote:</p>
488 <blockquote>
489 <p>This is a blockquote
490 inside a list item.</p>
491 </blockquote>
492 </li>
493 </ul>
494 """
495 var res = test.md_to_html.write_to_string
496 assert res == exp
497 end
498
499 fun test_process_nesting3 do
500 var test = """
501 * A list item with a code block:
502
503 <code goes here>
504 """
505 var exp = """
506 <ul>
507 <li><p>A list item with a code block:</p>
508 <pre><code>&lt;code goes here&gt;
509 </code></pre>
510 </li>
511 </ul>
512 """
513 var res = test.md_to_html.write_to_string
514 assert res == exp
515 end
516
517 fun test_process_nesting4 do
518 var test = """
519 * Tab
520 * Tab
521 * Tab
522 """
523 var exp = """
524 <ul>
525 <li>Tab<ul>
526 <li>Tab<ul>
527 <li>Tab</li>
528 </ul>
529 </li>
530 </ul>
531 </li>
532 </ul>
533 """
534 var res = test.md_to_html.write_to_string
535 assert res == exp
536 end
537
538 # TODO
539 # fun test_process_nesting5 do
540 # var test = """
541 # * this
542 #
543 # * sub
544 #
545 # that
546 # """
547 # var exp = """
548 # <ul>
549 # <li><p>this</p>
550 # <ul>
551 # <li>sub</li>
552 # </ul>
553 # <p>that</p>
554 # </li>
555 # </ul>
556 # """
557 # var res = test.md_to_html.write_to_string
558 # assert res == exp
559 # end
560
561 fun test_process_emph1 do
562 var test = """
563 *single asterisks*
564
565 _single underscores_
566
567 **double asterisks**
568
569 __double underscores__
570 """
571 var exp = """<p><em>single asterisks</em></p>
572 <p><em>single underscores</em></p>
573 <p><strong>double asterisks</strong></p>
574 <p><strong>double underscores</strong></p>
575 """
576 var res = test.md_to_html.write_to_string
577 assert res == exp
578 end
579
580 fun test_process_emph2 do
581 var test = "un*frigging*believable"
582 var exp = "<p>un<em>frigging</em>believable</p>\n"
583 var res = test.md_to_html.write_to_string
584 assert res == exp
585 end
586
587 fun test_process_xml1 do
588 var test = """
589 This is a regular paragraph.
590
591 <table>
592 <tr>
593 <td>Foo</td>
594 </tr>
595 </table>
596
597 This is another regular paragraph.
598 """
599 var exp = """
600 <p>This is a regular paragraph.</p>
601 <table>
602 <tr>
603 <td>Foo</td>
604 </tr>
605 </table>
606 <p>This is another regular paragraph.</p>
607 """
608 var res = test.md_to_html.write_to_string
609 assert res == exp
610 end
611
612 fun test_process_xml2 do
613 var test = """
614 This is an image <img src="foo/bar" alt="baz"/> in a regular paragraph.
615 """
616 var exp = """<p>This is an image <img src="foo/bar" alt="baz" /> in a regular paragraph.</p>
617 """
618 var res = test.md_to_html.write_to_string
619 assert res == exp
620 end
621
622 fun test_process_xml3 do
623 var test = """
624 <div style=">"/>
625 """
626 var exp = """
627 <div style=">"/>
628 """
629 var res = test.md_to_html.write_to_string
630 assert res == exp
631 end
632
633 fun test_process_span_code1 do
634 var test = "Use the `printf()` function."
635 var exp = "<p>Use the <code>printf()</code> function.</p>\n"
636 var res = test.md_to_html.write_to_string
637 assert res == exp
638 end
639
640 fun test_process_span_code2 do
641 var test = "``There is a literal backtick (`) here.``"
642 var exp = "<p><code>There is a literal backtick (`) here.</code></p>\n"
643 var res = test.md_to_html.write_to_string
644 assert res == exp
645 end
646
647 fun test_process_span_code3 do
648 var test = """
649 A single backtick in a code span: `` ` ``
650
651 A backtick-delimited string in a code span: `` `foo` ``
652 """
653 var exp = """
654 <p>A single backtick in a code span: <code>`</code></p>
655 <p>A backtick-delimited string in a code span: <code>`foo`</code></p>
656 """
657 var res = test.md_to_html.write_to_string
658 assert res == exp
659 end
660
661 fun test_process_span_code4 do
662 var test = "Please don't use any `<blink>` tags."
663 var exp = "<p>Please don't use any <code>&lt;blink&gt;</code> tags.</p>\n"
664 var res = test.md_to_html.write_to_string
665 assert res == exp
666 end
667
668 fun test_process_span_code5 do
669 var test = "`&#8212;` is the decimal-encoded equivalent of `&mdash;`."
670 var exp = "<p><code>&amp;#8212;</code> is the decimal-encoded equivalent of <code>&amp;mdash;</code>.</p>\n"
671 var res = test.md_to_html.write_to_string
672 assert res == exp
673 end
674
675 fun test_process_escape1 do
676 var test = "\\*this text is surrounded by literal asterisks\\*"
677 var exp = "<p>*this text is surrounded by literal asterisks*</p>\n"
678 var res = test.md_to_html.write_to_string
679 assert res == exp
680 end
681
682 fun test_process_escape2 do
683 var test = "1986\\. What a great season."
684 var exp = "<p>1986. What a great season.</p>\n"
685 var res = test.md_to_html.write_to_string
686 assert res == exp
687 end
688
689 fun test_process_escape3 do
690 var test = "Ben & Lux"
691 var exp = "<p>Ben &amp; Lux</p>\n"
692 var res = test.md_to_html.write_to_string
693 assert res == exp
694 end
695
696 fun test_process_link1 do
697 var test = """
698 This is [an example](http://example.com/ "Title") inline link.
699
700 [This link](http://example.net/) has no title attribute.
701 """
702 var exp = """<p>This is <a href="http://example.com/" title="Title">an example</a> inline link.</p>
703 <p><a href="http://example.net/">This link</a> has no title attribute.</p>
704 """
705 var res = test.md_to_html.write_to_string
706 assert res == exp
707 end
708
709 fun test_process_link2 do
710 var test = "See my [About](/about/) page for details."
711 var exp = "<p>See my <a href=\"/about/\">About</a> page for details.</p>\n"
712 var res = test.md_to_html.write_to_string
713 assert res == exp
714 end
715
716 fun test_process_link3 do
717 var test = """
718 This is [an example][id] reference-style link.
719
720 This is [an example] [id] reference-style link.
721
722 Some lorem ipsum
723
724 [id]: http://example.com/ "Optional Title Here"
725
726 Some other lipsum
727 """
728 var exp = """
729 <p>This is <a href="http://example.com/" title="Optional Title Here">an example</a> reference-style link.</p>
730 <p>This is <a href="http://example.com/" title="Optional Title Here">an example</a> reference-style link.</p>
731 <p>Some lorem ipsum</p>
732 <p>Some other lipsum</p>
733 """
734 var res = test.md_to_html.write_to_string
735 assert res == exp
736 end
737
738 fun test_process_link4 do
739 var test = """
740 This is multiple examples: [foo][1], [bar][2], [baz][3].
741
742 [1]: http://example.com/ "Optional Title Here"
743 [2]: http://example.com/ 'Optional Title Here'
744 [3]: http://example.com/ (Optional Title Here)
745 """
746 var exp = """
747 <p>This is multiple examples: <a href="http://example.com/" title="Optional Title Here">foo</a>, <a href="http://example.com/" title="Optional Title Here">bar</a>, <a href="http://example.com/" title="Optional Title Here">baz</a>.</p>
748 """
749 var res = test.md_to_html.write_to_string
750 assert res == exp
751 end
752
753 fun test_process_link5 do
754 var test = """
755 This is multiple examples: [foo][a], [bar][A], [a].
756
757 [a]: http://example.com/ "Optional Title Here"
758 """
759 var exp = """<p>This is multiple examples: <a href="http://example.com/" title="Optional Title Here">foo</a>, <a href="http://example.com/" title="Optional Title Here">bar</a>, <a href="http://example.com/" title="Optional Title Here">a</a>.</p>
760 """
761 var res = test.md_to_html.write_to_string
762 assert res == exp
763 end
764
765 fun test_process_link6 do
766 var test = """
767 I get 10 times more traffic from [Google][] than from [Yahoo][] or [MSN][].
768
769 [Google]: http://google.com/ "Google"
770 [Yahoo]: http://search.yahoo.com/ "Yahoo Search"
771 [MSN]: http://search.msn.com/ "MSN Search"
772 """
773 var exp = """<p>I get 10 times more traffic from <a href="http://google.com/" title="Google">Google</a> than from <a href="http://search.yahoo.com/" title="Yahoo Search">Yahoo</a> or <a href="http://search.msn.com/" title="MSN Search">MSN</a>.</p>
774 """
775 var res = test.md_to_html.write_to_string
776 assert res == exp
777 end
778
779 fun test_process_link7 do
780 var test = """
781 Visit [Daring Fireball][] for more information.
782
783 [Daring Fireball]: http://daringfireball.net/
784 """
785 var exp = """<p>Visit <a href="http://daringfireball.net/">Daring Fireball</a> for more information.</p>
786 """
787 var res = test.md_to_html.write_to_string
788 assert res == exp
789 end
790
791 fun test_process_link8 do
792 var test = """
793 This one has a [line
794 break].
795
796 This one has a [line
797 break] with a line-ending space.
798
799 [line break]: /foo
800 """
801 var exp = """
802 <p>This one has a <a href="/foo">line
803 break</a>.</p>
804 <p>This one has a <a href="/foo">line
805 break</a> with a line-ending space.</p>
806 """
807 var res = test.md_to_html.write_to_string
808 assert res == exp
809 end
810
811 # FIXME unignore test once escape strings fixed
812 # fun test_process_link9 do
813 # var test = """
814 # Foo [bar][].
815 #
816 # Foo [bar](/url/ "Title with \"quotes\" inside").
817 #
818 #
819 # [bar]: /url/ "Title with \"quotes\" inside"
820 # """
821 # var exp = """
822 # <p>Foo <a href="/url/" title="Title with &quot;quotes&quot; inside">bar</a>.</p>
823 # <p>Foo <a href="/url/" title="Title with &quot;quotes&quot; inside">bar</a>.</p>
824 # """
825 # var res = test.md_to_html.write_to_string
826 # assert res == exp
827 # end
828
829 fun test_process_img1 do
830 var test = """
831 ![Alt text](/path/to/img.jpg)
832
833 ![Alt text](/path/to/img.jpg "Optional title")
834 """
835 var exp = """<p><img src="/path/to/img.jpg" alt="Alt text"/></p>
836 <p><img src="/path/to/img.jpg" alt="Alt text" title="Optional title"/></p>
837 """
838 var res = test.md_to_html.write_to_string
839 assert res == exp
840 end
841
842 fun test_process_img2 do
843 var test = """
844 ![Alt text][id]
845
846 [id]: url/to/image "Optional title attribute"
847 """
848 var exp = """<p><img src="url/to/image" alt="Alt text" title="Optional title attribute"/></p>
849 """
850 var res = test.md_to_html.write_to_string
851 assert res == exp
852 end
853
854 fun test_daring_encoding do
855 var test = """
856 AT&T has an ampersand in their name.
857
858 AT&amp;T is another way to write it.
859
860 This & that.
861
862 4 < 5.
863
864 6 > 5.
865
866 Here's a [link] [1] with an ampersand in the URL.
867
868 Here's a link with an amersand in the link text: [AT&T] [2].
869
870 Here's an inline [link](/script?foo=1&bar=2).
871
872 Here's an inline [link](</script?foo=1&bar=2>).
873
874
875 [1]: http://example.com/?foo=1&bar=2
876 [2]: http://att.com/ "AT&T"
877 """
878
879 var exp = """
880 <p>AT&amp;T has an ampersand in their name.</p>
881 <p>AT&amp;T is another way to write it.</p>
882 <p>This &amp; that.</p>
883 <p>4 &lt; 5.</p>
884 <p>6 > 5.</p>
885 <p>Here's a <a href="http://example.com/?foo=1&amp;bar=2">link</a> with an ampersand in the URL.</p>
886 <p>Here's a link with an amersand in the link text: <a href="http://att.com/" title="AT&amp;T">AT&amp;T</a>.</p>
887 <p>Here's an inline <a href="/script?foo=1&amp;bar=2">link</a>.</p>
888 <p>Here's an inline <a href="/script?foo=1&amp;bar=2">link</a>.</p>
889 """
890 var res = test.md_to_html.write_to_string
891 assert res == exp
892
893 end
894
895 fun test_daring_autolinks do
896 var test = """
897 Link: <http://example.com/>.
898
899 With an ampersand: <http://example.com/?foo=1&bar=2>
900
901 * In a list?
902 * <http://example.com/>
903 * It should.
904
905 > Blockquoted: <http://example.com/>
906
907 Auto-links should not occur here: `<http://example.com/>`
908
909 or here: <http://example.com/>
910 """
911
912 var exp = """
913 <p>Link: <a href="http://example.com/">http://example.com/</a>.</p>
914 <p>With an ampersand: <a href="http://example.com/?foo=1&amp;bar=2">http://example.com/?foo=1&amp;bar=2</a></p>
915 <ul>
916 <li>In a list?</li>
917 <li><a href="http://example.com/">http://example.com/</a></li>
918 <li>It should.</li>
919 </ul>
920 <blockquote>
921 <p>Blockquoted: <a href="http://example.com/">http://example.com/</a></p>
922 </blockquote>
923 <p>Auto-links should not occur here: <code>&lt;http://example.com/&gt;</code></p>
924 <pre><code>or here: &lt;http://example.com/&gt;
925 </code></pre>
926 """
927 var res = test.md_to_html.write_to_string
928 assert res == exp
929 end
930
931 fun test_daring_escape do
932 var test = """
933 These should all get escaped:
934
935 Backslash: \\
936
937 Backtick: \`
938
939 Asterisk: \*
940
941 Underscore: \_
942
943 Left brace: \{
944
945 Right brace: \}
946
947 Left bracket: \[
948
949 Right bracket: \]
950
951 Left paren: \(
952
953 Right paren: \)
954
955 Greater-than: \>
956
957 Hash: \#
958
959 Period: \.
960
961 Bang: \!
962
963 Plus: \+
964
965 Minus: \-
966
967
968 These should not, because they occur within a code block:
969
970 Backslash: \\
971
972 Backtick: \`
973
974 Asterisk: \*
975
976 Underscore: \_
977
978 Left brace: \{
979
980 Right brace: \}
981
982 Left bracket: \[
983
984 Right bracket: \]
985
986 Left paren: \(
987
988 Right paren: \)
989
990 Greater-than: \>
991
992 Hash: \#
993
994 Period: \.
995
996 Bang: \!
997
998 Plus: \+
999
1000 Minus: \-
1001
1002 Nor should these, which occur in code spans:
1003
1004 Backslash: `\\`
1005
1006 Backtick: `` \` ``
1007
1008 Asterisk: `\*`
1009
1010 Underscore: `\_`
1011
1012 Left brace: `\{`
1013
1014 Right brace: `\}`
1015
1016 Left bracket: `\[`
1017
1018 Right bracket: `\]`
1019
1020 Left paren: `\(`
1021
1022 Right paren: `\)`
1023
1024 Greater-than: `\>`
1025
1026 Hash: `\#`
1027
1028 Period: `\.`
1029
1030 Bang: `\!`
1031
1032 Plus: `\+`
1033
1034 Minus: `\-`
1035
1036 These should get escaped, even though they're matching pairs for
1037 other Markdown constructs:
1038
1039 \\\*asterisks\\\*
1040
1041 \\\_underscores\\\_
1042
1043 \\\`backticks\\\`
1044
1045 This is a code span with a literal backslash-backtick sequence: `` \` ``
1046
1047 This is a tag with unescaped backticks <span attr='`ticks`'>bar</span>.
1048
1049 This is a tag with backslashes <span attr='\\\\backslashes\\\\'>bar</span>.
1050 """
1051 var exp = """
1052 <p>These should all get escaped:</p>
1053 <p>Backslash: \\</p>
1054 <p>Backtick: \`</p>
1055 <p>Asterisk: \*</p>
1056 <p>Underscore: \_</p>
1057 <p>Left brace: \{</p>
1058 <p>Right brace: \}</p>
1059 <p>Left bracket: \[</p>
1060 <p>Right bracket: \]</p>
1061 <p>Left paren: \(</p>
1062 <p>Right paren: \)</p>
1063 <p>Greater-than: \></p>
1064 <p>Hash: \#</p>
1065 <p>Period: \.</p>
1066 <p>Bang: \!</p>
1067 <p>Plus: \+</p>
1068 <p>Minus: \-</p>
1069 <p>These should not, because they occur within a code block:</p>
1070 <pre><code>Backslash: \\
1071
1072 Backtick: \`
1073
1074 Asterisk: \*
1075
1076 Underscore: \_
1077
1078 Left brace: \{
1079
1080 Right brace: \}
1081
1082 Left bracket: \[
1083
1084 Right bracket: \]
1085
1086 Left paren: \(
1087
1088 Right paren: \)
1089
1090 Greater-than: \&gt;
1091
1092 Hash: \#
1093
1094 Period: \.
1095
1096 Bang: \!
1097
1098 Plus: \+
1099
1100 Minus: \-
1101 </code></pre>
1102 <p>Nor should these, which occur in code spans:</p>
1103 <p>Backslash: <code>\\</code></p>
1104 <p>Backtick: <code>\`</code></p>
1105 <p>Asterisk: <code>\*</code></p>
1106 <p>Underscore: <code>\_</code></p>
1107 <p>Left brace: <code>\{</code></p>
1108 <p>Right brace: <code>\}</code></p>
1109 <p>Left bracket: <code>\[</code></p>
1110 <p>Right bracket: <code>\]</code></p>
1111 <p>Left paren: <code>\(</code></p>
1112 <p>Right paren: <code>\)</code></p>
1113 <p>Greater-than: <code>\&gt;</code></p>
1114 <p>Hash: <code>\#</code></p>
1115 <p>Period: <code>\.</code></p>
1116 <p>Bang: <code>\!</code></p>
1117 <p>Plus: <code>\+</code></p>
1118 <p>Minus: <code>\-</code></p>
1119 <p>These should get escaped, even though they're matching pairs for
1120 other Markdown constructs:</p>
1121 <p>*asterisks*</p>
1122 <p>_underscores_</p>
1123 <p>`backticks`</p>
1124 <p>This is a code span with a literal backslash-backtick sequence: <code>\`</code></p>
1125 <p>This is a tag with unescaped backticks <span attr='`ticks`'>bar</span>.</p>
1126 <p>This is a tag with backslashes <span attr='\\\\backslashes\\\\'>bar</span>.</p>
1127 """
1128
1129 var res = test.md_to_html.write_to_string
1130 assert res == exp
1131 end
1132
1133 fun test_daring_blockquotes do
1134 var test = """
1135 > Example:
1136 >
1137 > sub status {
1138 > print "working";
1139 > }
1140 >
1141 > Or:
1142 >
1143 > sub status {
1144 > return "working";
1145 > }
1146 """
1147
1148 var exp = """
1149 <blockquote>
1150 <p>Example:</p>
1151 <pre><code>sub status {
1152 print "working";
1153 }
1154 </code></pre>
1155 <p>Or:</p>
1156 <pre><code>sub status {
1157 return "working";
1158 }
1159 </code></pre>
1160 </blockquote>
1161 """
1162 var res = test.md_to_html.write_to_string
1163 assert res == exp
1164 end
1165
1166 fun test_daring_code_blocks do
1167 var test = """
1168 code block on the first line
1169
1170 Regular text.
1171
1172 code block indented by spaces
1173
1174 Regular text.
1175
1176 the lines in this block
1177 all contain trailing spaces
1178
1179 Regular Text.
1180
1181 code block on the last line
1182 """
1183
1184 var exp = """
1185 <pre><code>code block on the first line
1186 </code></pre>
1187 <p>Regular text.</p>
1188 <pre><code>code block indented by spaces
1189 </code></pre>
1190 <p>Regular text.</p>
1191 <pre><code>the lines in this block
1192 all contain trailing spaces
1193 </code></pre>
1194 <p>Regular Text.</p>
1195 <pre><code>code block on the last line
1196 </code></pre>
1197 """
1198 var res = test.md_to_html.write_to_string
1199 assert res == exp
1200 end
1201
1202 fun test_daring_code_spans do
1203 var test = """
1204 `<test a="` content of attribute `">`
1205
1206 Fix for backticks within HTML tag: <span attr='`ticks`'>like this</span>
1207
1208 Here's how you put `` `backticks` `` in a code span.
1209 """
1210
1211 var exp = """
1212 <p><code>&lt;test a="</code> content of attribute <code>"&gt;</code></p>
1213 <p>Fix for backticks within HTML tag: <span attr='`ticks`'>like this</span></p>
1214 <p>Here's how you put <code>`backticks`</code> in a code span.</p>
1215 """
1216 var res = test.md_to_html.write_to_string
1217 assert res == exp
1218 end
1219
1220 fun test_daring_pars do
1221 var test = """
1222 In Markdown 1.0.0 and earlier. Version
1223 8. This line turns into a list item.
1224 Because a hard-wrapped line in the
1225 middle of a paragraph looked like a
1226 list item.
1227
1228 Here's one with a bullet.
1229 * criminey.
1230 """
1231
1232 var exp = """
1233 <p>In Markdown 1.0.0 and earlier. Version
1234 8. This line turns into a list item.
1235 Because a hard-wrapped line in the
1236 middle of a paragraph looked like a
1237 list item.</p>
1238 <p>Here's one with a bullet.
1239 * criminey.</p>
1240 """
1241 var res = test.md_to_html.write_to_string
1242 assert res == exp
1243 end
1244
1245 fun test_daring_rules do
1246 var test = """
1247 Dashes:
1248
1249 ---
1250
1251 ---
1252
1253 ---
1254
1255 ---
1256
1257 ---
1258
1259 - - -
1260
1261 - - -
1262
1263 - - -
1264
1265 - - -
1266
1267 - - -
1268
1269
1270 Asterisks:
1271
1272 ***
1273
1274 ***
1275
1276 ***
1277
1278 ***
1279
1280 ***
1281
1282 * * *
1283
1284 * * *
1285
1286 * * *
1287
1288 * * *
1289
1290 * * *
1291
1292
1293 Underscores:
1294
1295 ___
1296
1297 ___
1298
1299 ___
1300
1301 ___
1302
1303 ___
1304
1305 _ _ _
1306
1307 _ _ _
1308
1309 _ _ _
1310
1311 _ _ _
1312
1313 _ _ _
1314 """
1315
1316 var exp = """
1317 <p>Dashes:</p>
1318 <hr/>
1319 <hr/>
1320 <hr/>
1321 <hr/>
1322 <pre><code>---
1323 </code></pre>
1324 <hr/>
1325 <hr/>
1326 <hr/>
1327 <hr/>
1328 <pre><code>- - -
1329 </code></pre>
1330 <p>Asterisks:</p>
1331 <hr/>
1332 <hr/>
1333 <hr/>
1334 <hr/>
1335 <pre><code>***
1336 </code></pre>
1337 <hr/>
1338 <hr/>
1339 <hr/>
1340 <hr/>
1341 <pre><code>* * *
1342 </code></pre>
1343 <p>Underscores:</p>
1344 <hr/>
1345 <hr/>
1346 <hr/>
1347 <hr/>
1348 <pre><code>___
1349 </code></pre>
1350 <hr/>
1351 <hr/>
1352 <hr/>
1353 <hr/>
1354 <pre><code>_ _ _
1355 </code></pre>
1356 """
1357 var res = test.md_to_html.write_to_string
1358 assert res == exp
1359 end
1360
1361 fun test_daring_images do
1362 var test = """
1363 ![Alt text](/path/to/img.jpg)
1364
1365 ![Alt text](/path/to/img.jpg "Optional title")
1366
1367 Inline within a paragraph: [alt text](/url/).
1368
1369 ![alt text](/url/ "title preceded by two spaces")
1370
1371 ![alt text](/url/ "title has spaces afterward" )
1372
1373 ![alt text](</url/>)
1374
1375 ![alt text](</url/> "with a title").
1376
1377 ![Empty]()
1378
1379 ![this is a stupid URL](http://example.com/(parens).jpg)
1380
1381
1382 ![alt text][foo]
1383
1384 [foo]: /url/
1385
1386 ![alt text][bar]
1387
1388 [bar]: /url/ "Title here"
1389 """
1390
1391 var exp = """
1392 <p><img src="/path/to/img.jpg" alt="Alt text"/></p>
1393 <p><img src="/path/to/img.jpg" alt="Alt text" title="Optional title"/></p>
1394 <p>Inline within a paragraph: <a href="/url/">alt text</a>.</p>
1395 <p><img src="/url/" alt="alt text" title="title preceded by two spaces"/></p>
1396 <p><img src="/url/" alt="alt text" title="title has spaces afterward"/></p>
1397 <p><img src="/url/" alt="alt text"/></p>
1398 <p><img src="/url/" alt="alt text" title="with a title"/>.</p>
1399 <p><img src="" alt="Empty"/></p>
1400 <p><img src="http://example.com/(parens).jpg" alt="this is a stupid URL"/></p>
1401 <p><img src="/url/" alt="alt text"/></p>
1402 <p><img src="/url/" alt="alt text" title="Title here"/></p>
1403 """
1404 var res = test.md_to_html.write_to_string
1405 assert res == exp
1406 end
1407
1408 fun test_daring_inline_html1 do
1409 var test = """
1410 Here's a simple block:
1411
1412 <div>
1413 foo
1414 </div>
1415
1416 This should be a code block, though:
1417
1418 <div>
1419 foo
1420 </div>
1421
1422 As should this:
1423
1424 <div>foo</div>
1425
1426 Now, nested:
1427
1428 <div>
1429 <div>
1430 <div>
1431 foo
1432 </div>
1433 </div>
1434 </div>
1435
1436 This should just be an HTML comment:
1437
1438 <!-- Comment -->
1439
1440 Multiline:
1441
1442 <!--
1443 Blah
1444 Blah
1445 -->
1446
1447 Code block:
1448
1449 <!-- Comment -->
1450
1451 Just plain comment, with trailing spaces on the line:
1452
1453 <!-- foo -->
1454
1455 Code:
1456
1457 <hr />
1458
1459 Hr's:
1460
1461 <hr>
1462
1463 <hr/>
1464
1465 <hr />
1466
1467 <hr>
1468
1469 <hr/>
1470
1471 <hr />
1472
1473 <hr class="foo" id="bar" />
1474
1475 <hr class="foo" id="bar"/>
1476
1477 <hr class="foo" id="bar" >
1478 """
1479
1480 var exp = """
1481 <p>Here's a simple block:</p>
1482 <div>
1483 foo
1484 </div>
1485 <p>This should be a code block, though:</p>
1486 <pre><code>&lt;div&gt;
1487 foo
1488 &lt;/div&gt;
1489 </code></pre>
1490 <p>As should this:</p>
1491 <pre><code>&lt;div&gt;foo&lt;/div&gt;
1492 </code></pre>
1493 <p>Now, nested:</p>
1494 <div>
1495 <div>
1496 <div>
1497 foo
1498 </div>
1499 </div>
1500 </div>
1501 <p>This should just be an HTML comment:</p>
1502 <!-- Comment -->
1503 <p>Multiline:</p>
1504 <!--
1505 Blah
1506 Blah
1507 -->
1508 <p>Code block:</p>
1509 <pre><code>&lt;!-- Comment --&gt;
1510 </code></pre>
1511 <p>Just plain comment, with trailing spaces on the line:</p>
1512 <!-- foo -->
1513 <p>Code:</p>
1514 <pre><code>&lt;hr /&gt;
1515 </code></pre>
1516 <p>Hr's:</p>
1517 <hr>
1518 <hr/>
1519 <hr />
1520 <hr>
1521 <hr/>
1522 <hr />
1523 <hr class="foo" id="bar" />
1524 <hr class="foo" id="bar"/>
1525 <hr class="foo" id="bar" >
1526 """
1527 var res = test.md_to_html.write_to_string
1528 assert res == exp
1529 end
1530
1531 fun test_daring_inline_html2 do
1532 var test = """
1533 Simple block on one line:
1534
1535 <div>foo</div>
1536
1537 And nested without indentation:
1538
1539 <div>
1540 <div>
1541 <div>
1542 foo
1543 </div>
1544 <div style=">"/>
1545 </div>
1546 <div>bar</div>
1547 </div>
1548
1549 And with attributes:
1550
1551 <div>
1552 <div id="foo">
1553 </div>
1554 </div>
1555
1556 This was broken in 1.0.2b7:
1557
1558 <div class="inlinepage">
1559 <div class="toggleableend">
1560 foo
1561 </div>
1562 </div>
1563 """
1564
1565 var exp = """
1566 <p>Simple block on one line:</p>
1567 <div>foo</div>
1568 <p>And nested without indentation:</p>
1569 <div>
1570 <div>
1571 <div>
1572 foo
1573 </div>
1574 <div style=">"/>
1575 </div>
1576 <div>bar</div>
1577 </div>
1578 <p>And with attributes:</p>
1579 <div>
1580 <div id="foo">
1581 </div>
1582 </div>
1583 <p>This was broken in 1.0.2b7:</p>
1584 <div class="inlinepage">
1585 <div class="toggleableend">
1586 foo
1587 </div>
1588 </div>
1589 """
1590 var res = test.md_to_html.write_to_string
1591 assert res == exp
1592 end
1593
1594 fun test_daring_inline_html3 do
1595 var test = """
1596 Paragraph one.
1597
1598 <!-- This is a simple comment -->
1599
1600 <!--
1601 This is another comment.
1602 -->
1603
1604 Paragraph two.
1605
1606 <!-- one comment block -- -- with two comments -->
1607
1608 The end.
1609 """
1610
1611 var exp = """
1612 <p>Paragraph one.</p>
1613 <!-- This is a simple comment -->
1614 <!--
1615 This is another comment.
1616 -->
1617 <p>Paragraph two.</p>
1618 <!-- one comment block -- -- with two comments -->
1619 <p>The end.</p>
1620 """
1621 var res = test.md_to_html.write_to_string
1622 assert res == exp
1623 end
1624
1625 fun test_daring_links1 do
1626 var test = """
1627 Just a [URL](/url/).
1628
1629 [URL and title](/url/ "title").
1630
1631 [URL and title](/url/ "title preceded by two spaces").
1632
1633 [URL and title](/url/ "title preceded by a tab").
1634
1635 [URL and title](/url/ "title has spaces afterward" ).
1636
1637 [URL wrapped in angle brackets](</url/>).
1638
1639 [URL w/ angle brackets + title](</url/> "Here's the title").
1640
1641 [Empty]().
1642
1643 [With parens in the URL](http://en.wikipedia.org/wiki/WIMP_(computing))
1644
1645 (With outer parens and [parens in url](/foo(bar)))
1646
1647
1648 [With parens in the URL](/foo(bar) "and a title")
1649
1650 (With outer parens and [parens in url](/foo(bar) "and a title"))
1651 """
1652
1653 var exp = """
1654 <p>Just a <a href="/url/">URL</a>.</p>
1655 <p><a href="/url/" title="title">URL and title</a>.</p>
1656 <p><a href="/url/" title="title preceded by two spaces">URL and title</a>.</p>
1657 <p><a href="/url/" title="title preceded by a tab">URL and title</a>.</p>
1658 <p><a href="/url/" title="title has spaces afterward">URL and title</a>.</p>
1659 <p><a href="/url/">URL wrapped in angle brackets</a>.</p>
1660 <p><a href="/url/" title="Here&apos;s the title">URL w/ angle brackets + title</a>.</p>
1661 <p><a href="">Empty</a>.</p>
1662 <p><a href="http://en.wikipedia.org/wiki/WIMP_(computing)">With parens in the URL</a></p>
1663 <p>(With outer parens and <a href="/foo(bar)">parens in url</a>)</p>
1664 <p><a href="/foo(bar)" title="and a title">With parens in the URL</a></p>
1665 <p>(With outer parens and <a href="/foo(bar)" title="and a title">parens in url</a>)</p>
1666 """
1667 var res = test.md_to_html.write_to_string
1668 assert res == exp
1669 end
1670
1671 fun test_daring_links2 do
1672 var test = """
1673 Foo [bar] [1].
1674
1675 Foo [bar][1].
1676
1677 Foo [bar]
1678 [1].
1679
1680 [1]: /url/ "Title"
1681
1682
1683 With [embedded [brackets]] [b].
1684
1685
1686 Indented [once][].
1687
1688 Indented [twice][].
1689
1690 Indented [thrice][].
1691
1692 Indented [four][] times.
1693
1694 [once]: /url
1695
1696 [twice]: /url
1697
1698 [thrice]: /url
1699
1700 [four]: /url
1701
1702
1703 [b]: /url/
1704
1705 * * *
1706
1707 [this] [this] should work
1708
1709 So should [this][this].
1710
1711 And [this] [].
1712
1713 And [this][].
1714
1715 And [this].
1716
1717 But not [that] [].
1718
1719 Nor [that][].
1720
1721 Nor [that].
1722
1723 [Something in brackets like [this][] should work]
1724
1725 [Same with [this].]
1726
1727 In this case, [this](/somethingelse/) points to something else.
1728
1729 Backslashing should suppress \\\[this] and [this\\\].
1730
1731 [this]: foo
1732
1733
1734 * * *
1735
1736 Here's one where the [link
1737 breaks] across lines.
1738
1739 Here's another where the [link
1740 breaks] across lines, but with a line-ending space.
1741
1742
1743 [link breaks]: /url/
1744 """
1745
1746 var exp = """
1747 <p>Foo <a href="/url/" title="Title">bar</a>.</p>
1748 <p>Foo <a href="/url/" title="Title">bar</a>.</p>
1749 <p>Foo <a href="/url/" title="Title">bar</a>.</p>
1750 <p>With <a href="/url/">embedded [brackets]</a>.</p>
1751 <p>Indented <a href="/url">once</a>.</p>
1752 <p>Indented <a href="/url">twice</a>.</p>
1753 <p>Indented <a href="/url">thrice</a>.</p>
1754 <p>Indented [four][] times.</p>
1755 <pre><code>[four]: /url
1756 </code></pre>
1757 <hr/>
1758 <p><a href="foo">this</a> should work</p>
1759 <p>So should <a href="foo">this</a>.</p>
1760 <p>And <a href="foo">this</a>.</p>
1761 <p>And <a href="foo">this</a>.</p>
1762 <p>And <a href="foo">this</a>.</p>
1763 <p>But not [that] [].</p>
1764 <p>Nor [that][].</p>
1765 <p>Nor [that].</p>
1766 <p>[Something in brackets like <a href="foo">this</a> should work]</p>
1767 <p>[Same with <a href="foo">this</a>.]</p>
1768 <p>In this case, <a href="/somethingelse/">this</a> points to something else.</p>
1769 <p>Backslashing should suppress [this] and [this].</p>
1770 <hr/>
1771 <p>Here's one where the <a href="/url/">link
1772 breaks</a> across lines.</p>
1773 <p>Here's another where the <a href="/url/">link
1774 breaks</a> across lines, but with a line-ending space.</p>
1775 """
1776 var res = test.md_to_html.write_to_string
1777 assert res == exp
1778 end
1779
1780 fun test_daring_links3 do
1781 var test = """
1782 This is the [simple case].
1783
1784 [simple case]: /simple
1785
1786
1787
1788 This one has a [line
1789 break].
1790
1791 This one has a [line
1792 break] with a line-ending space.
1793
1794 [line break]: /foo
1795
1796
1797 [this] [that] and the [other]
1798
1799 [this]: /this
1800 [that]: /that
1801 [other]: /other
1802 """
1803
1804 var exp = """
1805 <p>This is the <a href="/simple">simple case</a>.</p>
1806 <p>This one has a <a href="/foo">line
1807 break</a>.</p>
1808 <p>This one has a <a href="/foo">line
1809 break</a> with a line-ending space.</p>
1810 <p><a href="/that">this</a> and the <a href="/other">other</a></p>
1811 """
1812 var res = test.md_to_html.write_to_string
1813 assert res == exp
1814 end
1815
1816 fun test_daring_nested do
1817 var test = """
1818 > foo
1819 >
1820 > > bar
1821 >
1822 > foo
1823 """
1824
1825 var exp = """
1826 <blockquote>
1827 <p>foo</p>
1828 <blockquote>
1829 <p>bar</p>
1830 </blockquote>
1831 <p>foo</p>
1832 </blockquote>
1833 """
1834 var res = test.md_to_html.write_to_string
1835 assert res == exp
1836 end
1837
1838 fun test_daring_list do
1839 var test = """
1840 ## Unordered
1841
1842 Asterisks tight:
1843
1844 * asterisk 1
1845 * asterisk 2
1846 * asterisk 3
1847
1848
1849 Asterisks loose:
1850
1851 * asterisk 1
1852
1853 * asterisk 2
1854
1855 * asterisk 3
1856
1857 * * *
1858
1859 Pluses tight:
1860
1861 + Plus 1
1862 + Plus 2
1863 + Plus 3
1864
1865
1866 Pluses loose:
1867
1868 + Plus 1
1869
1870 + Plus 2
1871
1872 + Plus 3
1873
1874 * * *
1875
1876
1877 Minuses tight:
1878
1879 - Minus 1
1880 - Minus 2
1881 - Minus 3
1882
1883
1884 Minuses loose:
1885
1886 - Minus 1
1887
1888 - Minus 2
1889
1890 - Minus 3
1891
1892
1893 ## Ordered
1894
1895 Tight:
1896
1897 1. First
1898 2. Second
1899 3. Third
1900
1901 and:
1902
1903 1. One
1904 2. Two
1905 3. Three
1906
1907
1908 Loose using tabs:
1909
1910 1. First
1911
1912 2. Second
1913
1914 3. Third
1915
1916 and using spaces:
1917
1918 1. One
1919
1920 2. Two
1921
1922 3. Three
1923
1924 Multiple paragraphs:
1925
1926 1. Item 1, graf one.
1927
1928 Item 2. graf two. The quick brown fox jumped over the lazy dog's
1929 back.
1930
1931 2. Item 2.
1932
1933 3. Item 3.
1934
1935
1936
1937 ## Nested
1938
1939 * Tab
1940 * Tab
1941 * Tab
1942
1943 Here's another:
1944
1945 1. First
1946 2. Second:
1947 * Fee
1948 * Fie
1949 * Foe
1950 3. Third
1951
1952 Same thing but with paragraphs:
1953
1954 1. First
1955
1956 2. Second:
1957 * Fee
1958 * Fie
1959 * Foe
1960
1961 3. Third
1962 """
1963
1964 var exp = """
1965 <h2 id="Unordered">Unordered</h2>
1966 <p>Asterisks tight:</p>
1967 <ul>
1968 <li>asterisk 1</li>
1969 <li>asterisk 2</li>
1970 <li>asterisk 3</li>
1971 </ul>
1972 <p>Asterisks loose:</p>
1973 <ul>
1974 <li><p>asterisk 1</p>
1975 </li>
1976 <li><p>asterisk 2</p>
1977 </li>
1978 <li><p>asterisk 3</p>
1979 </li>
1980 </ul>
1981 <hr/>
1982 <p>Pluses tight:</p>
1983 <ul>
1984 <li>Plus 1</li>
1985 <li>Plus 2</li>
1986 <li>Plus 3</li>
1987 </ul>
1988 <p>Pluses loose:</p>
1989 <ul>
1990 <li><p>Plus 1</p>
1991 </li>
1992 <li><p>Plus 2</p>
1993 </li>
1994 <li><p>Plus 3</p>
1995 </li>
1996 </ul>
1997 <hr/>
1998 <p>Minuses tight:</p>
1999 <ul>
2000 <li>Minus 1</li>
2001 <li>Minus 2</li>
2002 <li>Minus 3</li>
2003 </ul>
2004 <p>Minuses loose:</p>
2005 <ul>
2006 <li><p>Minus 1</p>
2007 </li>
2008 <li><p>Minus 2</p>
2009 </li>
2010 <li><p>Minus 3</p>
2011 </li>
2012 </ul>
2013 <h2 id="Ordered">Ordered</h2>
2014 <p>Tight:</p>
2015 <ol>
2016 <li>First</li>
2017 <li>Second</li>
2018 <li>Third</li>
2019 </ol>
2020 <p>and:</p>
2021 <ol>
2022 <li>One</li>
2023 <li>Two</li>
2024 <li>Three</li>
2025 </ol>
2026 <p>Loose using tabs:</p>
2027 <ol>
2028 <li><p>First</p>
2029 </li>
2030 <li><p>Second</p>
2031 </li>
2032 <li><p>Third</p>
2033 </li>
2034 </ol>
2035 <p>and using spaces:</p>
2036 <ol>
2037 <li><p>One</p>
2038 </li>
2039 <li><p>Two</p>
2040 </li>
2041 <li><p>Three</p>
2042 </li>
2043 </ol>
2044 <p>Multiple paragraphs:</p>
2045 <ol>
2046 <li><p>Item 1, graf one.</p>
2047 <p>Item 2. graf two. The quick brown fox jumped over the lazy dog's
2048 back.</p>
2049 </li>
2050 <li><p>Item 2.</p>
2051 </li>
2052 <li><p>Item 3.</p>
2053 </li>
2054 </ol>
2055 <h2 id="Nested">Nested</h2>
2056 <ul>
2057 <li>Tab<ul>
2058 <li>Tab<ul>
2059 <li>Tab</li>
2060 </ul>
2061 </li>
2062 </ul>
2063 </li>
2064 </ul>
2065 <p>Here's another:</p>
2066 <ol>
2067 <li>First</li>
2068 <li>Second:<ul>
2069 <li>Fee</li>
2070 <li>Fie</li>
2071 <li>Foe</li>
2072 </ul>
2073 </li>
2074 <li>Third</li>
2075 </ol>
2076 <p>Same thing but with paragraphs:</p>
2077 <ol>
2078 <li><p>First</p>
2079 </li>
2080 <li><p>Second:</p>
2081 <ul>
2082 <li>Fee</li>
2083 <li>Fie</li>
2084 <li>Foe</li>
2085 </ul>
2086 </li>
2087 <li><p>Third</p>
2088 </li>
2089 </ol>
2090 """
2091 var res = test.md_to_html.write_to_string
2092 assert res == exp
2093 end
2094
2095 fun test_daring_strong_em do
2096 var test = """
2097 ***This is strong and em.***
2098
2099 So is ***this*** word.
2100
2101 ___This is strong and em.___
2102
2103 So is ___this___ word.
2104 """
2105
2106 var exp = """
2107 <p><strong><em>This is strong and em.</em></strong></p>
2108 <p>So is <strong><em>this</em></strong> word.</p>
2109 <p><strong><em>This is strong and em.</em></strong></p>
2110 <p>So is <strong><em>this</em></strong> word.</p>
2111 """
2112 var res = test.md_to_html.write_to_string
2113 assert res == exp
2114 end
2115
2116 fun test_daring_tabs do
2117 var test = """
2118 + this is a list item
2119 indented with tabs
2120
2121 + this is a list item
2122 indented with spaces
2123
2124 Code:
2125
2126 this code block is indented by one tab
2127
2128 And:
2129
2130 this code block is indented by two tabs
2131
2132 And:
2133
2134 + this is an example list item
2135 indented with tabs
2136
2137 + this is an example list item
2138 indented with spaces
2139 """
2140
2141 var exp = """
2142 <ul>
2143 <li><p>this is a list item
2144 indented with tabs</p>
2145 </li>
2146 <li><p>this is a list item
2147 indented with spaces</p>
2148 </li>
2149 </ul>
2150 <p>Code:</p>
2151 <pre><code>this code block is indented by one tab
2152 </code></pre>
2153 <p>And:</p>
2154 <pre><code> this code block is indented by two tabs
2155 </code></pre>
2156 <p>And:</p>
2157 <pre><code>+ this is an example list item
2158 indented with tabs
2159
2160 + this is an example list item
2161 indented with spaces
2162 </code></pre>
2163 """
2164 var res = test.md_to_html.write_to_string
2165 assert res == exp
2166 end
2167
2168 fun test_daring_tidyness do
2169 var test = """
2170 > A list within a blockquote:
2171 >
2172 > * asterisk 1
2173 > * asterisk 2
2174 > * asterisk 3
2175 """
2176
2177 var exp = """
2178 <blockquote>
2179 <p>A list within a blockquote:</p>
2180 <ul>
2181 <li>asterisk 1</li>
2182 <li>asterisk 2</li>
2183 <li>asterisk 3</li>
2184 </ul>
2185 </blockquote>
2186 """
2187 var res = test.md_to_html.write_to_string
2188 assert res == exp
2189 end
2190
2191
2192 end
2193
2194 class TestBlock
2195 super TestSuite
2196
2197 fun test_has_blocks do
2198 var subject = new MDBlock
2199 assert not subject.has_blocks
2200 subject.first_block = new MDBlock
2201 assert subject.has_blocks
2202 end
2203
2204 fun test_count_blocks do
2205 var subject = new MDBlock
2206 assert subject.count_blocks == 0
2207 subject.first_block = new MDBlock
2208 assert subject.count_blocks == 1
2209 subject.first_block.next = new MDBlock
2210 assert subject.count_blocks == 2
2211 end
2212
2213 fun test_has_lines do
2214 var subject = new MDBlock
2215 assert not subject.has_lines
2216 subject.first_line = new MDLine("")
2217 assert subject.has_lines
2218 end
2219
2220 fun test_count_lines do
2221 var subject = new MDBlock
2222 assert subject.count_lines == 0
2223 subject.first_line = new MDLine("")
2224 assert subject.count_lines == 1
2225 subject.first_line.next = new MDLine("")
2226 assert subject.count_lines == 2
2227 end
2228
2229 fun test_split do
2230 var line1 = new MDLine("line1")
2231 var line2 = new MDLine("line2")
2232 var line3 = new MDLine("line3")
2233 var subject = new MDBlock
2234 subject.add_line line1
2235 subject.add_line line2
2236 subject.add_line line3
2237 var block = subject.split(line2)
2238 assert subject.count_blocks == 1
2239 assert subject.count_lines == 1
2240 assert subject.first_line == line3
2241 assert block.count_blocks == 0
2242 assert block.count_lines == 2
2243 assert block.first_line == line1
2244 assert block.last_line == line2
2245 end
2246
2247 fun test_add_line do
2248 var subject = new MDBlock
2249 assert subject.count_lines == 0
2250 subject.add_line new MDLine("")
2251 assert subject.count_lines == 1
2252 subject.add_line new MDLine("")
2253 assert subject.count_lines == 2
2254 end
2255
2256 fun test_remove_line do
2257 var line1 = new MDLine("line1")
2258 var line2 = new MDLine("line2")
2259 var line3 = new MDLine("line3")
2260 var subject = new MDBlock
2261 subject.add_line line1
2262 subject.add_line line2
2263 subject.add_line line3
2264 subject.remove_line(line2)
2265 assert subject.count_lines == 2
2266 subject.remove_line(line1)
2267 assert subject.count_lines == 1
2268 assert subject.first_line == line3
2269 assert subject.last_line == line3
2270 end
2271
2272 fun test_transform_headline1 do
2273 var subject = new MDBlock
2274 var kind = new BlockHeadline(subject)
2275 subject.add_line new MDLine(" # Title 1 ")
2276 kind.transform_headline(subject)
2277 assert kind.depth == 1
2278 assert subject.first_line.value == "Title 1"
2279 end
2280
2281 fun test_transform_headline2 do
2282 var subject = new MDBlock
2283 var kind = new BlockHeadline(subject)
2284 subject.add_line new MDLine(" #####Title 5 ")
2285 kind.transform_headline(subject)
2286 assert kind.depth == 5
2287 assert subject.first_line.value == "Title 5"
2288 end
2289
2290 fun test_remove_quote_prefix do
2291 var subject = new MDBlock
2292 var kind = new BlockQuote(subject)
2293 subject.add_line new MDLine(" > line 1")
2294 subject.add_line new MDLine(" > line 2")
2295 subject.add_line new MDLine(" > line 3")
2296 kind.remove_block_quote_prefix(subject)
2297 assert subject.first_line.value == "line 1"
2298 assert subject.first_line.next.value == "line 2"
2299 assert subject.first_line.next.next.value == "line 3"
2300 end
2301
2302 fun test_remove_leading_empty_lines_1 do
2303 var block = new MDBlock
2304 block.add_line new MDLine("")
2305 block.add_line new MDLine("")
2306 block.add_line new MDLine("")
2307 block.add_line new MDLine("")
2308 block.add_line new MDLine(" text")
2309 block.add_line new MDLine("")
2310 assert block.remove_leading_empty_lines
2311 assert block.first_line.value == " text"
2312 end
2313
2314 fun test_remove_leading_empty_lines_2 do
2315 var block = new MDBlock
2316 block.add_line new MDLine(" text")
2317 block.remove_leading_empty_lines
2318 assert block.first_line.value == " text"
2319 end
2320
2321 fun test_remove_trailing_empty_lines_1 do
2322 var block = new MDBlock
2323 block.add_line new MDLine("")
2324 block.add_line new MDLine("text")
2325 block.add_line new MDLine("")
2326 block.add_line new MDLine("")
2327 block.add_line new MDLine("")
2328 block.add_line new MDLine("")
2329 assert block.remove_trailing_empty_lines
2330 assert block.last_line.value == "text"
2331 end
2332
2333 fun test_remove_trailing_empty_lines_2 do
2334 var block = new MDBlock
2335 block.add_line new MDLine("text ")
2336 assert not block.remove_trailing_empty_lines
2337 assert block.last_line.value == "text "
2338 end
2339
2340 fun test_remove_surrounding_empty_lines do
2341 var block = new MDBlock
2342 block.add_line new MDLine("")
2343 block.add_line new MDLine("text")
2344 block.add_line new MDLine("")
2345 block.add_line new MDLine("")
2346 block.add_line new MDLine("")
2347 block.add_line new MDLine("")
2348 assert block.remove_surrounding_empty_lines
2349 assert block.first_line.value == "text"
2350 assert block.last_line.value == "text"
2351 end
2352 end
2353
2354 class TestLine
2355 super TestSuite
2356
2357 var subject: MDLine
2358
2359 init do end
2360
2361 fun test_is_empty do
2362 subject = new MDLine("")
2363 assert subject.is_empty
2364 subject = new MDLine(" ")
2365 assert subject.is_empty
2366 subject = new MDLine("test")
2367 assert not subject.is_empty
2368 subject = new MDLine(" test")
2369 assert not subject.is_empty
2370 end
2371
2372 fun test_leading do
2373 subject = new MDLine("")
2374 assert subject.leading == 0
2375 subject = new MDLine(" ")
2376 assert subject.leading == 4
2377 subject = new MDLine("test")
2378 assert subject.leading == 0
2379 subject = new MDLine(" test")
2380 assert subject.leading == 4
2381 end
2382
2383 fun test_trailing do
2384 subject = new MDLine("")
2385 assert subject.trailing == 0
2386 subject = new MDLine(" ")
2387 assert subject.trailing == 0
2388 subject = new MDLine("test ")
2389 assert subject.trailing == 3
2390 subject = new MDLine(" test ")
2391 assert subject.trailing == 1
2392 end
2393
2394 fun test_line_type do
2395 var v = new MarkdownProcessor
2396 subject = new MDLine("")
2397 assert subject.kind(v) isa LineEmpty
2398 subject = new MDLine(" ")
2399 assert subject.kind(v) isa LineEmpty
2400 subject = new MDLine("text ")
2401 assert subject.kind(v) isa LineOther
2402 subject = new MDLine(" # Title")
2403 assert subject.kind(v) isa LineHeadline
2404 subject = new MDLine(" ### Title")
2405 assert subject.kind(v) isa LineHeadline
2406 subject = new MDLine(" code")
2407 assert subject.kind(v) isa LineCode
2408 subject = new MDLine(" ~~~")
2409 assert subject.kind(v) isa LineFence
2410 subject = new MDLine(" ```")
2411 assert subject.kind(v) isa LineFence
2412 subject = new MDLine(" Title ")
2413 subject.next = new MDLine("== ")
2414 assert subject.kind(v) isa LineHeadline1
2415 subject = new MDLine(" Title ")
2416 subject.next = new MDLine("-- ")
2417 assert subject.kind(v) isa LineHeadline2
2418 subject = new MDLine(" * * * ")
2419 assert subject.kind(v) isa LineHR
2420 subject = new MDLine(" *** ")
2421 assert subject.kind(v) isa LineHR
2422 subject = new MDLine("- -- ")
2423 assert subject.kind(v) isa LineHR
2424 subject = new MDLine("--------- ")
2425 assert subject.kind(v) isa LineHR
2426 subject = new MDLine(" >")
2427 assert subject.kind(v) isa LineBlockquote
2428 subject = new MDLine("<p></p>")
2429 assert subject.kind(v) isa LineXML
2430 subject = new MDLine("<p>")
2431 assert subject.kind(v) isa LineOther
2432 subject = new MDLine(" * foo")
2433 assert subject.kind(v) isa LineUList
2434 subject = new MDLine("- foo")
2435 assert subject.kind(v) isa LineUList
2436 subject = new MDLine("+ foo")
2437 assert subject.kind(v) isa LineUList
2438 subject = new MDLine("1. foo")
2439 assert subject.kind(v) isa LineOList
2440 subject = new MDLine(" 11111. foo")
2441 assert subject.kind(v) isa LineOList
2442 end
2443
2444 fun test_count_chars do
2445 subject = new MDLine("")
2446 assert subject.count_chars('*') == 0
2447 subject = new MDLine("* ")
2448 assert subject.count_chars('*') == 1
2449 subject = new MDLine(" * text")
2450 assert subject.count_chars('*') == 0
2451 subject = new MDLine(" * * *")
2452 assert subject.count_chars('*') == 3
2453 subject = new MDLine("text ** ")
2454 assert subject.count_chars('*') == 0
2455 end
2456
2457 fun test_count_chars_start do
2458 subject = new MDLine("")
2459 assert subject.count_chars_start('*') == 0
2460 subject = new MDLine("* ")
2461 assert subject.count_chars_start('*') == 1
2462 subject = new MDLine(" * text")
2463 assert subject.count_chars_start('*') == 1
2464 subject = new MDLine(" * * * text")
2465 assert subject.count_chars_start('*') == 3
2466 subject = new MDLine("text ** ")
2467 assert subject.count_chars_start('*') == 0
2468 end
2469 end
2470
2471 class TestHTMLDecorator
2472 super TestSuite
2473
2474 fun test_headlines do
2475 var test = """
2476 # **a**
2477 ## a.a
2478 ### a.a.b
2479 ### a.a.b
2480 ## a.b
2481 # [b](test)
2482 ## b.a
2483 ### b.a.c
2484 ## b.b
2485 ## b.c
2486 # c
2487 """
2488 var proc = new MarkdownProcessor
2489 var decorator = proc.emitter.decorator.as(HTMLDecorator)
2490 proc.process(test)
2491 var res = ""
2492 for id, headline in decorator.headlines do
2493 res += "{headline.title}:{id}\n"
2494 end
2495 var exp = """
2496 **a**:a
2497 a.a:a.a
2498 a.a.b:a.a.b
2499 a.a.b:a.a.b_1
2500 a.b:a.b
2501 [b](test):btest
2502 b.a:b.a
2503 b.a.c:b.a.c
2504 b.b:b.b
2505 b.c:b.c
2506 c:c
2507 """
2508 assert res == exp
2509 end
2510 end