forked from kristapsdz/lowdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
document.c
4094 lines (3325 loc) · 84.5 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
/* $Id$ */
/*
* Copyright (c) 2008, Natacha Porté
* Copyright (c) 2011, Vicent Martí
* Copyright (c) 2014, Xavier Mendez, Devin Torres and the Hoedown authors
* Copyright (c) 2016--2017, 2020 Kristaps Dzonsons <kristaps@bsd.lv>
*
* 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>
#if HAVE_ERR
# include <err.h>
#endif
#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 << 4) /* End of list item. */
/*
* Mask of all list item types.
*/
#define HLIST_FL_MASK (HLIST_FL_DEF | \
HLIST_FL_ORDERED | \
HLIST_FL_UNORDERED)
/*
* Used to hold metadata keys and values.
* This is for filling in the metadata value with references.
*/
struct hbufn {
const struct lowdown_buf *key; /* key of the value */
const struct lowdown_buf *val; /* value (or NULL for no value) */
TAILQ_ENTRY(hbufn) entries;
};
TAILQ_HEAD(hbufq, hbufn);
/*
* Reference to a link.
*/
struct link_ref {
struct lowdown_buf *name; /* identifier of link (or NULL) */
struct lowdown_buf *link; /* link address */
struct lowdown_buf *title; /* optional title */
TAILQ_ENTRY(link_ref) entries;
};
TAILQ_HEAD(link_refq, link_ref);
/*
* Feference to a footnote.
*/
struct footnote_ref {
int is_used; /* whether has been referenced */
size_t num; /* if referenced, the order */
struct lowdown_buf *name; /* identifier (or NULL) */
struct lowdown_buf *contents; /* contents of footnote */
TAILQ_ENTRY(footnote_ref) entries;
};
TAILQ_HEAD(footnote_refq, footnote_ref);
struct lowdown_doc {
const struct lowdown_opts *opts;
struct link_refq refq; /* all internal references */
struct footnote_refq footnotes; /* all footnotes */
size_t footnotesz; /* # of used footnotes */
int active_char[256]; /* jump table */
unsigned int ext_flags; /* options */
size_t cur_par; /* XXX: not used */
int in_link_body; /* parsing link body */
size_t nodes; /* number of nodes */
struct lowdown_node *current; /* current node */
struct hbufq metaq; /* raw metadata key/values */
size_t depth; /* current parse tree depth */
size_t maxdepth; /* max parse tree depth */
};
/*
* 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.
*/
typedef size_t (*char_trigger)(struct lowdown_doc *, char *, size_t, size_t);
static size_t char_emphasis(struct lowdown_doc *, char *, size_t, size_t);
static size_t char_linebreak(struct lowdown_doc *, char *, size_t, size_t);
static size_t char_codespan(struct lowdown_doc *, char *, size_t, size_t);
static size_t char_escape(struct lowdown_doc *, char *, size_t, size_t);
static size_t char_entity(struct lowdown_doc *, char *, size_t, size_t);
static size_t char_langle_tag(struct lowdown_doc *, char *, size_t, size_t);
static size_t char_autolink_url(struct lowdown_doc *, char *, size_t, size_t);
static size_t char_autolink_email(struct lowdown_doc *, char *, size_t, size_t);
static size_t char_autolink_www(struct lowdown_doc *, char *, size_t, size_t);
static size_t char_link(struct lowdown_doc *, char *, size_t, size_t);
static size_t char_image(struct lowdown_doc *, char *, size_t, size_t);
static size_t char_superscript(struct lowdown_doc *, char *, size_t, size_t);
static size_t char_math(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_QUOTE,
MD_CHAR_MATH
};
static 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,
NULL,
&char_math
};
/* Some forward declarations. */
static void parse_block(struct lowdown_doc *, char *, size_t);
static size_t parse_listitem(struct lowdown_buf *,
struct lowdown_doc *, char *, size_t, enum hlist_fl *, size_t);
static struct lowdown_node *
pushnode(struct lowdown_doc *doc, enum lowdown_rndrt t)
{
struct lowdown_node *n;
if ((doc->depth++ > doc->maxdepth) && doc->maxdepth)
errx(EXIT_FAILURE, "maximum parse depth exceeded");
n = xcalloc(1, sizeof(struct lowdown_node));
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;
}
static void
pushbuffer(struct lowdown_buf *buf, const char *data, size_t datasz)
{
memset(buf, 0, sizeof(struct lowdown_buf));
if (0 == datasz)
return;
buf->data = xmalloc(datasz);
buf->size = buf->asize = datasz;
memcpy(buf->data, data, datasz);
}
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;
}
static void
unscape_text(struct lowdown_buf *ob, struct lowdown_buf *src)
{
size_t i = 0, org;
while (i < src->size) {
org = i;
while (i < src->size && src->data[i] != '\\')
i++;
if (i > org)
hbuf_put(ob, src->data + org, i - org);
if (i + 1 >= src->size)
break;
hbuf_putc(ob, src->data[i + 1]);
i += 2;
}
}
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 ((NULL == ref->name && 0 == length) ||
(NULL != ref->name &&
ref->name->size == length &&
0 == memcmp(ref->name->data, name, length)))
return(ref);
return NULL;
}
static void
free_link_refs(struct link_refq *q)
{
struct link_ref *r;
while (NULL != (r = TAILQ_FIRST(q))) {
TAILQ_REMOVE(q, r, entries);
hbuf_free(r->link);
hbuf_free(r->name);
hbuf_free(r->title);
free(r);
}
}
static struct footnote_ref *
find_footnote_ref(struct footnote_refq *q, char *name, size_t sz)
{
struct footnote_ref *ref;
TAILQ_FOREACH(ref, q, entries)
if ((NULL == ref->name && 0 == sz) ||
(NULL != ref->name &&
ref->name->size == sz &&
0 == memcmp(ref->name->data, name, sz)))
return(ref);
return NULL;
}
static void
free_footnote_refs(struct footnote_refq *q)
{
struct footnote_ref *ref;
while (NULL != (ref = TAILQ_FIRST(q))) {
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.
*/
static void
replace_spacing(struct lowdown_buf *ob, const char *data, size_t size)
{
size_t i = 0, mark;
hbuf_grow(ob, size);
while (1) {
mark = i;
while (i < size && data[i] != '\n')
i++;
hbuf_put(ob, data + mark, i - mark);
if (i >= size)
break;
if (!(i > 0 && data[i-1] == ' '))
hbuf_putc(ob, ' ');
i++;
}
}
/*
* 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 = 0, nb = 0;
/* Assumed to be: [-@._a-zA-Z0-9]+ with exactly one '@'. */
for (i = 0; i < size; ++i) {
if (isalnum(data[i]))
continue;
switch (data[i]) {
case '@':
nb++;
case '-':
case '.':
case '_':
break;
case '>':
return (nb == 1) ? i + 1 : 0;
default:
return 0;
}
}
return 0;
}
/*
* Image nodes may be followed by extended attributes, if configured.
* We only recognise several of them---parse them here.
*/
static size_t
parse_image_attrs(struct rndr_image *img, const char *data, size_t size)
{
size_t offs, end, i, stack = 1;
struct lowdown_buf *attrbuf;
assert(data[0] == '{');
for (end = offs = 1; end < size; end++) {
if (data[end] == '{')
stack++;
else if (data[end] == '}' && --stack == 0)
break;
}
/* If at "size", we never reached the closing brace. */
if (end == size)
return 0;
/* Read in each attribute key-value pair. */
while (offs < end) {
while (offs < end && xisspace(data[offs]))
offs++;
if (offs == end)
break;
/* Require a breaking equal sign. */
i = offs;
while (offs < end && data[offs] != '=')
offs++;
if (offs == end)
return 0;
/* Which extended dimensions do we recognise? */
if (offs - i == 5 &&
strncasecmp(&data[i], "width", 5) == 0)
attrbuf = &img->attr_width;
else if (offs - i == 6 &&
strncasecmp(&data[i], "height", 6) == 0)
attrbuf = &img->attr_height;
else
attrbuf = NULL;
i = ++offs;
while (offs < end && !xisspace(data[offs]))
offs++;
if (attrbuf != NULL && offs > i)
pushbuffer(attrbuf, &data[i], offs - i);
}
return end + 1;
}
/*
* 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(data[i]))
return 0;
/* Scheme test. */
*ltype = HALINK_NONE;
/* Try to find the beginning of an URI. */
while (i < size && (isalnum(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").
*/
static void
parse_inline(struct lowdown_doc *doc, char *data, size_t size)
{
size_t i = 0, end = 0, consumed = 0;
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);
pushbuffer(&n->rndr_normal_text.text,
data + i, end - i);
popnode(doc, n);
}
/* End of file? */
if (end >= size)
break;
i = end;
end = markdown_char_ptrs[
active_char[(unsigned char)data[end]]]
(doc, data + i, i - consumed, size - i);
/* Check if no action from the callback. */
if (end == 0) {
end = i + 1;
continue;
}
i += end;
end = consumed = i;
/*
* Check for image attributes.
* This can be done for other in-line elements, but
* for now this is limited just to images.
*/
n = TAILQ_LAST(&doc->current->children, lowdown_nodeq);
if ((doc->ext_flags & LOWDOWN_IMG_EXT) &&
i < size && data[i] == '{' &&
n != NULL && n->type == LOWDOWN_IMAGE) {
i = end;
end = parse_image_attrs
(&n->rndr_image, data + i, size - i);
if (end == 0) {
end = i + 1;
continue;
}
i += end;
end = consumed = i;
}
}
}
/*
* 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;
}
/*
* 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. */
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.
*/
static size_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)
if (i + 1 < size &&
isalnum(data[i + 1]))
continue;
n = pushnode(doc, LOWDOWN_EMPHASIS);
parse_inline(doc, data, i);
popnode(doc, n);
return i + 1;
}
}
return 0;
}
/*
* Parsing single emphase.
*/
static size_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 (0 == len)
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;
n = pushnode(doc, t);
parse_inline(doc, data, i);
popnode(doc, n);
return i + 2;
}
i++;
}
return 0;
}
/*
* Parsing single emphase
* Finds the first closing tag, and delegates to the other emph.
*/
static size_t
parse_emph3(struct lowdown_doc *doc, char *data, size_t size, char c)
{
size_t i = 0, len;
struct lowdown_node *n;
while (i < size) {
len = find_emph_char(data + i, size - i, c);
if (0 == len)
return 0;
i += len;
/* Skip spacing preceded symbols. */
if (data[i] != c || xisspace(data[i - 1]))
continue;
if (i + 2 < size && data[i + 1] == c &&
data[i + 2] == c) {
/*
* Triple symbol (***) found.
*/
n = pushnode(doc, LOWDOWN_TRIPLE_EMPHASIS);
parse_inline(doc, data, i);
popnode(doc, n);
return i + 3;
} else if (i + 1 < size && data[i + 1] == c) {
/*
* Double symbol (**) found.
*/
len = parse_emph1(doc, data - 2, size + 2, c);
if (!len)
return 0;
else
return len - 2;
} else {
/*
* Single symbol found.
*/
len = parse_emph2(doc,
data - 1, size + 1, c);
if (!len)
return 0;
else
return len - 1;
}
}
return 0;
}
/*
* Parses a math span until the given ending delimiter.
*/
static size_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 = delimsz;
struct lowdown_node *n;
/*
* Find ending delimiter.
* All text within the equation is opaque, so we don't need to
* care about embedded macros.
*/
while (1) {
while (i < size && data[i] != end[0])
i++;
if (i >= size)
return 0;
if ( ! is_escaped(data, i) &&
! (i + delimsz > size) &&
0 == memcmp(data + i, end, delimsz))
break;
i++;
}
i += delimsz;
if ( ! (LOWDOWN_MATH & doc->ext_flags)) {
n = pushnode(doc, LOWDOWN_NORMAL_TEXT);
pushbuffer(&n->rndr_normal_text.text, data, i);
popnode(doc, n);
return i;
}
n = pushnode(doc, LOWDOWN_MATH_BLOCK);
pushbuffer(&n->rndr_math.text, data + delimsz, i - 2 * delimsz);
n->rndr_math.blockmode = blockmode;
popnode(doc, n);
return i;
}
/*
* Single and double emphasis parsing.
*/
static size_t
char_emphasis(struct lowdown_doc *doc,
char *data, size_t offset, size_t size)
{
char c = data[0];
size_t ret;
if (doc->ext_flags & LOWDOWN_NOINTEM)
if (offset > 0 && !xisspace(data[-1]) &&
data[-1] != '>' && data[-1] != '(')
return 0;
if (size > 2 && data[1] != c) {
/*
* Spacing cannot follow an opening emphasis;
* strikethrough and highlight only takes '~~'.
*/
if (c == '~' || c == '=' || xisspace(data[1]) ||
(ret = parse_emph1(doc,
data + 1, size - 1, c)) == 0)
return 0;
return ret + 1;
}
if (size > 3 && data[1] == c && data[2] != c) {
if (xisspace(data[2]) ||
(ret = parse_emph2(doc,
data + 2, size - 2, c)) == 0)
return 0;
return ret + 2;
}
if (size > 4 && data[1] == c && data[2] == c && data[3] != c) {
if (c == '~' || c == '=' || xisspace(data[3]) ||
(ret = parse_emph3(doc,
data + 3, size - 3, c)) == 0)
return 0;
return ret + 3;
}
return 0;
}
/*
* '\n' preceded by two spaces (assuming linebreak != 0)
*/
static size_t
char_linebreak(struct lowdown_doc *doc,
char *data, size_t offset, size_t size)
{
struct lowdown_node *n;
size_t w;
struct lowdown_buf *b;
if (offset < 2 || data[-1] != ' ' || data[-2] != ' ')
return 0;
/* Removing the last space from nodes. */
assert(NULL != doc->current);
n = TAILQ_LAST(&doc->current->children, lowdown_nodeq);
assert(NULL != n && LOWDOWN_NORMAL_TEXT == n->type);
b = &n->rndr_normal_text.text;
while (b->size && b->data[b->size - 1] == ' ')
b->size--;
/*
* Swallow leading white-space of next line.
* XXX: is this just CommonMark?
*/