Fresh IDE . Check-in [61bfb9f52c]
Not logged in

This repository is a mirror!

The original is located on: https://fresh.flatassembler.net/fossil/repo/fresh
If you want to follow the project, please update your remote-url

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:The new MiniMag parser added to the library. A minor fix to the BBCode parser. Also, all markup parsers excluded from the data/all.asm and have to be included manually in order to not be processed when not needed.
Timelines: family | ancestors | descendants | both | FreshLibDev
Files: files | file ages | folders
SHA1:61bfb9f52c91fb95587d568554442a908f4b087d
User & Date: johnfound 2018-12-08 00:56:20
Context
2018-12-08
15:41
HTML encoded link definitions should work the proper way. check-in: 8da41cc13f user: johnfound tags: FreshLibDev
00:56
The new MiniMag parser added to the library. A minor fix to the BBCode parser. Also, all markup parsers excluded from the data/all.asm and have to be included manually in order to not be processed when not needed. check-in: 61bfb9f52c user: johnfound tags: FreshLibDev
2018-12-05
09:18
Fixed small bug in procedure Exec. check-in: 58b8de14c7 user: johnfound tags: FreshLibDev
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to freshlib/data/all.asm.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
include 'i18n.asm'
include 'arrays.asm'
include 'strlib.asm'
include 'base64.asm'
include 'uconfig.asm'
include 'memstream.asm'
include 'markdown.asm'
include 'bbcode.asm'
include 'hashes.asm'

include 'bitstream.asm'
include 'bytestream.asm'
include 'deflate.asm'

include 'buffergap.asm'
include 'counter_array.asm'
include 'random.asm'






<
<









1
2
3
4
5
6


7
8
9
10
11
12
13
14
15
include 'i18n.asm'
include 'arrays.asm'
include 'strlib.asm'
include 'base64.asm'
include 'uconfig.asm'
include 'memstream.asm'


include 'hashes.asm'

include 'bitstream.asm'
include 'bytestream.asm'
include 'deflate.asm'

include 'buffergap.asm'
include 'counter_array.asm'
include 'random.asm'

Changes to freshlib/data/bbcode.asm.

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
...
235
236
237
238
239
240
241

















242
243
244
245
246
247
248
249
250
251
252
253
254
255
...
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
  tagUL           onetag txt '<ul>',                                  0,              txt '</ul>',        fBlockTag or fMinimagBlock
  tagOL           onetag txt '<ol type="',                            txt '">',       txt '</ol>',        fBlockTag or fEmptyAttr or fMinimagBlock
  tagListItem     onetag txt '<li>',                                  0,              txt '</li>',        fBlockTag
  tagTable        onetag txt '<table>',                               0,              txt '</table>',     fBlockTag or fMinimagBlock
  tagTR           onetag txt '<tr>',                                  0,              txt '</tr>',        fBlockTag
  tagTH           onetag txt '<th>',                                  0,              txt '</th>',        fBlockTag
  tagTD           onetag txt '<td>',                                  0,              txt '</td>',        fBlockTag
  tagSpoiler      onetag txt '<details><summary>',                   txt '</summary>',txt '</details>',   fBlockTag or fMinimagBlock
  tagURL          onetag txt '<a href="',                             txt '">',       txt '</a>',         0
  tagImg          onetag txt '<img alt="',                            txt '" src="',  txt '" />',         fBlockTag or fDisableTags

  tagSize         onetag txt '<span style="font-size:',               txt '">',       txt '</span>',      fInlineTag
  tagColor        onetag txt '<span style="color:',                   txt '">',       txt '</span>',      fInlineTag
  tagEmail        onetag txt '<a href="mailto:',                      txt '">',       txt '</a>',         0
  tagH1           onetag txt '<h1>',                                  0,              txt '</h1>',        fBlockTag or fLines
  tagH2           onetag txt '<h2>',                                  0,              txt '</h2>',        fBlockTag or fLines
  tagH3           onetag txt '<h3>',                                  0,              txt '</h3>',        fBlockTag or fLines
  tagH4           onetag txt '<h4>',                                  0,              txt '</h4>',        fBlockTag or fLines
  tagH5           onetag txt '<h5>',                                  0,              txt '</h5>',        fBlockTag or fLines
  tagH6           onetag txt '<h6>',                                  0,              txt '</h6>',        fBlockTag or fLines
  tagPara         onetag txt '<p>',                                   0,              txt '</p>',         fLines
  tagHR           onetag txt '<hr>',                                  0,              0,                  0





  ; Emoticons tags.

  emoSmile        onetag <'<img class="emoticon" src="', EMOTICONS_PATH, 'smile.gif" alt=":)">'>,   0,              0,              0
  emoLol          onetag <'<img class="emoticon" src="', EMOTICONS_PATH, 'lol.gif" alt=":D">'>,     0,              0,              0
  emoRofl         onetag <'<img class="emoticon" src="', EMOTICONS_PATH, 'rofl.gif" alt=":D">'>,    0,              0,              0
  emoWink         onetag <'<img class="emoticon" src="', EMOTICONS_PATH, 'wink.gif" alt=";)">'>,    0,              0,              0
................................................................................

        cmp     al, $0a
        je      .new_line

        cmp     al, $20
        jbe     .next           ; skip all whitespace


















        cmp     al, "["
        je      .start_tag

.continue:
        cmp     [.fNewLine], 1
        mov     [.fNewLine], 0
        ja      .insert_paragraph

; html encoding from here

        test    al, al          ; all values > 127 are unicode and should not be encoded.
        js      .next

        movzx   eax, byte [tbl_html+eax]
................................................................................
        xor     al, byte [edx+ecx]
        cmp     al, $0a xor $0d
        jne     .loop
        jmp     .next   ; skip the next char. it is part of CRLF (or LFCR sequence).

;-----------------------------------------------------------------------------

.insert_paragraph:
        and     [.fNewLine], 0

        test    [.tagFlags], fDisableTags
        jnz     .loop

        stdcall TextMoveGap, edx, ebx
        stdcall TextSetGapSize, edx, 4
        mov     eax, [edx+TText.GapBegin]
        mov     dword [edx+eax], "<p>"
        add     [edx+TText.GapBegin], 3
        add     ebx, 3

        jmp     .loop

;------------------------------------------------------------------------------------

.start_tag:

        xor     ecx, ecx
        mov     [.ofsStart], ebx

.first_non_ws:
        inc     ebx







|

|
>











>
>
>
>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>




<
<
<







 







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







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
...
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
...
326
327
328
329
330
331
332

















333
334
335
336
337
338
339
  tagUL           onetag txt '<ul>',                                  0,              txt '</ul>',        fBlockTag or fMinimagBlock
  tagOL           onetag txt '<ol type="',                            txt '">',       txt '</ol>',        fBlockTag or fEmptyAttr or fMinimagBlock
  tagListItem     onetag txt '<li>',                                  0,              txt '</li>',        fBlockTag
  tagTable        onetag txt '<table>',                               0,              txt '</table>',     fBlockTag or fMinimagBlock
  tagTR           onetag txt '<tr>',                                  0,              txt '</tr>',        fBlockTag
  tagTH           onetag txt '<th>',                                  0,              txt '</th>',        fBlockTag
  tagTD           onetag txt '<td>',                                  0,              txt '</td>',        fBlockTag
  tagSpoiler      onetag txt '<details><summary>',                    txt '</summary>',txt '</details>',   fBlockTag or fMinimagBlock
  tagURL          onetag txt '<a href="',                             txt '">',       txt '</a>',         0
  tagImg          onetag txt '<img class="block" alt="',              txt '" src="',  txt '" />',         fBlockTag  or fDisableTags
  tagInlineImg    onetag txt '<img class="inline" alt="',             txt '" src="',  txt '" />',         fInlineTag or fDisableTags
  tagSize         onetag txt '<span style="font-size:',               txt '">',       txt '</span>',      fInlineTag
  tagColor        onetag txt '<span style="color:',                   txt '">',       txt '</span>',      fInlineTag
  tagEmail        onetag txt '<a href="mailto:',                      txt '">',       txt '</a>',         0
  tagH1           onetag txt '<h1>',                                  0,              txt '</h1>',        fBlockTag or fLines
  tagH2           onetag txt '<h2>',                                  0,              txt '</h2>',        fBlockTag or fLines
  tagH3           onetag txt '<h3>',                                  0,              txt '</h3>',        fBlockTag or fLines
  tagH4           onetag txt '<h4>',                                  0,              txt '</h4>',        fBlockTag or fLines
  tagH5           onetag txt '<h5>',                                  0,              txt '</h5>',        fBlockTag or fLines
  tagH6           onetag txt '<h6>',                                  0,              txt '</h6>',        fBlockTag or fLines
  tagPara         onetag txt '<p>',                                   0,              txt '</p>',         fLines
  tagHR           onetag txt '<hr>',                                  0,              0,                  0

  tagImgMM        onetag txt '<img class="block" src="',              txt '" alt="',  txt '" />',           fBlockTag  or fDisableTags
  tagInlineImgMM  onetag txt '<img class="inline" src="',             txt '" alt="',  txt '" />',           fInlineTag or fDisableTags
  tagVideoMM      onetag txt '<video controls><source src="',         txt '">',       txt '</video>',     0

  ; Emoticons tags.

  emoSmile        onetag <'<img class="emoticon" src="', EMOTICONS_PATH, 'smile.gif" alt=":)">'>,   0,              0,              0
  emoLol          onetag <'<img class="emoticon" src="', EMOTICONS_PATH, 'lol.gif" alt=":D">'>,     0,              0,              0
  emoRofl         onetag <'<img class="emoticon" src="', EMOTICONS_PATH, 'rofl.gif" alt=":D">'>,    0,              0,              0
  emoWink         onetag <'<img class="emoticon" src="', EMOTICONS_PATH, 'wink.gif" alt=";)">'>,    0,              0,              0
................................................................................

        cmp     al, $0a
        je      .new_line

        cmp     al, $20
        jbe     .next           ; skip all whitespace

        cmp     [.fNewLine], 1
        mov     [.fNewLine], 0
        jbe     .paragraph_ok

        test    [.tagFlags], fDisableTags
        jnz     .continue

        push    eax
        stdcall TextMoveGap, edx, ebx
        stdcall TextSetGapSize, edx, 4
        mov     eax, [edx+TText.GapBegin]
        mov     dword [edx+eax], "<p>"
        add     [edx+TText.GapBegin], 3
        add     ebx, 3
        pop     eax

.paragraph_ok:
        cmp     al, "["
        je      .start_tag

.continue:




; html encoding from here

        test    al, al          ; all values > 127 are unicode and should not be encoded.
        js      .next

        movzx   eax, byte [tbl_html+eax]
................................................................................
        xor     al, byte [edx+ecx]
        cmp     al, $0a xor $0d
        jne     .loop
        jmp     .next   ; skip the next char. it is part of CRLF (or LFCR sequence).

;-----------------------------------------------------------------------------


















.start_tag:

        xor     ecx, ecx
        mov     [.ofsStart], ebx

.first_non_ws:
        inc     ebx

Added freshlib/data/minimag.asm.

















































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>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
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
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
module "MiniMag translator"


struct TMDItem
  .type   dd ?
  .indent dd ?
ends


;--------------------------------------------------------------
; Converts MiniMag code into HTML. Starts from the position [.from]
; and ends at the end of the text or at first NULL character.
;
; Arguments:
;    .pMiniMag - pointer to the TText structure with the BBCode
;               formatted text.
;    .from    - the position where to start.
;
; Returns:
;    edx - The same structure (possibly reallocated) with
;    the translated to html code text.
;

mmfDontFormat = $80000000
mmfNoInline   = $40000000

proc TranslateMiniMag, .pText, .from
.pStack dd ?

.whspc  dd ?    ; whitespace symbols count;
.flags  dd ?
.hdr    dd ?    ; header level

.pLinks dd ?

.lbl_hash dd ?
.tmp_link dd ?

begin
        pushad

        xor     eax, eax
        mov     [.flags], eax

        stdcall GetMem, 256*4
        mov     [.pLinks], eax

        stdcall CreateArray, sizeof.TMDItem
        mov     [.pStack], eax

        mov     edx, [.pText]
        stdcall TextMoveGap, edx, [.from]

        mov     ecx, [.from]
        add     ecx, [edx+TText.GapEnd]
        sub     ecx, [edx+TText.GapBegin]
        dec     ecx

.fp_new_line:
        inc     ecx
        cmp     ecx, [edx+TText.Length]
        jae     .second_pass

        mov     al, [edx+ecx]
        test    al, al
        jz      .second_pass

        cmp     al, '['
        je      .link_def_found
        jmp     .fp_check_eol

.fp_skip_eol:
        inc     ecx
        cmp     ecx, [edx+TText.Length]
        jae     .second_pass

        mov     al, [edx+ecx]
        test    al, al
        jz      .second_pass

.fp_check_eol:
        cmp     al, $0d
        je      .fp_new_line

        cmp     al, $0a
        je      .fp_new_line

; encode HTML.

        test    al, al
        js      .fp_skip_eol

        and     eax, $ff
        movzx   eax, byte [tbl_html+eax]

        test    al, al
        jns     .fp_skip_eol

.do_encode:
        lea     esi, [eax+tbl_html]
        lodsb

        stdcall TextSetGapSize, edx, eax

        xchg    eax, ecx
        sub     eax, [edx+TText.GapEnd]
        add     eax, [edx+TText.GapBegin]
        stdcall TextMoveGap, edx, eax
        inc     [edx+TText.GapEnd]      ; delete the replaced char.

        mov     edi, [edx+TText.GapBegin]
        add     edi, edx
        add     [edx+TText.GapBegin], ecx
        rep movsb

        mov     ecx, [edx+TText.GapEnd]
        dec     ecx
        jmp     .fp_skip_eol


.link_def_found:

        mov     eax, ecx
        sub     eax, [edx+TText.GapEnd]
        add     eax, [edx+TText.GapBegin]

        stdcall TextMoveGap, edx, eax   ; move the gap to the beginning of the link.

        lea     edi, [ecx+1]  ; the start of the label.
        xor     esi, esi
        xor     eax, eax

.hash_loop:
        inc     ecx
        cmp     ecx, [edx+TText.Length]
        jae     .second_pass

        mov     al, [edx+ecx]
        test    al, al
        jz      .second_pass

        cmp     al, ']'
        je      .end_of_lbl

        test    al, al
        js      .add_hash

        mov     al, [tbl_html+eax]
        test    al, al
        js      .do_encode   ; not a valid link definition!
        jz      .fp_skip_eol

.add_hash:
        xor     esi, eax
        movzx   esi, byte [tpl_func + esi]
        jmp     .hash_loop

.end_of_lbl:
        mov     [.lbl_hash], esi

        mov     esi, ecx

        inc     ecx
        cmp     ecx, [edx+TText.Length]
        jae     .second_pass

        mov     al, [edx+ecx]
        cmp     al, ' '
        jne     .fp_skip_eol

; valid label has been found...
; first: extract the label.

        sub     esi, edi
        jz      .fp_skip_eol

        cmp     esi, $ff
        ja      .fp_skip_eol

        lea     edi, [edx+edi]

        stdcall StrNew
        stdcall StrCharCat, eax, esi
        stdcall StrCatMem, eax, edi, esi
        mov     [.tmp_link], eax

.link_url_start:
        inc     ecx
        cmp     ecx, [edx+TText.Length]
        jae     .free_link

        mov     al, [edx+ecx]
        test    al, al
        jz      .free_link

        cmp     al, $0d
        je      .free_link
        cmp     al, $0a
        je      .free_link

        cmp     al, ' '
        jbe     .link_url_start

        mov     edi, ecx
        mov     esi, ecx

.loop_to_end_of_link:
        inc     ecx
        cmp     ecx, [edx+TText.Length]
        jae     .free_link

        mov     al, [edx+ecx]
        test    al, al
        jz      .end_found

        cmp     al, $0d
        je      .end_of_link
        cmp     al, $0a
        je      .end_of_link

        cmp     al, ' '
        jbe     .loop_to_end_of_link

        lea     esi, [ecx+1]
        jmp     .loop_to_end_of_link


.end_of_link:
        xor     al, $0d xor $0a

        inc     ecx
        cmp     ecx, [edx+TText.Length]
        jae     .end_found

        cmp     al, [edx+ecx]
        je      .end_found

        dec     ecx

.end_found:
        sub     esi, edi
        jz      .free_link

        lea     edi, [edx+edi]
        stdcall StrCatMemHTML, [.tmp_link], edi, esi

        mov     eax, [.lbl_hash]
        mov     edi, [.pLinks]
        mov     esi, [.tmp_link]

.hash_search_slot:                      ; resolve possible hash collisions.
        xchg    esi, [edi+4*eax]
        test    esi, esi
        jz      .end_link

        inc     al
        cmp     eax, [.lbl_hash]
        jne     .hash_search_slot

        stdcall StrDel, esi     ; the hash table is full, so simply delete the remaining link.

.end_link:
        mov     [edx+TText.GapEnd], ecx         ; delete the entire line of the link definition.
        jmp     .fp_new_line

.free_link:
        stdcall StrDel, [.tmp_link]

        cmp     ecx, [edx+TText.Length]
        jae     .second_pass
        jmp     .skip_to_eol


; Second processing pass. It will format all the tags.

.second_pass:

        mov     ebx, [.from]

.start_of_line:

        xor     eax, eax
        mov     [.hdr], eax
        mov     byte [.flags], al
        and     [.flags], not mmfNoInline

        stdcall TextMoveGap, edx, ebx

        xor     eax, eax
        mov     [.whspc], eax

.loop:
        mov     ecx, ebx
        add     ecx, [edx+TText.GapEnd]
        sub     ecx, [edx+TText.GapBegin]
        cmp     ecx, [edx+TText.Length]
        jae     .empty_line_found

        mov     al, [edx+ecx]

        test    al, al
        jz      .empty_line_found

        cmp     al, ';'
        je      .command_found

        test    [.flags], mmfDontFormat
        jnz     .format_ok

        cmp     al, '#'
        je      .header_found

        cmp     al, '*'
        je      .list_item_found

.format_ok:
        cmp     al, $0d
        je      .empty_line_found
        cmp     al, $0a
        je      .empty_line_found

        cmp     al, ' '
        ja      .non_empty_line_found

        inc     [.whspc]
        inc     [edx+TText.GapEnd]
        jmp     .loop


.command_found:

        cmp     [.whspc], 0
        jne     .non_empty_line_found

        test    [.flags], mmfDontFormat
        jnz     .check_for_end

        lea     eax, [ecx+8]             ; spoiler
        cmp     eax, [edx+TText.Length]
        jae     .less_than_8

        cmp     dword [edx+ecx], ';spo'
        jne     .not_spoiler
        cmp     dword [edx+ecx+4], 'iler'
        jne     .not_spoiler
        cmp     byte [edx+ecx+8], ' '
        ja      .not_spoiler

        call    .close_all_non_minimag

        push    ecx
        mov     eax, tagSpoiler
        call    .add_to_stack
        pop     ecx

        mov     eax, tagSpoiler
        call    .prefix_tag

        add     ecx, 8
        add     [edx+TText.GapEnd], 8

        push    ebx
        call    .skip_tag_arg
        pop     eax
        cmp     eax, ebx
        jne     .sum_ok

        mov     ecx, [edx+TText.GapBegin]
        mov     dword [edx+ecx], 'Spoi'
        mov     dword [edx+ecx+4], 'ler '
        add     [edx+TText.GapBegin], 7
        add     ebx, 7

.sum_ok:
        mov     eax, tagSpoiler
        call    .suffix_tag

        jmp     .start_of_line


.not_spoiler:
.less_than_8:
        lea     eax, [ecx+6]
        cmp     eax, [edx+TText.Length]
        jae     .less_than_6

        cmp     dword [edx+ecx], ';quo'
        jne     .not_quote
        cmp     word [edx+ecx+4], 'te'
        jne     .not_quote
        cmp     byte [edx+ecx+6], ' '
        ja      .not_quote

; quote command here

        call    .close_all_non_minimag

        push    ecx
        mov     eax, tagQuote
        call    .add_to_stack
        pop     ecx

        mov     eax, tagQuote
        call    .prefix_tag

        add     ecx, 6
        add     [edx+TText.GapEnd], 6

        call    .skip_tag_arg

        mov     eax, tagQuote
        call    .suffix_tag

        jmp     .start_of_line


.not_quote:

        cmp     dword [edx+ecx], ';tab'
        jne     .not_table
        cmp     word [edx+ecx+4], 'le'
        jne     .not_table
        cmp     byte [edx+ecx+6], ' '
        ja      .not_table

; table command here

        call    .close_all_non_minimag

        mov     eax, tagTable
        call    .open_tag

        jmp     .delete_to_eol


.not_table:
        cmp     dword [edx+ecx], ';beg'
        jne     .not_begin
        cmp     word [edx+ecx+4], 'in'
        jne     .not_begin
        cmp     byte [edx+ecx+6], ' '
        ja      .not_begin

        or      [.flags], mmfDontFormat

        call    .close_all_non_minimag

        push    ecx
        mov     eax, tagCode
        call    .add_to_stack
        pop     ecx

        mov     eax, tagCode
        call    .prefix_tag

        add     ecx, 6
        add     [edx+TText.GapEnd], 6

        call    .skip_tag_arg

        mov     eax, tagCode
        call    .suffix_tag

        jmp     .start_of_line


.not_begin:

        cmp     dword [edx+ecx], ';uli'
        jne     .not_ulist
        cmp     word [edx+ecx+4], 'st'
        jne     .not_ulist
        cmp     byte [edx+ecx+6], ' '
        ja      .not_ulist

        call    .close_all_non_minimag

        mov     eax, tagUL
        call    .open_tag

        jmp     .delete_to_eol


.not_ulist:
        cmp     dword [edx+ecx], ';oli'
        jne     .not_olist
        cmp     word [edx+ecx+4], 'st'
        jne     .not_olist
        cmp     byte [edx+ecx+6], ' '
        ja      .not_olist

        call    .close_all_non_minimag

        mov     eax, tagOL
        call    .open_tag

        jmp     .delete_to_eol


.not_olist:

.less_than_6:
.less_than_5:

.check_for_end:

        lea     eax, [ecx+4]
        cmp     eax, [edx+TText.Length]
        jae     .less_than_4

        cmp     dword [edx+ecx], ';end'
        jne     .not_end

        cmp     byte [edx+ecx+4], ' '
        ja      .not_end

; end command here

        call    .close_all_non_minimag
        call    .pop_one_tag

        and     [.flags], not mmfDontFormat

        jmp     .delete_to_eol


.not_end:
        test    [.flags], mmfDontFormat
        jnz     .non_empty_line_found

        cmp     dword [edx+ecx], ';---'
        jne     .not_ruler

; it is a horizontal ruller command. It can separate paragraphs by <hr> tag, or the table rows by <tr> tags.

        push    ecx
        mov     esi, [.pStack]


.row_close:
        mov     ecx, [esi+TArray.count]
        dec     ecx
        js      .put_hr ; empty stack!

        mov     eax, [esi+TArray.array + sizeof.TMDItem * ecx + TMDItem.type]

        cmp     eax, tagTD
        je      .close_this
        cmp     eax, tagTH
        je      .close_this
        cmp     eax, tagTR
        je      .close_this

        cmp     eax, tagTable
        je      .open_tr

.put_hr:
        mov     eax, tagHR
        call    .prefix_tag
        jmp     .end_hr

.close_this:
        mov     [esi+TArray.count], ecx
        call    .close_tag
        jmp     .row_close

.open_tr:
        mov     eax, tagTR
        call    .open_tag

.end_hr:
        pop     ecx
        jmp     .delete_to_eol


.less_than_4:
.not_ruler:

; it is simply a comment. Delete it up to the end of the line.

.delete_to_eol:
        inc     ecx
        cmp     ecx, [edx+TText.Length]
        jae     .eol_ok

        mov     al, [edx+ecx]
        cmp     al, $0d
        je      .eol_found2

        cmp     al, $0a
        jne     .delete_to_eol

.eol_found2:
        inc     ecx
        xor     al, $0d xor $0a
        cmp     al, [edx+ecx]
        jne     .eol_ok
        inc     ecx
.eol_ok:
        mov     [edx+TText.GapEnd], ecx
        jmp     .start_of_line


.list_item_found:

        inc     ecx
        cmp     ecx, [edx+TText.Length]
        jae     .non_empty_line_found

        cmp     byte [edx+ecx], ' '
        ja      .non_empty_line_found

        add     [edx+TText.GapEnd], 2

; here list item begins:

        mov     esi, [.pStack]
        mov     ecx, [esi+TArray.count]
        dec     ecx
        js      .add_leaf

        mov     eax, [esi + TArray.array + sizeof.TMDItem * ecx + TMDItem.type]
        cmp     eax, tagUL
        je      .open_li
        cmp     eax, tagOL
        jne     .add_leaf

.open_li:
        mov     eax, tagListItem
        call    .open_tag
        jmp     .add_leaf


.headers dd tagH1, tagH2, tagH3, tagH4, tagH5, tagH6

.header_found:

        xor     eax, eax

.hcnt:
        inc     eax
        inc     ecx
        cmp     ecx, [edx+TText.Length]
        jae     .non_empty_line_found           ; not a header?

        cmp     byte [edx+ecx], '#'
        je      .hcnt

        cmp     eax, 6
        ja      .non_empty_line_found

        cmp     byte [edx+ecx], ' '
        jne     .non_empty_line_found           ; not a header?

        stc
        adc     [edx+TText.GapEnd], eax

        mov     [.hdr], eax


.non_empty_line_found:
        mov     esi, [.pStack]
        mov     ecx, [esi+TArray.count]
        dec     ecx
        js      .add_leaf

        mov     eax, [esi + TArray.array + sizeof.TMDItem * ecx + TMDItem.type] ; don't add paragraphs inside elements that can handle text by themselves.
        test    [eax+TOneTag.flags], fLines
        jnz     .skip_to_eol

        cmp     eax, tagCode
        je      .skip_to_eol

        cmp     eax, tagTable
        jne     .check_for_row

        mov     eax, tagTR
        call    .open_tag
        jmp     .open_td_th

.check_for_row:
        cmp     eax, tagTR
        jne     .add_leaf

.open_td_th:
        mov     eax, tagTD
        cmp     [.hdr], 0
        je      .open_td
        mov     eax, tagTH
        and     [.hdr], 0
.open_td:
        call    .open_tag

.add_leaf:
        mov     eax, tagPara

        mov     ecx, [.hdr]
        dec     ecx
        js      .do_add_leaf
        mov     eax, [.headers + 4*ecx]

.do_add_leaf:
        call    .open_tag
        jmp     .skip_to_eol


.empty_line_found:

        mov     esi, [.pStack]
        mov     ecx, [esi+TArray.count]

.loop_empty_line:
        dec     ecx
        js      .skip_to_eol   ; the empty line is not inside code block because there is no blocks at all.

        mov     eax, [esi + TArray.array + sizeof.TMDItem*ecx + TMDItem.type]
        test    [eax+TOneTag.flags], fLines or fInlineTag
        jz      .check_wrap

        cmp     eax, tagInlineCode
        jne     .inline_code_ok
        and     [.flags], not mmfDontFormat
.inline_code_ok:

        mov     [esi+TArray.count], ecx ; close text leaf blocks.
        call    .close_tag
        jmp     .loop_empty_line

        dec     ecx
        js      .skip_to_eol   ; the empty line is not inside code block because there is no blocks at all.

.check_wrap:
        cmp     eax, tagTD
        je      .close_wrap
        cmp     eax, tagTH
        je      .close_wrap
        cmp     eax, tagListItem
        jne     .skip_to_eol

.close_wrap:
        mov     [esi+TArray.count], ecx
        call    .close_tag
        jmp     .skip_to_eol

; Here seems to be a good place for processing the inline formatting elements, together with the HTML encoding.

.skip_to_eol:

        xor     eax, eax
        mov     ecx, ebx
        add     ecx, [edx+TText.GapEnd]
        sub     ecx, [edx+TText.GapBegin]
        cmp     ecx, [edx+TText.Length]
        jae     .end_of_text

        mov     al, [edx+ecx]
        test    al, al
        jz      .end_of_text
        cmp     al, $0d
        je      .eol_found
        cmp     al, $0a
        je      .eol_found

        cmp     al, '`'
        je      .code

        test    [.flags], mmfDontFormat
        jnz     .next

        test    [.flags], mmfNoInline
        jnz     .inline_ok

        cmp     al, '*'
        je      .bold
        cmp     al, '/'
        je      .italic
        cmp     al, '_'
        je      .underline
        cmp     al, '-'
        je      .del

.inline_ok:
        cmp     al, '['
        je      .link

        mov     byte [.flags], al       ; the previous character.

.next:
        inc     ebx
        jmp     .skip_to_eol


.eol_found:
        mov     ah, al
        xor     ah, $0d xor $0a

        inc     ecx
        inc     ebx
        cmp     [edx+ecx], ah
        jne     .start_of_line
        inc     ebx
        jmp     .start_of_line


.link:
locals
  .linkT dd ?
  .imgW  dd ?
  .imgH  dd ?
  .linkStart dd ?
  .lblStart dd ?
  .lblLen   dd ?
  .urlLen   dd ?
  .level    dd ?
endl
        mov     ebx, ecx
        sub     ebx, [edx+TText.GapEnd]
        add     ebx, [edx+TText.GapBegin]
        stdcall TextMoveGap, edx, ebx

        inc     ecx
        cmp     ecx, [edx+TText.Length]
        jae     .next

        xor     eax, eax
        mov     [.linkT], eax

        mov     al, [edx+ecx]
        test    al, al
        jz      .next

        cmp     al, '!'
        je      .block_image

        cmp     al, '?'
        je      .inline_image

        cmp     al, '$'
        je      .video

        mov     [.linkT], tagURL
        jmp     .image_ok

.video:
        mov     [.linkT], tagVideoMM
        jmp     .image

.block_image:
        mov     [.linkT], tagImgMM
        jmp     .image

.inline_image:

        mov     [.linkT], tagInlineImgMM

.image:
        inc     ecx
        cmp     ecx, [edx+TText.Length]
        jae     .next

        mov     al, [edx+ecx]
        test    al, al
        jz      .next

        cmp     al, '0'
        jb      .image_ok
        cmp     al, '9'
        ja      .image_ok

; For now...
.ignore_size:
        inc     ecx
        cmp     ecx, [edx+TText.Length]
        jae     .next

        mov     al, [edx+ecx]
        test    al, al
        jz      .next

        cmp     al, ':'
        jne     .ignore_size

        inc     ecx
        cmp     ecx, [edx+TText.Length]
        jae     .next

        mov     al, [edx+ecx]
        test    al, al
        jz      .next

.image_ok:
; here eax contains the current byte of the address label.
; The first value of the hash is computed outside the hash loop.

        mov     edi, ecx  ; the start of the label.
        movzx   esi, byte [tpl_func + eax]    ; the initial value of the hash.

.hash_loop2:
        inc     ecx
        cmp     ecx, [edx+TText.Length]
        jae     .next

        mov     al, [edx+ecx]
        test    al, al
        jz      .next

        cmp     al, ']'
        je      .end_of_link_lbl

        xor     esi, eax
        movzx   esi, byte [tpl_func + esi]
        jmp     .hash_loop2

.end_of_link_lbl:
        mov     [.lbl_hash], esi

        mov     eax, ecx
        sub     eax, edi

        mov     [.lblStart], edi
        mov     [.lblLen], eax

; search the label in the hash table.

        push    ebx ecx

        mov     ebx, [.lbl_hash]

.lbl_search:
        mov     edi, [.pLinks]
        mov     eax, [edi+4*ebx]
        test    eax, eax
        jz      .hash_not_found

        stdcall StrPtr, eax
        mov     esi, eax
        mov     edi, edx
        add     edi, [.lblStart]
        mov     ecx, [.lblLen]

        mov     eax, [esi+string.len]
        mov     [.urlLen], eax

        movzx   eax, byte [esi] ; the first byte is the length of the label part.
        inc     esi

        sub     [.urlLen], eax
        dec     [.urlLen]

        cmp     ecx, eax
        jne     .not_this_hash

        repe cmpsb
        je      .insert_link

.not_this_hash:
        inc     bl
        cmp     ebx, [.lbl_hash]
        jne     .lbl_search

.hash_not_found:
; the link is not in the hash table, so it is a inline link.

        mov     esi, [.lblStart]
        add     esi, edx

        mov     eax, [.lblLen]
        mov     [.urlLen], eax

.insert_link:
; the label matches! Insert the tag prefix.

        pop     ecx ebx

        mov     eax, [.lblStart]
        mov     [edx+TText.GapEnd], eax ; delete the start "["

        mov     eax, [.linkT]
        call    .prefix_tag

; copy the url here. It is pointed by ESI right now.

        stdcall TextSetGapSize, edx, [.urlLen]
        mov     edi, [edx+TText.GapBegin]
        add     edi, edx
        mov     ecx, [.urlLen]
        add     ebx, ecx
        add     [edx+TText.GapBegin], ecx

        rep movsb

        mov     eax, [.linkT]
        call    .suffix_tag

; Here check for additional link content/alt content.

        mov     ecx, [edx+TText.GapEnd]
        add     ecx, [.lblLen]

        inc     ecx
        cmp     ecx, [edx+TText.Length]
        jae     .skip_link_content

        mov     al, [edx+ecx]
        cmp     al, '['
        jne     .skip_link_content

        lea     eax, [ecx+1]
        mov     [.lblStart], eax

        and     [.level], 0

.search_second_end:
        inc     ecx
        cmp     ecx, [edx+TText.Length]
        jae     .skip_link_content

        mov     al, [edx+ecx]
        test    al, al
        jz      .skip_link_content

        cmp     al, $0d
        je      .skip_link_content
        cmp     al, $0a
        je      .skip_link_content

        cmp     al, '['         ; nested link
        jne     .nested_ok
        inc     [.level]

.nested_ok:
        cmp     al, ']'
        jne     .search_second_end

        cmp     [.level], 0
        je      .second_end

        dec     [.level]
        jmp     .search_second_end


.second_end:
        sub     ecx, [.lblStart]
        mov     [.lblLen], ecx

        mov     ecx, [.lblStart]
        mov     [edx+TText.GapEnd], ecx ; delete the link URL/label part, because there is second one.

        push    ebx

        add     ebx, [.lblLen]
        stdcall TextMoveGap, edx, ebx
        inc     [edx+TText.GapEnd]

        mov     eax, [.linkT]
        call    .close_tag

        pop     ebx
        stdcall TextMoveGap, edx, ebx

        or      [.flags], mmfNoInline
        jmp     .skip_to_eol


.skip_link_content:

        add     ebx, [.lblLen]
        stdcall TextMoveGap, edx, ebx
        inc     [edx+TText.GapEnd]

        mov     eax, [.linkT]
        call    .close_tag

        and     [.flags], not mmfNoInline
        jmp     .skip_to_eol



.bold:
        mov     eax, tagStrong
        jmp     .inline_tag

.italic:
        mov     eax, tagEm
        jmp     .inline_tag

.underline:
        mov     eax, tagUnderlined
        jmp     .inline_tag

.del:
        mov     eax, tagDel
        jmp     .inline_tag

.code:
        mov     eax, tagInlineCode

.inline_tag:

        stdcall TextMoveGap, edx, ebx

        mov     esi, [.pStack]
        mov     ecx, [esi+TArray.count]
        dec     ecx
        js      .open_inline

        cmp     eax, [esi+TArray.array + sizeof.TMDItem*ecx + TMDItem.type]
        jne     .open_inline

        mov     [esi+TArray.count], ecx
        cmp     eax, tagInlineCode
        jne     @f
        and     [.flags], not mmfDontFormat
@@:
        call    .close_tag
        jmp     .end_inline

.open_inline:
        cmp     byte [.flags], ' '      ; the previous character.
        ja      .next

        mov     ecx, ebx
        add     ecx, [edx+TText.GapEnd]
        sub     ecx, [edx+TText.GapBegin]
        cmp     byte [edx+ecx+1], ' '
        jbe     .next

        cmp     eax, tagInlineCode
        jne     @f
        or      [.flags], mmfDontFormat
@@:
        call    .open_tag

.end_inline:
        inc     [edx+TText.GapEnd]
        jmp     .skip_to_eol


.end_of_text:

; Close all not closed tags:

        stdcall TextMoveGap, edx, ebx
        mov     edi, [.pStack]
        mov     ecx, [edi+TArray.count]

.close_all:
        dec     ecx
        js      .finalize

        mov     eax, [edi + TArray.array + sizeof.TMDItem*ecx + TMDItem.type]
        call    .close_tag
        jmp     .close_all

.finalize:
        mov     esi, [.pLinks]
        mov     ecx, $ff

.freelinks:
        stdcall StrDel, [esi+4*ecx]
        dec     ecx
        jns     .freelinks

        stdcall FreeMem, esi
        stdcall FreeMem, [.pStack]

        mov     [esp + 4*regEDX], edx
        popad
        return



.close_all_non_minimag:
        push    ecx esi

        mov     esi, [.pStack]
        mov     ecx, [esi+TArray.count]

.close_loop:
        dec     ecx
        js      .end_close

        mov     eax, [esi+TArray.array + sizeof.TMDItem*ecx + TMDItem.type]
        test    [eax+TOneTag.flags], fMinimagBlock
        jnz     .end_close

        mov     [esi+TArray.count], ecx
        call    .close_tag
        jmp     .close_loop

.end_close:
        pop     esi ecx
        retn


.pop_one_tag:
        push    ecx esi

        mov     esi, [.pStack]
        mov     ecx, [esi+TArray.count]
        dec     ecx
        js      .end_pop_one

        mov     [esi+TArray.count], ecx
        mov     eax, [esi+TArray.array + sizeof.TMDItem*ecx + TMDItem.type]
        call    .close_tag

.end_pop_one:
        pop     esi ecx
        retn


.open_tag:
        push    ecx esi edi
        push    eax eax
        call    .add_to_stack
        pop     eax
        call    .prefix_tag
        pop     eax
        call    .suffix_tag
        pop     edi esi ecx
        retn


.suffix_tag:
        lea     eax, [eax+TOneTag.pOpenS]
        jmp     .copy_tag

.close_tag:
        lea     eax, [eax+TOneTag.pClose]

.prefix_tag:
.copy_tag:
        push    ecx esi edi

        mov     esi, [eax]
        mov     ecx, [eax+4]
        test    esi, esi
        jz      .tag_copy_end

        add     ebx, ecx

        stdcall TextSetGapSize, edx, ecx
        mov     edi, [edx+TText.GapBegin]
        add     edi, edx
        add     [edx+TText.GapBegin], ecx

        rep movsb

.tag_copy_end:
        pop     edi esi ecx
        retn


.add_to_stack:
        push    edx eax
        stdcall AddArrayItems, [.pStack], 1
        mov     [.pStack], edx
        pop     [eax + TMDItem.type]
        pop     edx
        retn


.skip_tag_arg:
        push    eax ecx esi

.skip_tag_loop:
        inc     ecx
        cmp     ecx, [edx+TText.Length]
        jae     .end_arg

        mov     al, [edx+ecx]
        test    al, al
        jz      .end_arg

        cmp     al, $0a
        je      .eol1
        cmp     al, $0d
        je      .eol1

        cmp     al, ' '
        jbe     .skip_tag_loop

        mov     [edx+TText.GapEnd], ecx
        mov     esi, ebx
        dec     ecx

.search_eol:
        inc     ecx
        inc     esi
        cmp     ecx, [edx+TText.Length]
        jae     .end_arg

        movzx   eax, byte [edx+ecx]
        test    al, al
        jz      .end_arg

        cmp     al, $0a
        je      .eol1

        cmp     al, $0d
        je      .eol1

        cmp     al, ' '
        cmova   ebx, esi
        jmp     .search_eol

.eol1:
        inc     ecx
        xor     al, $0d xor $0a
        cmp     al, [edx+ecx]
        jne     .end_arg
        inc     ecx
.end_arg:
        stdcall TextMoveGap, edx, ebx
        mov     [edx+TText.GapEnd], ecx

        pop     esi ecx eax
        retn

endp





proc DumpStack, .pStack
begin
        pushad

        mov     esi, [.pStack]
        xor     ecx, ecx

.loop:
        cmp     ecx, [esi+TArray.count]
        jae     .finish

        mov     edi, [esi+TArray.array + sizeof.TMDItem*ecx + TMDItem.type]

        stdcall FileWrite, [STDERR], [edi+TOneTag.pOpenP], [edi+TOneTag.lOpenP]
        stdcall FileWrite, [STDERR], [edi+TOneTag.pOpenS], [edi+TOneTag.lOpenS]

        stdcall FileWriteString, [STDERR], txt ": "

        stdcall NumToStr, [esi+TArray.array + sizeof.TMDItem*ecx + TMDItem.indent], ntsDec
        push    eax
        stdcall FileWriteString, [STDERR], eax
        stdcall StrDel ; from the stack

        stdcall FileWriteString, [STDERR], <txt 13, 10>

        inc     ecx
        jmp     .loop

.finish:
        popad
        return

endp



proc StrCatMemHTML, .dest, .psource, .length
begin
        pushad

        mov     esi, [.psource]
        mov     ecx, [.length]
        xor     eax, eax

.loop:
        dec     ecx
        js      .finish

        lodsb
        test    al, al
        js      .store

        mov     al, [tbl_html+eax]
        test    al, al
        jz      .loop
        jns     .store

        push    esi ecx

        lea     esi, [eax+tbl_html]
        lodsb

        stdcall StrCatMem, [.dest], esi, eax
        pop     ecx esi
        jmp     .loop

.store:
        stdcall StrCharCat, [.dest], eax
        jmp     .loop

.finish:
        popad
        return
endp







; Not finished, not working!!!

proc TranslateMarkdown, .pText, .from
.pStack dd ?

.qlvl   dd ?    ; quote level.
.whspc  dd ?    ; whitespace symbols count;
.flags  dd ?
.hdr    dd ?    ; header level

.q_from dd ?

.prev   dd ?

begin
        pushad

        stdcall CreateArray, sizeof.TMDItem
        mov     [.pStack], eax

        mov     edx, [.pText]
        mov     ebx, [.from]

.start_of_line:

        xor     eax, eax
        mov     [.qlvl], eax
        mov     [.flags], eax
        mov     [.hdr], eax

        dec     eax
        mov     [.q_from], eax          ; start from -1 on every line.

        stdcall TextMoveGap, edx, ebx

.zerospc:
        xor     eax, eax
        mov     [.whspc], eax

.loop:
        mov     ecx, ebx
        add     ecx, [edx+TText.GapEnd]
        sub     ecx, [edx+TText.GapBegin]

        mov     al, [edx+ecx]

        test    al, al
        jz      .empty_line_found

        cmp     al, '>'
        je      .quote_found

        cmp     al, '-'
        je      .ul_found
        cmp     al, '*'
        je      .ul_found
        cmp     al, '+'
        je      .ul_found

        cmp     al, '#'
        je      .header_found

        cmp     al, $0d
        je      .empty_line_found
        cmp     al, $0a
        je      .empty_line_found

        cmp     al, ' '
        ja      .non_empty_line_found

        inc     [.whspc]
        inc     [edx+TText.GapEnd]
        jmp     .loop


; closes all tags up to position ecx (excluding the tag on ecx)
.close_to_ecx:
        push    eax

.ecx_loop:
        mov     eax, [esi+TArray.count]
        dec     eax
        cmp     eax, ecx
        je      .end_close_ecx

        mov     [esi+TArray.count], eax
        mov     eax, [esi+TArray.array + sizeof.TMDItem*eax + TMDItem.type]
        call    .close_tag
        jmp     .ecx_loop

.end_close_ecx:
        pop     eax
        retn


; closes all tags up to position ecx (includig the tag on ecx)
.close_to_ecx_inc:
        push    eax

.ecx_loop2:
        mov     eax, [esi+TArray.count]
        dec     eax
        cmp     eax, ecx
        jl      .end_close_ecx

        mov     [esi+TArray.count], eax
        mov     eax, [esi+TArray.array + sizeof.TMDItem*eax + TMDItem.type]
        call    .close_tag
        jmp     .ecx_loop2


; Searches the tags stack for listitem and returns it's index in the stack.
; Arguments:
;   esi - pointer to the stack array.
; Returns:
;   esi - pointer to the tag stack.
;   If the list item has been found: CF = 0; ecx = index in the array;
;   If the list item has not been found: CF = 1
; Modifies eax

.search_for_li:

        mov     esi, [.pStack]
        mov     ecx, [esi+TArray.count]

.search_li:
        dec     ecx
        cmp     ecx, [.q_from]
        jle     .li_not_found

        mov     eax, [esi+TArray.array + sizeof.TMDItem*ecx + TMDItem.type]

        cmp     eax, tagListItem
        je      .li_found

        cmp     eax, tagQuote
        jne     .search_li

.li_not_found:
        stc
        retn

.li_found:
        clc
        retn


.search_for_bq:

        mov     esi, [.pStack]
        mov     ecx, [.q_from]

.search_bq:
        inc     ecx
        cmp     ecx, [esi+TArray.count]
        jae     .bq_not_found

        cmp     [esi+TArray.array + sizeof.TMDItem*ecx + TMDItem.type], tagQuote
        jne     .search_bq

        clc
        retn

.bq_not_found:
        stc
        retn


.ul_found:
        call    .check_for_code_block
        jc      .skip_to_eol

        inc     ecx
        cmp     ecx, [edx+TText.Length]
        jae     .empty_line_found

        cmp     byte [edx+ecx], ' '     ; mandatory space
        ja      .non_empty_line_found

        add     [edx+TText.GapEnd], 2   ; remove the marker.

.search_list:

        call    .search_for_li
        jnc     .list_found

        call    .close_to_ecx
        jmp     .create_new_ul


.list_found:

        OutputValue "List found at: ", [.whspc], 10, -1

        mov     edi, [.whspc]
        sub     edi, [esi + TArray.array + sizeof.TMDItem*ecx + TMDItem.indent]

        call    .close_to_ecx

        cmp     edi, 4
        jge     .create_new_ul  ; create list one level of nesting up.

        mov     [esi + TArray.count], ecx  ; close the current listitem.
        call    .close_tag

        cmp     edi, -4
        jg      .create_new_li

        dec     ecx

        mov     eax, [esi+TArray.array + sizeof.TMDItem*ecx + TMDItem.type]             ; eax should be tagUL or tagOL and nothing else!
        mov     [esi+TArray.count], ecx
        call    .close_tag             ; close the list as well. (can be UL or OL!)

        jmp     .search_list           ; and then search back for the list on the previous level.


.create_new_ul:

        mov     eax, tagUL
        call    .open_tag

.create_new_li:
        OutputValue "List item created on:", [.whspc], 10, -1

        mov     eax, tagListItem
        call    .open_tag
        jmp     .loop           ; after creating list items, the whitespace for the current line should not be reset.


.check_for_code_block:

        mov     esi, [.pStack]
        mov     eax, [esi+TArray.count]
        dec     eax
        js      .not_a_code_block

        cmp     [esi+TArray.array + sizeof.TMDItem*eax + TMDItem.type], tagCode
        jne     .not_a_code_block

; there is open code block
        cmp     [.whspc], 4
        jb      .close_code_block

        stc
        retn

.close_code_block:

        mov     eax, tagCode
        call    .close_tag

.not_a_code_block:
        clc
        retn


.quote_found:

        DebugMsg "Quote found"

        mov     eax, [.q_from]
        OutputValue "Search quote from: ", eax, 10, -1

.quote_search:
        inc     eax
        mov     [.q_from], eax
        cmp     eax, [esi+TArray.count]
        jae     .create_new_quote

        cmp     [esi+TArray.array+sizeof.TMDItem*eax+TMDItem.type], tagQuote
        je      .del_marker
        jmp     .quote_search

.create_new_quote:

        DebugMsg "New quote created"

        call    .check_for_code_block
        jc      .skip_to_eol

        mov     eax, tagQuote
        call    .open_tag

        mov     eax, [esi+TArray.count]
        dec     eax
        mov     [.q_from], eax


.del_marker:

        DebugMsg "Delete quote marker."
        inc     [edx+TText.GapEnd]

        inc     ecx
        cmp     ecx, [edx+TText.Length]
        jae     .empty_line_found

        cmp     byte [edx+ecx], ' '     ; optional space.
        jne     .zerospc

        inc     [edx+TText.GapEnd]
        jmp     .zerospc




.headers dd tagH1, tagH2, tagH3, tagH4, tagH5, tagH6

.header_found:

        cmp     [.whspc], 4
        jae     .code_block

        xor     eax, eax

.hcnt:
        inc     eax
        inc     ecx
        cmp     ecx, [edx+TText.Length]
        jae     .non_empty_line_found           ; not a header?

        cmp     byte [edx+ecx], '#'
        je      .hcnt

        cmp     eax, 6
        ja      .non_empty_line_found

        cmp     byte [edx+ecx], ' '
        jne     .non_empty_line_found           ; not a header?

        stc
        adc     [edx+TText.GapEnd], eax

        mov     [.hdr], eax

.non_empty_line_found:

        OutputValue "Non empty line indent: ", [.whspc], 10, -1

        mov     esi, [.pStack]
        mov     ecx, [esi+TArray.count]
        dec     ecx
        js      .add_leaf

        mov     ecx, [esi + TArray.array + sizeof.TMDItem * ecx + TMDItem.type] ; don't add paragraphs inside elements that can handle text by themselves.
        test    [ecx+TOneTag.flags], fLines
        jnz     .skip_to_eol

.add_leaf:
        call    .search_for_bq
        jc      .quote_ok

        call    .close_to_ecx_inc

.quote_ok:

        call    .search_for_li
        mov     eax, [.whspc]

        jc      .indent_ok
        sub     eax, [esi + TArray.array + sizeof.TMDItem*ecx+TMDItem.indent]
.indent_ok:

        cmp     eax, 4
        jge     .code_block

        cmp     eax, -4
        jg      .para

        call    .close_to_ecx_inc

        dec     ecx
        mov     eax, [esi+TArray.array + sizeof.TMDItem*ecx + TMDItem.type]
        mov     [esi+TArray.count], ecx
        call    .close_tag

        jmp     .add_leaf

.para:
        mov     eax, tagPara
        mov     ecx, [.hdr]
        dec     ecx
        js      .do_add_leaf
        mov     eax, [.headers + 4*ecx]

.do_add_leaf:
        call    .open_tag
        jmp     .skip_to_eol


.empty_line_found:

        call    .search_for_bq
        jc      .quote_ok2

        call    .close_to_ecx_inc

.quote_ok2:

        mov     esi, [.pStack]
        mov     ecx, [esi+TArray.count]
        dec     ecx
        js      .skip_to_eol   ; the empty line is not inside code block because there is no blocks at all.

        mov     eax, [esi + TArray.array + sizeof.TMDItem*ecx + TMDItem.type]
        test    [eax+TOneTag.flags], fLines
        jz      .close_list

        mov     [esi+TArray.count], ecx ; close text leaf blocks.
        call    .close_tag
        jmp     .skip_to_eol

.close_list:
        DebugMsg "Close list on second empty line."

        cmp     eax, tagListItem
        jne     .skip_to_eol

        call    .close_tag

        mov     [esi+TArray.count], ecx
        dec     ecx
        js      .skip_to_eol

        mov     eax, [esi + TArray.array + sizeof.TMDItem*ecx + TMDItem.type]
        cmp     eax, tagUL
        jne     .skip_to_eol

        call    .close_tag
        mov     [esi+TArray.count], ecx
        jmp     .skip_to_eol


.code_block:

        DebugMsg "Code block detected!"

        mov     esi, [.pStack]
        mov     ecx, [esi+TArray.count]
        dec     ecx
        js      .add_new_code_block

        cmp     [esi+TArray.array + sizeof.TMDItem*ecx + TMDItem.type], tagCode
        je      .skip_to_eol

.add_new_code_block:

        mov     eax, tagCode
        call    .open_tag

;        add     [edx+TText.GapEnd], 4   ; the gap should be properly positioned here!
        jmp     .skip_to_eol


.open_new:
        cmp     ecx, [esi + TArray.count]       ; esi = indicators list
        jae     .skip_to_eol

        mov     eax, [esi + TArray.array + sizeof.TMDItem*ecx + TMDItem.indent]
        mov     [.whspc], eax
        mov     eax, [esi + TArray.array + sizeof.TMDItem*ecx + TMDItem.type]
        call    .open_tag

        inc     ecx
        jmp     .open_new

; Here seems to be a good place for processing the inline formatting elements, together with the HTML encoding.

.skip_to_eol:

        xor     eax, eax
        mov     ecx, ebx
        add     ecx, [edx+TText.GapEnd]
        sub     ecx, [edx+TText.GapBegin]
        cmp     ecx, [edx+TText.Length]
        jae     .end_of_text

        mov     al, [edx+ecx]
        test    al, al
        jz      .end_of_text
        cmp     al, $0d
        je      .eol_found
        cmp     al, $0a
        je      .eol_found

; html encoding from here

        test    al, al          ; all values > 127 are unicode and should not be encoded.
        js      .next

        movzx   eax, byte [tbl_html+eax]
        test    al, al
        jns     .next           ; the same as above

        lea     esi, [eax+tbl_html]     ; the address of the replacement string.
        lodsb
        movzx   ecx, al         ; length

; insert the replacement html encoding from esi

        stdcall TextSetGapSize, edx, ecx
        stdcall TextMoveGap, edx, ebx
        inc     [edx+TText.GapEnd]      ; delete the previous char.

        mov     edi, [edx+TText.GapBegin]
        add     edi, edx
        add     [edx+TText.GapBegin], ecx
        add     ebx, ecx

        rep movsb

.next:
        inc     ebx
        jmp     .skip_to_eol


.eol_found:
        mov     ah, al
        xor     ah, $0d xor $0a

        inc     ecx
        inc     ebx
        cmp     [edx+ecx], ah
        jne     .start_of_line
        inc     ebx
        jmp     .start_of_line

.end_of_text:

; Close all not closed tags:

        stdcall TextMoveGap, edx, ebx
        mov     edi, [.pStack]
        mov     ecx, [edi+TArray.count]

.close_all:
        dec     ecx
        js      .finalize

        mov     eax, [edi + TArray.array + sizeof.TMDItem*ecx + TMDItem.type]
        call    .close_tag
        jmp     .close_all

.finalize:
        stdcall FreeMem, [.pStack]
;        stdcall FreeMem, [.pIList]

        mov     [esp + 4*regEDX], edx
        popad
        return


.open_tag:
        push    ecx esi edi
        push    eax eax
        call    .add_to_stack
        pop     eax
        call    .prefix_tag
        pop     eax
        call    .suffix_tag
        pop     edi esi ecx
        retn


.suffix_tag:
        lea     eax, [eax+TOneTag.pOpenS]
        jmp     .copy_tag

.close_tag:
        lea     eax, [eax+TOneTag.pClose]

.prefix_tag:
.copy_tag:
        push    ecx esi edi

        mov     esi, [eax]
        mov     ecx, [eax+4]
        test    esi, esi
        jz      .tag_copy_end

        add     ebx, ecx

        stdcall TextSetGapSize, edx, ecx
        mov     edi, [edx+TText.GapBegin]
        add     edi, edx
        add     [edx+TText.GapBegin], ecx

        rep movsb

.tag_copy_end:
        pop     edi esi ecx
        retn


.add_to_stack:
        push    edx eax [.whspc]
        stdcall AddArrayItems, [.pStack], 1
        mov     [.pStack], edx
        pop     [eax+TMDItem.indent]
        pop     [eax + TMDItem.type]
        pop     edx
        retn

endp






endmodule