-
Notifications
You must be signed in to change notification settings - Fork 8
/
markdown.c
1761 lines (1487 loc) · 49.9 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.c - generic markdown parser */
/*
* Copyright (c) 2009, Natacha Porté
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "markdown.h"
#include "array.h"
#include <assert.h>
#include <string.h>
#include <strings.h> /* for strncasecmp */
#define TEXT_UNIT 64 /* unit for the copy of the input buffer */
#define WORK_UNIT 64 /* block-level working buffer */
#define MKD_LI_END 8 /* internal list flag */
/***************
* LOCAL TYPES *
***************/
/* link_ref • reference to a link */
struct link_ref {
struct buf * id;
struct buf * link;
struct buf * title; };
/* char_trigger • function pointer to render active chars */
/* returns the number of chars taken care of */
/* data is the pointer of the beginning of the span */
/* offset is the number of valid chars before data */
struct render;
typedef size_t
(*char_trigger)(struct buf *ob, struct render *rndr,
char *data, size_t offset, size_t size);
/* render • structure containing one particular render */
struct render {
struct mkd_renderer make;
struct array refs;
char_trigger active_char[256];
struct parray work; };
/* html_tag • structure for quick HTML tag search (inspired from discount) */
struct html_tag {
char * text;
int size; };
/********************
* GLOBAL VARIABLES *
********************/
/* block_tags • recognised block tags, sorted by cmp_html_tag */
static struct html_tag block_tags[] = {
/*0*/ { "p", 1 },
{ "dl", 2 },
{ "h1", 2 },
{ "h2", 2 },
{ "h3", 2 },
{ "h4", 2 },
{ "h5", 2 },
{ "h6", 2 },
{ "ol", 2 },
{ "ul", 2 },
/*10*/ { "del", 3 },
{ "div", 3 },
/*12*/ { "ins", 3 },
{ "pre", 3 },
{ "form", 4 },
{ "math", 4 },
{ "table", 5 },
{ "iframe", 6 },
{ "script", 6 },
{ "fieldset", 8 },
{ "noscript", 8 },
{ "blockquote", 10 } };
#define INS_TAG (block_tags + 12)
#define DEL_TAG (block_tags + 10)
/***************************
* STATIC HELPER FUNCTIONS *
***************************/
/* build_ref_id • collapse whitespace from input text to make it a ref id */
static int
build_ref_id(struct buf *id, const char *data, size_t size) {
size_t beg, i;
/* skip leading whitespace */
while (size > 0
&& (data[0] == ' ' || data[0] == '\t' || data[0] == '\n')) {
data += 1;
size -= 1; }
/* skip trailing whitespace */
while (size > 0
&& (data[size - 1] == ' ' || data[size - 1] == '\t'
|| data[size - 1] == '\n'))
size -= 1;
if (size == 0) return -1;
/* making the ref id */
i = 0;
id->size = 0;
while (i < size) {
/* copy non-whitespace into the output buffer */
beg = i;
while (i < size
&& !(data[i] == ' ' || data[i] == '\t' || data[i] == '\n'))
i += 1;
bufput(id, data + beg, i - beg);
/* add a single space and skip all consecutive whitespace */
if (i < size) bufputc(id, ' ');
while (i < size
&& (data[i] == ' ' || data[i] == '\t' || data[i] == '\n'))
i += 1; }
return 0; }
/* cmp_link_ref • comparison function for link_ref sorted arrays */
static int
cmp_link_ref(void *key, void *array_entry) {
struct link_ref *lr = array_entry;
return bufcasecmp(key, lr->id); }
/* cmp_link_ref_sort • comparison function for link_ref qsort */
static int
cmp_link_ref_sort(const void *a, const void *b) {
const struct link_ref *lra = a;
const struct link_ref *lrb = b;
return bufcasecmp(lra->id, lrb->id); }
/* cmp_html_tag • comparison function for bsearch() (stolen from discount) */
static int
cmp_html_tag(const void *a, const void *b) {
const struct html_tag *hta = a;
const struct html_tag *htb = b;
if (hta->size != htb->size) return hta->size - htb->size;
return strncasecmp(hta->text, htb->text, hta->size); }
/* find_block_tag • returns the current block tag */
static struct html_tag *
find_block_tag(char *data, size_t size) {
size_t i = 0;
struct html_tag key;
/* looking for the word end */
while (i < size && ((data[i] >= '0' && data[i] <= '9')
|| (data[i] >= 'A' && data[i] <= 'Z')
|| (data[i] >= 'a' && data[i] <= 'z')))
i += 1;
if (i >= size) return 0;
/* binary search of the tag */
key.text = data;
key.size = i;
return bsearch(&key, block_tags,
sizeof block_tags / sizeof block_tags[0],
sizeof block_tags[0], cmp_html_tag); }
/* new_work_buffer • get a new working buffer from the stack or create one */
static struct buf *
new_work_buffer(struct render *rndr) {
struct buf *ret = 0;
if (rndr->work.size < rndr->work.asize) {
ret = rndr->work.item[rndr->work.size ++];
ret->size = 0; }
else {
ret = bufnew(WORK_UNIT);
parr_push(&rndr->work, ret); }
return ret; }
/* release_work_buffer • release the given working buffer */
static void
release_work_buffer(struct render *rndr, struct buf *buf) {
assert(rndr->work.size > 0
&& rndr->work.item[rndr->work.size - 1] == buf);
rndr->work.size -= 1; }
/****************************
* INLINE PARSING FUNCTIONS *
****************************/
/* is_mail_autolink • looks for the address part of a mail autolink and '>' */
/* this is less strict than the original markdown e-mail address matching */
static size_t
is_mail_autolink(char *data, size_t size) {
size_t i = 0, nb = 0;
/* address is assumed to be: [-@._a-zA-Z0-9]+ with exactly one '@' */
while (i < size && (data[i] == '-' || data[i] == '.'
|| data[i] == '_' || data[i] == '@'
|| (data[i] >= 'a' && data[i] <= 'z')
|| (data[i] >= 'A' && data[i] <= 'Z')
|| (data[i] >= '0' && data[i] <= '9'))) {
if (data[i] == '@') nb += 1;
i += 1; }
if (i >= size || data[i] != '>' || nb != 1) return 0;
return i + 1; }
/* tag_length • returns the length of the given tag, or 0 if it's not valid */
static size_t
tag_length(char *data, size_t size, enum mkd_autolink *autolink) {
size_t i, j;
/* a valid tag can't be shorter than 3 chars */
if (size < 3) return 0;
/* begins with a '<' optionally followed by '/', followed by letter */
if (data[0] != '<') return 0;
i = (data[1] == '/') ? 2 : 1;
if ((data[i] < 'a' || data[i] > 'z')
&& (data[i] < 'A' || data[i] > 'Z')) return 0;
/* scheme test */
*autolink = MKDA_NOT_AUTOLINK;
if (size > 6 && strncasecmp(data + 1, "http", 4) == 0 && (data[5] == ':'
|| ((data[5] == 's' || data[5] == 'S') && data[6] == ':'))) {
i = data[5] == ':' ? 6 : 7;
*autolink = MKDA_NORMAL; }
else if (size > 5 && strncasecmp(data + 1, "ftp:", 4) == 0) {
i = 5;
*autolink = MKDA_NORMAL; }
else if (size > 7 && strncasecmp(data + 1, "mailto:", 7) == 0) {
i = 8;
/* not changing *autolink to go to the address test */ }
/* completing autolink test: no whitespace or ' or " */
if (i >= size || i == '>')
*autolink = MKDA_NOT_AUTOLINK;
else if (*autolink) {
j = i;
while (i < size && data[i] != '>' && data[i] != '\''
&& data[i] != '"' && data[i] != ' ' && data[i] != '\t'
&& data[i] != '\n')
i += 1;
if (i >= size) return 0;
if (i > j && data[i] == '>') return i + 1;
/* one of the forbidden chars has been found */
*autolink = MKDA_NOT_AUTOLINK; }
else if ((j = is_mail_autolink(data + i, size - i)) != 0) {
*autolink = (i == 8)
? MKDA_EXPLICIT_EMAIL : MKDA_IMPLICIT_EMAIL;
return i + j; }
/* looking for something like a tag end */
while (i < size && data[i] != '>') i += 1;
if (i >= size) return 0;
return i + 1; }
/* parse_inline • parses inline markdown elements */
static void
parse_inline(struct buf *ob, struct render *rndr, char *data, size_t size) {
size_t i = 0, end = 0;
char_trigger action = 0;
struct buf work = { 0, 0, 0, 0, 0 };
if (rndr->work.size > rndr->make.max_work_stack) {
if (size) bufput(ob, data, size);
return; }
while (i < size) {
/* copying inactive chars into the output */
while (end < size
&& (action = rndr->active_char[(unsigned char)data[end]]) == 0)
end += 1;
if (rndr->make.normal_text) {
work.data = data + i;
work.size = end - i;
rndr->make.normal_text(ob, &work, rndr->make.opaque); }
else
bufput(ob, data + i, end - i);
if (end >= size) break;
i = end;
/* calling the trigger */
end = action(ob, rndr, data + i, i, size - i);
if (!end) /* no action from the callback */
end = i + 1;
else {
i += end;
end = i; } } }
/* find_emph_char • looks for the next emph char, skipping other constructs */
static size_t
find_emph_char(char *data, size_t size, char c) {
size_t i = 1;
while (i < size) {
while (i < size && data[i] != c
&& data[i] != '`' && data[i] != '[')
i += 1;
if (i >= size) return 0;
if (data[i] == c) return i;
/* not counting escaped chars */
if (i && data[i - 1] == '\\') { i += 1; continue; }
/* skipping a code span */
if (data[i] == '`') {
size_t span_nb = 0, bt;
size_t tmp_i = 0;
/* counting the number of opening backticks */
while (i < size && data[i] == '`') {
i += 1;
span_nb += 1; }
if (i >= size) return 0;
/* finding the matching closing sequence */
bt = 0;
while (i < size && bt < span_nb) {
if (!tmp_i && data[i] == c) tmp_i = i;
if (data[i] == '`') bt += 1;
else bt = 0;
i += 1; }
if (i >= size) return tmp_i;
i += 1; }
/* skipping a link */
else if (data[i] == '[') {
size_t tmp_i = 0;
char cc;
i += 1;
while (i < size && data[i] != ']') {
if (!tmp_i && data[i] == c) tmp_i = i;
i += 1; }
i += 1;
while (i < size && (data[i] == ' '
|| data[i] == '\t' || data[i] == '\n'))
i += 1;
if (i >= size) return tmp_i;
if (data[i] != '[' && data[i] != '(') { /* not a link*/
if (tmp_i) return tmp_i;
else continue; }
cc = data[i];
i += 1;
while (i < size && data[i] != cc) {
if (!tmp_i && data[i] == c) tmp_i = i;
i += 1; }
if (i >= size) return tmp_i;
i += 1; } }
return 0; }
/* parse_emph1 • parsing single emphasis */
/* closed by a symbol not preceded by whitespace and not followed by symbol */
static size_t
parse_emph1(struct buf *ob, struct render *rndr,
char *data, size_t size, char c) {
size_t i = 0, len;
struct buf *work = 0;
int r;
if (!rndr->make.emphasis) return 0;
/* skipping one symbol if coming from emph3 */
if (size > 1 && data[0] == c && data[1] == c) i = 1;
while (i < size) {
len = find_emph_char(data + i, size - i, c);
if (!len) return 0;
i += len;
if (i >= size) return 0;
if (i + 1 < size && data[i + 1] == c) {
i += 1;
continue; }
if (data[i] == c && data[i - 1] != ' '
&& data[i - 1] != '\t' && data[i - 1] != '\n') {
work = new_work_buffer(rndr);
parse_inline(work, rndr, data, i);
r = rndr->make.emphasis(ob, work, c, rndr->make.opaque);
release_work_buffer(rndr, work);
return r ? i + 1 : 0; } }
return 0; }
/* parse_emph2 • parsing single emphasis */
static size_t
parse_emph2(struct buf *ob, struct render *rndr,
char *data, size_t size, char c) {
size_t i = 0, len;
struct buf *work = 0;
int r;
if (!rndr->make.double_emphasis) return 0;
while (i < size) {
len = find_emph_char(data + i, size - i, c);
if (!len) return 0;
i += len;
if (i + 1 < size && data[i] == c && data[i + 1] == c
&& i && data[i - 1] != ' '
&& data[i - 1] != '\t' && data[i - 1] != '\n') {
work = new_work_buffer(rndr);
parse_inline(work, rndr, data, i);
r = rndr->make.double_emphasis(ob, work, c,
rndr->make.opaque);
release_work_buffer(rndr, work);
return r ? i + 2 : 0; }
i += 1; }
return 0; }
/* parse_emph3 • parsing single emphasis */
/* finds the first closing tag, and delegates to the other emph */
static size_t
parse_emph3(struct buf *ob, struct render *rndr,
char *data, size_t size, char c) {
size_t i = 0, len;
int r;
while (i < size) {
len = find_emph_char(data + i, size - i, c);
if (!len) return 0;
i += len;
/* skip whitespace preceded symbols */
if (data[i] != c || data[i - 1] == ' '
|| data[i - 1] == '\t' || data[i - 1] == '\n')
continue;
if (i + 2 < size && data[i + 1] == c && data[i + 2] == c
&& rndr->make.triple_emphasis) {
/* triple symbol found */
struct buf *work = new_work_buffer(rndr);
parse_inline(work, rndr, data, i);
r = rndr->make.triple_emphasis(ob, work, c,
rndr->make.opaque);
release_work_buffer(rndr, work);
return r ? i + 3 : 0; }
else if (i + 1 < size && data[i + 1] == c) {
/* double symbol found, handing over to emph1 */
len = parse_emph1(ob, rndr, data - 2, size + 2, c);
if (!len) return 0;
else return len - 2; }
else {
/* single symbol found, handing over to emph2 */
len = parse_emph2(ob, rndr, data - 1, size + 1, c);
if (!len) return 0;
else return len - 1; } }
return 0; }
/* char_emphasis • single and double emphasis parsing */
static size_t
char_emphasis(struct buf *ob, struct render *rndr,
char *data, size_t offset, size_t size) {
char c = data[0];
size_t ret;
if (size > 2 && data[1] != c) {
/* whitespace cannot follow an opening emphasis */
if (data[1] == ' ' || data[1] == '\t' || data[1] == '\n'
|| (ret = parse_emph1(ob, rndr, data + 1, size - 1, c)) == 0)
return 0;
return ret + 1; }
if (size > 3 && data[1] == c && data[2] != c) {
if (data[2] == ' ' || data[2] == '\t' || data[2] == '\n'
|| (ret = parse_emph2(ob, rndr, data + 2, size - 2, c)) == 0)
return 0;
return ret + 2; }
if (size > 4 && data[1] == c && data[2] == c && data[3] != c) {
if (data[3] == ' ' || data[3] == '\t' || data[3] == '\n'
|| (ret = parse_emph3(ob, rndr, data + 3, size - 3, c)) == 0)
return 0;
return ret + 3; }
return 0; }
/* char_linebreak • '\n' preceded by two spaces (assuming linebreak != 0) */
static size_t
char_linebreak(struct buf *ob, struct render *rndr,
char *data, size_t offset, size_t size) {
if (offset < 2 || data[-1] != ' ' || data[-2] != ' ') return 0;
/* removing the last space from ob and rendering */
if (ob->size && ob->data[ob->size - 1] == ' ') ob->size -= 1;
return rndr->make.linebreak(ob, rndr->make.opaque) ? 1 : 0; }
/* char_codespan • '`' parsing a code span (assuming codespan != 0) */
static size_t
char_codespan(struct buf *ob, struct render *rndr,
char *data, size_t offset, size_t size) {
size_t end, nb = 0, i, f_begin, f_end;
/* counting the number of backticks in the delimiter */
while (nb < size && data[nb] == '`') nb += 1;
/* finding the next delimiter */
i = 0;
for (end = nb; end < size && i < nb; end += 1)
if (data[end] == '`') i += 1;
else i = 0;
if (i < nb && end >= size) return 0; /* no matching delimiter */
/* trimming outside whitespaces */
f_begin = nb;
while (f_begin < end && (data[f_begin] == ' ' || data[f_begin] == '\t'))
f_begin += 1;
f_end = end - nb;
while (f_end > nb && (data[f_end-1] == ' ' || data[f_end-1] == '\t'))
f_end -= 1;
/* real code span */
if (f_begin < f_end) {
struct buf work = { data + f_begin, f_end - f_begin, 0, 0, 0 };
if (!rndr->make.codespan(ob, &work, rndr->make.opaque))
end = 0; }
else {
if (!rndr->make.codespan(ob, 0, rndr->make.opaque))
end = 0; }
return end; }
/* char_escape • '\\' backslash escape */
static size_t
char_escape(struct buf *ob, struct render *rndr,
char *data, size_t offset, size_t size) {
struct buf work = { 0, 0, 0, 0, 0 };
if (size > 1) {
if (rndr->make.normal_text) {
work.data = data + 1;
work.size = 1;
rndr->make.normal_text(ob, &work, rndr->make.opaque); }
else bufputc(ob, data[1]); }
return 2; }
/* char_entity • '&' escaped when it doesn't belong to an entity */
/* valid entities are assumed to be anything matching &#?[A-Za-z0-9]+; */
static size_t
char_entity(struct buf *ob, struct render *rndr,
char *data, size_t offset, size_t size) {
size_t end = 1;
struct buf work;
if (end < size && data[end] == '#') end += 1;
while (end < size
&& ((data[end] >= '0' && data[end] <= '9')
|| (data[end] >= 'a' && data[end] <= 'z')
|| (data[end] >= 'A' && data[end] <= 'Z')))
end += 1;
if (end < size && data[end] == ';') {
/* real entity */
end += 1; }
else {
/* lone '&' */
return 0; }
if (rndr->make.entity) {
work.data = data;
work.size = end;
rndr->make.entity(ob, &work, rndr->make.opaque); }
else bufput(ob, data, end);
return end; }
/* char_langle_tag • '<' when tags or autolinks are allowed */
static size_t
char_langle_tag(struct buf *ob, struct render *rndr,
char *data, size_t offset, size_t size) {
enum mkd_autolink altype = MKDA_NOT_AUTOLINK;
size_t end = tag_length(data, size, &altype);
struct buf work = { data, end, 0, 0, 0 };
int ret = 0;
if (end) {
if (rndr->make.autolink && altype != MKDA_NOT_AUTOLINK) {
work.data = data + 1;
work.size = end - 2;
ret = rndr->make.autolink(ob, &work, altype,
rndr->make.opaque); }
else if (rndr->make.raw_html_tag)
ret = rndr->make.raw_html_tag(ob, &work,
rndr->make.opaque); }
if (!ret) return 0;
else return end; }
/* get_link_inline • extract inline-style link and title from parenthesed data*/
static int
get_link_inline(struct buf *link, struct buf *title, char *data, size_t size) {
size_t i = 0, mark;
size_t link_b, link_e;
size_t title_b = 0, title_e = 0;
link->size = title->size = 0;
/* skipping initial whitespace */
while (i < size
&& (data[i] == ' ' || data[i] == '\t' || data[i] == '\n'))
i += 1;
link_b = i;
/* looking for link end: ' " */
while (i < size && data[i] != '\'' && data[i] != '"')
i += 1;
link_e = i;
/* looking for title end if present */
if (data[i] == '\'' || data[i] == '"') {
i += 1;
title_b = i;
/* skipping whitespaces after title */
title_e = size - 1;
while (title_e > title_b && (data[title_e] == ' '
|| data[title_e] == '\t' || data[title_e] == '\n'))
title_e -= 1;
/* checking for closing quote presence */
if (data[title_e] != '\'' && data[title_e] != '"') {
title_b = title_e = 0;
link_e = i; } }
/* remove whitespace at the end of the link */
while (link_e > link_b
&& (data[link_e - 1] == ' ' || data[link_e - 1] == '\t'
|| data[link_e - 1] == '\n'))
link_e -= 1;
/* remove optional angle brackets around the link */
if (data[link_b] == '<') link_b += 1;
if (data[link_e - 1] == '>') link_e -= 1;
/* escape backslashed character from link */
link->size = 0;
i = link_b;
while (i < link_e) {
mark = i;
while (i < link_e && data[i] != '\\') i += 1;
bufput(link, data + mark, i - mark);
while (i < link_e && data[i] == '\\') i += 1; }
/* handing back title */
title->size = 0;
if (title_e > title_b)
bufput(title, data + title_b, title_e - title_b);
/* this function always succeed */
return 0; }
/* get_link_ref • extract referenced link and title from id */
static int
get_link_ref(struct render *rndr, struct buf *link, struct buf *title,
char * data, size_t size) {
struct link_ref *lr;
/* find the link from its id (stored temporarily in link) */
link->size = 0;
if (build_ref_id(link, data, size) < 0)
return -1;
lr = arr_sorted_find(&rndr->refs, link, cmp_link_ref);
if (!lr) return -1;
/* fill the output buffers */
link->size = 0;
if (lr->link)
bufput(link, lr->link->data, lr->link->size);
title->size = 0;
if (lr->title)
bufput(title, lr->title->data, lr->title->size);
return 0; }
/* char_link • '[': parsing a link or an image */
static size_t
char_link(struct buf *ob, struct render *rndr,
char *data, size_t offset, size_t size) {
int is_img = (offset && data[-1] == '!'), level;
size_t i = 1, txt_e;
struct buf *content = 0;
struct buf *link = 0;
struct buf *title = 0;
int ret;
/* checking whether the correct renderer exists */
if ((is_img && !rndr->make.image) || (!is_img && !rndr->make.link))
return 0;
/* looking for the matching closing bracket */
for (level = 1; i < size; i += 1)
if (data[i - 1] == '\\') continue;
else if (data[i] == '[') level += 1;
else if (data[i] == ']') {
level -= 1;
if (level <= 0) break; }
if (i >= size) return 0;
txt_e = i;
i += 1;
/* skip any amount of whitespace or newline */
/* (this is much more laxist than original markdown syntax) */
while (i < size
&& (data[i] == ' ' || data[i] == '\t' || data[i] == '\n'))
i += 1;
/* allocate temporary buffers to store content, link and title */
content = new_work_buffer(rndr);
link = new_work_buffer(rndr);
title = new_work_buffer(rndr);
ret = 0; /* error if we don't get to the callback */
/* inline style link */
if (i < size && data[i] == '(') {
size_t span_end = i;
while (span_end < size
&& !(data[span_end] == ')'
&& (span_end == i || data[span_end - 1] != '\\')))
span_end += 1;
if (span_end >= size
|| get_link_inline(link, title,
data + i+1, span_end - (i+1)) < 0)
goto char_link_cleanup;
i = span_end + 1; }
/* reference style link */
else if (i < size && data[i] == '[') {
char *id_data;
size_t id_size, id_end = i;
while (id_end < size && data[id_end] != ']')
id_end += 1;
if (id_end >= size)
goto char_link_cleanup;
if (i + 1 == id_end) {
/* implicit id - use the contents */
id_data = data + 1;
id_size = txt_e - 1; }
else {
/* explicit id - between brackets */
id_data = data + i + 1;
id_size = id_end - (i + 1); }
if (get_link_ref(rndr, link, title, id_data, id_size) < 0)
goto char_link_cleanup;
i = id_end + 1; }
/* shortcut reference style link */
else {
if (get_link_ref(rndr, link, title, data + 1, txt_e - 1) < 0)
goto char_link_cleanup;
/* rewinding the whitespace */
i = txt_e + 1; }
/* building content: img alt is escaped, link content is parsed */
if (txt_e > 1) {
if (is_img) bufput(content, data + 1, txt_e - 1);
else parse_inline(content, rndr, data + 1, txt_e - 1); }
/* calling the relevant rendering function */
if (is_img) {
if (ob->size && ob->data[ob->size - 1] == '!') ob->size -= 1;
ret = rndr->make.image(ob, link, title, content,
rndr->make.opaque); }
else ret = rndr->make.link(ob, link, title, content, rndr->make.opaque);
/* cleanup */
char_link_cleanup:
release_work_buffer(rndr, title);
release_work_buffer(rndr, link);
release_work_buffer(rndr, content);
return ret ? i : 0; }
/*********************************
* BLOCK-LEVEL PARSING FUNCTIONS *
*********************************/
/* is_empty • returns the line length when it is empty, 0 otherwise */
static size_t
is_empty(char *data, size_t size) {
size_t i;
for (i = 0; i < size && data[i] != '\n'; i += 1)
if (data[i] != ' ' && data[i] != '\t') return 0;
return i + 1; }
/* is_hrule • returns whether a line is a horizontal rule */
static int
is_hrule(char *data, size_t size) {
size_t i = 0, n = 0;
char c;
/* skipping initial spaces */
if (size < 3) return 0;
if (data[0] == ' ') { i += 1;
if (data[1] == ' ') { i += 1;
if (data[2] == ' ') { i += 1; } } }
/* looking at the hrule char */
if (i + 2 >= size
|| (data[i] != '*' && data[i] != '-' && data[i] != '_'))
return 0;
c = data[i];
/* the whole line must be the char or whitespace */
while (i < size && data[i] != '\n') {
if (data[i] == c) n += 1;
else if (data[i] != ' ' && data[i] != '\t')
return 0;
i += 1; }
return n >= 3; }
/* is_headerline • returns whether the line is a setext-style hdr underline */
static int
is_headerline(char *data, size_t size) {
size_t i = 0;
/* test of level 1 header */
if (data[i] == '=') {
for (i = 1; i < size && data[i] == '='; i += 1);
while (i < size && (data[i] == ' ' || data[i] == '\t')) i += 1;
return (i >= size || data[i] == '\n') ? 1 : 0; }
/* test of level 2 header */
if (data[i] == '-') {
for (i = 1; i < size && data[i] == '-'; i += 1);
while (i < size && (data[i] == ' ' || data[i] == '\t')) i += 1;
return (i >= size || data[i] == '\n') ? 2 : 0; }
return 0; }
/* is_table_sep • returns whether there is a table separator at the given pos */
static int
is_table_sep(char *data, size_t pos) {
return data[pos] == '|' && (pos == 0 || data[pos - 1] != '\\'); }
/* is_tableline • returns the number of column tables in the given line */
static int
is_tableline(char *data, size_t size) {
size_t i = 0;
int n_sep = 0, outer_sep = 0;
/* skip initial blanks */
while (i < size && (data[i] == ' ' || data[i] == '\t'))
i += 1;
/* check for initial '|' */
if (i < size && data[i] == '|')
outer_sep += 1;
/* count the number of pipes in the line */
for (n_sep = 0; i < size && data[i] != '\n'; i += 1)
if (is_table_sep(data, i))
n_sep += 1;
/* march back to check for optional last '|' before blanks and EOL */
while (i
&& (data[i - 1] == ' ' || data[i - 1] == '\t' || data[i - 1] == '\n'))
i -= 1;
if (i && is_table_sep(data, i - 1))
outer_sep += 1;
/* return the number of column or 0 if it's not a table line */
return (n_sep > 0) ? (n_sep - outer_sep + 1) : 0; }
/* prefix_quote • returns blockquote prefix length */
static size_t
prefix_quote(char *data, size_t size) {
size_t i = 0;
if (i < size && data[i] == ' ') i += 1;
if (i < size && data[i] == ' ') i += 1;
if (i < size && data[i] == ' ') i += 1;
if (i < size && data[i] == '>') {
if (i + 1 < size && (data[i + 1] == ' ' || data[i+1] == '\t'))
return i + 2;
else return i + 1; }
else return 0; }
/* prefix_code • returns prefix length for block code */
static size_t
prefix_code(char *data, size_t size) {
if (size > 0 && data[0] == '\t') return 1;
if (size > 3 && data[0] == ' ' && data[1] == ' '
&& data[2] == ' ' && data[3] == ' ') return 4;
return 0; }
/* prefix_oli • returns ordered list item prefix */
static size_t
prefix_oli(char *data, size_t size) {
size_t i = 0;
if (i < size && data[i] == ' ') i += 1;
if (i < size && data[i] == ' ') i += 1;
if (i < size && data[i] == ' ') i += 1;
if (i >= size || data[i] < '0' || data[i] > '9') return 0;
while (i < size && data[i] >= '0' && data[i] <= '9') i += 1;
if (i + 1 >= size || data[i] != '.'
|| (data[i + 1] != ' ' && data[i + 1] != '\t')) return 0;
i = i + 2;
while (i < size && (data[i] == ' ' || data[i] == '\t')) i += 1;
return i; }
/* prefix_uli • returns ordered list item prefix */
static size_t
prefix_uli(char *data, size_t size) {
size_t i = 0;
if (i < size && data[i] == ' ') i += 1;
if (i < size && data[i] == ' ') i += 1;
if (i < size && data[i] == ' ') i += 1;
if (i + 1 >= size
|| (data[i] != '*' && data[i] != '+' && data[i] != '-')
|| (data[i + 1] != ' ' && data[i + 1] != '\t'))
return 0;
i = i + 2;
while (i < size && (data[i] == ' ' || data[i] == '\t')) i += 1;
return i; }
/* parse_block • parsing of one block, returning next char to parse */
static void parse_block(struct buf *ob, struct render *rndr,
char *data, size_t size);
/* parse_blockquote • handles parsing of a blockquote fragment */
static size_t
parse_blockquote(struct buf *ob, struct render *rndr,
char *data, size_t size) {
size_t beg, end = 0, pre, work_size = 0;
char *work_data = 0;
struct buf *out = new_work_buffer(rndr);
beg = 0;
while (beg < size) {
for (end = beg + 1; end < size && data[end - 1] != '\n';
end += 1);
pre = prefix_quote(data + beg, end - beg);
if (pre) beg += pre; /* skipping prefix */
else if (is_empty(data + beg, end - beg)
&& (end >= size || (prefix_quote(data + end, size - end) == 0
&& !is_empty(data + end, size - end))))
/* empty line followed by non-quote line */
break;
if (beg < end) { /* copy into the in-place working buffer */
/* bufput(work, data + beg, end - beg); */
if (!work_data)
work_data = data + beg;
else if (data + beg != work_data + work_size)
memmove(work_data + work_size, data + beg,
end - beg);
work_size += end - beg; }
beg = end; }
parse_block(out, rndr, work_data, work_size);
if (rndr->make.blockquote)
rndr->make.blockquote(ob, out, rndr->make.opaque);
release_work_buffer(rndr, out);
return end; }
/* parse_paragraph • handles parsing of a regular paragraph */
static size_t
parse_paragraph(struct buf *ob, struct render *rndr,
char *data, size_t size) {