-
Notifications
You must be signed in to change notification settings - Fork 35
/
document.c
5301 lines (4435 loc) · 114 KB
/
document.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
/*
* Copyright (c) 2008, Natacha Porté
* Copyright (c) 2011, Vicent Martí
* Copyright (c) 2014, Xavier Mendez, Devin Torres and the Hoedown authors
* Copyright (c) Kristaps Dzonsons <[email protected]>
*
* 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 "config.h"
#if HAVE_SYS_QUEUE
# include <sys/queue.h>
#endif
#include <assert.h>
#include <ctype.h>
#include <stdint.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lowdown.h"
#include "extern.h"
/*
* Make sure these are larger than enum hlist_fl.
*/
#define HLIST_LI_END (1 << 7) /* End of list item. */
/*
* Mask of all list item types.
*/
#define HLIST_FL_MASK (HLIST_FL_DEF | \
HLIST_FL_ORDERED | \
HLIST_FL_UNORDERED)
/*
* Reference to a link.
*/
struct link_ref {
struct lowdown_buf *name; /* id of link (or NULL) */
struct lowdown_buf *link; /* link address */
struct lowdown_buf *title; /* optional title */
struct lowdown_buf *attrs; /* optional attributes */
TAILQ_ENTRY(link_ref) entries;
};
TAILQ_HEAD(link_refq, link_ref);
/*
* Reference to a footnote. This keeps track of all footnotes
* definitions and whether there's both a definition and reference.
*/
struct foot_ref {
size_t num; /* if used, the order */
struct lowdown_node *ref; /* if used, the reference */
struct lowdown_buf name; /* identifier */
struct lowdown_buf contents; /* definition */
TAILQ_ENTRY(foot_ref) entries;
};
TAILQ_HEAD(foot_refq, foot_ref);
struct lowdown_doc {
struct link_refq refq; /* all internal references */
struct foot_refq footq; /* all footnotes */
size_t foots; /* # of used footnotes */
int active_char[256]; /* jump table */
unsigned int ext_flags; /* options */
int in_link_body; /* parsing link body */
int in_footnote; /* prevent nested */
size_t nodes; /* number of nodes */
struct lowdown_node *current; /* current node */
struct lowdown_metaq *metaq; /* raw metadata key/values */
size_t depth; /* current parse tree depth */
size_t maxdepth; /* max parse tree depth */
char **meta; /* primer metadata */
size_t metasz; /* size of meta */
char **metaovr; /* override metadata */
size_t metaovrsz; /* size of metaovr */
};
/*
* Function pointer to render active chars, where "data" is the pointer
* of the beginning of the span and "offset" is the number of valid
* chars before data.
* Returns the number of chars taken care of, or <0 on failure.
*/
typedef ssize_t (*char_trigger)(struct lowdown_doc *, char *, size_t, size_t);
static ssize_t char_emphasis(struct lowdown_doc *, char *, size_t, size_t);
static ssize_t char_linebreak(struct lowdown_doc *, char *, size_t, size_t);
static ssize_t char_codespan(struct lowdown_doc *, char *, size_t, size_t);
static ssize_t char_escape(struct lowdown_doc *, char *, size_t, size_t);
static ssize_t char_entity(struct lowdown_doc *, char *, size_t, size_t);
static ssize_t char_langle_tag(struct lowdown_doc *, char *, size_t, size_t);
static ssize_t char_autolink_url(struct lowdown_doc *, char *, size_t, size_t);
static ssize_t char_autolink_email(struct lowdown_doc *, char *, size_t, size_t);
static ssize_t char_autolink_www(struct lowdown_doc *, char *, size_t, size_t);
static ssize_t char_link(struct lowdown_doc *, char *, size_t, size_t);
static ssize_t char_image(struct lowdown_doc *, char *, size_t, size_t);
static ssize_t char_superscript(struct lowdown_doc *, char *, size_t, size_t);
static ssize_t char_math(struct lowdown_doc *, char *, size_t, size_t);
static ssize_t char_subscript(struct lowdown_doc *, char *, size_t, size_t);
enum markdown_char_t {
MD_CHAR_NONE = 0,
MD_CHAR_EMPHASIS,
MD_CHAR_CODESPAN,
MD_CHAR_LINEBREAK,
MD_CHAR_LINK,
MD_CHAR_IMAGE,
MD_CHAR_LANGLE,
MD_CHAR_ESCAPE,
MD_CHAR_ENTITY,
MD_CHAR_AUTOLINK_URL,
MD_CHAR_AUTOLINK_EMAIL,
MD_CHAR_AUTOLINK_WWW,
MD_CHAR_SUPERSCRIPT,
MD_CHAR_SUBSCRIPT,
MD_CHAR_QUOTE,
MD_CHAR_MATH
};
static const char_trigger markdown_char_ptrs[] = {
NULL,
&char_emphasis,
&char_codespan,
&char_linebreak,
&char_link,
&char_image,
&char_langle_tag,
&char_escape,
&char_entity,
&char_autolink_url,
&char_autolink_email,
&char_autolink_www,
&char_superscript,
&char_subscript,
NULL,
&char_math
};
static int
parse_block(struct lowdown_doc *, char *, size_t);
static ssize_t
parse_listitem(struct lowdown_buf *, struct lowdown_doc *,
char *, size_t, enum hlist_fl *, size_t);
/*
* Add a node to the parse stack or retrieve a current node if
* requesting multiple similar LOWDOWN_NORMAL_TEXT in sequence. Returns
* the node, initialised to the given type, after adjusting the parse
* position. Returns NULL on memory allocation failure.
*/
static struct lowdown_node *
pushnode_full(struct lowdown_doc *doc, enum lowdown_rndrt t, int fl)
{
struct lowdown_node *n;
/*
* Special case: if we're pushing a NORMAL_TEXT node, see if one
* already exists with the same flags and return that. This
* means that each push for text nodes should be careful to use
* hbuf_push() instead of hbuf_create() when adding text
* content.
*/
if (t == LOWDOWN_NORMAL_TEXT && doc->current != NULL) {
n = TAILQ_LAST(&doc->current->children, lowdown_nodeq);
if (n != NULL &&
n->type == LOWDOWN_NORMAL_TEXT &&
n->rndr_normal_text.flags == fl) {
doc->depth++;
doc->current = n;
return n;
}
}
/* New node. */
if ((doc->depth++ > doc->maxdepth) && doc->maxdepth)
return NULL;
if ((n = calloc(1, sizeof(struct lowdown_node))) == NULL)
return NULL;
n->id = doc->nodes++;
n->type = t;
n->parent = doc->current;
TAILQ_INIT(&n->children);
if (n->parent != NULL)
TAILQ_INSERT_TAIL(&n->parent->children, n, entries);
doc->current = n;
return n;
}
/*
* Push a new node or, if LOWDOWN_NORMAL_TEXT, retrieve the existing one
* if the flags exactly match.
*/
static struct lowdown_node *
pushnode(struct lowdown_doc *doc, enum lowdown_rndrt t)
{
return pushnode_full(doc, t, 0);
}
/*
* Push a new LOWDOWN_NORMAL_TEXT or retrieve the existing one if the
* flags exactly match.
*/
static struct lowdown_node *
pushtext(struct lowdown_doc *doc, int flags)
{
return pushnode_full(doc, LOWDOWN_NORMAL_TEXT, flags);
}
/*
* Sets a buffer with the contents of "data" of size "datasz". The
* buffer must be empty. Return FALSE on failure, TRUE on success.
*/
static int
hbuf_create(struct lowdown_buf *buf, const char *data, size_t datasz)
{
assert(buf->size == 0);
assert(buf->data == NULL);
memset(buf, 0, sizeof(struct lowdown_buf));
if (datasz) {
if ((buf->data = malloc(datasz)) == NULL)
return 0;
buf->unit = 1;
buf->size = buf->maxsize = datasz;
memcpy(buf->data, data, datasz);
}
return 1;
}
/*
* See hbuf_create().
*/
static int
hbuf_createb(struct lowdown_buf *buf, const struct lowdown_buf *nbuf)
{
return hbuf_create(buf, nbuf->data, nbuf->size);
}
/*
* Pushes data into the buffer, which is initialised if empty. Return
* FALSE on failure, TRUE on success.
*/
static int
hbuf_push(struct lowdown_buf *buf, const char *data, size_t datasz)
{
if (buf->size == 0 || buf->data == NULL)
return hbuf_create(buf, data, datasz);
return hbuf_put(buf, data, datasz);
}
/*
* See pushnode().
* Pops the current node on the stack, replacing it with the parent.
*/
static void
popnode(struct lowdown_doc *doc, const struct lowdown_node *n)
{
assert(doc->depth > 0);
doc->depth--;
assert(doc->current == n);
doc->current = doc->current->parent;
}
/*
* Remove the backslash from a text sequence.
* Return zero on failure (memory), non-zero on success.
*/
static int
unscape_text(struct lowdown_buf *ob, struct lowdown_buf *src)
{
size_t i, org;
for (i = 0; i < src->size; i += 2) {
org = i;
while (i < src->size && src->data[i] != '\\')
i++;
if (i > org &&
!hbuf_put(ob, src->data + org, i - org))
return 0;
if (i + 1 >= src->size)
break;
if (!hbuf_putc(ob, src->data[i + 1]))
return 0;
}
return 1;
}
static struct link_ref *
find_link_ref(struct link_refq *q, char *name, size_t length)
{
struct link_ref *ref;
TAILQ_FOREACH(ref, q, entries)
if ((ref->name == NULL && length == 0) ||
(ref->name != NULL &&
ref->name->size == length &&
memcmp(ref->name->data, name, length) == 0))
return ref;
return NULL;
}
static void
free_link_refs(struct link_refq *q)
{
struct link_ref *r;
while ((r = TAILQ_FIRST(q)) != NULL) {
TAILQ_REMOVE(q, r, entries);
hbuf_free(r->link);
hbuf_free(r->name);
hbuf_free(r->title);
hbuf_free(r->attrs);
free(r);
}
}
static void
free_foot_refq(struct foot_refq *q)
{
struct foot_ref *ref;
while ((ref = TAILQ_FIRST(q)) != NULL) {
TAILQ_REMOVE(q, ref, entries);
hbuf_free(&ref->contents);
hbuf_free(&ref->name);
free(ref);
}
}
/*
* Check whether a char is a Markdown spacing char.
* Right now we only consider spaces the actual space and a newline:
* tabs and carriage returns are filtered out during the preprocessing
* phase.
* If we wanted to actually be UTF-8 compliant, we should instead
* extract an Unicode codepoint from this character and check for space
* properties.
*/
static int
xisspace(int c)
{
return c == ' ' || c == '\n';
}
/*
* Returns the number of leading spaces from data starting from offset
* to size.
* If maxlen is greater than zero, only at most maxlen number of leading
* spaces will be counted.
* Otherwise, all leading spaces will be counted.
*/
static size_t
countspaces(const char *data, size_t offset, size_t size, size_t maxlen)
{
size_t i;
for (i = offset; i < size; i++) {
if (maxlen > 0 && i - offset == maxlen)
break;
if (data[i] != ' ')
break;
}
return i;
}
/*
* Replace all spacing characters in data with spaces.
* As a special case, this collapses a newline with the previous space,
* if possible.
* Return zero on failure (memory), non-zero on success.
*/
static int
replace_spacing(struct lowdown_buf *ob, const char *data, size_t size)
{
size_t i, mark;
if (!hbuf_grow(ob, size))
return 0;
for (i = 0; ; i++) {
mark = i;
while (i < size && data[i] != '\n')
i++;
if (!hbuf_put(ob, data + mark, i - mark))
return 0;
if (i >= size)
break;
if (!(i > 0 && data[i - 1] == ' '))
if (!hbuf_putc(ob, ' '))
return 0;
}
return 1;
}
/*
* 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(const char *data, size_t size)
{
size_t i, nb = 0;
/* Assumed to be: [-@._a-zA-Z0-9]+ with exactly one '@'. */
for (i = 0; i < size; ++i) {
if (isalnum((unsigned char)data[i]))
continue;
switch (data[i]) {
case '@':
nb++;
case '-':
case '.':
case '_':
break;
case '>':
return (nb == 1) ? i + 1 : 0;
default:
return 0;
}
}
return 0;
}
/*
* Returns the length of the given tag, or 0 is it's not valid.
*/
static size_t
tag_length(const char *data, size_t size, enum halink_type *ltype)
{
size_t i, j;
/* A valid tag can't be shorter than 3 chars. */
if (size < 3)
return 0;
if (data[0] != '<')
return 0;
/* HTML comment, laxist form. */
if (size > 5 && data[1] == '!' &&
data[2] == '-' && data[3] == '-') {
i = 5;
while (i < size && !(data[i - 2] == '-' &&
data[i - 1] == '-' && data[i] == '>'))
i++;
i++;
if (i <= size)
return i;
}
/*
* Begins with a '<' optionally followed by '/', followed by letter or
* number.
*/
i = (data[1] == '/') ? 2 : 1;
if (!isalnum((unsigned char)data[i]))
return 0;
/* Scheme test. */
*ltype = HALINK_NONE;
/* Try to find the beginning of an URI. */
while (i < size && (isalnum((unsigned char)data[i]) ||
data[i] == '.' || data[i] == '+' || data[i] == '-'))
i++;
if (i > 1 && data[i] == '@')
if ((j = is_mail_autolink(data + i, size - i)) != 0) {
*ltype = HALINK_EMAIL;
return i + j;
}
if (i > 2 && data[i] == ':') {
*ltype = HALINK_NORMAL;
i++;
}
/* Completing autolink test: no spacing or ' or ". */
if (i >= size)
*ltype = HALINK_NONE;
else if (*ltype) {
j = i;
while (i < size) {
if (data[i] == '\\')
i += 2;
else if (data[i] == '>' || data[i] == '\'' ||
data[i] == '"' || data[i] == ' ' ||
data[i] == '\n')
break;
else
i++;
}
if (i >= size)
return 0;
if (i > j && data[i] == '>')
return i + 1;
/* One of the forbidden chars has been found. */
*ltype = HALINK_NONE;
}
/* Looking for something looking like a tag end. */
while (i < size && data[i] != '>')
i++;
if (i >= size)
return 0;
return i + 1;
}
/*
* Parses inline markdown elements.
* This function is important because it handles raw input that we pass
* directly to the output formatter ("normal_text").
* Return zero on failure, non-zero on success.
*/
static int
parse_inline(struct lowdown_doc *doc, char *data, size_t size)
{
size_t i = 0, end = 0, consumed = 0;
ssize_t rc;
struct lowdown_buf work;
const int *active_char = doc->active_char;
struct lowdown_node *n;
memset(&work, 0, sizeof(struct lowdown_buf));
while (i < size) {
/* Copying non-macro chars into the output. */
while (end < size &&
active_char[(unsigned char)data[end]] == 0)
end++;
/* Only allocate if non-empty... */
if (end - i > 0) {
n = pushnode(doc, LOWDOWN_NORMAL_TEXT);
if (n == NULL)
return 0;
if (!hbuf_push(&n->rndr_normal_text.text,
data + i, end - i))
return 0;
popnode(doc, n);
}
/* End of file? */
if (end >= size)
break;
i = end;
rc = markdown_char_ptrs[
active_char[(unsigned char)data[end]]]
(doc, data + i, i - consumed, size - i);
if (rc < 0)
return 0;
end = rc;
/* Check if no action from the callback. */
if (end == 0) {
end = i + 1;
continue;
}
i += end;
end = consumed = i;
}
return 1;
}
/*
* Returns whether special char at data[loc] is escaped by '\\'.
*/
static int
is_escaped(const char *data, size_t loc)
{
size_t i = loc;
while (i >= 1 && data[i - 1] == '\\')
i--;
/* Odd numbers of backslashes escapes data[loc]. */
return (loc - i) % 2;
}
/*
* Test if the buffer "data" of size "sz" might be a pandoc metadata
* block. This consists of at most three lines starting with percent
* marks. It can be more than three lines if lines start with
* whitespace (continuations). Returns zero if not a metadata block,
* otherwise the block length if it may be.
*/
static size_t
is_metadata_block_pandoc(const char *data, size_t sz)
{
size_t i = 0, j;
if (sz == 0 || data[0] != '%')
return 0;
for (i = j = 0; i < sz && j < 3; j++, i++) {
if (data[i] != '%')
break;
for ( ; i < sz; i++)
if (data[i] == '\n' &&
(i + 1 >= sz || data[i + 1] != ' '))
break;
}
/* Run past trailing newlines. */
while (i < sz && data[i] == '\n')
i++;
return i;
}
/*
* Test if the buffer "data" of size "sz" might be a MMD metadata block.
* This only sees if the first line starts with an alnum and contains a
* colon, and that the block ends in two newlines. Alternatively, the
* first line is "---" and last is "---" or "....", which is the YAML
* syntax ("is_yaml" will be set).
* Returns zero if not a metadata block, otherwise the block length if
* it may be.
*/
static size_t
is_metadata_block_mmd(const char *data, size_t sz, int *is_yaml)
{
size_t i = 0;
const char *cp;
if (sz == 0)
return 0;
if (sz > 4 && strncmp(data, "---\n", 4) == 0) {
*is_yaml = 1;
i += 4;
}
if (!isalnum((unsigned char)data[i]))
return 0;
for ( ; i < sz; i++)
if (data[i] == '\n' || data[i] == ':')
break;
if (i == sz || data[i] != ':')
return 0;
/*
* Look for trailing paragraph (if not YAML) or the YAML markers
* otherwise.
*/
if (*is_yaml && sz - i > 5) {
cp = memmem(&data[i], sz - i, "\n---\n", 5);
if (cp == NULL)
cp = memmem(&data[i], sz - i, "\n...\n", 5);
if (cp != NULL)
return (ptrdiff_t)(cp - data) + 5;
} else if (!*is_yaml && sz - i > 2) {
cp = memmem(&data[i], sz - i, "\n\n", 2);
if (cp != NULL)
return (ptrdiff_t)(cp - data) + 2;
}
return 0;
}
/*
* Looks for the next emph char, skipping other constructs.
*/
static size_t
find_emph_char(const char *data, size_t size, char c)
{
size_t i = 0, span_nb, bt, tmp_i;
char cc;
while (i < size) {
while (i < size && data[i] != c &&
data[i] != '[' && data[i] != '`')
i++;
if (i == size)
return 0;
/* Not counting escaped chars. */
if (is_escaped(data, i)) {
i++;
continue;
}
if (data[i] == c)
return i;
/* Skipping a codespan. */
if (data[i] == '`') {
span_nb = 0;
tmp_i = 0;
/* Counting the number of opening backticks. */
while (i < size && data[i] == '`') {
i++;
span_nb++;
}
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++;
else
bt = 0;
i++;
}
/*
* Not a well-formed codespan; use found
* matching emph char.
*/
if (bt < span_nb && i >= size)
return tmp_i;
} else if (data[i] == '[') {
tmp_i = 0;
/* Skipping a link. */
/*
* XXX: it's trivially possible to allow for
* nested links by maintaining a stack depth
* here on the opening and closing bracket.
* However, no other Markdowns appear to support
* this syntax, so don't do so.
*/
i++;
while (i < size && data[i] != ']') {
if (!tmp_i && data[i] == c)
tmp_i = i;
i++;
}
i++;
while (i < size && xisspace(data[i]))
i++;
if (i >= size)
return tmp_i;
switch (data[i]) {
case '[':
cc = ']';
break;
case '(':
cc = ')';
break;
default:
if (tmp_i)
return tmp_i;
else
continue;
}
i++;
while (i < size && data[i] != cc) {
if (!tmp_i && data[i] == c)
tmp_i = i;
i++;
}
if (i >= size)
return tmp_i;
i++;
}
}
return 0;
}
/*
* Parsing single emphase.
* Closed by a symbol not preceded by spacing and not followed by
* symbol.
* Return 0 if not an emphasis, <0 on failure, >0 on success.
*/
static ssize_t
parse_emph1(struct lowdown_doc *doc, char *data, size_t size, char c)
{
size_t i = 0, len;
struct lowdown_node *n;
/* 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 (data[i] == c && !xisspace(data[i - 1])) {
if ((doc->ext_flags & LOWDOWN_NOINTEM) &&
i + 1 < size &&
isalnum((unsigned char)data[i + 1]))
continue;
n = pushnode(doc, LOWDOWN_EMPHASIS);
if (n == NULL)
return -1;
if (!parse_inline(doc, data, i))
return -1;
popnode(doc, n);
return i + 1;
}
}
return 0;
}
/*
* Parsing single emphase.
* Return 0 if not an emphasis, <0 on failure, >0 on success.
*/
static ssize_t
parse_emph2(struct lowdown_doc *doc, char *data, size_t size, char c)
{
size_t i = 0, len;
struct lowdown_node *n;
enum lowdown_rndrt t;
while (i < size) {
len = find_emph_char(data + i, size - i, c);
if (len == 0)
return 0;
i += len;
if (i + 1 < size && data[i] == c &&
data[i + 1] == c && i &&
!xisspace(data[i - 1])) {
if (c == '~')
t = LOWDOWN_STRIKETHROUGH;
else if (c == '=')
t = LOWDOWN_HIGHLIGHT;
else
t = LOWDOWN_DOUBLE_EMPHASIS;
if ((n = pushnode(doc, t)) == NULL)
return -1;
if (!parse_inline(doc, data, i))
return -1;
popnode(doc, n);
return i + 2;
}
i++;
}
return 0;
}
/*
* Parsing single emphase
* Finds the first closing tag, and delegates to the other emph.
* Return 0 if not an emphasis, <0 on failure, >0 on success.
*/
static size_t
parse_emph3(struct lowdown_doc *doc, char *data, size_t size, char c)
{
size_t i = 0, len;
ssize_t rc;
struct lowdown_node *n;
while (i < size) {
len = find_emph_char(data + i, size - i, c);
if (len == 0)
return 0;
i += len;
/* Skip spacing preceded symbols. */
if (data[i] != c || xisspace(data[i - 1]))
continue;
/* Case for triple, double, and single asterisk. */
if (i + 2 < size && data[i + 1] == c &&
data[i + 2] == c) {
n = pushnode(doc, LOWDOWN_TRIPLE_EMPHASIS);
if (n == NULL)
return -1;
if (!parse_inline(doc, data, i))
return -1;
popnode(doc, n);
return i + 3;
} else if (i + 1 < size && data[i + 1] == c) {
rc = parse_emph1(doc, data - 2, size + 2, c);
if (rc < 0)
return -1;
assert(rc == 0 || rc >= 2);
return rc == 0 ? 0 : rc - 2;
} else {
rc = parse_emph2(doc, data - 1, size + 1, c);
if (rc < 0)
return -1;
return rc == 0 ? 0 : rc - 1;
}
}
return 0;
}
/*
* Parses a math span until the given ending delimiter.
* Return 0 if not math, <0 on failure, >0 on success.
*/
static ssize_t
parse_math(struct lowdown_doc *doc, char *data, size_t offset,
size_t size, const char *end, size_t delimsz, int blockmode)
{
size_t i;
struct lowdown_node *n;
/*
* Find ending delimiter.
* All text within the equation is opaque, so we don't need to
* care about embedded macros.
*/
for (i = delimsz; ; i++) {
while (i < size && data[i] != end[0])
i++;
if (i >= size)
return 0;
if (!is_escaped(data, i) && !(i + delimsz > size) &&
memcmp(data + i, end, delimsz) == 0)
break;
}
i += delimsz;