-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmarkdown.c
1102 lines (898 loc) · 21.2 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 David L Parsons.
* The redistribution terms are provided in the COPYRIGHT file that must
* be distributed with this source code.
*/
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
#include "config.h"
#include "cstring.h"
#include "markdown.h"
#include "amalloc.h"
/* block-level tags for passing html blocks through the blender
*/
struct kw {
char *id;
int size;
int selfclose;
} ;
#define KW(x) { x, sizeof(x)-1, 0 }
#define SC(x) { x, sizeof(x)-1, 1 }
static struct kw blocktags[] = { KW("!--"), KW("STYLE"), KW("SCRIPT"),
KW("ADDRESS"), KW("BDO"), KW("BLOCKQUOTE"),
KW("CENTER"), KW("DFN"), KW("DIV"), KW("H1"),
KW("H2"), KW("H3"), KW("H4"), KW("H5"),
KW("H6"), KW("LISTING"), KW("NOBR"),
KW("UL"), KW("P"), KW("OL"), KW("DL"),
KW("PLAINTEXT"), KW("PRE"), KW("TABLE"),
KW("WBR"), KW("XMP"), SC("HR"), SC("BR"),
KW("IFRAME"), KW("MAP") };
#define SZTAGS (sizeof blocktags / sizeof blocktags[0])
#define MAXTAG 11 /* sizeof "BLOCKQUOTE" */
typedef int (*stfu)(const void*,const void*);
typedef ANCHOR(Paragraph) ParagraphRoot;
/* case insensitive string sort (for qsort() and bsearch() of block tags)
*/
static int
casort(struct kw *a, struct kw *b)
{
if ( a->size != b->size )
return a->size - b->size;
return strncasecmp(a->id, b->id, b->size);
}
/* 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 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 *
isopentag(Line *p)
{
int i=0, len;
struct kw key, *ret;
if ( !p ) return 0;
len = S(p->text);
if ( len < 3 || T(p->text)[0] != '<' )
return 0;
/* 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 )
;
key.id = T(p->text)+1;
key.size = i-1;
if ( ret = bsearch(&key, blocktags, SZTAGS, sizeof key, (stfu)casort))
return ret;
return 0;
}
typedef struct _flo {
Line *t;
int i;
} FLO;
static int
flogetc(FLO *f)
{
if ( f && f->t ) {
if ( f->i < S(f->t->text) )
return T(f->t->text)[f->i++];
f->t = f->t->next;
f->i = 0;
return flogetc(f);
}
return EOF;
}
static Line *
htmlblock(Paragraph *p, struct kw *tag)
{
Line *ret;
FLO f = { p->text, 0 };
int c;
int i, closing, depth=0;
if ( tag->selfclose || (tag->size >= MAXTAG) ) {
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; 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 ( !f.t )
return 0;
ret = f.t->next;
f.t->next = 0;
return ret;
}
}
}
}
}
return 0;
}
static Line *
comment(Paragraph *p)
{
Line *t, *ret;
for ( t = p->text; t ; t = t->next) {
if ( strstr(T(t->text), "-->") ) {
ret = t->next;
t->next = 0;
return ret;
}
}
return t;
}
/* tables look like
* header|header{|header}
* ------|------{|......}
* {body lines}
*/
static int
istable(Line *t)
{
char *p;
Line *dashes = t->next;
int contains = 0; /* found character bits; 0x01 is |, 0x02 is - */
/* two lines, first must contain | */
if ( !(dashes && memchr(T(t->text), '|', S(t->text))) )
return 0;
/* second line must contain - or | and nothing
* else except for whitespace or :
*/
for ( p = T(dashes->text)+S(dashes->text)-1; p >= T(dashes->text); --p)
if ( *p == '|' )
contains |= 0x01;
else if ( *p == '-' )
contains |= 0x02;
else if ( ! ((*p == ':') || isspace(*p)) )
return 0;
return (contains & 0x03);
}
/* 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 int
isquote(Line *t)
{
int j;
for ( j=0; j < 4; j++ )
if ( T(t->text)[j] == '>' )
return 1;
else if ( !isspace(T(t->text)[j]) )
return 0;
return 0;
}
static int
dashchar(char c)
{
return (c == '*') || (c == '-') || (c == '_');
}
static int
iscode(Line *t)
{
return (t->dle >= 4);
}
static int
ishr(Line *t)
{
int i, count=0;
char dash = 0;
char c;
if ( iscode(t) ) return 0;
for ( i = 0; i < S(t->text); i++) {
c = T(t->text)[i];
if ( (dash == 0) && dashchar(c) )
dash = c;
if ( c == dash ) ++count;
else if ( !isspace(c) )
return 0;
}
return (count >= 3);
}
static int
ishdr(Line *t, int *htyp)
{
int i;
/* first check for etx-style ###HEADER###
*/
/* leading run of `#`'s ?
*/
for ( i=0; T(t->text)[i] == '#'; ++i)
;
/* ANY leading `#`'s make this into an ETX header
*/
if ( i && (i < S(t->text) || i > 1) ) {
*htyp = ETX;
return 1;
}
/* then check for setext-style HEADER
* ======
*/
if ( t->next ) {
char *q = T(t->next->text);
int last = S(t->next->text);
if ( (*q == '=') || (*q == '-') ) {
/* ignore trailing whitespace */
while ( (last > 1) && isspace(q[last-1]) )
--last;
for (i=1; i < last; i++)
if ( q[0] != q[i] )
return 0;
*htyp = SETEXT;
return 1;
}
}
return 0;
}
static int
isdefinition(Line *t)
{
#if DL_TAG_EXTENSION
return t && t->next
&& (S(t->text) > 2)
&& (t->dle == 0)
&& (T(t->text)[0] == '=')
&& (T(t->text)[S(t->text)-1] == '=')
&& ( (t->next->dle >= 4) || isdefinition(t->next) );
#else
return 0;
#endif
}
static int
islist(Line *t, int *trim)
{
int i, j;
char *q;
if ( iscode(t) || blankline(t) || ishdr(t,&i) || ishr(t) )
return 0;
if ( isdefinition(t) ) {
*trim = 4;
return DL;
}
if ( strchr("*-+", T(t->text)[t->dle]) && isspace(T(t->text)[t->dle+1]) ) {
i = nextnonblank(t, t->dle+1);
*trim = (i > 4) ? 4 : i;
return UL;
}
if ( (j = nextblank(t,t->dle)) > t->dle ) {
if ( T(t->text)[j-1] == '.' ) {
#if ALPHA_LIST
if ( (j == t->dle + 2) && isalpha(T(t->text)[t->dle]) ) {
j = nextnonblank(t,j);
*trim = j;
return AL;
}
#endif
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);
*trim = j;
return OL;
}
}
}
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 < 6); i++)
;
pp->hnumber = i;
while ( (i < S(p->text)) && isspace(T(p->text)[i]) )
++i;
CLIP(p->text, 0, i);
for (j=S(p->text); (j > 1) && (T(p->text)[j-1] == '#'); --j)
;
while ( j && isspace(T(p->text)[j-1]) )
--j;
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 ) {
CLIP(t->text,0,4);
t->dle = mkd_firstnonblank(t);
if ( !( (r = skipempty(t->next)) && iscode(r)) ) {
___mkd_freeLineRange(t,r);
t->next = 0;
return r;
}
}
return t;
}
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)
{
int z;
if ( blankline(t)||isquote(t)||iscode(t)||ishdr(t,&z)||ishr(t) )
return 1;
/* HORRIBLE STANDARDS KLUDGE: Toplevel paragraphs eat absorb adjacent
* list items, but sublevel blocks behave properly.
*/
return toplevelblock ? 0 : islist(t,&z);
}
static Line *
textblock(Paragraph *p, int toplevel)
{
Line *t, *next;
for ( t = p->text; t ; t = next ) {
if ( ((next = t->next) == 0) || endoftextblock(next, toplevel) ) {
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%
*/
static int
isdivmarker(Line *p, int start)
{
#if DIV_QUOTE
char *s = T(p->text);
int len = S(p->text);
int i;
if ( !(len && s[start] == '%' && s[len-1] == '%') ) return 0;
i = szmarkerclass(s+start+1)+start;
len -= start+1;
while ( ++i < len )
if ( !isalnum(s[i]) )
return 0;
return 1;
#else
return 0;
#endif
}
/*
* 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)
{
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++;
CLIP(t->text, 0, qp);
t->dle = mkd_firstnonblank(t);
}
q = skipempty(t->next);
if ( (q == 0) || ((q != t->next) && (!isquote(q) || isdivmarker(q,1))) ) {
___mkd_freeLineRange(t, q);
t = q;
break;
}
}
if ( isdivmarker(p->text,0) ) {
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;
}
/*
* A table block starts with a table header (see istable()), and continues
* until EOF or a line that /doesn't/ contain a |.
*/
static Line *
tableblock(Paragraph *p)
{
Line *t, *q;
for ( t = p->text; t && (q = t->next); t = t->next ) {
if ( !memchr(T(q->text), '|', S(q->text)) ) {
t->next = 0;
return q;
}
}
return 0;
}
static Paragraph *Pp(ParagraphRoot *, Line *, int);
static Paragraph *compile(Line *, int, MMIOT *);
/*
* 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)
{
Line *t, *q;
int clip = indent;
int z;
for ( t = p->text; t ; t = q) {
CLIP(t->text, 0, clip);
t->dle = mkd_firstnonblank(t);
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 as far as the initial line was indented. */
indent = clip;
}
if ( (q->dle < indent) && (ishr(q) || islist(q,&z)) && !ishdr(q,&z) ) {
q = t->next;
t->next = 0;
return q;
}
clip = (q->dle > indent) ? indent : q->dle;
}
return t;
}
static Line *
listblock(Paragraph *top, int trim, MMIOT *f)
{
ParagraphRoot d = { 0, 0 };
Paragraph *p;
Line *q = top->text, *text, *label;
int isdl = (top->typ == DL),
para = 0,
ltype;
while (( text = q )) {
if ( top->typ == DL ) {
Line *lp;
for ( lp = label = text; lp ; lp = lp->next ) {
text = lp->next;
CLIP(lp->text, 0, 1);
S(lp->text)--;
if ( !isdefinition(lp->next) )
lp->next = 0;
}
}
else label = 0;
p = Pp(&d, text, LISTITEM);
text = listitem(p, trim);
p->down = compile(p->text, 0, f);
p->text = label;
if ( para && (top->typ != DL) && p->down ) p->down->align = PARA;
if ( !(q = skipempty(text)) || ((ltype = islist(q, &trim)) == 0)
|| (isdl != (ltype == DL)) )
break;
if ( para = (q != text) ) {
Line anchor;
anchor.next = text;
___mkd_freeLineRange(&anchor, q);
}
if ( para && (top->typ != DL) && p->down ) p->down->align = PARA;
}
top->text = 0;
top->down = T(d);
return text;
}
static int
tgood(char c)
{
switch (c) {
case '\'':
case '"': return c;
case '(': return ')';
}
return 0;
}
/*
* add a new (image or link) footnote to the footnote table
*/
static Line*
addfootnote(Line *p, MMIOT* f)
{
int j, i;
int c;
Line *np = p->next;
Footnote *foot = &EXPAND(*f->footnotes);
CREATE(foot->tag);
CREATE(foot->link);
CREATE(foot->title);
foot->height = foot->width = 0;
for (j=i=p->dle+1; T(p->text)[j] != ']'; j++)
EXPAND(foot->tag) = T(p->text)[j];
EXPAND(foot->tag) = 0;
S(foot->tag)--;
j = nextnonblank(p, j+2);
while ( (j < S(p->text)) && !isspace(T(p->text)[j]) )
EXPAND(foot->link) = T(p->text)[j++];
EXPAND(foot->link) = 0;
S(foot->link)--;
j = nextnonblank(p,j);
if ( T(p->text)[j] == '=' ) {
sscanf(T(p->text)+j, "=%dx%d", &foot->width, &foot->height);
while ( (j < S(p->text)) && !isspace(T(p->text)[j]) )
++j;
j = nextnonblank(p,j);
}
if ( (j >= S(p->text)) && np && np->dle && tgood(T(np->text)[np->dle]) ) {
___mkd_freeLine(p);
p = np;
np = p->next;
j = p->dle;
}
if ( (c = tgood(T(p->text)[j])) ) {
/* Try to take the rest of the line as a comment; read to
* EOL, then shrink the string back to before the final
* quote.
*/
++j; /* skip leading quote */
while ( j < S(p->text) )
EXPAND(foot->title) = T(p->text)[j++];
while ( S(foot->title) && T(foot->title)[S(foot->title)-1] != c )
--S(foot->title);
if ( S(foot->title) ) /* skip trailing quote */
--S(foot->title);
EXPAND(foot->title) = 0;
--S(foot->title);
}
___mkd_freeLine(p);
return np;
}
/*
* allocate a paragraph header, link it to the
* tail of the current document
*/
static Paragraph *
Pp(ParagraphRoot *d, Line *ptr, int typ)
{
Paragraph *ret = calloc(sizeof *ret, 1);
ret->text = ptr;
ret->typ = typ;
return ATTACH(*d, ret);
}
static Line*
consume(Line *ptr, int *eaten)
{
Line *next;
int blanks=0;
for (; ptr && blankline(ptr); ptr = next, blanks++ ) {
next = ptr->next;
___mkd_freeLine(ptr);
}
if ( ptr ) *eaten = blanks;
return ptr;
}
/*
* top-level compilation; break the document into
* style, html, and source blocks with footnote links
* weeded out.
*/
static Paragraph *
compile_document(Line *ptr, MMIOT *f)
{
ParagraphRoot d = { 0, 0 };
ANCHOR(Line) source = { 0, 0 };
Paragraph *p = 0;
struct kw *tag;
int eaten;
while ( ptr ) {
if ( !(f->flags & DENY_HTML) && (tag = isopentag(ptr)) ) {
/* If we encounter a html/style block, compile and save all
* of the cached source BEFORE processing the html/style.
*/
if ( T(source) ) {
E(source)->next = 0;
p = Pp(&d, 0, SOURCE);
p->down = compile(T(source), 1, f);
T(source) = E(source) = 0;
}
p = Pp(&d, ptr, strcmp(tag->id, "STYLE") == 0 ? STYLE : HTML);
if ( strcmp(tag->id, "!--") == 0 )
ptr = comment(p);
else
ptr = htmlblock(p, tag);
}
else if ( isfootnote(ptr) ) {
/* footnotes, like cats, sleep anywhere; pull them
* out of the input stream and file them away for
* later processing
*/
ptr = consume(addfootnote(ptr, f), &eaten);
}
else {
/* source; cache it up to wait for eof or the
* next html/style block
*/
ATTACH(source,ptr);
ptr = ptr->next;
}
}
if ( T(source) ) {
/* if there's any cached source at EOF, compile
* it now.
*/
E(source)->next = 0;
p = Pp(&d, 0, SOURCE);
p->down = compile(T(source), 1, f);
}
return T(d);
}
/*
* break a collection of markdown input into
* blocks of lists, code, html, and text to
* be marked up.
*/
static Paragraph *
compile(Line *ptr, int toplevel, MMIOT *f)
{
ParagraphRoot d = { 0, 0 };
Paragraph *p = 0;
Line *r;
int para = toplevel;
int blocks = 0;
int hdr_type, list_type, indent;
ptr = consume(ptr, ¶);
while ( ptr ) {
if ( iscode(ptr) ) {
p = Pp(&d, ptr, CODE);