Debian Codegraph @

ext/markdown.c

raw symbols SCIP
1 /* markdown: a C implementation of John Gruber's Markdown markup language.
2 *
3 * Copyright (C) 2007 David L Parsons.
4 * The redistribution terms are provided in the COPYRIGHT file that must
5 * be distributed with this source code.
6 */
7 #include "config.h"
8
9 #include <stdio.h>
10 #include <string.h>
11 #include <stdarg.h>
12 #include <stdlib.h>
13 #include <time.h>
14 #include <ctype.h>
15
16 #include "cstring.h"
17 #include "markdown.h"
18 #include "amalloc.h"
19 #include "tags.h"
20
21 typedef int (*stfu)(const void*,const void*);
22
23 typedef ANCHOR(Paragraph) ParagraphRoot;
24
25 static Paragraph *Pp(ParagraphRoot *, Line *, int);
26 static Paragraph *compile(Line *, int, MMIOT *);
27
28 /* case insensitive string sort for Footnote tags.
29 */
30 int
31 __mkd_footsort(Footnote *a, Footnote *b)
32 {
33 int i;
34 char ac, bc;
35
36 if ( S(a->tag) != S(b->tag) )
37 return S(a->tag) - S(b->tag);
38
39 for ( i=0; i < S(a->tag); i++) {
40 ac = tolower(T(a->tag)[i]);
41 bc = tolower(T(b->tag)[i]);
42
43 if ( isspace(ac) && isspace(bc) )
44 continue;
45 if ( ac != bc )
46 return ac - bc;
47 }
48 return 0;
49 }
50
51
52 /* find the first blank character after position <i>
53 */
54 static int
55 nextblank(Line *t, int i)
56 {
57 while ( (i < S(t->text)) && !isspace(T(t->text)[i]) )
58 ++i;
59 return i;
60 }
61
62
63 /* find the next nonblank character after position <i>
64 */
65 static int
66 nextnonblank(Line *t, int i)
67 {
68 while ( (i < S(t->text)) && isspace(T(t->text)[i]) )
69 ++i;
70 return i;
71 }
72
73
74 /* find the first nonblank character on the Line.
75 */
76 int
77 mkd_firstnonblank(Line *p)
78 {
79 return nextnonblank(p,0);
80 }
81
82
83 static inline int
84 blankline(Line *p)
85 {
86 return ! (p && (S(p->text) > p->dle) );
87 }
88
89
90 static Line *
91 skipempty(Line *p)
92 {
93 while ( p && (p->dle == S(p->text)) )
94 p = p->next;
95 return p;
96 }
97
98
99 void
100 ___mkd_tidy(Cstring *t)
101 {
102 while ( S(*t) && isspace(T(*t)[S(*t)-1]) )
103 --S(*t);
104 }
105
106
107 static struct kw comment = { "!--", 3, 0 };
108
109 static struct kw *
110 isopentag(Line *p)
111 {
112 int i=0, len;
113 char *line;
114
115 if ( !p ) return 0;
116
117 line = T(p->text);
118 len = S(p->text);
119
120 if ( len < 3 || line[0] != '<' )
121 return 0;
122
123 if ( line[1] == '!' && line[2] == '-' && line[3] == '-' )
124 /* comments need special case handling, because
125 * the !-- doesn't need to end in a whitespace
126 */
127 return &comment;
128
129 /* find how long the tag is so we can check to see if
130 * it's a block-level tag
131 */
132 for ( i=1; i < len && T(p->text)[i] != '>'
133 && T(p->text)[i] != '/'
134 && !isspace(T(p->text)[i]); ++i )
135 ;
136
137
138 return mkd_search_tags(T(p->text)+1, i-1);
139 }
140
141
142 typedef struct _flo {
143 Line *t;
144 int i;
145 } FLO;
146
147 #define floindex(x) (x.i)
148
149
150 static unsigned int
151 flogetc(FLO *f)
152 {
153 if ( f && f->t ) {
154 if ( f->i < S(f->t->text) )
155 return (unsigned char)T(f->t->text)[f->i++];
156 f->t = f->t->next;
157 f->i = 0;
158 return flogetc(f);
159 }
160 return EOF;
161 }
162
163
164 static void
165 splitline(Line *t, int cutpoint)
166 {
167 if ( t && (cutpoint < S(t->text)) ) {
168 Line *tmp = calloc(1, sizeof *tmp);
169
170 tmp->next = t->next;
171 t->next = tmp;
172
173 SUFFIX(tmp->text, T(t->text)+cutpoint, S(t->text)-cutpoint);
174 EXPAND(tmp->text) = 0;
175 S(tmp->text)--;
176
177 S(t->text) = cutpoint;
178 }
179 }
180
181 #define UNCHECK(l) ((l)->flags &= ~CHECKED)
182
183 #define UNLESS_FENCED(t) if (fenced) { \
184 other = 1; l->count += (c == ' ' ? 0 : -1); \
185 } else { t; }
186
187 /*
188 * walk a line, seeing if it's any of half a dozen interesting regular
189 * types.
190 */
191 static void
192 checkline(Line *l, mkd_flag_t flags)
193 {
194 int eol, i;
195 int dashes = 0, spaces = 0,
196 equals = 0, underscores = 0,
197 stars = 0, tildes = 0, other = 0,
198 backticks = 0, fenced = 0;
199
200 l->flags |= CHECKED;
201 l->kind = chk_text;
202 l->count = 0;
203
204 if (l->dle >= 4) { l->kind=chk_code; return; }
205
206 for ( eol = S(l->text); eol > l->dle && isspace(T(l->text)[eol-1]); --eol )
207 ;
208
209 for (i=l->dle; i<eol; i++) {
210 register int c = T(l->text)[i];
211 int is_fence_char = 0;
212
213 if ( c != ' ' ) l->count++;
214
215 switch (c) {
216 case '-': UNLESS_FENCED(dashes = 1); break;
217 case ' ': UNLESS_FENCED(spaces = 1); break;
218 case '=': UNLESS_FENCED(equals = 1); break;
219 case '_': UNLESS_FENCED(underscores = 1); break;
220 case '*': stars = 1; break;
221 default:
222 if ( is_flag_set(flags, MKD_FENCEDCODE) ) {
223 switch (c) {
224 case '~': if (other) return; is_fence_char = 1; tildes = 1; break;
225 case '`': if (other) return; is_fence_char = 1; backticks = 1; break;
226 }
227 if (is_fence_char) {
228 fenced = 1;
229 break;
230 }
231 }
232 other = 1;
233 l->count--;
234 if (!fenced) return;
235 }
236 }
237
238 if ( dashes + equals + underscores + stars + tildes + backticks > 1 )
239 return;
240
241 if ( spaces ) {
242 if ( (underscores || stars || dashes) )
243 l->kind = chk_hr;
244 return;
245 }
246
247 if ( stars || underscores ) { l->kind = chk_hr; }
248 else if ( dashes ) { l->kind = chk_dash; }
249 else if ( equals ) { l->kind = chk_equal; }
250 else if ( tildes ) { l->kind = chk_tilde; }
251 else if ( backticks ) { l->kind = chk_backtick; }
252 }
253
254
255
256 /* markdown only does special handling of comments if the comment end
257 * is at the end of a line
258 */
259 static Line *
260 commentblock(Paragraph *p, int *unclosed)
261 {
262 Line *t, *ret;
263 char *end;
264
265 for ( t = p->text; t ; t = t->next) {
266 if ( end = strstr(T(t->text), "-->") ) {
267 if ( nextnonblank(t, 3 + (end - T(t->text))) < S(t->text) )
268 continue;
269 /*splitline(t, 3 + (end - T(t->text)) );*/
270 ret = t->next;
271 t->next = 0;
272 return ret;
273 }
274 }
275
276 *unclosed = 1;
277 return t;
278
279 }
280
281
282 static Line *
283 htmlblock(Paragraph *p, struct kw *tag, int *unclosed)
284 {
285 Line *ret;
286 FLO f = { p->text, 0 };
287 int c;
288 int i, closing, depth=0;
289
290 *unclosed = 0;
291
292 if ( tag == &comment )
293 return commentblock(p, unclosed);
294
295 if ( tag->selfclose ) {
296 ret = f.t->next;
297 f.t->next = 0;
298 return ret;
299 }
300
301 while ( (c = flogetc(&f)) != EOF ) {
302 if ( c == '<' ) {
303 /* tag? */
304 c = flogetc(&f);
305 if ( c == '!' ) { /* comment? */
306 if ( flogetc(&f) == '-' && flogetc(&f) == '-' ) {
307 /* yes */
308 while ( (c = flogetc(&f)) != EOF ) {
309 if ( c == '-' && flogetc(&f) == '-'
310 && flogetc(&f) == '>')
311 /* consumed whole comment */
312 break;
313 }
314 }
315 }
316 else {
317 if ( closing = (c == '/') ) c = flogetc(&f);
318
319 for ( i=0; i < tag->size; i++, c=flogetc(&f) ) {
320 if ( tag->id[i] != toupper(c) )
321 break;
322 }
323
324 if ( (i == tag->size) && !isalnum(c) ) {
325 depth = depth + (closing ? -1 : 1);
326 if ( depth == 0 ) {
327 while ( c != EOF && c != '>' ) {
328 /* consume trailing gunk in close tag */
329 c = flogetc(&f);
330 }
331 if ( c == EOF )
332 break;
333 if ( !f.t )
334 return 0;
335 splitline(f.t, floindex(f));
336 ret = f.t->next;
337 f.t->next = 0;
338 return ret;
339 }
340 }
341 }
342 }
343 }
344 *unclosed = 1;
345 return 0;
346 }
347
348
349 /* footnotes look like ^<whitespace>{0,3}[stuff]: <content>$
350 */
351 static int
352 isfootnote(Line *t)
353 {
354 int i;
355
356 if ( ( (i = t->dle) > 3) || (T(t->text)[i] != '[') )
357 return 0;
358
359 for ( ++i; i < S(t->text) ; ++i ) {
360 if ( T(t->text)[i] == '[' )
361 return 0;
362 else if ( T(t->text)[i] == ']' )
363 return ( T(t->text)[i+1] == ':' ) ;
364 }
365 return 0;
366 }
367
368
369 static inline int
370 isquote(Line *t)
371 {
372 return (t->dle < 4 && T(t->text)[t->dle] == '>');
373 }
374
375
376 static inline int
377 iscode(Line *t)
378 {
379 return (t->dle >= 4);
380 }
381
382
383 static inline int
384 ishr(Line *t, mkd_flag_t flags)
385 {
386 if ( ! (t->flags & CHECKED) )
387 checkline(t, flags);
388
389 if ( t->count > 2 )
390 return t->kind == chk_hr || t->kind == chk_dash || t->kind == chk_equal;
391 return 0;
392 }
393
394
395 static int
396 issetext(Line *t, int *htyp, mkd_flag_t flags)
397 {
398 Line *n;
399
400 /* check for setext-style HEADER
401 * ======
402 */
403
404 if ( (n = t->next) ) {
405 if ( !(n->flags & CHECKED) )
406 checkline(n, flags);
407
408 if ( n->kind == chk_dash || n->kind == chk_equal ) {
409 *htyp = SETEXT;
410 return 1;
411 }
412 }
413 return 0;
414 }
415
416
417 static int
418 ishdr(Line *t, int *htyp, mkd_flag_t flags)
419 {
420 /* ANY leading `#`'s make this into an ETX header
421 */
422 if ( (t->dle == 0) && (S(t->text) > 1) && (T(t->text)[0] == '#') ) {
423 *htyp = ETX;
424 return 1;
425 }
426
427 /* And if not, maybe it's a SETEXT header instead
428 */
429 return issetext(t, htyp, flags);
430 }
431
432
433 static inline int
434 end_of_block(Line *t, mkd_flag_t flags)
435 {
436 int dummy;
437
438 if ( !t )
439 return 0;
440
441 return ( (S(t->text) <= t->dle) || ishr(t, flags) || ishdr(t, &dummy, flags) );
442 }
443
444
445 static Line*
446 is_discount_dt(Line *t, int *clip, mkd_flag_t flags)
447 {
448 if ( !is_flag_set(flags, MKD_NODLDISCOUNT)
449 && t
450 && t->next
451 && (S(t->text) > 2)
452 && (t->dle == 0)
453 && (T(t->text)[0] == '=')
454 && (T(t->text)[S(t->text)-1] == '=') ) {
455 if ( t->next->dle >= 4 ) {
456 *clip = 4;
457 return t;
458 }
459 else
460 return is_discount_dt(t->next, clip, flags);
461 }
462 return 0;
463 }
464
465
466 static int
467 is_extra_dd(Line *t)
468 {
469 return (t->dle < 4) && (T(t->text)[t->dle] == ':')
470 && isspace(T(t->text)[t->dle+1]);
471 }
472
473
474 static Line*
475 is_extra_dt(Line *t, int *clip, mkd_flag_t flags)
476 {
477 if ( is_flag_set(flags, MKD_DLEXTRA)
478 && t
479 && t->next && S(t->text) && T(t->text)[0] != '='
480 && T(t->text)[S(t->text)-1] != '=') {
481 Line *x;
482
483 if ( iscode(t) || end_of_block(t, flags) )
484 return 0;
485
486 if ( (x = skipempty(t->next)) && is_extra_dd(x) ) {
487 *clip = x->dle+2;
488 return t;
489 }
490
491 if ( x=is_extra_dt(t->next, clip, flags) )
492 return x;
493 }
494 return 0;
495 }
496
497
498 static Line*
499 isdefinition(Line *t, int *clip, int *kind, mkd_flag_t flags)
500 {
501 Line *ret;
502
503 *kind = 1;
504 if ( ret = is_discount_dt(t,clip,flags) )
505 return ret;
506
507 *kind=2;
508 return is_extra_dt(t,clip,flags);
509 }
510
511
512 static int
513 islist(Line *t, int *clip, mkd_flag_t flags, int *list_type)
514 {
515 int i, j;
516 char *q;
517
518 if ( end_of_block(t, flags) )
519 return 0;
520
521 if ( !(is_flag_set(flags, MKD_NODLIST) || is_flag_set(flags, MKD_STRICT))
522 && isdefinition(t,clip,list_type,flags) )
523 return DL;
524
525 if ( strchr("*-+", T(t->text)[t->dle]) && isspace(T(t->text)[t->dle+1]) ) {
526 i = nextnonblank(t, t->dle+1);
527 *clip = (i > 4) ? 4 : i;
528 *list_type = UL;
529 return is_flag_set(flags, MKD_EXPLICITLIST) ? UL : AL;
530 }
531
532 if ( (j = nextblank(t,t->dle)) > t->dle ) {
533 if ( T(t->text)[j-1] == '.' ) {
534
535 if ( !(is_flag_set(flags, MKD_NOALPHALIST) || is_flag_set(flags, MKD_STRICT))
536 && (j == t->dle + 2)
537 && isalpha(T(t->text)[t->dle]) ) {
538 j = nextnonblank(t,j);
539 *clip = (j > 4) ? 4 : j;
540 *list_type = AL;
541 return AL;
542 }
543
544 strtoul(T(t->text)+t->dle, &q, 10);
545 if ( (q > T(t->text)+t->dle) && (q == T(t->text) + (j-1)) ) {
546 j = nextnonblank(t,j);
547 *clip = j;
548 *list_type = OL;
549 return AL;
550 }
551 }
552 }
553 return 0;
554 }
555
556
557 static Line *
558 headerblock(Paragraph *pp, int htyp)
559 {
560 Line *ret = 0;
561 Line *p = pp->text;
562 int i, j;
563
564 switch (htyp) {
565 case SETEXT:
566 /* p->text is header, p->next->text is -'s or ='s
567 */
568 pp->hnumber = (T(p->next->text)[0] == '=') ? 1 : 2;
569
570 ret = p->next->next;
571 ___mkd_freeLine(p->next);
572 p->next = 0;
573 break;
574
575 case ETX:
576 /* p->text is ###header###, so we need to trim off
577 * the leading and trailing `#`'s
578 */
579
580 for (i=0; (T(p->text)[i] == T(p->text)[0]) && (i < S(p->text)-1)
581 && (i < 6); i++)
582 ;
583
584 pp->hnumber = i;
585
586 while ( (i < S(p->text)) && isspace(T(p->text)[i]) )
587 ++i;
588
589 CLIP(p->text, 0, i);
590 UNCHECK(p);
591
592 for (j=S(p->text); (j > 1) && (T(p->text)[j-1] == '#'); --j)
593 ;
594
595 while ( j && isspace(T(p->text)[j-1]) )
596 --j;
597
598 S(p->text) = j;
599
600 ret = p->next;
601 p->next = 0;
602 break;
603 }
604 return ret;
605 }
606
607
608 static Line *
609 codeblock(Paragraph *p)
610 {
611 Line *t = p->text, *r;
612
613 for ( ; t; t = r ) {
614 __mkd_trim_line(t,4);
615
616 if ( !( (r = skipempty(t->next)) && iscode(r)) ) {
617 ___mkd_freeLineRange(t,r);
618 t->next = 0;
619 return r;
620 }
621 }
622 return t;
623 }
624
625
626 static int
627 iscodefence(Line *r, int size, line_type kind, mkd_flag_t flags)
628 {
629 if ( !is_flag_set(flags, MKD_FENCEDCODE) )
630 return 0;
631
632 if ( !(r->flags & CHECKED) )
633 checkline(r, flags);
634
635 if ( kind )
636 return (r->kind == kind) && (r->count >= size);
637 else
638 return (r->kind == chk_tilde || r->kind == chk_backtick) && (r->count >= size);
639 }
640
641
642 static Paragraph *
643 fencedcodeblock(ParagraphRoot *d, Line **ptr, mkd_flag_t flags)
644 {
645 Line *first, *r;
646 Paragraph *ret;
647
648 first = (*ptr);
649
650 /* don't allow zero-length code fences
651 */
652 if ( (first->next == 0) || iscodefence(first->next, first->count, 0, flags) )
653 return 0;
654
655 /* find the closing fence, discard the fences,
656 * return a Paragraph with the contents
657 */
658 for ( r = first; r && r->next; r = r->next )
659 if ( iscodefence(r->next, first->count, first->kind, flags) ) {
660 (*ptr) = r->next->next;
661 ret = Pp(d, first->next, CODE);
662 if (S(first->text) - first->count > 0) {
663 char *lang_attr = T(first->text) + first->count;
664 while ( *lang_attr != 0 && *lang_attr == ' ' ) lang_attr++;
665 ret->lang = strdup(lang_attr);
666 }
667 else {
668 ret->lang = 0;
669 }
670 ___mkd_freeLine(first);
671 ___mkd_freeLine(r->next);
672 r->next = 0;
673 return ret;
674 }
675 return 0;
676 }
677
678
679 static int
680 centered(Line *first, Line *last)
681 {
682
683 if ( first&&last ) {
684 int len = S(last->text);
685
686 if ( (len > 2) && (strncmp(T(first->text), "->", 2) == 0)
687 && (strncmp(T(last->text)+len-2, "<-", 2) == 0) ) {
688 CLIP(first->text, 0, 2);
689 S(last->text) -= 2;
690 return CENTER;
691 }
692 }
693 return 0;
694 }
695
696
697 static int
698 endoftextblock(Line *t, int toplevelblock, mkd_flag_t flags)
699 {
700 int z;
701
702 if ( end_of_block(t, flags) || isquote(t) )
703 return 1;
704
705 /* HORRIBLE STANDARDS KLUDGES:
706 * 1. non-toplevel paragraphs absorb adjacent code blocks
707 * 2. Toplevel paragraphs eat absorb adjacent list items,
708 * but sublevel blocks behave properly.
709 * (What this means is that we only need to check for code
710 * blocks at toplevel, and only check for list items at
711 * nested levels.)
712 */
713 return toplevelblock ? 0 : islist(t,&z,flags,&z);
714 }
715
716
717 static Line *
718 textblock(Paragraph *p, int toplevel, mkd_flag_t flags)
719 {
720 Line *t, *next;
721
722 for ( t = p->text; t ; t = next ) {
723 if ( ((next = t->next) == 0) || endoftextblock(next, toplevel, flags) ) {
724 p->align = centered(p->text, t);
725 t->next = 0;
726 return next;
727 }
728 }
729 return t;
730 }
731
732
733 /* length of the id: or class: kind in a special div-not-quote block
734 */
735 static int
736 szmarkerclass(char *p)
737 {
738 if ( strncasecmp(p, "id:", 3) == 0 )
739 return 3;
740 if ( strncasecmp(p, "class:", 6) == 0 )
741 return 6;
742 return 0;
743 }
744
745
746 /*
747 * check if the first line of a quoted block is the special div-not-quote
748 * marker %[kind:]name%
749 */
750 #define iscsschar(c) (isalpha(c) || (c == '-') || (c == '_') )
751
752 static int
753 isdivmarker(Line *p, int start, mkd_flag_t flags)
754 {
755 char *s;
756 int last, i;
757
758 if ( is_flag_set(flags, MKD_NODIVQUOTE) || is_flag_set(flags, MKD_STRICT) )
759 return 0;
760
761 start = nextnonblank(p, start);
762 last= S(p->text) - (1 + start);
763 s = T(p->text) + start;
764
765 if ( (last <= 0) || (*s != '%') || (s[last] != '%') )
766 return 0;
767
768 i = szmarkerclass(s+1);
769
770 if ( !iscsschar(s[i+1]) )
771 return 0;
772 while ( ++i < last )
773 if ( !(isdigit(s[i]) || iscsschar(s[i])) )
774 return 0;
775
776 return 1;
777 }
778
779
780 /*
781 * accumulate a blockquote.
782 *
783 * one sick horrible thing about blockquotes is that even though
784 * it just takes ^> to start a quote, following lines, if quoted,
785 * assume that the prefix is ``> ''. This means that code needs
786 * to be indented *5* spaces from the leading '>', but *4* spaces
787 * from the start of the line. This does not appear to be
788 * documented in the reference implementation, but it's the
789 * way the markdown sample web form at Daring Fireball works.
790 */
791 static Line *
792 quoteblock(Paragraph *p, mkd_flag_t flags)
793 {
794 Line *t, *q;
795 int qp;
796
797 for ( t = p->text; t ; t = q ) {
798 if ( isquote(t) ) {
799 /* clip leading spaces */
800 for (qp = 0; T(t->text)[qp] != '>'; qp ++)
801 /* assert: the first nonblank character on this line
802 * will be a >
803 */;
804 /* clip '>' */
805 qp++;
806 /* clip next space, if any */
807 if ( T(t->text)[qp] == ' ' )
808 qp++;
809 __mkd_trim_line(t,qp);
810 UNCHECK(t);
811 }
812
813 q = skipempty(t->next);
814
815 if ( (q == 0) || ((q != t->next) && (!isquote(q) || isdivmarker(q,1,flags))) ) {
816 ___mkd_freeLineRange(t, q);
817 t = q;
818 break;
819 }
820 }
821 if ( isdivmarker(p->text,0,flags) ) {
822 char *prefix = "class";
823 int i;
824
825 q = p->text;
826 p->text = p->text->next;
827
828 if ( (i = szmarkerclass(1+T(q->text))) == 3 )
829 /* and this would be an "%id:" prefix */
830 prefix="id";
831
832 if ( p->ident = malloc(4+strlen(prefix)+S(q->text)) )
833 sprintf(p->ident, "%s=\"%.*s\"", prefix, S(q->text)-(i+2),
834 T(q->text)+(i+1) );
835
836 ___mkd_freeLine(q);
837 }
838 return t;
839 }
840
841
842 typedef int (*linefn)(Line *);
843
844
845 /*
846 * pull in a list block. A list block starts with a list marker and
847 * runs until the next list marker, the next non-indented paragraph,
848 * or EOF. You do not have to indent nonblank lines after the list
849 * marker, but multiple paragraphs need to start with a 4-space indent.
850 */
851 static Line *
852 listitem(Paragraph *p, int indent, mkd_flag_t flags, linefn check)
853 {
854 Line *t, *q;
855 int clip = indent;
856 int z;
857 #ifdef GITHUB_CHECKBOX
858 int firstpara = 1;
859 int ischeck;
860 #define CHECK_NOT 0
861 #define CHECK_NO 1
862 #define CHECK_YES 2
863 #endif
864
865 for ( t = p->text; t ; t = q) {
866 UNCHECK(t);
867 __mkd_trim_line(t, clip);
868
869 #ifdef GITHUB_CHECKBOX
870 if ( firstpara ) {
871 ischeck = CHECK_NOT;
872 if ( strncmp(T(t->text)+t->dle, "[ ]", 3) == 0 )
873 ischeck = CHECK_NO;
874 else if ( strncasecmp(T(t->text)+t->dle, "[x]", 3) == 0 )
875 ischeck = CHECK_YES;
876
877 if ( ischeck != CHECK_NOT ) {
878 __mkd_trim_line(t, 3);
879 p->flags |= GITHUB_CHECK;
880 if ( ischeck == CHECK_YES )
881 p->flags |= IS_CHECKED;
882 }
883 firstpara = 0;
884 }
885 #endif
886
887 /* even though we had to trim a long leader off this item,
888 * the indent for trailing paragraphs is still 4...
889 */
890 if (indent > 4) {
891 indent = 4;
892 }
893 if ( (q = skipempty(t->next)) == 0 ) {
894 ___mkd_freeLineRange(t,q);
895 return 0;
896 }
897
898 /* after a blank line, the next block needs to start with a line
899 * that's indented 4(? -- reference implementation allows a 1
900 * character indent, but that has unfortunate side effects here)
901 * spaces, but after that the line doesn't need any indentation
902 */
903 if ( q != t->next ) {
904 if (q->dle < indent) {
905 q = t->next;
906 t->next = 0;
907 return q;
908 }
909 /* indent at least 2, and at most as
910 * as far as the initial line was indented. */
911 indent = clip ? clip : 2;
912 }
913
914 if ( (q->dle < indent) && (ishr(q,flags) || islist(q,&z,flags,&z)
915 || (check && (*check)(q)))
916 && !issetext(q,&z,flags) ) {
917 q = t->next;
918 t->next = 0;
919 return q;
920 }
921
922 clip = (q->dle > indent) ? indent : q->dle;
923 }
924 return t;
925 }
926
927
928 static Line *
929 definition_block(Paragraph *top, int clip, MMIOT *f, int kind)
930 {
931 ParagraphRoot d = { 0, 0 };
932 Paragraph *p;
933 Line *q = top->text, *text = 0, *labels;
934 int z, para;
935
936 while (( labels = q )) {
937
938 if ( (q = isdefinition(labels, &z, &kind, f->flags)) == 0 )
939 break;
940
941 if ( (text = skipempty(q->next)) == 0 )
942 break;
943
944 if ( para = (text != q->next) )
945 ___mkd_freeLineRange(q, text);
946
947 q->next = 0;
948 if ( kind == 1 /* discount dl */ )
949 for ( q = labels; q; q = q->next ) {
950 CLIP(q->text, 0, 1);
951 UNCHECK(q);
952 S(q->text)--;
953 }
954
955 dd_block:
956 p = Pp(&d, text, LISTITEM);
957
958 text = listitem(p, clip, f->flags, (kind==2) ? is_extra_dd : 0);
959 p->down = compile(p->text, 0, f);
960 p->text = labels; labels = 0;
961
962 if ( para && p->down ) p->down->align = PARA;
963
964 if ( (q = skipempty(text)) == 0 )
965 break;
966
967 if ( para = (q != text) ) {
968 Line anchor;
969
970 anchor.next = text;
971 ___mkd_freeLineRange(&anchor,q);
972 text = q;
973
974 }
975
976 if ( kind == 2 && is_extra_dd(q) )
977 goto dd_block;
978 }
979 top->text = 0;
980 top->down = T(d);
981 return text;
982 }
983
984
985 static Line *
986 enumerated_block(Paragraph *top, int clip, MMIOT *f, int list_class)
987 {
988 ParagraphRoot d = { 0, 0 };
989 Paragraph *p;
990 Line *q = top->text, *text;
991 int para = 0, z;
992
993 while (( text = q )) {
994
995 p = Pp(&d, text, LISTITEM);
996 text = listitem(p, clip, f->flags, 0);
997
998 p->down = compile(p->text, 0, f);
999 p->text = 0;
1000
1001 if ( para && p->down ) p->down->align = PARA;
1002
1003 if ( (q = skipempty(text)) == 0
1004 || islist(q, &clip, f->flags, &z) != list_class )
1005 break;
1006
1007 if ( para = (q != text) ) {
1008 Line anchor;
1009
1010 anchor.next = text;
1011 ___mkd_freeLineRange(&anchor, q);
1012
1013 if ( p->down ) p->down->align = PARA;
1014 }
1015 }
1016 top->text = 0;
1017 top->down = T(d);
1018 return text;
1019 }
1020
1021
1022 static int
1023 tgood(char c)
1024 {
1025 switch (c) {
1026 case '\'':
1027 case '"': return c;
1028 case '(': return ')';
1029 }
1030 return 0;
1031 }
1032
1033
1034 /*
1035 * eat lines for a markdown extra footnote
1036 */
1037 static Line *
1038 extrablock(Line *p)
1039 {
1040 Line *np;
1041
1042 while ( p && p->next ) {
1043 np = p->next;
1044
1045 if ( np->dle < 4 && np->dle < S(np->text) ) {
1046 p->next = 0;
1047 return np;
1048 }
1049 __mkd_trim_line(np,4);
1050 p = np;
1051 }
1052 return 0;
1053 }
1054
1055
1056 /*
1057 * add a new (image or link) footnote to the footnote table
1058 */
1059 static Line*
1060 addfootnote(Line *p, MMIOT* f)
1061 {
1062 int j, i;
1063 int c;
1064 Line *np = p->next;
1065
1066 Footnote *foot = &EXPAND(f->footnotes->note);
1067
1068 CREATE(foot->tag);
1069 CREATE(foot->link);
1070 CREATE(foot->title);
1071 foot->text = 0;
1072 foot->flags = foot->height = foot->width = 0;
1073
1074 /* keep the footnote label */
1075 for (j=i=p->dle+1; T(p->text)[j] != ']'; j++)
1076 EXPAND(foot->tag) = T(p->text)[j];
1077 EXPAND(foot->tag) = 0;
1078 S(foot->tag)--;
1079
1080 /* consume the closing ]: */
1081 j = nextnonblank(p, j+2);
1082
1083 if ( is_flag_set(f->flags, MKD_EXTRA_FOOTNOTE) && (T(foot->tag)[0] == '^') ) {
1084 /* markdown extra footnote: All indented lines past this point;
1085 * the first line includes the footnote reference, so we need to
1086 * snip that out as we go.
1087 */
1088 foot->flags |= EXTRA_FOOTNOTE;
1089 __mkd_trim_line(p,j);
1090
1091 np = extrablock(p);
1092
1093 foot->text = compile(p, 0, f);
1094
1095 return np;
1096 }
1097
1098 while ( (j < S(p->text)) && !isspace(T(p->text)[j]) )
1099 EXPAND(foot->link) = T(p->text)[j++];
1100 EXPAND(foot->link) = 0;
1101 S(foot->link)--;
1102 j = nextnonblank(p,j);
1103
1104 if ( T(p->text)[j] == '=' ) {
1105 sscanf(T(p->text)+j, "=%dx%d", &foot->width, &foot->height);
1106 j = nextblank(p, j);
1107 j = nextnonblank(p,j);
1108 }
1109
1110
1111 if ( (j >= S(p->text)) && np && np->dle && tgood(T(np->text)[np->dle]) ) {
1112 ___mkd_freeLine(p);
1113 p = np;
1114 np = p->next;
1115 j = p->dle;
1116 }
1117
1118 if ( (c = tgood(T(p->text)[j])) ) {
1119 /* Try to take the rest of the line as a comment; read to
1120 * EOL, then shrink the string back to before the final
1121 * quote.
1122 */
1123 ++j; /* skip leading quote */
1124
1125 while ( j < S(p->text) )
1126 EXPAND(foot->title) = T(p->text)[j++];
1127
1128 while ( S(foot->title) && T(foot->title)[S(foot->title)-1] != c )
1129 --S(foot->title);
1130 if ( S(foot->title) ) /* skip trailing quote */
1131 --S(foot->title);
1132 EXPAND(foot->title) = 0;
1133 --S(foot->title);
1134 }
1135
1136 ___mkd_freeLine(p);
1137 return np;
1138 }
1139
1140
1141 /*
1142 * allocate a paragraph header, link it to the
1143 * tail of the current document
1144 */
1145 static Paragraph *
1146 Pp(ParagraphRoot *d, Line *ptr, int typ)
1147 {
1148 Paragraph *ret = calloc(sizeof *ret, 1);
1149
1150 ret->text = ptr;
1151 ret->typ = typ;
1152
1153 return ATTACH(*d, ret);
1154 }
1155
1156
1157
1158 static Line*
1159 consume(Line *ptr, int *eaten)
1160 {
1161 Line *next;
1162 int blanks=0;
1163
1164 for (; ptr && blankline(ptr); ptr = next, blanks++ ) {
1165 next = ptr->next;
1166 ___mkd_freeLine(ptr);
1167 }
1168 if ( ptr ) *eaten = blanks;
1169 return ptr;
1170 }
1171
1172
1173 typedef ANCHOR(Line) Cache;
1174
1175 static void
1176 uncache(Cache *cache, ParagraphRoot *d, MMIOT *f)
1177 {
1178 Paragraph *p;
1179
1180 if ( T(*cache) ) {
1181 E(*cache)->next = 0;
1182 p = Pp(d, 0, SOURCE);
1183 p->down = compile(T(*cache), 1, f);
1184 T(*cache) = E(*cache) = 0;
1185 }
1186 }
1187
1188
1189 /*
1190 * top-level compilation; break the document into
1191 * style, html, and source blocks with footnote links
1192 * weeded out.
1193 */
1194 static Paragraph *
1195 compile_document(Line *ptr, MMIOT *f)
1196 {
1197 ParagraphRoot d = { 0, 0 };
1198 Cache source = { 0, 0 };
1199 Paragraph *p = 0;
1200 struct kw *tag;
1201 int eaten, unclosed;
1202 int previous_was_break = 1;
1203
1204 while ( ptr ) {
1205 if ( !is_flag_set(f->flags, MKD_NOHTML) && (tag = isopentag(ptr)) ) {
1206 int blocktype;
1207 /* If we encounter a html/style block, compile and save all
1208 * of the cached source BEFORE processing the html/style.
1209 */
1210 uncache(&source, &d, f);
1211
1212 if (is_flag_set(f->flags, MKD_NOSTYLE) )
1213 blocktype = HTML;
1214 else
1215 blocktype = strcmp(tag->id, "STYLE") == 0 ? STYLE : HTML;
1216 p = Pp(&d, ptr, blocktype);
1217 ptr = htmlblock(p, tag, &unclosed);
1218 if ( unclosed ) {
1219 p->typ = SOURCE;
1220 p->down = compile(p->text, 1, f);
1221 p->text = 0;
1222 }
1223 previous_was_break = 1;
1224 }
1225 else if ( isfootnote(ptr) ) {
1226 /* footnotes, like cats, sleep anywhere; pull them
1227 * out of the input stream and file them away for
1228 * later processing
1229 */
1230 ptr = consume(addfootnote(ptr, f), &eaten);
1231 previous_was_break = 1;
1232 }
1233 else if ( previous_was_break && iscodefence(ptr,3,0,f->flags)) {
1234 uncache(&source, &d, f);
1235 if ( !fencedcodeblock(&d, &ptr, f->flags) ) /* just source */
1236 goto attach;
1237 }
1238 else {
1239 attach:
1240 /* source; cache it up to wait for eof or the
1241 * next html/style block
1242 */
1243 ATTACH(source,ptr);
1244 previous_was_break = blankline(ptr);
1245 ptr = ptr->next;
1246 }
1247 }
1248 /* if there's any cached source at EOF, compile
1249 * it now.
1250 */
1251 uncache(&source, &d, f);
1252 return T(d);
1253 }
1254
1255
1256 static int
1257 first_nonblank_before(Line *j, int dle)
1258 {
1259 return (j->dle < dle) ? j->dle : dle;
1260 }
1261
1262
1263 static int
1264 actually_a_table(MMIOT *f, Line *pp)
1265 {
1266 Line *r;
1267 int j;
1268 int c;
1269
1270 /* tables need to be turned on */
1271 if ( is_flag_set(f->flags, MKD_STRICT) || is_flag_set(f->flags, MKD_NOTABLES) )
1272 return 0;
1273
1274 /* tables need three lines */
1275 if ( !(pp && pp->next && pp->next->next) ) {
1276 return 0;
1277 }
1278
1279 /* all lines must contain |'s */
1280 for (r = pp; r; r = r->next )
1281 if ( !(r->flags & PIPECHAR) ) {
1282 return 0;
1283 }
1284
1285 /* if the header has a leading |, all lines must have leading |'s */
1286 if ( T(pp->text)[pp->dle] == '|' ) {
1287 for ( r = pp; r; r = r->next )
1288 if ( T(r->text)[first_nonblank_before(r,pp->dle)] != '|' ) {
1289 return 0;
1290 }
1291 }
1292
1293 /* second line must be only whitespace, -, |, or : */
1294 r = pp->next;
1295
1296 for ( j=r->dle; j < S(r->text); ++j ) {
1297 c = T(r->text)[j];
1298
1299 if ( !(isspace(c)||(c=='-')||(c==':')||(c=='|')) ) {
1300 return 0;
1301 }
1302 }
1303
1304 return 1;
1305 }
1306
1307
1308 /*
1309 * break a collection of markdown input into
1310 * blocks of lists, code, html, and text to
1311 * be marked up.
1312 */
1313 static Paragraph *
1314 compile(Line *ptr, int toplevel, MMIOT *f)
1315 {
1316 ParagraphRoot d = { 0, 0 };
1317 Paragraph *p = 0;
1318 Line *r;
1319 int para = toplevel;
1320 int blocks = 0;
1321 int hdr_type, list_type, list_class, indent;
1322
1323 ptr = consume(ptr, &para);
1324
1325 while ( ptr ) {
1326
1327 if ( iscode(ptr) ) {
1328 p = Pp(&d, ptr, CODE);
1329
1330 if ( is_flag_set(f->flags, MKD_1_COMPAT) ) {
1331 /* HORRIBLE STANDARDS KLUDGE: the first line of every block
1332 * has trailing whitespace trimmed off.
1333 */
1334 ___mkd_tidy(&p->text->text);
1335 }
1336
1337 ptr = codeblock(p);
1338 }
1339 else if ( iscodefence(ptr,3,0,f->flags) && (p=fencedcodeblock(&d, &ptr, f->flags)) )
1340 /* yay, it's already done */ ;
1341 else if ( ishr(ptr, f->flags) ) {
1342 p = Pp(&d, 0, HR);
1343 r = ptr;
1344 ptr = ptr->next;
1345 ___mkd_freeLine(r);
1346 }
1347 else if ( list_class = islist(ptr, &indent, f->flags, &list_type) ) {
1348 if ( list_class == DL ) {
1349 p = Pp(&d, ptr, DL);
1350 ptr = definition_block(p, indent, f, list_type);
1351 }
1352 else {
1353 p = Pp(&d, ptr, list_type);
1354 ptr = enumerated_block(p, indent, f, list_class);
1355 }
1356 }
1357 else if ( isquote(ptr) ) {
1358 p = Pp(&d, ptr, QUOTE);
1359 ptr = quoteblock(p, f->flags);
1360 p->down = compile(p->text, 1, f);
1361 p->text = 0;
1362 }
1363 else if ( ishdr(ptr, &hdr_type, f->flags) ) {
1364 p = Pp(&d, ptr, HDR);
1365 ptr = headerblock(p, hdr_type);
1366 }
1367 else {
1368 /* either markup or an html block element
1369 */
1370 struct kw *tag;
1371 int unclosed = 1;
1372
1373 p = Pp(&d, ptr, MARKUP); /* default to regular markup,
1374 * then check if it's an html
1375 * block. If it IS an html
1376 * block, htmlblock() will
1377 * populate this paragraph &
1378 * all we need to do is reset
1379 * the paragraph type to HTML,
1380 * otherwise the paragraph
1381 * remains empty and ready for
1382 * processing with textblock()
1383 */
1384
1385 if ( !is_flag_set(f->flags, MKD_NOHTML) && (tag = isopentag(ptr)) ) {
1386 /* possibly an html block
1387 */
1388
1389 ptr = htmlblock(p, tag, &unclosed);
1390 if ( ! unclosed ) {
1391 p->typ = HTML;
1392 }
1393 }
1394 if ( unclosed ) {
1395 ptr = textblock(p, toplevel, f->flags);
1396 /* tables are a special kind of paragraph */
1397 if ( actually_a_table(f, p->text) )
1398 p->typ = TABLE;
1399 }
1400 }
1401 if ( (para||toplevel) && !p->align )
1402 p->align = PARA;
1403
1404 blocks++;
1405 para = toplevel || (blocks > 1);
1406 ptr = consume(ptr, &para);
1407
1408 if ( para && !p->align )
1409 p->align = PARA;
1410
1411 }
1412 return T(d);
1413 }
1414
1415
1416 /*
1417 * the guts of the markdown() function, ripped out so I can do
1418 * debugging.
1419 */
1420
1421 /*
1422 * prepare and compile `text`, returning a Paragraph tree.
1423 */
1424 int
1425 mkd_compile(Document *doc, mkd_flag_t flags)
1426 {
1427 if ( !doc )
1428 return 0;
1429
1430 flags &= USER_FLAGS;
1431
1432 if ( doc->compiled ) {
1433 if ( doc->ctx->flags == flags && !doc->dirty)
1434 return 1;
1435 else {
1436 doc->compiled = doc->dirty = 0;
1437 if ( doc->code)
1438 ___mkd_freeParagraph(doc->code);
1439 if ( doc->ctx->footnotes )
1440 ___mkd_freefootnotes(doc->ctx);
1441 }
1442 }
1443
1444 doc->compiled = 1;
1445 memset(doc->ctx, 0, sizeof(MMIOT) );
1446 doc->ctx->ref_prefix= doc->ref_prefix;
1447 doc->ctx->cb = &(doc->cb);
1448 doc->ctx->flags = flags;
1449 CREATE(doc->ctx->in);
1450 doc->ctx->footnotes = malloc(sizeof doc->ctx->footnotes[0]);
1451 doc->ctx->footnotes->reference = 0;
1452 CREATE(doc->ctx->footnotes->note);
1453
1454 mkd_initialize();
1455
1456 doc->code = compile_document(T(doc->content), doc->ctx);
1457 qsort(T(doc->ctx->footnotes->note), S(doc->ctx->footnotes->note),
1458 sizeof T(doc->ctx->footnotes->note)[0],
1459 (stfu)__mkd_footsort);
1460 memset(&doc->content, 0, sizeof doc->content);
1461 return 1;
1462 }
1463