summaryrefslogtreecommitdiff
path: root/test/rdoc/test_rdoc_markdown_test.rb
blob: d464cba263ccb941e939637de55c391844c4c96e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
require 'rubygems'
require 'minitest/autorun'
require 'pp'

require 'rdoc'
require 'rdoc/markdown'

class TestRDocMarkdownTest < RDoc::TestCase

  MARKDOWN_TEST_PATH = File.expand_path '../MarkdownTest_1.0.3/', __FILE__

  def setup
    super

    @parser = RDoc::Markdown.new
  end

  def test_amps_and_angle_encoding
    input = File.read "#{MARKDOWN_TEST_PATH}/Amps and angle encoding.text"

    doc = @parser.parse input

    expected =
      doc(
        para("AT&T has an ampersand in their name."),
        para("AT&T is another way to write it."),
        para("This & that."),
        para("4 < 5."),
        para("6 > 5."),
        para("Here's a {link}[http://example.com/?foo=1&bar=2] with " +
             "an ampersand in the URL."),
        para("Here's a link with an amersand in the link text: " +
             "{AT&T}[http://att.com/]."),
        para("Here's an inline {link}[/script?foo=1&bar=2]."),
        para("Here's an inline {link}[/script?foo=1&bar=2]."))

    assert_equal expected, doc
  end

  def test_auto_links
    input = File.read "#{MARKDOWN_TEST_PATH}/Auto links.text"

    doc = @parser.parse input

    # TODO verify rdoc auto-links too
    expected =
      doc(
        para("Link: http://example.com/."),
        para("With an ampersand: http://example.com/?foo=1&bar=2"),
        list(:BULLET,
          item(nil, para("In a list?")),
          item(nil, para("http://example.com/")),
          item(nil, para("It should."))),
        block(
          para("Blockquoted: http://example.com/")),
        para("Auto-links should not occur here: " +
             "<code><http://example.com/></code>"),
        verb("or here: <http://example.com/>\n"))

    assert_equal expected, doc
  end

  def test_backslash_escapes
    input = File.read "#{MARKDOWN_TEST_PATH}/Backslash escapes.text"

    doc = @parser.parse input

    expected =
      doc(
        para("These should all get escaped:"),

        para("Backslash: \\"),
        para("Backtick: `"),
        para("Asterisk: *"),
        para("Underscore: _"),
        para("Left brace: {"),
        para("Right brace: }"),
        para("Left bracket: ["),
        para("Right bracket: ]"),
        para("Left paren: ("),
        para("Right paren: )"),
        para("Greater-than: >"),
        para("Hash: #"),
        para("Period: ."),
        para("Bang: !"),
        para("Plus: +"),
        para("Minus: -"),

        para("These should not, because they occur within a code block:"),

        verb("Backslash: \\\\\n",
             "\n",
             "Backtick: \\`\n",
             "\n",
             "Asterisk: \\*\n",
             "\n",
             "Underscore: \\_\n",
             "\n",
             "Left brace: \\{\n",
             "\n",
             "Right brace: \\}\n",
             "\n",
             "Left bracket: \\[\n",
             "\n",
             "Right bracket: \\]\n",
             "\n",
             "Left paren: \\(\n",
             "\n",
             "Right paren: \\)\n",
             "\n",
             "Greater-than: \\>\n",
             "\n",
             "Hash: \\#\n",
             "\n",
             "Period: \\.\n",
             "\n",
             "Bang: \\!\n",
             "\n",
             "Plus: \\+\n",
             "\n",
             "Minus: \\-\n"),

        para("Nor should these, which occur in code spans:"),

        para("Backslash: <code>\\\\</code>"),
        para("Backtick: <code>\\`</code>"),
        para("Asterisk: <code>\\*</code>"),
        para("Underscore: <code>\\_</code>"),
        para("Left brace: <code>\\{</code>"),
        para("Right brace: <code>\\}</code>"),
        para("Left bracket: <code>\\[</code>"),
        para("Right bracket: <code>\\]</code>"),
        para("Left paren: <code>\\(</code>"),
        para("Right paren: <code>\\)</code>"),
        para("Greater-than: <code>\\></code>"),
        para("Hash: <code>\\#</code>"),
        para("Period: <code>\\.</code>"),
        para("Bang: <code>\\!</code>"),
        para("Plus: <code>\\+</code>"),
        para("Minus: <code>\\-</code>"),

        para("These should get escaped, even though they're matching pairs for\n" +
             "other Markdown constructs:"),

        para("\*asterisks\*"),
        para("\_underscores\_"),
        para("`backticks`"),

        para("This is a code span with a literal backslash-backtick " +
             "sequence: <code>\\`</code>"),

        para("This is a tag with unescaped backticks " +
             "<span attr='`ticks`'>bar</span>."),

        para("This is a tag with backslashes " +
             "<span attr='\\\\backslashes\\\\'>bar</span>."))

    assert_equal expected, doc
  end

  def test_blockquotes_with_code_blocks
    input = File.read "#{MARKDOWN_TEST_PATH}/Blockquotes with code blocks.text"

    doc = @parser.parse input

    expected =
      doc(
        block(
          para("Example:"),
          verb("sub status {\n",
               "    print \"working\";\n",
               "}\n"),
          para("Or:"),
          verb("sub status {\n",
               "    return \"working\";\n",
               "}\n")))

    assert_equal expected, doc
  end

  def test_code_blocks
    input = File.read "#{MARKDOWN_TEST_PATH}/Code Blocks.text"

    doc = @parser.parse input

    expected =
      doc(
        verb("code block on the first line\n"),
        para("Regular text."),

        verb("code block indented by spaces\n"),
        para("Regular text."),

        verb("the lines in this block  \n",
             "all contain trailing spaces  \n"),
        para("Regular Text."),

        verb("code block on the last line\n"))

    assert_equal expected, doc
  end

  def test_code_spans
    input = File.read "#{MARKDOWN_TEST_PATH}/Code Spans.text"

    doc = @parser.parse input

    expected = doc(
      para("<code><test a=\"</code> content of attribute <code>\"></code>"),
      para("Fix for backticks within HTML tag: " +
           "<span attr='`ticks`'>like this</span>"),
      para("Here's how you put <code>`backticks`</code> in a code span."))

    assert_equal expected, doc
  end

  def test_hard_wrapped_paragraphs_with_list_like_lines
    input = File.read "#{MARKDOWN_TEST_PATH}/Hard-wrapped paragraphs with list-like lines.text"

    doc = @parser.parse input

    expected =
      doc(
        para("In Markdown 1.0.0 and earlier. Version\n" +
             "8. This line turns into a list item.\n"   +
             "Because a hard-wrapped line in the\n"     +
             "middle of a paragraph looked like a\n"    +
             "list item."),
        para("Here's one with a bullet.\n" +
             "* criminey."))

    assert_equal expected, doc
  end

  def test_horizontal_rules
    input = File.read "#{MARKDOWN_TEST_PATH}/Horizontal rules.text"

    doc = @parser.parse input

    expected =
      doc(
        para("Dashes:"),

        rule(1),
        rule(1),
        rule(1),
        rule(1),
        verb("---\n"),

        rule(1),
        rule(1),
        rule(1),
        rule(1),
        verb("- - -\n"),

        para("Asterisks:"),

        rule(1),
        rule(1),
        rule(1),
        rule(1),
        verb("***\n"),

        rule(1),
        rule(1),
        rule(1),
        rule(1),
        verb("* * *\n"),

        para("Underscores:"),

        rule(1),
        rule(1),
        rule(1),
        rule(1),
        verb("___\n"),

        rule(1),
        rule(1),
        rule(1),
        rule(1),
        verb("_ _ _\n"))

    assert_equal expected, doc
  end

  def test_inline_html_advanced
    input = File.read "#{MARKDOWN_TEST_PATH}/Inline HTML (Advanced).text"

    @parser.html = true

    doc = @parser.parse input

    expected =
      doc(
        para("Simple block on one line:"),
        raw("<div>foo</div>"),
        para("And nested without indentation:"),
        raw(<<-RAW.chomp))
<div>
<div>
<div>
foo
</div>
<div style=">"/>
</div>
<div>bar</div>
</div>
        RAW

    assert_equal expected, doc
  end

  def test_inline_html_simple
    input = File.read "#{MARKDOWN_TEST_PATH}/Inline HTML (Simple).text"

    @parser.html = true

    doc = @parser.parse input

    expected =
      doc(
       para("Here's a simple block:"),
       raw("<div>\n\tfoo\n</div>"),

       para("This should be a code block, though:"),
       verb("<div>\n",
            "\tfoo\n",
            "</div>\n"),

       para("As should this:"),
       verb("<div>foo</div>\n"),

       para("Now, nested:"),
       raw("<div>\n\t<div>\n\t\t<div>\n\t\t\tfoo\n" +
           "\t\t</div>\n\t</div>\n</div>"),

       para("This should just be an HTML comment:"),
       raw("<!-- Comment -->"),

       para("Multiline:"),
       raw("<!--\nBlah\nBlah\n-->"),

       para("Code block:"),
       verb("<!-- Comment -->\n"),

       para("Just plain comment, with trailing spaces on the line:"),
       raw("<!-- foo -->"),

       para("Code:"),
       verb("<hr />\n"),

       para("Hr's:"),
       raw("<hr>"),
       raw("<hr/>"),
       raw("<hr />"),

       raw("<hr>"),
       raw("<hr/>"),
       raw("<hr />"),

       raw("<hr class=\"foo\" id=\"bar\" />"),
       raw("<hr class=\"foo\" id=\"bar\"/>"),
       raw("<hr class=\"foo\" id=\"bar\" >"))

    assert_equal expected, doc
  end

  def test_inline_html_comments
    input = File.read "#{MARKDOWN_TEST_PATH}/Inline HTML comments.text"

    doc = @parser.parse input

    expected =
      doc(
        para("Paragraph one."),

        raw("<!-- This is a simple comment -->"),

        raw("<!--\n\tThis is another comment.\n-->"),

        para("Paragraph two."),

        raw("<!-- one comment block -- -- with two comments -->"),

        para("The end."))

    assert_equal expected, doc
  end

  def test_links_inline_style
    input = File.read "#{MARKDOWN_TEST_PATH}/Links, inline style.text"

    doc = @parser.parse input

    expected =
      doc(
        para("Just a {URL}[/url/]."),
        para("{URL and title}[/url/]."),
        para("{URL and title}[/url/]."),
        para("{URL and title}[/url/]."),
        para("{URL and title}[/url/]."),
        para("{Empty}[]."))

    assert_equal expected, doc
  end

  def test_links_reference_style
    input = File.read "#{MARKDOWN_TEST_PATH}/Links, reference style.text"

    doc = @parser.parse input

    expected =
      doc(
        para("Foo {bar}[/url/]."),
        para("Foo {bar}[/url/]."),
        para("Foo {bar}[/url/]."),

        para("With {embedded [brackets]}[/url/]."),

        para("Indented {once}[/url]."),
        para("Indented {twice}[/url]."),
        para("Indented {thrice}[/url]."),
        para("Indented [four][] times."),

        verb("[four]: /url\n"),

        rule(1),

        para("{this}[foo] should work"),
        para("So should {this}[foo]."),
        para("And {this}[foo]."),
        para("And {this}[foo]."),
        para("And {this}[foo]."),

        para("But not [that] []."),
        para("Nor [that][]."),
        para("Nor [that]."),

        para("[Something in brackets like {this}[foo] should work]"),
        para("[Same with {this}[foo].]"),

        para("In this case, {this}[/somethingelse/] points to something else."),
        para("Backslashing should suppress [this] and [this]."),

        rule(1),

        para("Here's one where the {link breaks}[/url/] across lines."),
        para("Here's another where the {link breaks}[/url/] across lines, " +
             "but with a line-ending space."))

    assert_equal expected, doc
  end

  def test_links_shortcut_references
    input = File.read "#{MARKDOWN_TEST_PATH}/Links, shortcut references.text"

    doc = @parser.parse input

    expected =
      doc(
        para("This is the {simple case}[/simple]."),
        para("This one has a {line break}[/foo]."),
        para("This one has a {line break}[/foo] with a line-ending space."),
        para("{this}[/that] and the {other}[/other]"))

    assert_equal expected, doc
  end

  def test_literal_quotes_in_titles
    input = File.read "#{MARKDOWN_TEST_PATH}/Literal quotes in titles.text"

    doc = @parser.parse input

    # TODO support title attribute
    expected =
      doc(
        para("Foo {bar}[/url/]."),
        para("Foo {bar}[/url/]."))

    assert_equal expected, doc
  end

  def test_markdown_documentation_basics
    input = File.read "#{MARKDOWN_TEST_PATH}/Markdown Documentation - Basics.text"

    doc = @parser.parse input

    expected =
      doc(
        head(1, "Markdown: Basics"),

        raw(<<-RAW.chomp),
<ul id="ProjectSubmenu">
    <li><a href="/projects/markdown/" title="Markdown Project Page">Main</a></li>
    <li><a class="selected" title="Markdown Basics">Basics</a></li>
    <li><a href="/projects/markdown/syntax" title="Markdown Syntax Documentation">Syntax</a></li>
    <li><a href="/projects/markdown/license" title="Pricing and License Information">License</a></li>
    <li><a href="/projects/markdown/dingus" title="Online Markdown Web Form">Dingus</a></li>
</ul>
        RAW

        head(2, "Getting the Gist of Markdown's Formatting Syntax"),

        para("This page offers a brief overview of what it's like to use Markdown.\n" +
             "The {syntax page}[/projects/markdown/syntax] provides complete, detailed documentation for\n" +
             "every feature, but Markdown should be very easy to pick up simply by\n" +
             "looking at a few examples of it in action. The examples on this page\n" +
             "are written in a before/after style, showing example syntax and the\n" +
             "HTML output produced by Markdown."),

        para("It's also helpful to simply try Markdown out; the {Dingus}[/projects/markdown/dingus] is a\n" +
             "web application that allows you type your own Markdown-formatted text\n" +
             "and translate it to XHTML."),

        para("<b>Note:</b> This document is itself written using Markdown; you\n" +
             "can {see the source for it by adding '.text' to the URL}[/projects/markdown/basics.text]."),

        head(2, "Paragraphs, Headers, Blockquotes"),

        para("A paragraph is simply one or more consecutive lines of text, separated\n" +
             "by one or more blank lines. (A blank line is any line that looks like a\n" +
             "blank line -- a line containing nothing spaces or tabs is considered\n" +
             "blank.) Normal paragraphs should not be intended with spaces or tabs."),

        para("Markdown offers two styles of headers: _Setext_ and _atx_.\n" +
             "Setext-style headers for <code><h1></code> and <code><h2></code> are created by\n" +
             "\"underlining\" with equal signs (<code>=</code>) and hyphens (<code>-</code>), respectively.\n" +
             "To create an atx-style header, you put 1-6 hash marks (<code>#</code>) at the\n" +
             "beginning of the line -- the number of hashes equals the resulting\n" +
             "HTML header level."),

        para("Blockquotes are indicated using email-style '<code>></code>' angle brackets."),

        para("Markdown:"),

        verb("A First Level Header\n",
             "====================\n",
             "\n",
             "A Second Level Header\n",
             "---------------------\n",
             "\n",
             "Now is the time for all good men to come to\n",
             "the aid of their country. This is just a\n",
             "regular paragraph.\n",
             "\n",
             "The quick brown fox jumped over the lazy\n",
             "dog's back.\n",
             "\n",
             "### Header 3\n",
             "\n",
             "> This is a blockquote.\n",
             "> \n",
             "> This is the second paragraph in the blockquote.\n",
             ">\n",
             "> ## This is an H2 in a blockquote\n"),

        para("Output:"),

        verb("<h1>A First Level Header</h1>\n",
             "\n",
             "<h2>A Second Level Header</h2>\n",
             "\n",
             "<p>Now is the time for all good men to come to\n",
             "the aid of their country. This is just a\n",
             "regular paragraph.</p>\n",
             "\n",
             "<p>The quick brown fox jumped over the lazy\n",
             "dog's back.</p>\n",
             "\n",
             "<h3>Header 3</h3>\n",
             "\n",
             "<blockquote>\n",
             "    <p>This is a blockquote.</p>\n",
             "\n",
             "    <p>This is the second paragraph in the blockquote.</p>\n",
             "\n",
             "    <h2>This is an H2 in a blockquote</h2>\n",
             "</blockquote>\n"),

        head(3, "Phrase Emphasis"),
        para("Markdown uses asterisks and underscores to indicate spans of emphasis."),

        para("Markdown:"),

        verb("Some of these words *are emphasized*.\n",
             "Some of these words _are emphasized also_.\n",
             "\n",
             "Use two asterisks for **strong emphasis**.\n",
             "Or, if you prefer, __use two underscores instead__.\n"),

        para("Output:"),

        verb("<p>Some of these words <em>are emphasized</em>.\n",
             "Some of these words <em>are emphasized also</em>.</p>\n",
             "\n",
             "<p>Use two asterisks for <strong>strong emphasis</strong>.\n",
             "Or, if you prefer, <strong>use two underscores instead</strong>.</p>\n"),

        head(2, "Lists"),

        para("Unordered (bulleted) lists use asterisks, pluses, and hyphens (<code>*</code>,\n" +
             "<code>+</code>, and <code>-</code>) as list markers. These three markers are\n" +
             "interchangable; this:"),

        verb("*   Candy.\n",
             "*   Gum.\n",
             "*   Booze.\n"),

        para("this:"),

        verb("+   Candy.\n",
             "+   Gum.\n",
             "+   Booze.\n"),

        para("and this:"),

        verb("-   Candy.\n",
             "-   Gum.\n",
             "-   Booze.\n"),

        para("all produce the same output:"),

        verb("<ul>\n",
             "<li>Candy.</li>\n",
             "<li>Gum.</li>\n",
             "<li>Booze.</li>\n",
             "</ul>\n"),

        para("Ordered (numbered) lists use regular numbers, followed by periods, as\n" +
             "list markers:"),

        verb("1.  Red\n",
             "2.  Green\n",
             "3.  Blue\n"),

        para("Output:"),

        verb("<ol>\n",
             "<li>Red</li>\n",
             "<li>Green</li>\n",
             "<li>Blue</li>\n",
             "</ol>\n"),

        para("If you put blank lines between items, you'll get <code><p></code> tags for the\n" +
             "list item text. You can create multi-paragraph list items by indenting\n" +
             "the paragraphs by 4 spaces or 1 tab:"),

        verb("*   A list item.\n",
             "\n",
             "    With multiple paragraphs.\n",
             "\n",
             "*   Another item in the list.\n"),

        para("Output:"),

        verb("<ul>\n",
             "<li><p>A list item.</p>\n",
             "<p>With multiple paragraphs.</p></li>\n",
             "<li><p>Another item in the list.</p></li>\n",
             "</ul>\n"),

        head(3, "Links"),

        para("Markdown supports two styles for creating links: _inline_ and\n" +
             "_reference_. With both styles, you use square brackets to delimit the\n" +
             "text you want to turn into a link."),

        para("Inline-style links use parentheses immediately after the link text.\n" +
             "For example:"),

        verb("This is an [example link](http://example.com/).\n"),

        para("Output:"),

        verb("<p>This is an <a href=\"http://example.com/\">\n",
             "example link</a>.</p>\n"),

        para("Optionally, you may include a title attribute in the parentheses:"),

        verb("This is an [example link](http://example.com/ \"With a Title\").\n"),

        para("Output:"),

        verb("<p>This is an <a href=\"http://example.com/\" title=\"With a Title\">\n",
             "example link</a>.</p>\n"),

        para("Reference-style links allow you to refer to your links by names, which\n" +
             "you define elsewhere in your document:"),

        verb("I get 10 times more traffic from [Google][1] than from\n",
             "[Yahoo][2] or [MSN][3].\n",
             "\n",
             "[1]: http://google.com/        \"Google\"\n",
             "[2]: http://search.yahoo.com/  \"Yahoo Search\"\n",
             "[3]: http://search.msn.com/    \"MSN Search\"\n"),

        para("Output:"),

        verb("<p>I get 10 times more traffic from <a href=\"http://google.com/\"\n",
             "title=\"Google\">Google</a> than from <a href=\"http://search.yahoo.com/\"\n",
             "title=\"Yahoo Search\">Yahoo</a> or <a href=\"http://search.msn.com/\"\n",
             "title=\"MSN Search\">MSN</a>.</p>\n"),

        para("The title attribute is optional. Link names may contain letters,\n" +
             "numbers and spaces, but are _not_ case sensitive:"),

        verb("I start my morning with a cup of coffee and\n",
             "[The New York Times][NY Times].\n",
             "\n",
             "[ny times]: http://www.nytimes.com/\n"),

        para("Output:"),

        verb("<p>I start my morning with a cup of coffee and\n",
             "<a href=\"http://www.nytimes.com/\">The New York Times</a>.</p>\n"),

        head(3, "Images"),

        para("Image syntax is very much like link syntax."),

        para("Inline (titles are optional):"),

        verb("![alt text](/path/to/img.jpg \"Title\")\n"),

        para("Reference-style:"),

        verb("![alt text][id]\n",
             "\n",
             "[id]: /path/to/img.jpg \"Title\"\n"),

        para("Both of the above examples produce the same output:"),

        verb("<img src=\"/path/to/img.jpg\" alt=\"alt text\" title=\"Title\" />\n"),

        head(3, "Code"),

        para("In a regular paragraph, you can create code span by wrapping text in\n" +
             "backtick quotes. Any ampersands (<code>&</code>) and angle brackets (<code><</code> or\n" +
             "<code>></code>) will automatically be translated into HTML entities. This makes\n" +
             "it easy to use Markdown to write about HTML example code:"),

        verb(
             "I strongly recommend against using any `<blink>` tags.\n",
             "\n",
             "I wish SmartyPants used named entities like `&mdash;`\n",
             "instead of decimal-encoded entites like `&#8212;`.\n"),

        para("Output:"),

        verb("<p>I strongly recommend against using any\n",
             "<code>&lt;blink&gt;</code> tags.</p>\n",
             "\n",
             "<p>I wish SmartyPants used named entities like\n",
             "<code>&amp;mdash;</code> instead of decimal-encoded\n",
             "entites like <code>&amp;#8212;</code>.</p>\n"),

        para("To specify an entire block of pre-formatted code, indent every line of\n" +
             "the block by 4 spaces or 1 tab. Just like with code spans, <code>&</code>, <code><</code>,\n" +
             "and <code>></code> characters will be escaped automatically."),

        para("Markdown:"),

        verb("If you want your page to validate under XHTML 1.0 Strict,\n",
             "you've got to put paragraph tags in your blockquotes:\n",
             "\n",
             "    <blockquote>\n",
             "        <p>For example.</p>\n",
             "    </blockquote>\n"),

        para("Output:"),

        verb("<p>If you want your page to validate under XHTML 1.0 Strict,\n",
             "you've got to put paragraph tags in your blockquotes:</p>\n",
             "\n",
             "<pre><code>&lt;blockquote&gt;\n",
             "    &lt;p&gt;For example.&lt;/p&gt;\n",
             "&lt;/blockquote&gt;\n",
             "</code></pre>\n"))

    assert_equal expected, doc
  end

  def test_markdown_documentation_syntax
    input = File.read "#{MARKDOWN_TEST_PATH}/Markdown Documentation - Syntax.text"

    doc = @parser.parse input

    expected =
      doc(
        head(1, "Markdown: Syntax"),

        raw(<<-RAW.chomp),
<ul id="ProjectSubmenu">
    <li><a href="/projects/markdown/" title="Markdown Project Page">Main</a></li>
    <li><a href="/projects/markdown/basics" title="Markdown Basics">Basics</a></li>
    <li><a class="selected" title="Markdown Syntax Documentation">Syntax</a></li>
    <li><a href="/projects/markdown/license" title="Pricing and License Information">License</a></li>
    <li><a href="/projects/markdown/dingus" title="Online Markdown Web Form">Dingus</a></li>
</ul>
        RAW

        list(:BULLET,
          item(nil,
            para("{Overview}[#overview]"),
            list(:BULLET,
              item(nil,
                para("{Philosophy}[#philosophy]")),
              item(nil,
                para("{Inline HTML}[#html]")),
              item(nil,
                para("{Automatic Escaping for Special Characters}[#autoescape]")))),
          item(nil,
            para("{Block Elements}[#block]"),
            list(:BULLET,
              item(nil,
                para("{Paragraphs and Line Breaks}[#p]")),
              item(nil,
                para("{Headers}[#header]")),
              item(nil,
                para("{Blockquotes}[#blockquote]")),
              item(nil,
                para("{Lists}[#list]")),
              item(nil,
                para("{Code Blocks}[#precode]")),
              item(nil,
                para("{Horizontal Rules}[#hr]")))),
          item(nil,
            para("{Span Elements}[#span]"),
            list(:BULLET,
              item(nil,
                para("{Links}[#link]")),
              item(nil,
                para("{Emphasis}[#em]")),
              item(nil,
                para("{Code}[#code]")),
              item(nil,
                para("{Images}[#img]")))),
          item(nil,
            para("{Miscellaneous}[#misc]"),
            list(:BULLET,
              item(nil,
                para("{Backslash Escapes}[#backslash]")),
              item(nil,
                para("{Automatic Links}[#autolink]"))))),

        para("<b>Note:</b> This document is itself written using Markdown; you\n" +
             "can {see the source for it by adding '.text' to the URL}[/projects/markdown/syntax.text]."),

        rule(1),

        raw("<h2 id=\"overview\">Overview</h2>"),

        raw("<h3 id=\"philosophy\">Philosophy</h3>"),

        para("Markdown is intended to be as easy-to-read and easy-to-write as is feasible."),

        para("Readability, however, is emphasized above all else. A Markdown-formatted\n" +
             "document should be publishable as-is, as plain text, without looking\n" +
             "like it's been marked up with tags or formatting instructions. While\n" +
             "Markdown's syntax has been influenced by several existing text-to-HTML\n" +
             "filters -- including {Setext}[http://docutils.sourceforge.net/mirror/setext.html], {atx}[http://www.aaronsw.com/2002/atx/], {Textile}[http://textism.com/tools/textile/], {reStructuredText}[http://docutils.sourceforge.net/rst.html],\n" +
             "{Grutatext}[http://www.triptico.com/software/grutatxt.html], and {EtText}[http://ettext.taint.org/doc/] -- the single biggest source of\n" +
             "inspiration for Markdown's syntax is the format of plain text email."),

        para("To this end, Markdown's syntax is comprised entirely of punctuation\n" +
             "characters, which punctuation characters have been carefully chosen so\n" +
             "as to look like what they mean. E.g., asterisks around a word actually\n" +
             "look like \*emphasis\*. Markdown lists look like, well, lists. Even\n" +
             "blockquotes look like quoted passages of text, assuming you've ever\n" +
             "used email."),

        raw("<h3 id=\"html\">Inline HTML</h3>"),

        para("Markdown's syntax is intended for one purpose: to be used as a\n" +
             "format for _writing_ for the web."),

        para("Markdown is not a replacement for HTML, or even close to it. Its\n" +
             "syntax is very small, corresponding only to a very small subset of\n" +
             "HTML tags. The idea is _not_ to create a syntax that makes it easier\n" +
             "to insert HTML tags. In my opinion, HTML tags are already easy to\n" +
             "insert. The idea for Markdown is to make it easy to read, write, and\n" +
             "edit prose. HTML is a _publishing_ format; Markdown is a _writing_\n" +
             "format. Thus, Markdown's formatting syntax only addresses issues that\n" +
             "can be conveyed in plain text."),

        para("For any markup that is not covered by Markdown's syntax, you simply\n" +
             "use HTML itself. There's no need to preface it or delimit it to\n" +
             "indicate that you're switching from Markdown to HTML; you just use\n" +
             "the tags."),

        para("The only restrictions are that block-level HTML elements -- e.g. <code><div></code>,\n" +
             "<code><table></code>, <code><pre></code>, <code><p></code>, etc. -- must be separated from surrounding\n" +
             "content by blank lines, and the start and end tags of the block should\n" +
             "not be indented with tabs or spaces. Markdown is smart enough not\n" +
             "to add extra (unwanted) <code><p></code> tags around HTML block-level tags."),

        para("For example, to add an HTML table to a Markdown article:"),

        verb("This is a regular paragraph.\n",
             "\n",
             "<table>\n",
             "    <tr>\n",
             "        <td>Foo</td>\n",
             "    </tr>\n",
             "</table>\n",
             "\n",
             "This is another regular paragraph.\n"),

        para("Note that Markdown formatting syntax is not processed within block-level\n" +
             "HTML tags. E.g., you can't use Markdown-style <code>*emphasis*</code> inside an\n" +
             "HTML block."),

        para("Span-level HTML tags -- e.g. <code><span></code>, <code><cite></code>, or <code><del></code> -- can be\n" +
             "used anywhere in a Markdown paragraph, list item, or header. If you\n" +
             "want, you can even use HTML tags instead of Markdown formatting; e.g. if\n" +
             "you'd prefer to use HTML <code><a></code> or <code><img></code> tags instead of Markdown's\n" +
             "link or image syntax, go right ahead."),

        para("Unlike block-level HTML tags, Markdown syntax _is_ processed within\n" +
             "span-level tags."),

        raw("<h3 id=\"autoescape\">Automatic Escaping for Special Characters</h3>"),

        para("In HTML, there are two characters that demand special treatment: <code><</code>\n" +
             "and <code>&</code>. Left angle brackets are used to start tags; ampersands are\n" +
             "used to denote HTML entities. If you want to use them as literal\n" +
             "characters, you must escape them as entities, e.g. <code>&lt;</code>, and\n" +
             "<code>&amp;</code>."),

        para("Ampersands in particular are bedeviling for web writers. If you want to\n" +
             "write about 'AT&T', you need to write '<code>AT&amp;T</code>'. You even need to\n" +
             "escape ampersands within URLs. Thus, if you want to link to:"),

        verb("http://images.google.com/images?num=30&q=larry+bird\n"),

        para("you need to encode the URL as:"),

        verb("http://images.google.com/images?num=30&amp;q=larry+bird\n"),

        para("in your anchor tag <code>href</code> attribute. Needless to say, this is easy to\n" +
             "forget, and is probably the single most common source of HTML validation\n" +
             "errors in otherwise well-marked-up web sites."),

        para("Markdown allows you to use these characters naturally, taking care of\n" +
             "all the necessary escaping for you. If you use an ampersand as part of\n" +
             "an HTML entity, it remains unchanged; otherwise it will be translated\n" +
             "into <code>&amp;</code>."),

        para("So, if you want to include a copyright symbol in your article, you can write:"),

        verb("&copy;\n"),

        para("and Markdown will leave it alone. But if you write:"),

        verb("AT&T\n"),

        para("Markdown will translate it to:"),

        verb("AT&amp;T\n"),

        para("Similarly, because Markdown supports {inline HTML}[#html], if you use\n" +
             "angle brackets as delimiters for HTML tags, Markdown will treat them as\n" +
             "such. But if you write:"),

        verb("4 < 5\n"),

        para("Markdown will translate it to:"),

        verb("4 &lt; 5\n"),

        para("However, inside Markdown code spans and blocks, angle brackets and\n" +
             "ampersands are _always_ encoded automatically. This makes it easy to use\n" +
             "Markdown to write about HTML code. (As opposed to raw HTML, which is a\n" +
             "terrible format for writing about HTML syntax, because every single <code><</code>\n" +
             "and <code>&</code> in your example code needs to be escaped.)"),

        rule(1),

        raw("<h2 id=\"block\">Block Elements</h2>"),

        raw("<h3 id=\"p\">Paragraphs and Line Breaks</h3>"),

        para("A paragraph is simply one or more consecutive lines of text, separated\n" +
             "by one or more blank lines. (A blank line is any line that looks like a\n" +
             "blank line -- a line containing nothing but spaces or tabs is considered\n" +
             "blank.) Normal paragraphs should not be intended with spaces or tabs."),

        para("The implication of the \"one or more consecutive lines of text\" rule is\n" +
             "that Markdown supports \"hard-wrapped\" text paragraphs. This differs\n" +
             "significantly from most other text-to-HTML formatters (including Movable\n" +
             "Type's \"Convert Line Breaks\" option) which translate every line break\n" +
             "character in a paragraph into a <code><br /></code> tag."),

        para("When you _do_ want to insert a <code><br /></code> break tag using Markdown, you\n" +
             "end a line with two or more spaces, then type return."),

        para("Yes, this takes a tad more effort to create a <code><br /></code>, but a simplistic\n" +
             "\"every line break is a <code><br /></code>\" rule wouldn't work for Markdown.\n" +
             "Markdown's email-style {blockquoting}[#blockquote] and multi-paragraph {list items}[#list]\n" +
             "work best -- and look better -- when you format them with hard breaks."),

        raw("<h3 id=\"header\">Headers</h3>"),

        para("Markdown supports two styles of headers, {Setext}[http://docutils.sourceforge.net/mirror/setext.html] and {atx}[http://www.aaronsw.com/2002/atx/]."),

        para("Setext-style headers are \"underlined\" using equal signs (for first-level\n" +
             "headers) and dashes (for second-level headers). For example:"),

        verb("This is an H1\n",
             "=============\n",
             "\n",
             "This is an H2\n",
             "-------------\n"),

        para("Any number of underlining <code>=</code>'s or <code>-</code>'s will work."),

        para("Atx-style headers use 1-6 hash characters at the start of the line,\n" +
             "corresponding to header levels 1-6. For example:"),

        verb("# This is an H1\n",
             "\n",
             "## This is an H2\n",
             "\n",
             "###### This is an H6\n"),

        para("Optionally, you may \"close\" atx-style headers. This is purely\n" +
             "cosmetic -- you can use this if you think it looks better. The\n" +
             "closing hashes don't even need to match the number of hashes\n" +
             "used to open the header. (The number of opening hashes\n" +
             "determines the header level.) :"),

        verb("# This is an H1 #\n",
             "\n",
             "## This is an H2 ##\n",
             "\n",
             "### This is an H3 ######\n"),

        raw("<h3 id=\"blockquote\">Blockquotes</h3>"),

        para(
             "Markdown uses email-style <code>></code> characters for blockquoting. If you're\n" +
             "familiar with quoting passages of text in an email message, then you\n" +
             "know how to create a blockquote in Markdown. It looks best if you hard\n" +
             "wrap the text and put a <code>></code> before every line:"),

        verb("> This is a blockquote with two paragraphs. Lorem ipsum dolor sit amet,\n",
             "> consectetuer adipiscing elit. Aliquam hendrerit mi posuere lectus.\n",
             "> Vestibulum enim wisi, viverra nec, fringilla in, laoreet vitae, risus.\n",
             "> \n",
             "> Donec sit amet nisl. Aliquam semper ipsum sit amet velit. Suspendisse\n",
             "> id sem consectetuer libero luctus adipiscing.\n"),

        para("Markdown allows you to be lazy and only put the <code>></code> before the first\n" +
             "line of a hard-wrapped paragraph:"),

        verb("> This is a blockquote with two paragraphs. Lorem ipsum dolor sit amet,\n",
             "consectetuer adipiscing elit. Aliquam hendrerit mi posuere lectus.\n",
             "Vestibulum enim wisi, viverra nec, fringilla in, laoreet vitae, risus.\n",
             "\n",
             "> Donec sit amet nisl. Aliquam semper ipsum sit amet velit. Suspendisse\n",
             "id sem consectetuer libero luctus adipiscing.\n"),

        para("Blockquotes can be nested (i.e. a blockquote-in-a-blockquote) by\n" +
             "adding additional levels of <code>></code>:"),

        verb("> This is the first level of quoting.\n",
             ">\n",
             "> > This is nested blockquote.\n",
             ">\n",
             "> Back to the first level.\n"),

        para("Blockquotes can contain other Markdown elements, including headers, lists,\n" +
             "and code blocks:"),

        verb("> ## This is a header.\n",
             "> \n",
             "> 1.   This is the first list item.\n",
             "> 2.   This is the second list item.\n",
             "> \n",
             "> Here's some example code:\n",
             "> \n",
             ">     return shell_exec(\"echo $input | $markdown_script\");\n"),

        para("Any decent text editor should make email-style quoting easy. For\n" +
             "example, with BBEdit, you can make a selection and choose Increase\n" +
             "Quote Level from the Text menu."),

        raw("<h3 id=\"list\">Lists</h3>"),

        para("Markdown supports ordered (numbered) and unordered (bulleted) lists."),

        para("Unordered lists use asterisks, pluses, and hyphens -- interchangably\n" +
             "-- as list markers:"),

        verb("*   Red\n",
             "*   Green\n",
             "*   Blue\n"),

        para("is equivalent to:"),

        verb("+   Red\n",
             "+   Green\n",
             "+   Blue\n"),

        para("and:"),

        verb("-   Red\n",
             "-   Green\n",
             "-   Blue\n"),

        para("Ordered lists use numbers followed by periods:"),

        verb("1.  Bird\n",
             "2.  McHale\n",
             "3.  Parish\n"),

        para("It's important to note that the actual numbers you use to mark the\n" +
             "list have no effect on the HTML output Markdown produces. The HTML\n" +
             "Markdown produces from the above list is:"),

        verb("<ol>\n",
             "<li>Bird</li>\n",
             "<li>McHale</li>\n",
             "<li>Parish</li>\n",
             "</ol>\n"),

        para("If you instead wrote the list in Markdown like this:"),

        verb("1.  Bird\n",
             "1.  McHale\n",
             "1.  Parish\n"),

        para("or even:"),

        verb("3. Bird\n",
             "1. McHale\n",
             "8. Parish\n"),

        para("you'd get the exact same HTML output. The point is, if you want to,\n" +
             "you can use ordinal numbers in your ordered Markdown lists, so that\n" +
             "the numbers in your source match the numbers in your published HTML.\n" +
             "But if you want to be lazy, you don't have to."),

        para("If you do use lazy list numbering, however, you should still start the\n" +
             "list with the number 1. At some point in the future, Markdown may support\n" +
             "starting ordered lists at an arbitrary number."),

        para("List markers typically start at the left margin, but may be indented by\n" +
             "up to three spaces. List markers must be followed by one or more spaces\n" +
             "or a tab."),

        para("To make lists look nice, you can wrap items with hanging indents:"),

        verb("*   Lorem ipsum dolor sit amet, consectetuer adipiscing elit.\n",
             "    Aliquam hendrerit mi posuere lectus. Vestibulum enim wisi,\n",
             "    viverra nec, fringilla in, laoreet vitae, risus.\n",
             "*   Donec sit amet nisl. Aliquam semper ipsum sit amet velit.\n",
             "    Suspendisse id sem consectetuer libero luctus adipiscing.\n"),

        para("But if you want to be lazy, you don't have to:"),

        verb("*   Lorem ipsum dolor sit amet, consectetuer adipiscing elit.\n",
             "Aliquam hendrerit mi posuere lectus. Vestibulum enim wisi,\n",
             "viverra nec, fringilla in, laoreet vitae, risus.\n",
             "*   Donec sit amet nisl. Aliquam semper ipsum sit amet velit.\n",
             "Suspendisse id sem consectetuer libero luctus adipiscing.\n"),

        para("If list items are separated by blank lines, Markdown will wrap the\n" +
             "items in <code><p></code> tags in the HTML output. For example, this input:"),

        verb("*   Bird\n",
             "*   Magic\n"),

        para("will turn into:"),

        verb("<ul>\n",
             "<li>Bird</li>\n",
             "<li>Magic</li>\n",
             "</ul>\n"),

        para("But this:"),

        verb("*   Bird\n",
             "\n",
             "*   Magic\n"),

        para("will turn into:"),

        verb("<ul>\n",
             "<li><p>Bird</p></li>\n",
             "<li><p>Magic</p></li>\n",
             "</ul>\n"),

        para("List items may consist of multiple paragraphs. Each subsequent\n" +
             "paragraph in a list item must be intended by either 4 spaces\n" +
             "or one tab:"),

        verb("1.  This is a list item with two paragraphs. Lorem ipsum dolor\n",
             "    sit amet, consectetuer adipiscing elit. Aliquam hendrerit\n",
             "    mi posuere lectus.\n",
             "\n",
             "    Vestibulum enim wisi, viverra nec, fringilla in, laoreet\n",
             "    vitae, risus. Donec sit amet nisl. Aliquam semper ipsum\n",
             "    sit amet velit.\n",
             "\n",
             "2.  Suspendisse id sem consectetuer libero luctus adipiscing.\n"),

        para("It looks nice if you indent every line of the subsequent\n" +
             "paragraphs, but here again, Markdown will allow you to be\n" +
             "lazy:"),

        verb("*   This is a list item with two paragraphs.\n",
             "\n",
             "    This is the second paragraph in the list item. You're\n",
             "only required to indent the first line. Lorem ipsum dolor\n",
             "sit amet, consectetuer adipiscing elit.\n",
             "\n",
             "*   Another item in the same list.\n"),

        para("To put a blockquote within a list item, the blockquote's <code>></code>\n" +
             "delimiters need to be indented:"),

        verb("*   A list item with a blockquote:\n",
             "\n",
             "    > This is a blockquote\n",
             "    > inside a list item.\n"),

        para(
             "To put a code block within a list item, the code block needs\n" +
             "to be indented _twice_ -- 8 spaces or two tabs:"),

        verb("*   A list item with a code block:\n",
             "\n",
             "        <code goes here>\n"),

        para("It's worth noting that it's possible to trigger an ordered list by\n" +
             "accident, by writing something like this:"),

        verb("1986. What a great season.\n"),

        para("In other words, a <em>number-period-space</em> sequence at the beginning of a\n" +
             "line. To avoid this, you can backslash-escape the period:"),

        verb("1986\\. What a great season.\n"),

        raw("<h3 id=\"precode\">Code Blocks</h3>"),

        para("Pre-formatted code blocks are used for writing about programming or\n" +
             "markup source code. Rather than forming normal paragraphs, the lines\n" +
             "of a code block are interpreted literally. Markdown wraps a code block\n" +
             "in both <code><pre></code> and <code><code></code> tags."),

        para("To produce a code block in Markdown, simply indent every line of the\n" +
             "block by at least 4 spaces or 1 tab. For example, given this input:"),

        verb("This is a normal paragraph:\n",
             "\n",
             "    This is a code block.\n"),

        para("Markdown will generate:"),

        verb("<p>This is a normal paragraph:</p>\n",
             "\n",
             "<pre><code>This is a code block.\n",
             "</code></pre>\n"),

        para("One level of indentation -- 4 spaces or 1 tab -- is removed from each\n" +
             "line of the code block. For example, this:"),

        verb("Here is an example of AppleScript:\n",
             "\n",
             "    tell application \"Foo\"\n",
             "        beep\n",
             "    end tell\n"),

        para("will turn into:"),

        verb("<p>Here is an example of AppleScript:</p>\n",
             "\n",
             "<pre><code>tell application \"Foo\"\n",
             "    beep\n",
             "end tell\n",
             "</code></pre>\n"),

        para("A code block continues until it reaches a line that is not indented\n" +
             "(or the end of the article)."),

        para("Within a code block, ampersands (<code>&</code>) and angle brackets (<code><</code> and <code>></code>)\n" +
             "are automatically converted into HTML entities. This makes it very\n" +
             "easy to include example HTML source code using Markdown -- just paste\n" +
             "it and indent it, and Markdown will handle the hassle of encoding the\n" +
             "ampersands and angle brackets. For example, this:"),

        verb("    <div class=\"footer\">\n",
             "        &copy; 2004 Foo Corporation\n",
             "    </div>\n"),

        para("will turn into:"),

        verb("<pre><code>&lt;div class=\"footer\"&gt;\n",
             "    &amp;copy; 2004 Foo Corporation\n",
             "&lt;/div&gt;\n",
             "</code></pre>\n"),

        para("Regular Markdown syntax is not processed within code blocks. E.g.,\n" +
             "asterisks are just literal asterisks within a code block. This means\n" +
             "it's also easy to use Markdown to write about Markdown's own syntax."),

        raw("<h3 id=\"hr\">Horizontal Rules</h3>"),

        para("You can produce a horizontal rule tag (<code><hr /></code>) by placing three or\n" +
             "more hyphens, asterisks, or underscores on a line by themselves. If you\n" +
             "wish, you may use spaces between the hyphens or asterisks. Each of the\n" +
             "following lines will produce a horizontal rule:"),

        verb("* * *\n",
             "\n",
             "***\n",
             "\n",
             "*****\n",
             "\n",
             "- - -\n",
             "\n",
             "---------------------------------------\n",
             "\n",
             "_ _ _\n"),

        rule(1),

        raw("<h2 id=\"span\">Span Elements</h2>"),

        raw("<h3 id=\"link\">Links</h3>"),

        para("Markdown supports two style of links: _inline_ and _reference_."),

        para("In both styles, the link text is delimited by [square brackets]."),

        para("To create an inline link, use a set of regular parentheses immediately\n" +
             "after the link text's closing square bracket. Inside the parentheses,\n" +
             "put the URL where you want the link to point, along with an _optional_\n" +
             "title for the link, surrounded in quotes. For example:"),

        verb("This is [an example](http://example.com/ \"Title\") inline link.\n",
             "\n",
             "[This link](http://example.net/) has no title attribute.\n"),

        para("Will produce:"),

        verb("<p>This is <a href=\"http://example.com/\" title=\"Title\">\n",
             "an example</a> inline link.</p>\n",
             "\n",
             "<p><a href=\"http://example.net/\">This link</a> has no\n",
             "title attribute.</p>\n"),

        para("If you're referring to a local resource on the same server, you can\n" +
             "use relative paths:"),

        verb("See my [About](/about/) page for details.\n"),

        para("Reference-style links use a second set of square brackets, inside\n" +
             "which you place a label of your choosing to identify the link:"),

        verb("This is [an example][id] reference-style link.\n"),

        para("You can optionally use a space to separate the sets of brackets:"),

        verb("This is [an example] [id] reference-style link.\n"),

        para("Then, anywhere in the document, you define your link label like this,\n" +
             "on a line by itself:"),

        verb("[id]: http://example.com/  \"Optional Title Here\"\n"),

        para("That is:"),

        list(:BULLET,
          item(nil,
            para("Square brackets containing the link identifier (optionally\n" +
                 "indented from the left margin using up to three spaces);")),
          item(nil,
            para("followed by a colon;")),
          item(nil,
            para("followed by one or more spaces (or tabs);")),
          item(nil,
            para("followed by the URL for the link;")),
          item(nil,
            para("optionally followed by a title attribute for the link, enclosed\n" +
                 "in double or single quotes."))),

        para("The link URL may, optionally, be surrounded by angle brackets:"),

        verb("[id]: <http://example.com/>  \"Optional Title Here\"\n"),

        para("You can put the title attribute on the next line and use extra spaces\n" +
             "or tabs for padding, which tends to look better with longer URLs:"),

        verb("[id]: http://example.com/longish/path/to/resource/here\n",
             "    \"Optional Title Here\"\n"),

        para("Link definitions are only used for creating links during Markdown\n" +
             "processing, and are stripped from your document in the HTML output."),

        para("Link definition names may constist of letters, numbers, spaces, and punctuation -- but they are _not_ case sensitive. E.g. these two links:"),

        verb("[link text][a]\n",
             "[link text][A]\n"),

        para("are equivalent."),

        para("The <em>implicit link name</em> shortcut allows you to omit the name of the\n" +
             "link, in which case the link text itself is used as the name.\n" +
             "Just use an empty set of square brackets -- e.g., to link the word\n" +
             "\"Google\" to the google.com web site, you could simply write:"),

        verb("[Google][]\n"),

        para("And then define the link:"),

        verb("[Google]: http://google.com/\n"),

        para("Because link names may contain spaces, this shortcut even works for\n" +
            "multiple words in the link text:"),


        verb("Visit [Daring Fireball][] for more information.\n"),

        para("And then define the link:"),

        verb("[Daring Fireball]: http://daringfireball.net/\n"),

        para("Link definitions can be placed anywhere in your Markdown document. I\n" +
             "tend to put them immediately after each paragraph in which they're\n" +
             "used, but if you want, you can put them all at the end of your\n" +
             "document, sort of like footnotes."),

        para("Here's an example of reference links in action:"),

        verb("I get 10 times more traffic from [Google] [1] than from\n",
             "[Yahoo] [2] or [MSN] [3].\n",
             "\n",
             "  [1]: http://google.com/        \"Google\"\n",
             "  [2]: http://search.yahoo.com/  \"Yahoo Search\"\n",
             "  [3]: http://search.msn.com/    \"MSN Search\"\n"),

        para("Using the implicit link name shortcut, you could instead write:"),

        verb("I get 10 times more traffic from [Google][] than from\n",
             "[Yahoo][] or [MSN][].\n",
             "\n",
             "  [google]: http://google.com/        \"Google\"\n",
             "  [yahoo]:  http://search.yahoo.com/  \"Yahoo Search\"\n",
             "  [msn]:    http://search.msn.com/    \"MSN Search\"\n"),

        para("Both of the above examples will produce the following HTML output:"),

        verb("<p>I get 10 times more traffic from <a href=\"http://google.com/\"\n",
             "title=\"Google\">Google</a> than from\n",
             "<a href=\"http://search.yahoo.com/\" title=\"Yahoo Search\">Yahoo</a>\n",
             "or <a href=\"http://search.msn.com/\" title=\"MSN Search\">MSN</a>.</p>\n"),

        para("For comparison, here is the same paragraph written using\n" +
             "Markdown's inline link style:"),

        verb("I get 10 times more traffic from [Google](http://google.com/ \"Google\")\n",
             "than from [Yahoo](http://search.yahoo.com/ \"Yahoo Search\") or\n",
             "[MSN](http://search.msn.com/ \"MSN Search\").\n"),

        para("The point of reference-style links is not that they're easier to\n" +
             "write. The point is that with reference-style links, your document\n" +
             "source is vastly more readable. Compare the above examples: using\n" +
             "reference-style links, the paragraph itself is only 81 characters\n" +
             "long; with inline-style links, it's 176 characters; and as raw HTML,\n" +
             "it's 234 characters. In the raw HTML, there's more markup than there\n" +
             "is text."),

        para("With Markdown's reference-style links, a source document much more\n" +
             "closely resembles the final output, as rendered in a browser. By\n" +
             "allowing you to move the markup-related metadata out of the paragraph,\n" +
             "you can add links without interrupting the narrative flow of your\n" +
             "prose."),

        raw("<h3 id=\"em\">Emphasis</h3>"),

        para("Markdown treats asterisks (<code>*</code>) and underscores (<code>_</code>) as indicators of\n" +
             "emphasis. Text wrapped with one <code>*</code> or <code>_</code> will be wrapped with an\n" +
             "HTML <code><em></code> tag; double <code>*</code>'s or <code>_</code>'s will be wrapped with an HTML\n" +
             "<code><strong></code> tag. E.g., this input:"),

        verb("*single asterisks*\n",
             "\n",
             "_single underscores_\n",
             "\n",
             "**double asterisks**\n",
             "\n",
             "__double underscores__\n"),

        para("will produce:"),

        verb("<em>single asterisks</em>\n",
             "\n",
             "<em>single underscores</em>\n",
             "\n",
             "<strong>double asterisks</strong>\n",
             "\n",
             "<strong>double underscores</strong>\n"),

        para("You can use whichever style you prefer; the lone restriction is that\n" +
             "the same character must be used to open and close an emphasis span."),

        para("Emphasis can be used in the middle of a word:"),

        verb("un*fucking*believable\n"),

        para("But if you surround an <code>*</code> or <code>_</code> with spaces, it'll be treated as a\n" +
             "literal asterisk or underscore."),

        para("To produce a literal asterisk or underscore at a position where it\n" +
             "would otherwise be used as an emphasis delimiter, you can backslash\n" +
             "escape it:"),

        verb("\\*this text is surrounded by literal asterisks\\*\n"),

        raw("<h3 id=\"code\">Code</h3>"),

        para("To indicate a span of code, wrap it with backtick quotes (<code>`</code>).\n" +
             "Unlike a pre-formatted code block, a code span indicates code within a\n" +
             "normal paragraph. For example:"),

        verb("Use the `printf()` function.\n"),

        para("will produce:"),

        verb("<p>Use the <code>printf()</code> function.</p>\n"),

        para("To include a literal backtick character within a code span, you can use\n" +
             "multiple backticks as the opening and closing delimiters:"),

        verb("``There is a literal backtick (`) here.``\n"),

        para("which will produce this:"),

        verb("<p><code>There is a literal backtick (`) here.</code></p>\n"),

        para("The backtick delimiters surrounding a code span may include spaces --\n" +
             "one after the opening, one before the closing. This allows you to place\n" +
             "literal backtick characters at the beginning or end of a code span:"),

        verb("A single backtick in a code span: `` ` ``\n",
             "\n",
             "A backtick-delimited string in a code span: `` `foo` ``\n"),

        para("will produce:"),

        verb("<p>A single backtick in a code span: <code>`</code></p>\n",
             "\n",
             "<p>A backtick-delimited string in a code span: <code>`foo`</code></p>\n"),

        para("With a code span, ampersands and angle brackets are encoded as HTML\n" +
             "entities automatically, which makes it easy to include example HTML\n" +
             "tags. Markdown will turn this:"),

        verb("Please don't use any `<blink>` tags.\n"),

        para("into:"),

        verb("<p>Please don't use any <code>&lt;blink&gt;</code> tags.</p>\n"),

        para("You can write this:"),

        verb("`&#8212;` is the decimal-encoded equivalent of `&mdash;`.\n"),

        para("to produce:"),

        verb( "<p><code>&amp;#8212;</code> is the decimal-encoded\n",
             "equivalent of <code>&amp;mdash;</code>.</p>\n"),

        raw("<h3 id=\"img\">Images</h3>"),

        para("Admittedly, it's fairly difficult to devise a \"natural\" syntax for\n" +
             "placing images into a plain text document format."),

        para("Markdown uses an image syntax that is intended to resemble the syntax\n" +
             "for links, allowing for two styles: _inline_ and _reference_."),

        para("Inline image syntax looks like this:"),

        verb("![Alt text](/path/to/img.jpg)\n",
             "\n",
             "![Alt text](/path/to/img.jpg \"Optional title\")\n"),

        para("That is:"),

        list(:BULLET,
          item(nil,
            para("An exclamation mark: <code>!</code>;")),
          item(nil,
            para("followed by a set of square brackets, containing the <code>alt</code>\n" +
                 "attribute text for the image;")),
          item(nil,
            para("followed by a set of parentheses, containing the URL or path to\n" +
                 "the image, and an optional <code>title</code> attribute enclosed in double\n" +
                 "or single quotes."))),

        para("Reference-style image syntax looks like this:"),

        verb("![Alt text][id]\n"),

        para("Where \"id\" is the name of a defined image reference. Image references\n" +
             "are defined using syntax identical to link references:"),

        verb("[id]: url/to/image  \"Optional title attribute\"\n"),

        para("As of this writing, Markdown has no syntax for specifying the\n" +
             "dimensions of an image; if this is important to you, you can simply\n" +
             "use regular HTML <code><img></code> tags."),

        rule(1),

        raw("<h2 id=\"misc\">Miscellaneous</h2>"),

        raw("<h3 id=\"autolink\">Automatic Links</h3>"),

        para("Markdown supports a shortcut style for creating \"automatic\" links for URLs and email addresses: simply surround the URL or email address with angle brackets. What this means is that if you want to show the actual text of a URL or email address, and also have it be a clickable link, you can do this:"),

        verb("<http://example.com/>\n"),

        para("Markdown will turn this into:"),

        verb("<a href=\"http://example.com/\">http://example.com/</a>\n"),

        para("Automatic links for email addresses work similarly, except that\n" +
             "Markdown will also perform a bit of randomized decimal and hex\n" +
             "entity-encoding to help obscure your address from address-harvesting\n" +
             "spambots. For example, Markdown will turn this:"),

        verb("<address@example.com>\n"),

        para("into something like this:"),

        verb("<a href=\"&#x6D;&#x61;i&#x6C;&#x74;&#x6F;:&#x61;&#x64;&#x64;&#x72;&#x65;\n",
             "&#115;&#115;&#64;&#101;&#120;&#x61;&#109;&#x70;&#x6C;e&#x2E;&#99;&#111;\n",
             "&#109;\">&#x61;&#x64;&#x64;&#x72;&#x65;&#115;&#115;&#64;&#101;&#120;&#x61;\n",
             "&#109;&#x70;&#x6C;e&#x2E;&#99;&#111;&#109;</a>\n"),

        para("which will render in a browser as a clickable link to \"address@example.com\"."),

        para("(This sort of entity-encoding trick will indeed fool many, if not\n" +
               "most, address-harvesting bots, but it definitely won't fool all of\n" +
               "them. It's better than nothing, but an address published in this way\n" +
               "will probably eventually start receiving spam.)"),

        raw("<h3 id=\"backslash\">Backslash Escapes</h3>"),

        para("Markdown allows you to use backslash escapes to generate literal\n" +
             "characters which would otherwise have special meaning in Markdown's\n" +
             "formatting syntax. For example, if you wanted to surround a word with\n" +
             "literal asterisks (instead of an HTML <code><em></code> tag), you can backslashes\n" +
             "before the asterisks, like this:"),

        verb("\\*literal asterisks\\*\n"),

        para("Markdown provides backslash escapes for the following characters:"),

        verb("\\   backslash\n",
             "`   backtick\n",
             "*   asterisk\n",
             "_   underscore\n",
             "{}  curly braces\n",
             "[]  square brackets\n",
             "()  parentheses\n",
             "#   hash mark\n",
             "+	plus sign\n",
             "-	minus sign (hyphen)\n",
             ".   dot\n",
             "!   exclamation mark\n"))

    assert_equal expected, doc
  end

  def test_nested_blockquotes
    input = File.read "#{MARKDOWN_TEST_PATH}/Nested blockquotes.text"

    doc = @parser.parse input

    expected =
      doc(
        block(
          para("foo"),
          block(
            para("bar")),
          para("foo")))

    assert_equal expected, doc
  end

  def test_ordered_and_unordered_lists
    input = File.read "#{MARKDOWN_TEST_PATH}/Ordered and unordered lists.text"

    doc = @parser.parse input

    expected =
      doc(
        head(2, 'Unordered'),

        para('Asterisks tight:'),
        list(:BULLET,
          item(nil, para("asterisk 1")),
          item(nil, para("asterisk 2")),
          item(nil, para("asterisk 3"))),
        para('Asterisks loose:'),
        list(:BULLET,
          item(nil, para("asterisk 1")),
          item(nil, para("asterisk 2")),
          item(nil, para("asterisk 3"))),

        rule(1),

        para("Pluses tight:"),
        list(:BULLET,
          item(nil, para("Plus 1")),
          item(nil, para("Plus 2")),
          item(nil, para("Plus 3"))),
        para("Pluses loose:"),
        list(:BULLET,
          item(nil, para("Plus 1")),
          item(nil, para("Plus 2")),
          item(nil, para("Plus 3"))),

        rule(1),

        para("Minuses tight:"),
        list(:BULLET,
          item(nil, para("Minus 1")),
          item(nil, para("Minus 2")),
          item(nil, para("Minus 3"))),
        para("Minuses loose:"),
        list(:BULLET,
          item(nil, para("Minus 1")),
          item(nil, para("Minus 2")),
          item(nil, para("Minus 3"))),

        head(2, "Ordered"),

        para("Tight:"),
        list(:NUMBER,
          item(nil, para("First")),
          item(nil, para("Second")),
          item(nil, para("Third"))),
        para("and:"),
        list(:NUMBER,
          item(nil, para("One")),
          item(nil, para("Two")),
          item(nil, para("Three"))),

        para("Loose using tabs:"),
        list(:NUMBER,
          item(nil, para("First")),
          item(nil, para("Second")),
          item(nil, para("Third"))),
        para("and using spaces:"),
        list(:NUMBER,
          item(nil, para("One")),
          item(nil, para("Two")),
          item(nil, para("Three"))),

        para("Multiple paragraphs:"),
        list(:NUMBER,
          item(nil,
            para("Item 1, graf one."),
            para("Item 2. graf two. The quick brown fox " +
                 "jumped over the lazy dog's\nback.")),
          item(nil, para("Item 2.")),
          item(nil, para("Item 3."))),

        head(2, "Nested"),
        list(:BULLET,
          item(nil,
            para("Tab"),
            list(:BULLET,
              item(nil,
                para("Tab"),
                list(:BULLET,
                  item(nil,
                    para("Tab"))))))),

        para("Here's another:"),
        list(:NUMBER,
          item(nil, para("First")),
          item(nil, para("Second:"),
            list(:BULLET,
              item(nil, para("Fee")),
              item(nil, para("Fie")),
              item(nil, para("Foe")))),
          item(nil, para("Third"))),

        para("Same thing but with paragraphs:"),
        list(:NUMBER,
          item(nil, para("First")),
          item(nil, para("Second:"),
            list(:BULLET,
              item(nil, para("Fee")),
              item(nil, para("Fie")),
              item(nil, para("Foe")))),
          item(nil, para("Third"))),

        para("This was an error in Markdown 1.0.1:"),
        list(:BULLET,
          item(nil,
            para("this"),
            list(:BULLET,
              item(nil, para("sub"))),
            para("that"))))

    assert_equal expected, doc
  end

  def test_strong_and_em_together
    input = File.read "#{MARKDOWN_TEST_PATH}/Strong and em together.text"

    doc = @parser.parse input

    expected =
      doc(
        para("<b><em>This is strong and em.</em></b>"),
        para("So is <b>_this_</b> word."),
        para("<b><em>This is strong and em.</em></b>"),
        para("So is <b>_this_</b> word."))

    assert_equal expected, doc
  end

  def test_tabs
    input = File.read "#{MARKDOWN_TEST_PATH}/Tabs.text"

    doc = @parser.parse input

    expected =
      doc(
        list(:BULLET,
          item(nil,
            para("this is a list item\nindented with tabs")),
          item(nil,
            para("this is a list item\nindented with spaces"))),

        para("Code:"),

        verb("this code block is indented by one tab\n"),

        para("And:"),

        verb("\tthis code block is indented by two tabs\n"),

        para("And:"),

        verb(
          "+\tthis is an example list item\n",
          "\tindented with tabs\n",
          "\n",
          "+   this is an example list item\n",
          "    indented with spaces\n"))

    assert_equal expected, doc
  end

  def test_tidyness
    input = File.read "#{MARKDOWN_TEST_PATH}/Tidyness.text"

    doc = @parser.parse input

    expected =
      doc(
        block(
          para("A list within a blockquote:"),
          list(:BULLET,
            item(nil, para("asterisk 1")),
            item(nil, para("asterisk 2")),
            item(nil, para("asterisk 3")))))

    assert_equal expected, doc
  end

end