-
Notifications
You must be signed in to change notification settings - Fork 0
/
mtxrender.c
1207 lines (1017 loc) · 38.3 KB
/
mtxrender.c
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
/* vim:set ts=8 sw=4 et: */
/*
MDVIEW MTX
Copyright (C) 2024 step, https://github.com/step-
Licensed under the GNU General Public License Version 2
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/*
2024-08-15 step:
This file was derived from MD4C md4c_html.c with substantial changes and feature
additions for MDVIEW MTX.
*/
/****************/
/* *INDENT-OFF* */
/****************/
/*
* MD4C: Markdown parser for C
* (http://github.com/mity/md4c)
*
* Copyright (c) 2016-2024 Martin Mitáš
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include <stdio.h>
#include <string.h>
#include "mtxrender.h"
#include "entity.h"
#include "mtxcmm.h"
#include "mtxcmmprivate.h"
void mtx_cmm_parser_unit_new (MtxCmm *, const MtxCmmParserUnitType, const MtxCmmParserUnitFlag);
int mtx_cmm_parser_find_unit_index (MtxCmm *, const MtxCmmParserUnitType, const MtxCmmParserUnitFlag, const int, MtxCmmParserUnit **);
gboolean mtx_cmm_parser_top_unit_ends_line (MtxCmm *);
#define PARSER(r) ((MtxCmm *)(r)->userdata)
#define R2_FLUSH(r) RENDER_VERBATIM(r, "")
#define R2_NEW_UNIT(r, type, flag_mask) \
mtx_cmm_parser_unit_new (PARSER(r), type, flag_mask)
#define R2_ADD_ARG(r) \
R2_NEW_UNIT(r, MTX_CMM_PARSER_UNIT_ARG, MTX_CMM_PARSER_UNIT_FLAG_OPEN)
#define R2_SEAL_ARG(r) do { \
R2_NEW_UNIT(r, MTX_CMM_PARSER_UNIT_ARG, MTX_CMM_PARSER_UNIT_FLAG_CLOSE); \
R2_FLUSH(r); \
} while (0)
#define R2_ADD_ARG_INLINES(r) \
R2_NEW_UNIT(r, MTX_CMM_PARSER_UNIT_ARG_INLINES, MTX_CMM_PARSER_UNIT_FLAG_OPEN)
#define R2_SEAL_ARG_INLINES(r) do { \
R2_NEW_UNIT(r, MTX_CMM_PARSER_UNIT_ARG_INLINES, MTX_CMM_PARSER_UNIT_FLAG_CLOSE); \
R2_FLUSH(r); \
} while (0)
#define R2_GET_TOP_UNIT(r) mtx_cmm_parser_get_unit_head (PARSER(r))
#define R2_IS_TOP_UNIT(r, type_mask, flag_mask) \
(mtx_cmm_parser_find_unit_index (PARSER(r), type_mask, flag_mask, 0, NULL) == 0)
#define R2_IS_UNIT_BELOW(r, type_mask, flag_mask) \
(mtx_cmm_parser_find_unit_index (PARSER(r), type_mask, flag_mask, 1, NULL) == 1)
#define R2_TOP_UNIT_ENDS_WITH_NEWLINE(r) \
mtx_cmm_parser_top_unit_ends_line (PARSER(r))
#define R2_SEAL_UNIT(r) R2_FLUSH(r)
#if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199409L
/* C89/90 or old compilers in general may not understand "inline". */
#if defined __GNUC__
#define inline __inline__
#elif defined _MSC_VER
#define inline __inline
#else
#define inline
#endif
#endif
#ifdef _WIN32
#define snprintf _snprintf
#endif
typedef struct MD_HTML_tag MD_HTML;
struct MD_HTML_tag {
void (*process_output)(const MD_CHAR*, MD_SIZE, void*);
void* userdata;
unsigned flags;
int image_nesting_level;
char escape_map[256];
int li_level;
unsigned long li_is_tight; /* bit mask */
int li_ordinal[MTX_MAX_LI_LEVEL];
const MtxCmmTags* tags;
gboolean output_html;
gboolean escape;
gboolean softbreak_tweak;
gboolean html5_tweak;
gboolean indent_li_block;
gboolean inside_table;
};
#define NEED_HTML_ESC_FLAG 0x1
#define NEED_URL_ESC_FLAG 0x2
/*****************************************
*** HTML rendering helper functions ***
*****************************************/
#define ISDIGIT(ch) ('0' <= (ch) && (ch) <= '9')
#define ISLOWER(ch) ('a' <= (ch) && (ch) <= 'z')
#define ISUPPER(ch) ('A' <= (ch) && (ch) <= 'Z')
#define ISALNUM(ch) (ISLOWER(ch) || ISUPPER(ch) || ISDIGIT(ch))
static inline void
render_verbatim(MD_HTML* r, const MD_CHAR* text, MD_SIZE size)
{
r->process_output(text, size, r->userdata);
}
/* Keep this as a macro. Most compiler should then be smart enough to replace
* the strlen() call with a compile-time constant if the string is a C literal. */
#define RENDER_VERBATIM(r, verbatim) \
render_verbatim((r), (verbatim), (MD_SIZE) (strlen(verbatim)))
static void
render_html_escaped(MD_HTML* r, const MD_CHAR* data, MD_SIZE size)
{
MD_OFFSET beg = 0;
MD_OFFSET off = 0;
/* Some characters need to be escaped in normal HTML text. */
#define NEED_HTML_ESC(ch) (r->escape_map[(unsigned char)(ch)] & NEED_HTML_ESC_FLAG)
while(1) {
/* Optimization: Use some loop unrolling. */
while(off + 3 < size && !NEED_HTML_ESC(data[off+0]) && !NEED_HTML_ESC(data[off+1])
&& !NEED_HTML_ESC(data[off+2]) && !NEED_HTML_ESC(data[off+3]))
off += 4;
while(off < size && !NEED_HTML_ESC(data[off]))
off++;
if(off > beg)
render_verbatim(r, data + beg, off - beg);
if(off < size) {
switch(data[off]) {
case '&': RENDER_VERBATIM(r, sUNIPUA_AMP); break;
case '<': RENDER_VERBATIM(r, sUNIPUA_LT); break;
case '>': RENDER_VERBATIM(r, sUNIPUA_GT); break;
case '"': RENDER_VERBATIM(r, sUNIPUA_QUOT); break;
}
off++;
} else {
break;
}
beg = off;
}
}
static void
render_url_escaped(MD_HTML* r, const MD_CHAR* data, MD_SIZE size)
{
static const MD_CHAR hex_chars[] = "0123456789ABCDEF";
MD_OFFSET beg = 0;
MD_OFFSET off = 0;
/* Some characters need to be escaped in URL attributes. */
#define NEED_URL_ESC(ch) (r->escape_map[(unsigned char)(ch)] & NEED_URL_ESC_FLAG)
while(1) {
while(off < size && !NEED_URL_ESC(data[off]))
off++;
if(off > beg)
render_verbatim(r, data + beg, off - beg);
if(off < size) {
char hex[3];
switch(data[off]) {
case '&': RENDER_VERBATIM(r, "&"); break;
default:
hex[0] = '%';
hex[1] = hex_chars[((unsigned)data[off] >> 4) & 0xf];
hex[2] = hex_chars[((unsigned)data[off] >> 0) & 0xf];
render_verbatim(r, hex, 3);
break;
}
off++;
} else {
break;
}
beg = off;
}
}
static unsigned
hex_val(char ch)
{
if('0' <= ch && ch <= '9')
return ch - '0';
if('A' <= ch && ch <= 'Z')
return ch - 'A' + 10;
else
return ch - 'a' + 10;
}
static void
render_utf8_codepoint(MD_HTML* r, unsigned codepoint,
void (*fn_append)(MD_HTML*, const MD_CHAR*, MD_SIZE))
{
static const MD_CHAR utf8_replacement_char[] = { (char)0xef, (char)0xbf, (char)0xbd };
unsigned char utf8[4];
size_t n;
if(codepoint <= 0x7f) {
n = 1;
utf8[0] = codepoint;
} else if(codepoint <= 0x7ff) {
n = 2;
utf8[0] = 0xc0 | ((codepoint >> 6) & 0x1f);
utf8[1] = 0x80 + ((codepoint >> 0) & 0x3f);
} else if(codepoint <= 0xffff) {
n = 3;
utf8[0] = 0xe0 | ((codepoint >> 12) & 0xf);
utf8[1] = 0x80 + ((codepoint >> 6) & 0x3f);
utf8[2] = 0x80 + ((codepoint >> 0) & 0x3f);
} else {
n = 4;
utf8[0] = 0xf0 | ((codepoint >> 18) & 0x7);
utf8[1] = 0x80 + ((codepoint >> 12) & 0x3f);
utf8[2] = 0x80 + ((codepoint >> 6) & 0x3f);
utf8[3] = 0x80 + ((codepoint >> 0) & 0x3f);
}
if(0 < codepoint && codepoint <= 0x10ffff)
fn_append(r, (char*)utf8, (MD_SIZE)n);
else
fn_append(r, utf8_replacement_char, 3);
}
/* Translate entity to its UTF-8 equivalent, or output the verbatim one
* if such entity is unknown (or if the translation is disabled). */
static void
render_entity(MD_HTML* r, const MD_CHAR* text, MD_SIZE size,
void (*fn_append)(MD_HTML*, const MD_CHAR*, MD_SIZE))
{
if(r->flags & MD_HTML_FLAG_VERBATIM_ENTITIES) {
render_verbatim(r, text, size);
return;
}
/* We assume UTF-8 output is what is desired. */
if(size > 3 && text[1] == '#') {
unsigned codepoint = 0;
if(text[2] == 'x' || text[2] == 'X') {
/* Hexadecimal entity (e.g. "�")). */
MD_SIZE i;
for(i = 3; i < size-1; i++)
codepoint = 16 * codepoint + hex_val(text[i]);
} else {
/* Decimal entity (e.g. "&1234;") */
MD_SIZE i;
for(i = 2; i < size-1; i++)
codepoint = 10 * codepoint + (text[i] - '0');
}
render_utf8_codepoint(r, codepoint, fn_append);
return;
} else {
/* Named entity (e.g. " "). */
const ENTITY* ent;
ent = entity_lookup(text, size);
if(ent != NULL) {
render_utf8_codepoint(r, ent->codepoints[0], fn_append);
if(ent->codepoints[1])
render_utf8_codepoint(r, ent->codepoints[1], fn_append);
return;
}
}
fn_append(r, text, size);
}
static void
render_attribute(MD_HTML* r, const MD_ATTRIBUTE* attr,
void (*fn_append)(MD_HTML*, const MD_CHAR*, MD_SIZE))
{
int i;
for(i = 0; attr->substr_offsets[i] < attr->size; i++) {
MD_TEXTTYPE type = attr->substr_types[i];
MD_OFFSET off = attr->substr_offsets[i];
MD_SIZE size = attr->substr_offsets[i+1] - off;
const MD_CHAR* text = attr->text + off;
switch(type) {
case MD_TEXT_NULLCHAR: render_utf8_codepoint(r, 0x0000, render_verbatim); break;
case MD_TEXT_ENTITY: render_entity(r, text, size, fn_append); break;
default: fn_append(r, text, size); break;
}
}
}
static void
render_open_blockquote_block(MD_HTML* r)
{
R2_NEW_UNIT(r, MTX_CMM_PARSER_UNIT_BLOCK_QUOTE, MTX_CMM_PARSER_UNIT_FLAG_OPEN);
RENDER_VERBATIM(r, r->tags->blockquote_start);
}
static void
render_close_blockquote_block(MD_HTML* r)
{
R2_NEW_UNIT(r, MTX_CMM_PARSER_UNIT_BLOCK_QUOTE, MTX_CMM_PARSER_UNIT_FLAG_CLOSE);
RENDER_VERBATIM(r, r->tags->blockquote_end);
R2_SEAL_UNIT(r);
}
static void
render_intro_open_ol_ul_block(MD_HTML* r, const void* det, const MtxCmmParserUnitType type)
{
int is_tight, start = -1, add_newline = 1;
if (type == MTX_CMM_PARSER_UNIT_BLOCK_OL) {
is_tight = ((MD_BLOCK_OL_DETAIL*)det)->is_tight;
start = ((MD_BLOCK_OL_DETAIL*)det)->start;
}
else {
is_tight = ((MD_BLOCK_UL_DETAIL*)det)->is_tight;
}
r->li_level++;
if (is_tight)
r->li_is_tight |= (1L << r->li_level);
r->li_ordinal[r->li_level % MTX_MAX_LI_LEVEL] = start;
add_newline = !R2_TOP_UNIT_ENDS_WITH_NEWLINE(r);
R2_NEW_UNIT(r, type, MTX_CMM_PARSER_UNIT_FLAG_OPEN);
if (add_newline && r->li_level > 0)
RENDER_VERBATIM(r, "\n");
}
static inline void
render_outtro_open_ol_ul_block(MD_HTML* r)
{
/* clear level */
r->li_is_tight &= ~(1UL << r->li_level);
r->li_level--;
}
static void
render_open_ul_block(MD_HTML* r, const MD_BLOCK_UL_DETAIL* det)
{
render_intro_open_ol_ul_block(r, det, MTX_CMM_PARSER_UNIT_BLOCK_UL);
RENDER_VERBATIM(r, r->tags->ulist_start);
}
static void
render_close_ul_block(MD_HTML* r)
{
R2_NEW_UNIT(r, MTX_CMM_PARSER_UNIT_BLOCK_UL, MTX_CMM_PARSER_UNIT_FLAG_CLOSE);
RENDER_VERBATIM(r, r->tags->ulist_end);
R2_SEAL_UNIT(r);
render_outtro_open_ol_ul_block(r);
}
static void
render_open_ol_block(MD_HTML* r, const MD_BLOCK_OL_DETAIL* det)
{
char buf[64];
int len;
render_intro_open_ol_ul_block(r, det, MTX_CMM_PARSER_UNIT_BLOCK_OL);
if(!r->output_html) {
RENDER_VERBATIM(r, r->tags->olist_start);
return;
}
if(det->start == 1) {
RENDER_VERBATIM(r, "<ol>\n");
return;
}
len = snprintf(buf, sizeof(buf), "<ol start=\"%u\">\n", det->start);
render_verbatim(r, buf, len);
}
static void
render_close_ol_block(MD_HTML* r)
{
R2_NEW_UNIT(r, MTX_CMM_PARSER_UNIT_BLOCK_OL, MTX_CMM_PARSER_UNIT_FLAG_CLOSE);
RENDER_VERBATIM(r, r->tags->olist_end);
R2_SEAL_UNIT(r);
render_outtro_open_ol_ul_block(r);
}
static void
render_open_li_block(MD_HTML* r, const MD_BLOCK_LI_DETAIL* det __attribute__((unused)))
{
int ordinal = r->li_ordinal[r->li_level];
int len;
char arg[16];
if (ordinal >= 0)
++r->li_ordinal[r->li_level];
R2_NEW_UNIT(r, MTX_CMM_PARSER_UNIT_BLOCK_LI, MTX_CMM_PARSER_UNIT_FLAG_ARGS | MTX_CMM_PARSER_UNIT_FLAG_OPEN);
R2_ADD_ARG(r);
if(r->output_html) {
#if 1
RENDER_VERBATIM(r, r->tags->li_start[r->li_level % 2]);
#else /* TODO (likely never) */
if(det->is_task) {
RENDER_VERBATIM(r, "<li class=\"task-list-item\">"
"<input type=\"checkbox\" class=\"task-list-item-checkbox\" disabled");
if(det->task_mark == 'x' || det->task_mark == 'X')
RENDER_VERBATIM(r, " checked");
RENDER_VERBATIM(r, ">");
} else {
RENDER_VERBATIM(r, "<li>");
}
#endif
}
else {
char buf[MTX_MAX_LI_LEVEL * 2] = /* 63 spaces (indentation) */
" ";
int ind_len = 2 * (1 + r->li_level % (sizeof(buf) / 2));
buf[ind_len] = '\0';
/* Render indentation. */
if (r->indent_li_block)
render_verbatim(r, buf, ind_len);
/* Render start tag for ol or ul. */
if (ordinal >= 0)
len = snprintf (buf, sizeof(buf), "%d. ", ordinal); /* ol */
else
len = snprintf (buf, sizeof(buf), "%s ", r->tags->li_start[r->li_level % 2]); /* ul */
render_verbatim(r, buf, len);
}
R2_SEAL_ARG(r);
R2_ADD_ARG(r);
render_verbatim(r, r->li_is_tight & (1L << r->li_level) ? "T" : "F", 1);
R2_SEAL_ARG(r);
len = snprintf(arg, sizeof(arg), "%d", r->li_level + 1);
R2_ADD_ARG(r);
render_verbatim(r, arg, len);
R2_SEAL_ARG(r);
len = snprintf(arg, sizeof(arg), "%d", ordinal);
R2_ADD_ARG(r);
render_verbatim(r, arg, len);
R2_SEAL_ARG(r);
/* FIXME li can contain blockquote => arbitrary blocks! */
/* For units to come (auto-closing). */
R2_NEW_UNIT(r, MTX_CMM_PARSER_UNIT_INLINES, MTX_CMM_PARSER_UNIT_FLAG_OPEN);
}
static void
render_close_li_block(MD_HTML* r)
{
/*
For non-HTML output modes, collapse runs of bullet endings to preserve
vertical space. Consider the following cases:
- "</p></li>" -- loose lists
- "</li></li>", "</ol></li>" and "</ul></li>" -- nested lists
*/
int collapse_li_ends =
R2_IS_TOP_UNIT(r, MTX_CMM_PARSER_UNIT_BLOCK_P | MTX_CMM_PARSER_UNIT_BLOCK_LI | MTX_CMM_PARSER_UNIT_BLOCK_OL |
MTX_CMM_PARSER_UNIT_BLOCK_UL, MTX_CMM_PARSER_UNIT_FLAG_CLOSE);
R2_NEW_UNIT(r, MTX_CMM_PARSER_UNIT_BLOCK_LI, MTX_CMM_PARSER_UNIT_FLAG_ARGS | MTX_CMM_PARSER_UNIT_FLAG_CLOSE);
R2_ADD_ARG(r);
if (r->output_html) {
RENDER_VERBATIM(r, r->tags->li_end);
}
else {
if (!collapse_li_ends) {
RENDER_VERBATIM(r, r->tags->li_end);
}
}
R2_SEAL_ARG(r);
R2_SEAL_UNIT(r);
}
static void
render_open_hr_block(MD_HTML* r)
{
R2_NEW_UNIT(r, MTX_CMM_PARSER_UNIT_BLOCK_HR, MTX_CMM_PARSER_UNIT_FLAG_OPEN | MTX_CMM_PARSER_UNIT_FLAG_CLOSE);
RENDER_VERBATIM(r, r->tags->rule);
if (r->output_html) {
RENDER_VERBATIM(r, r->html5_tweak ? ">\n" : " />\n");
}
R2_SEAL_UNIT(r);
}
static void
render_open_h_block(MD_HTML* r, const MD_BLOCK_H_DETAIL* det)
{
R2_NEW_UNIT(r, MTX_CMM_PARSER_UNIT_BLOCK_H, MTX_CMM_PARSER_UNIT_FLAG_ARGS | MTX_CMM_PARSER_UNIT_FLAG_OPEN);
R2_ADD_ARG(r);
switch (det->level) {
case 1: RENDER_VERBATIM(r, r->tags->h1_start); break;
case 2: RENDER_VERBATIM(r, r->tags->h2_start); break;
case 3: RENDER_VERBATIM(r, r->tags->h3_start); break;
case 4: RENDER_VERBATIM(r, r->tags->h4_start); break;
case 5: RENDER_VERBATIM(r, r->tags->h5_start); break;
case 6: RENDER_VERBATIM(r, r->tags->h6_start); break;
}
R2_SEAL_ARG(r);
R2_SEAL_UNIT(r);
/* For inlines to come (auto-closing). */
R2_NEW_UNIT(r, MTX_CMM_PARSER_UNIT_INLINES, MTX_CMM_PARSER_UNIT_FLAG_OPEN);
}
static void
render_close_h_block(MD_HTML* r, const MD_BLOCK_H_DETAIL* det)
{
R2_NEW_UNIT(r, MTX_CMM_PARSER_UNIT_BLOCK_H, MTX_CMM_PARSER_UNIT_FLAG_ARGS | MTX_CMM_PARSER_UNIT_FLAG_CLOSE);
R2_ADD_ARG(r);
switch (det->level) {
case 1: RENDER_VERBATIM(r, r->tags->h1_end); break;
case 2: RENDER_VERBATIM(r, r->tags->h2_end); break;
case 3: RENDER_VERBATIM(r, r->tags->h3_end); break;
case 4: RENDER_VERBATIM(r, r->tags->h4_end); break;
case 5: RENDER_VERBATIM(r, r->tags->h5_end); break;
case 6: RENDER_VERBATIM(r, r->tags->h6_end); break;
}
R2_SEAL_ARG(r);
R2_SEAL_UNIT(r);
}
static void
render_open_code_block(MD_HTML* r, const MD_BLOCK_CODE_DETAIL* det)
{
R2_NEW_UNIT(r, MTX_CMM_PARSER_UNIT_BLOCK_CODE, MTX_CMM_PARSER_UNIT_FLAG_OPEN);
if (r->output_html) {
RENDER_VERBATIM(r, r->tags->codeblock_start);
/* If known, output the HTML 5 attribute class="language-LANGNAME". */
if(det->lang.text != NULL) {
RENDER_VERBATIM(r, " class=\"language-");
render_attribute(r, &det->lang, render_html_escaped);
RENDER_VERBATIM(r, "\"");
}
RENDER_VERBATIM(r, ">");
}
else
RENDER_VERBATIM(r, r->tags->codeblock_start);
}
static void
render_close_code_block(MD_HTML* r, const MD_BLOCK_CODE_DETAIL* det __attribute__((unused)))
{
/*
Pass end tag as the argument of a new unit to allow mtx_cmm_mtx to access
the text field of the opening unit without having to pry it open.
*/
R2_NEW_UNIT(r, MTX_CMM_PARSER_UNIT_BLOCK_CODE, MTX_CMM_PARSER_UNIT_FLAG_ARGS | MTX_CMM_PARSER_UNIT_FLAG_CLOSE);
R2_ADD_ARG(r);
RENDER_VERBATIM(r, r->tags->codeblock_end);
R2_SEAL_ARG(r);
}
static inline void
render_open_html_block(MD_HTML* r)
{
R2_NEW_UNIT(r, MTX_CMM_PARSER_UNIT_BLOCK_HTML, MTX_CMM_PARSER_UNIT_FLAG_OPEN);
}
static inline void
render_close_html_block(MD_HTML* r)
{
R2_NEW_UNIT(r, MTX_CMM_PARSER_UNIT_BLOCK_HTML, MTX_CMM_PARSER_UNIT_FLAG_CLOSE);
}
static void
render_open_p_block (MD_HTML *r)
{
/* Imitate cmark's output in loose list. */
if (r->output_html && R2_IS_UNIT_BELOW(r, MTX_CMM_PARSER_UNIT_BLOCK_LI, MTX_CMM_PARSER_UNIT_FLAG_OPEN))
RENDER_VERBATIM(r, "\n");
R2_NEW_UNIT(r, MTX_CMM_PARSER_UNIT_BLOCK_P, MTX_CMM_PARSER_UNIT_FLAG_ARGS | MTX_CMM_PARSER_UNIT_FLAG_OPEN);
R2_ADD_ARG(r);
RENDER_VERBATIM (r, r->tags->para_start);
R2_SEAL_ARG(r);
/* For inlines to come (auto-closing). */
R2_NEW_UNIT(r, MTX_CMM_PARSER_UNIT_INLINES, MTX_CMM_PARSER_UNIT_FLAG_OPEN);
}
static void
render_close_p_block (MD_HTML *r)
{
R2_NEW_UNIT(r, MTX_CMM_PARSER_UNIT_BLOCK_P, MTX_CMM_PARSER_UNIT_FLAG_ARGS | MTX_CMM_PARSER_UNIT_FLAG_CLOSE);
R2_ADD_ARG(r);
if (r->output_html) {
RENDER_VERBATIM(r, r->tags->para_end);
}
else {
RENDER_VERBATIM(r, r->tags->para_end);
}
R2_SEAL_ARG(r);
R2_SEAL_UNIT(r);
}
static void
render_open_table_block(MD_HTML* r)
{
r->inside_table = 1;
R2_NEW_UNIT (r, MTX_CMM_PARSER_UNIT_BLOCK_TABLE,
MTX_CMM_PARSER_UNIT_FLAG_ARGS |
MTX_CMM_PARSER_UNIT_FLAG_OPEN);
if (r->output_html) {
RENDER_VERBATIM (r, r->tags->table_start);
} else {
/* Placeholder for serialized max_col_width array */
R2_ADD_ARG(r);
RENDER_VERBATIM (r, "");
R2_SEAL_ARG(r);
RENDER_VERBATIM (r, r->tags->table_start);
}
R2_SEAL_UNIT(r);
}
static void
render_close_table_block(MD_HTML* r)
{
r->inside_table = 0;
R2_NEW_UNIT (r, MTX_CMM_PARSER_UNIT_BLOCK_TABLE,
MTX_CMM_PARSER_UNIT_FLAG_ARGS |
MTX_CMM_PARSER_UNIT_FLAG_CLOSE);
RENDER_VERBATIM (r, r->tags->table_end);
R2_SEAL_UNIT(r);
}
static void
render_open_thead_block(MD_HTML* r)
{
R2_NEW_UNIT (r, MTX_CMM_PARSER_UNIT_BLOCK_THEAD,
MTX_CMM_PARSER_UNIT_FLAG_ARGS |
MTX_CMM_PARSER_UNIT_FLAG_OPEN);
RENDER_VERBATIM (r, r->tags->thead_start);
R2_SEAL_UNIT(r);
}
static void
render_close_thead_block(MD_HTML* r)
{
R2_NEW_UNIT (r, MTX_CMM_PARSER_UNIT_BLOCK_THEAD,
MTX_CMM_PARSER_UNIT_FLAG_ARGS |
MTX_CMM_PARSER_UNIT_FLAG_CLOSE);
RENDER_VERBATIM (r, r->tags->thead_end);
R2_SEAL_UNIT(r);
}
static void
render_open_tbody_block(MD_HTML* r)
{
R2_NEW_UNIT (r, MTX_CMM_PARSER_UNIT_BLOCK_TBODY,
MTX_CMM_PARSER_UNIT_FLAG_ARGS |
MTX_CMM_PARSER_UNIT_FLAG_OPEN);
if (r->output_html) {
RENDER_VERBATIM (r, r->tags->tbody_start);
}
else {
R2_ADD_ARG(r);
RENDER_VERBATIM (r, r->tags->tbody_start);
R2_SEAL_ARG(r);
}
R2_SEAL_UNIT(r);
}
static void
render_close_tbody_block(MD_HTML* r)
{
R2_NEW_UNIT (r, MTX_CMM_PARSER_UNIT_BLOCK_TBODY,
MTX_CMM_PARSER_UNIT_FLAG_ARGS |
MTX_CMM_PARSER_UNIT_FLAG_CLOSE);
if (r->output_html) {
RENDER_VERBATIM (r, r->tags->tbody_end);
}
else {
R2_ADD_ARG(r);
RENDER_VERBATIM (r, r->tags->tbody_end);
R2_SEAL_ARG(r);
}
R2_SEAL_UNIT(r);
}
static void
render_open_tr_block(MD_HTML* r)
{
R2_NEW_UNIT (r, MTX_CMM_PARSER_UNIT_BLOCK_TR,
MTX_CMM_PARSER_UNIT_FLAG_ARGS |
MTX_CMM_PARSER_UNIT_FLAG_OPEN);
RENDER_VERBATIM (r, r->tags->tr_start);
R2_SEAL_UNIT(r);
}
static void
render_close_tr_block(MD_HTML* r)
{
R2_NEW_UNIT (r, MTX_CMM_PARSER_UNIT_BLOCK_TR,
MTX_CMM_PARSER_UNIT_FLAG_ARGS |
MTX_CMM_PARSER_UNIT_FLAG_CLOSE);
RENDER_VERBATIM (r, r->tags->tr_end);
R2_SEAL_UNIT(r);
}
static void
render_open_td_block(MD_HTML* r, const int cell_type, const MD_BLOCK_TD_DETAIL* det)
{
const char *align;
int len;
char buf[32];
R2_NEW_UNIT (r, cell_type == 0 ? MTX_CMM_PARSER_UNIT_BLOCK_TH :
MTX_CMM_PARSER_UNIT_BLOCK_TD,
MTX_CMM_PARSER_UNIT_FLAG_ARGS | MTX_CMM_PARSER_UNIT_FLAG_OPEN);
if (r->output_html) {
switch (det->align) {
case MD_ALIGN_LEFT:
align = " align=\"left\"";
break;
case MD_ALIGN_CENTER:
align = " align=\"center\"";
break;
case MD_ALIGN_RIGHT:
align = " align=\"right\"";
break;
default:
align = "";
break;
}
len = snprintf (buf, sizeof buf, cell_type == 0 ? r->tags->th_start :
r->tags->td_start, align);
render_verbatim (r, buf, len);
}
else {
/* Prefix cell text with its alignment encoded as an ASCII character.
This avoids having empty cells, which sink before reaching the queue. */
buf[0] = "NLCR"[det->align]; /* (N)one L(eft) C(enter) R(ight) */
render_verbatim (r, buf, 1);
R2_ADD_ARG (r);
RENDER_VERBATIM (r,
cell_type ==
0 ? r->tags->th_start : r->tags->td_start);
R2_SEAL_ARG (r);
}
R2_SEAL_UNIT (r);
}
static void
render_close_td_block(MD_HTML* r, const int cell_type)
{
R2_NEW_UNIT (r, cell_type == 0 ? MTX_CMM_PARSER_UNIT_BLOCK_TH :
MTX_CMM_PARSER_UNIT_BLOCK_TD,
MTX_CMM_PARSER_UNIT_FLAG_ARGS |
MTX_CMM_PARSER_UNIT_FLAG_CLOSE);
RENDER_VERBATIM (r, cell_type == 0 ? r->tags->th_end : r->tags->td_end);
R2_SEAL_UNIT (r);
}
static void
render_open_a_span(MD_HTML* r, const MD_SPAN_A_DETAIL* det)
{
R2_NEW_UNIT(r, MTX_CMM_PARSER_UNIT_SPAN_A, MTX_CMM_PARSER_UNIT_FLAG_ARGS | MTX_CMM_PARSER_UNIT_FLAG_OPEN);
/* Provide choice of link format: URI-encoded and verbatim.
*/
R2_ADD_ARG(r);
render_attribute (r, &det->href, render_url_escaped);
RENDER_VERBATIM(r, "\n"); /*value separator*/
render_attribute (r, &det->href, render_verbatim);
R2_SEAL_ARG(r);
R2_ADD_ARG(r);
if (det->title.text)
render_attribute(r, &det->title, r->escape ? render_html_escaped : render_verbatim);
R2_SEAL_ARG(r);
/* Upcoming arg is this span's text. */
R2_ADD_ARG_INLINES(r);
/* Inject a prefix to the span's text. With this, the rendering coda will be
able to compensate <table> column alignment for <a> links. */
render_verbatim (r, r->inside_table ? "1" : "0", 1);
}
static void
render_close_a_span(MD_HTML* r, const MD_SPAN_A_DETAIL* det __attribute__((unused)))
{
/* After text_callback. */
R2_SEAL_ARG_INLINES(r);
/* With URL, title and link text in head unit's args. */
/* For inlines to come (auto-closing). */
R2_NEW_UNIT(r, MTX_CMM_PARSER_UNIT_INLINES, MTX_CMM_PARSER_UNIT_FLAG_OPEN);
}
static void
render_open_img_span(MD_HTML* r, const MD_SPAN_IMG_DETAIL* det)
{
/*
When nested images are involved, render_open_img_span is called only once:
for the outer image with image_nesting_level == 1.
*/
g_assert (r->image_nesting_level == 1);
R2_NEW_UNIT(r, MTX_CMM_PARSER_UNIT_SPAN_IMG, MTX_CMM_PARSER_UNIT_FLAG_ARGS | MTX_CMM_PARSER_UNIT_FLAG_OPEN);
/* Provide choice of path format: URI-encoded and verbatim.
*/
R2_ADD_ARG(r);
render_attribute (r, &det->src, render_url_escaped);
RENDER_VERBATIM(r, "\n"); /*value separator*/
render_attribute (r, &det->src, render_verbatim);
R2_SEAL_ARG(r);
R2_ADD_ARG(r);
if(det->title.text)
render_attribute(r, &det->title, render_verbatim);
R2_SEAL_ARG(r);
/* Upcoming arg is this span's text. */
R2_ADD_ARG(r);
/*
For nested images, this is the outer image and the above ARG will stay open
to conflate render text() calls until the outer image will be closed in
render_close_img_span.
*/
/* Inject a prefix to the span's text. With this, the rendering coda will be
able to compensate <table> column alignment for <a> links. */
render_verbatim (r, r->inside_table ? "1" : "0", 1);
}
static void
render_close_img_span(MD_HTML* r, const MD_SPAN_IMG_DETAIL* det __attribute__((unused)))
{
/* Ditto as in render_open_img_span but image_nesting_level == 0. */
g_assert (r->image_nesting_level == 0);
/* After text_callback. */
R2_SEAL_ARG(r);
/* With path, title and alt text in head unit's args. */
/* For inlines to come (auto-closing). */
R2_NEW_UNIT(r, MTX_CMM_PARSER_UNIT_INLINES, MTX_CMM_PARSER_UNIT_FLAG_OPEN);
}
static void
render_open_code_span(MD_HTML* r)
{
R2_NEW_UNIT(r, MTX_CMM_PARSER_UNIT_SPAN_CODE, MTX_CMM_PARSER_UNIT_FLAG_OPEN);
RENDER_VERBATIM(r, r->tags->code_span_start);
}
static void
render_close_code_span(MD_HTML* r)
{
/* after text_callback */
/* With formatted code in head unit's text. */
RENDER_VERBATIM(r, r->tags->code_span_end);
/* For inlines to come (auto-closing). */
R2_NEW_UNIT(r, MTX_CMM_PARSER_UNIT_INLINES, MTX_CMM_PARSER_UNIT_FLAG_OPEN);
}
#if 0 /*TODO future*/
static void
render_open_wikilink_span(MD_HTML* r, const MD_SPAN_WIKILINK_DETAIL* det)
{
RENDER_VERBATIM(r, "<x-wikilink data-target=\"");
render_attribute(r, &det->target, render_html_escaped);
RENDER_VERBATIM(r, "\">");
}
#endif
static void
render_text_hardbr(MD_HTML* r)
{
/* image_nesting_level == 0 means this text is outside an image span. */
RENDER_VERBATIM(r, (r->image_nesting_level == 0 ? sUNIPUA_BR : " "));
}
static void
render_text_softbr(MD_HTML* r)
{
/* image_nesting_level == 0 means this text is outside an image span. */
RENDER_VERBATIM (r, (r->softbreak_tweak && r->image_nesting_level == 0) ?
"\n" : " ");
}
static inline void
render_text_html(MD_HTML* r, const MD_CHAR* text, const MD_SIZE size)
{
if (R2_IS_TOP_UNIT(r, MTX_CMM_PARSER_UNIT_BLOCK_HTML, MTX_CMM_PARSER_UNIT_FLAG_OPEN))
/* Line inside a larger HTML block. */
render_verbatim(r, text, size);
else {
/* Inline HTML tags. */
int is_inline = R2_IS_TOP_UNIT(r, MTX_CMM_PARSER_UNIT_INLINES, MTX_CMM_PARSER_UNIT_FLAG_OPEN);
R2_NEW_UNIT(r, MTX_CMM_PARSER_UNIT_RAW_HTML, 0);
render_verbatim(r, text, size);
if (is_inline)
R2_NEW_UNIT(r, MTX_CMM_PARSER_UNIT_INLINES, MTX_CMM_PARSER_UNIT_FLAG_OPEN);
}
}
/**************************************
*** HTML renderer implementation ***
**************************************/
static int
enter_block_callback(MD_BLOCKTYPE type, void* detail, void* userdata)
{