-
Notifications
You must be signed in to change notification settings - Fork 156
/
markdown.c
1510 lines (1214 loc) · 30.5 KB
/
markdown.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
/* markdown: a C implementation of John Gruber's Markdown markup language.
*
* Copyright (C) 2007 Jessica L Parsons.
* The redistribution terms are provided in the COPYRIGHT file that must
* be distributed with this source code.
*/
#include "config.h"
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
#include "cstring.h"
#include "markdown.h"
#include "amalloc.h"
#include "tags.h"
typedef int (*stfu)(const void*,const void*);
static Paragraph *Pp(ParagraphRoot *, Line *, int);
static Paragraph *compile(Line *, int, MMIOT *);
/* case insensitive string sort for Footnote tags.
*/
int
__mkd_footsort(Footnote *a, Footnote *b)
{
int i;
char ac, bc;
if ( S(a->tag) != S(b->tag) )
return S(a->tag) - S(b->tag);
for ( i=0; i < S(a->tag); i++) {
ac = tolower(T(a->tag)[i]);
bc = tolower(T(b->tag)[i]);
if ( isspace(ac) && isspace(bc) )
continue;
if ( ac != bc )
return ac - bc;
}
return 0;
}
/* find the first blank character after position <i>
*/
static int
nextblank(Line *t, int i)
{
while ( (i < S(t->text)) && !isspace(T(t->text)[i]) )
++i;
return i;
}
/* find the next nonblank character after position <i>
*/
static int
nextnonblank(Line *t, int i)
{
while ( (i < S(t->text)) && isspace(T(t->text)[i]) )
++i;
return i;
}
/* find the first nonblank character on the Line.
*/
int
mkd_firstnonblank(Line *p)
{
return nextnonblank(p,0);
}
static inline int
blankline(Line *p)
{
return ! (p && (S(p->text) > p->dle) );
}
static Line *
skipempty(Line *p)
{
while ( p && (p->dle == S(p->text)) )
p = p->next;
return p;
}
void
___mkd_tidy(Cstring *t)
{
while ( S(*t) && isspace(T(*t)[S(*t)-1]) )
--S(*t);
}
static struct kw comment = { "!--", 3, 0 };
static struct kw *
isopentag(Line *p)
{
int i=0, len;
char *line;
if ( !p ) return 0;
line = T(p->text);
len = S(p->text);
if ( len < 3 || line[0] != '<' )
return 0;
if ( line[1] == '!' && line[2] == '-' && line[3] == '-' )
/* comments need special case handling, because
* the !-- doesn't need to end in a whitespace
*/
return &comment;
/* find how long the tag is so we can check to see if
* it's a block-level tag
*/
for ( i=1; i < len && T(p->text)[i] != '>'
&& T(p->text)[i] != '/'
&& !isspace(T(p->text)[i]); ++i )
;
return mkd_search_tags(T(p->text)+1, i-1);
}
typedef struct _flo {
Line *t;
int i;
} FLO;
#define floindex(x) (x.i)
static unsigned int
flogetc(FLO *f)
{
if ( f && f->t ) {
if ( f->i < S(f->t->text) )
return (unsigned char)T(f->t->text)[f->i++];
f->t = f->t->next;
f->i = 0;
return flogetc(f);
}
return EOF;
}
static void
splitline(Line *t, int cutpoint)
{
if ( t && (cutpoint < S(t->text)) ) {
Line *tmp = calloc(1, sizeof *tmp);
tmp->next = t->next;
t->next = tmp;
SUFFIX(tmp->text, T(t->text)+cutpoint, S(t->text)-cutpoint);
EXPAND(tmp->text) = 0;
S(tmp->text)--;
S(t->text) = cutpoint;
}
}
#define UNCHECK(t) ((t)->is_checked = 0)
/*
* walk a line, seeing if it's any of half a dozen interesting regular
* types.
*/
static void
checkline(Line *l, mkd_flag_t *flags)
{
int eol, i;
int dashes = 0, spaces = 0,
equals = 0, underscores = 0,
stars = 0, tildes = 0, other = 0,
backticks = 0;
register int c, first;
l->is_checked = 1;
l->kind = chk_text;
l->is_fenced = 0;
l->count = 0;
if (l->dle >= 4) { l->kind=chk_code; return; }
for ( eol = S(l->text); eol > l->dle && isspace(T(l->text)[eol-1]); --eol )
;
if ( is_flag_set(flags, MKD_FENCEDCODE) && !is_flag_set(flags, MKD_STRICT) ) {
first = T(l->text)[l->dle];
if ( first == '~' || first == '`' ) {
for ( i=l->dle; i<eol; i++ ) {
c = T(l->text)[i];
if ( (c != '~') && (c != '`') && (c != first) )
break;
l->count++;
}
if ( l->count > 1 ) {
l->kind = (first == '`' ? chk_backtick : chk_tilde);
l->is_fenced = 1;
return;
}
}
}
for (i=l->dle; i<eol; i++) {
if ( (c = T(l->text)[i]) != ' ' ) l->count++;
switch (c) {
case '-': dashes = 1; break;
case ' ': spaces = 1; break;
case '=': equals = 1; break;
case '_': underscores = 1; break;
case '*': stars = 1; break;
default: other = 1; break;
}
}
if ( dashes + equals + underscores + stars + tildes + backticks > 1 )
return;
if ( !other ) {
if (underscores || stars )
l->kind = chk_hr;
else if ( dashes )
l->kind = chk_dash;
else if ( equals )
l->kind = chk_equal;
return;
}
if ( tildes ) { l->kind = chk_tilde; }
else if ( backticks ) { l->kind = chk_backtick; }
}
/* markdown only does special handling of comments if the comment end
* is at the end of a line
*/
static Line *
commentblock(Paragraph *p, int *unclosed)
{
Line *t, *ret;
char *end;
for ( t = p->text; t ; t = t->next) {
if ( end = strstr(T(t->text), "-->") ) {
if ( nextnonblank(t, 3 + (end - T(t->text))) < S(t->text) )
continue;
/*splitline(t, 3 + (end - T(t->text)) );*/
ret = t->next;
t->next = 0;
return ret;
}
}
*unclosed = 1;
return t;
}
static Line *
htmlblock(Paragraph *p, struct kw *tag, int *unclosed)
{
Line *ret;
FLO f = { p->text, 0 };
int c;
int i, closing, depth=0;
*unclosed = 0;
if ( tag == &comment )
return commentblock(p, unclosed);
if ( tag->selfclose ) {
ret = f.t->next;
f.t->next = 0;
return ret;
}
while ( (c = flogetc(&f)) != EOF ) {
if ( c == '<' ) {
/* tag? */
c = flogetc(&f);
if ( c == '!' ) { /* comment? */
if ( flogetc(&f) == '-' && flogetc(&f) == '-' ) {
/* yes */
while ( (c = flogetc(&f)) != EOF ) {
if ( c == '-' && flogetc(&f) == '-'
&& flogetc(&f) == '>')
/* consumed whole comment */
break;
}
}
}
else {
if ( closing = (c == '/') ) c = flogetc(&f);
for ( i=0; i < tag->size; i++, c=flogetc(&f) ) {
if ( tag->id[i] != toupper(c) )
break;
}
if ( (i == tag->size) && !isalnum(c) ) {
depth = depth + (closing ? -1 : 1);
if ( depth == 0 ) {
while ( c != EOF && c != '>' ) {
/* consume trailing gunk in close tag */
c = flogetc(&f);
}
if ( c == EOF )
break;
if ( !f.t )
return 0;
splitline(f.t, floindex(f));
ret = f.t->next;
f.t->next = 0;
return ret;
}
}
}
}
}
*unclosed = 1;
return 0;
}
/* footnotes look like ^<whitespace>{0,3}[stuff]: <content>$
*/
static int
isfootnote(Line *t)
{
int i;
if ( ( (i = t->dle) > 3) || (T(t->text)[i] != '[') )
return 0;
for ( ++i; i < S(t->text) ; ++i ) {
if ( T(t->text)[i] == '[' )
return 0;
else if ( T(t->text)[i] == ']' )
return ( T(t->text)[i+1] == ':' ) ;
}
return 0;
}
static inline int
isquote(Line *t)
{
return (t->dle < 4 && T(t->text)[t->dle] == '>');
}
static inline int
iscode(Line *t)
{
return (t->dle >= 4);
}
static inline int
ishr(Line *t, mkd_flag_t *flags)
{
if ( ! (t->is_checked) )
checkline(t, flags);
if ( t->count > 2 )
return t->kind == chk_hr || t->kind == chk_dash || t->kind == chk_equal;
return 0;
}
static int
issetext(Line *t, int *htyp, mkd_flag_t *flags)
{
Line *n;
/* check for setext-style HEADER
* ======
*/
if ( (n = t->next) ) {
if ( !(n->is_checked) )
checkline(n, flags);
if ( n->kind == chk_dash || n->kind == chk_equal ) {
*htyp = SETEXT;
return 1;
}
}
return 0;
}
static int
ishdr(Line *t, int *htyp, mkd_flag_t *flags)
{
/* ANY leading `#`'s make this into an ETX header
*/
if ( (t->dle == 0) && (S(t->text) > 1) && (T(t->text)[0] == '#') ) {
*htyp = ETX;
return 1;
}
/* And if not, maybe it's a SETEXT header instead
*/
return issetext(t, htyp, flags);
}
static inline int
end_of_block(Line *t, mkd_flag_t *flags)
{
int dummy;
if ( !t )
return 0;
return ( (S(t->text) <= t->dle) || ishr(t, flags) || ishdr(t, &dummy, flags) );
}
static Line*
is_discount_dt(Line *t, int *clip, mkd_flag_t *flags)
{
if ( t && t->next
&& (S(t->text) > 2)
&& (t->dle == 0)
&& (T(t->text)[0] == '=')
&& (T(t->text)[S(t->text)-1] == '=') ) {
if ( t->next->dle >= 4 ) {
*clip = 4;
return t;
}
else
return is_discount_dt(t->next, clip, flags);
}
return 0;
}
static int
is_extra_dd(Line *t)
{
return (t->dle < 4) && (T(t->text)[t->dle] == ':')
&& isspace(T(t->text)[t->dle+1]);
}
static Line*
is_extra_dt(Line *t, int *clip, mkd_flag_t* flags)
{
if ( t && t->next && S(t->text) ) {
Line *x;
if ( iscode(t) || end_of_block(t, flags) )
return 0;
if ( (x = skipempty(t->next)) && is_extra_dd(x) ) {
*clip = x->dle+2;
return t;
}
if ( x=is_extra_dt(t->next, clip, flags) )
return x;
}
return 0;
}
static Line *
isdefinition(Line *t, int *clip, int *list_type, mkd_flag_t *flags)
{
Line *ret;
if ( !is_flag_set(flags, MKD_STRICT) ) {
if ( is_flag_set(flags, MKD_DLDISCOUNT) && (ret = is_discount_dt(t,clip,flags)) ) {
*list_type = 1;
return ret;
}
if ( is_flag_set(flags, MKD_DLEXTRA) && (ret = is_extra_dt(t,clip,flags)) ) {
*list_type = 2;
return ret;
}
}
return 0;
}
static int
islist(Line *t, int *clip, mkd_flag_t *flags, int *list_type)
{
int i, j;
char *q;
if ( end_of_block(t, flags) )
return 0;
if ( isdefinition(t,clip,list_type,flags) )
return DL;
if ( strchr("*-+", T(t->text)[t->dle]) && isspace(T(t->text)[t->dle+1]) ) {
i = nextnonblank(t, t->dle+1);
*clip = (i > 4) ? 4 : i;
*list_type = UL;
return is_flag_set(flags, MKD_EXPLICITLIST) ? UL : AL;
}
if ( (j = nextblank(t,t->dle)) > t->dle ) {
if ( T(t->text)[j-1] == '.' ) {
if ( !(is_flag_set(flags, MKD_NOALPHALIST) || is_flag_set(flags, MKD_STRICT))
&& (j == t->dle + 2)
&& isalpha(T(t->text)[t->dle]) ) {
j = nextnonblank(t,j);
*clip = (j > 4) ? 4 : j;
*list_type = AL;
return AL;
}
strtoul(T(t->text)+t->dle, &q, 10);
if ( (q > T(t->text)+t->dle) && (q == T(t->text) + (j-1)) ) {
j = nextnonblank(t,j);
*clip = j;
*list_type = OL;
return AL;
}
}
}
return 0;
}
static Line *
headerblock(Paragraph *pp, int htyp)
{
Line *ret = 0;
Line *p = pp->text;
int i, j;
switch (htyp) {
case SETEXT:
/* p->text is header, p->next->text is -'s or ='s
*/
pp->hnumber = (T(p->next->text)[0] == '=') ? 1 : 2;
ret = p->next->next;
___mkd_freeLine(p->next);
p->next = 0;
break;
case ETX:
/* p->text is ###header###, so we need to trim off
* the leading and trailing `#`'s
*/
for (i=0; (T(p->text)[i] == T(p->text)[0]) && (i < S(p->text)-1); i++)
;
pp->hnumber = (i > 6) ? 6 : i;;
while ( (i < S(p->text)) && isspace(T(p->text)[i]) )
++i;
CLIP(p->text, 0, i);
for (i=S(p->text); (i > 0) && isspace(T(p->text)[i-1]); --i)
;
S(p->text) = i;
T(p->text)[i] = 0;
UNCHECK(p);
for (j=S(p->text); (j > 1) && (T(p->text)[j-1] == '#'); --j)
;
while ( j && isspace(T(p->text)[j-1]) )
--j;
if ( j < S(p->text) ) {
T(p->text)[j] = 0;
S(p->text) = j;
}
ret = p->next;
p->next = 0;
break;
}
return ret;
}
static Line *
codeblock(Paragraph *p)
{
Line *t = p->text, *r;
for ( ; t; t = r ) {
__mkd_trim_line(t,4);
if ( !( (r = skipempty(t->next)) && iscode(r)) ) {
___mkd_freeLineRange(t,r);
t->next = 0;
return r;
}
}
return t;
}
static int
iscodefence(Line *r, int size, line_type kind, mkd_flag_t *flags)
{
if ( !is_flag_set(flags, MKD_FENCEDCODE) || is_flag_set(flags, MKD_STRICT) )
return 0;
if ( !(r->is_checked) )
checkline(r, flags);
if ( !r->is_fenced )
return 0;
if ( kind )
return (r->kind == kind) && (r->count >= size);
else
return (r->kind == chk_tilde || r->kind == chk_backtick) && (r->count >= size);
}
static Line *
fencedcodechunk(Line *first, mkd_flag_t *flags)
{
Line *r, *q;
/* don't allow zero-length code fences
*/
if ( (first->next == 0) || iscodefence(first->next, first->count, first->kind, flags) ) {
first->kind = chk_text;
first->is_fenced = 0;
if ( first->next) {
first->next->kind = chk_text;
first->next->is_fenced = 0;
}
return first->next;
}
/* find the closing fence, return a pointer to the Line
* past the closing fence
*/
for ( r = first->next; r; r = r->next ) {
if ( iscodefence(r, first->count, first->kind, flags) ) {
if (S(first->text) - first->count > 0) {
char *lang_attr = T(first->text) + first->count;
while ( *lang_attr != 0 && *lang_attr == ' ' ) lang_attr++;
if ( lang_attr && *lang_attr ) {
first->fence_class = strdup(lang_attr);
}
}
for ( q = first; q && (q != r); q = q->next )
q->is_fenced = 1;
S(first->text) = S(r->text) = 0;
r->is_fenced = 0;
return r->next;
}
}
first->is_fenced = 0;
first->kind = chk_text;
return first;
}
static int
centered(Line *first, Line *last)
{
if ( first&&last ) {
int len = S(last->text);
if ( (len > 2) && (strncmp(T(first->text), "->", 2) == 0)
&& (strncmp(T(last->text)+len-2, "<-", 2) == 0) ) {
CLIP(first->text, 0, 2);
S(last->text) -= 2;
return CENTER;
}
}
return 0;
}
static int
endoftextblock(Line *t, int toplevelblock, mkd_flag_t *flags)
{
int z;
if ( end_of_block(t, flags) || isquote(t) )
return 1;
/* HORRIBLE STANDARDS KLUDGES:
* 1. non-toplevel paragraphs absorb adjacent code blocks
* 2. Toplevel paragraphs eat absorb adjacent list items,
* but sublevel blocks behave properly.
* (What this means is that we only need to check for code
* blocks at toplevel, and only check for list items at
* nested levels.)
*/
return toplevelblock ? 0 : islist(t,&z,flags,&z);
}
static Line *
textblock(Paragraph *p, int toplevel, mkd_flag_t *flags)
{
Line *t, *next;
for ( t = p->text; t ; t = next ) {
if ( iscodefence(t, t->count, 0, flags) )
next = fencedcodechunk(t, flags);
else if ( ((next = t->next) == 0) || endoftextblock(next, toplevel, flags) ) {
p->align = centered(p->text, t);
t->next = 0;
return next;
}
}
return t;
}
/* length of the id: or class: kind in a special div-not-quote block
*/
static int
szmarkerclass(char *p)
{
if ( strncasecmp(p, "id:", 3) == 0 )
return 3;
if ( strncasecmp(p, "class:", 6) == 0 )
return 6;
return 0;
}
/*
* check if the first line of a quoted block is the special div-not-quote
* marker %[kind:]name%
*/
#define iscsschar(c) (isalpha(c) || (c == '-') || (c == '_') || (c == ' ') )
static int
isdivmarker(Line *p, int start, mkd_flag_t *flags)
{
char *s;
int last, i;
if ( is_flag_set(flags, MKD_NODIVQUOTE) || is_flag_set(flags, MKD_STRICT) )
return 0;
start = nextnonblank(p, start);
last= S(p->text) - (1 + start);
s = T(p->text) + start;
if ( (last <= 0) || (*s != '%') || (s[last] != '%') )
return 0;
i = szmarkerclass(s+1);
if ( !iscsschar(s[i+1]) )
return 0;
while ( ++i < last )
if ( !(isdigit(s[i]) || iscsschar(s[i])) )
return 0;
return 1;
}
/*
* accumulate a blockquote.
*
* one sick horrible thing about blockquotes is that even though
* it just takes ^> to start a quote, following lines, if quoted,
* assume that the prefix is ``> ''. This means that code needs
* to be indented *5* spaces from the leading '>', but *4* spaces
* from the start of the line. This does not appear to be
* documented in the reference implementation, but it's the
* way the markdown sample web form at Daring Fireball works.
*/
static Line *
quoteblock(Paragraph *p, mkd_flag_t *flags)
{
Line *t, *q;
int qp;
for ( t = p->text; t ; t = q ) {
if ( isquote(t) ) {
/* clip leading spaces */
for (qp = 0; T(t->text)[qp] != '>'; qp ++)
/* assert: the first nonblank character on this line
* will be a >
*/;
/* clip '>' */
qp++;
/* clip next space, if any */
if ( T(t->text)[qp] == ' ' )
qp++;
__mkd_trim_line(t,qp);
checkline(t, flags);
}
q = skipempty(t->next);
if ( (q == 0) || ((q != t->next) && (!isquote(q) || isdivmarker(q,1,flags))) ) {
___mkd_freeLineRange(t, q);
t = q;
break;
}
}
if ( isdivmarker(p->text,0,flags) ) {
char *prefix = "class";
int i;
q = p->text;
p->text = p->text->next;
if ( (i = szmarkerclass(1+T(q->text))) == 3 )
/* and this would be an "%id:" prefix */
prefix="id";
if ( p->ident = malloc(4+strlen(prefix)+S(q->text)) )
sprintf(p->ident, "%s=\"%.*s\"", prefix, S(q->text)-(i+2),
T(q->text)+(i+1) );
___mkd_freeLine(q);
}
return t;
}
typedef int (*linefn)(Line *);
/*
* pull in a list block. A list block starts with a list marker and
* runs until the next list marker, the next non-indented paragraph,
* or EOF. You do not have to indent nonblank lines after the list
* marker, but multiple paragraphs need to start with a 4-space indent.
*/
static Line *
listitem(Paragraph *p, int indent, mkd_flag_t *flags, linefn check)
{
Line *t, *q;
int clip = indent;
int z;
int firstpara = 1;
int ischeck;
#define CHECK_NOT 0
#define CHECK_NO 1
#define CHECK_YES 2
for ( t = p->text; t ; t = q) {
UNCHECK(t);
__mkd_trim_line(t, clip);
if ( firstpara && !(is_flag_set(flags, MKD_NORMAL_LISTITEM) || is_flag_set(flags, MKD_STRICT)) ) {
ischeck = CHECK_NOT;
if ( strncmp(T(t->text)+t->dle, "[ ]", 3) == 0 )
ischeck = CHECK_NO;
else if ( strncasecmp(T(t->text)+t->dle, "[x]", 3) == 0 )
ischeck = CHECK_YES;
if ( ischeck != CHECK_NOT ) {
__mkd_trim_line(t, 3);
p->para_flags |= GITHUB_CHECK;
if ( ischeck == CHECK_YES )
p->para_flags |= IS_CHECKED;
}
firstpara = 0;
}
/* even though we had to trim a long leader off this item,
* the indent for trailing paragraphs is still 4...
*/
if (indent > 4) {
indent = 4;
}
if ( (q = skipempty(t->next)) == 0 ) {
___mkd_freeLineRange(t,q);
return 0;
}
/* after a blank line, the next block needs to start with a line
* that's indented 4(? -- reference implementation allows a 1
* character indent, but that has unfortunate side effects here)
* spaces, but after that the line doesn't need any indentation
*/
if ( q != t->next ) {
if (q->dle < indent) {
q = t->next;
t->next = 0;
return q;
}
/* indent at least 2, and at most as
* as far as the initial line was indented. */
indent = clip ? clip : 2;
}
if ( (q->dle < indent) && (ishr(q,flags) || islist(q,&z,flags,&z)
|| (check && (*check)(q)))
&& !issetext(q,&z,flags) ) {
q = t->next;
t->next = 0;
return q;
}
clip = (q->dle > indent) ? indent : q->dle;
}
return t;
}
static Line *
definition_block(Paragraph *top, int clip, MMIOT *f, int kind)
{
ParagraphRoot d = { 0, 0 };
Paragraph *p;
Line *q = top->text, *text = 0, *labels;
int z, para;
while (( labels = q )) {
if ( (q = isdefinition(labels, &z, &kind, &(f->flags))) == NULL )
break;
if ( (text = skipempty(q->next)) == 0 )
break;
if ( para = (text != q->next) )
___mkd_freeLineRange(q, text);
q->next = 0;
if ( kind == 1 /* discount dl */ )
for ( q = labels; q; q = q->next ) {
CLIP(q->text, 0, 1);
UNCHECK(q);
S(q->text)--;
}
do {
p = Pp(&d, text, LISTITEM);
text = listitem(p, clip, &(f->flags), (kind==2) ? is_extra_dd : 0);
p->down = compile(p->text, 0, f);
p->text = labels; labels = 0;
if ( para && p->down ) p->down->align = PARA;
if ( (q = skipempty(text)) == 0 )
goto flee;
if ( para = (q != text) ) {
Line anchor;
anchor.next = text;
___mkd_freeLineRange(&anchor,q);
text = q;
}
} while ( kind == 2 && is_extra_dd(q) );
}