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