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
|
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>
#include "bstrlib.h"
#include "stmd.h"
#include "uthash.h"
#include "debug.h"
#include "scanners.h"
static block* make_block(int tag, int start_line, int start_column)
{
block* e;
e = (block*) malloc(sizeof(block));
e->tag = tag;
e->open = true;
e->last_line_blank = false;
e->start_line = start_line;
e->start_column = start_column;
e->end_line = start_line;
e->children = NULL;
e->last_child = NULL;
e->parent = NULL;
e->top = NULL;
e->attributes.refmap = NULL;
e->string_content = bfromcstr("");
e->inline_content = NULL;
e->next = NULL;
e->prev = NULL;
return e;
}
// Create a root document block.
extern block* make_document()
{
block * e = make_block(document, 1, 1);
reference * map = NULL;
reference ** refmap;
refmap = (reference**) malloc(sizeof(reference*));
*refmap = map;
e->attributes.refmap = refmap;
e->top = e;
return e;
}
// Returns true if line has only space characters, else false.
bool is_blank(bstring s, int offset)
{
char c;
while ((c = bchar(s, offset))) {
if (c == '\n') {
return true;
} else if (c == ' ') {
offset++;
} else {
return false;
}
}
return true;
}
static inline bool can_contain(int parent_type, int child_type)
{
return ( parent_type == document ||
parent_type == block_quote ||
parent_type == list_item ||
(parent_type == list && child_type == list_item) );
}
static inline bool accepts_lines(int block_type)
{
return (block_type == paragraph ||
block_type == atx_header ||
block_type == indented_code ||
block_type == fenced_code);
}
static int add_line(block* block, bstring ln, int offset)
{
bstring s = bmidstr(ln, offset, blength(ln) - offset);
check(block->open, "attempted to add line (%s) to closed container (%d)",
ln->data, block->tag);
check(bformata(block->string_content, "%s", s->data) == 0,
"could not append line to string_content");
bdestroy(s);
return 0;
error:
return -1;
}
static int remove_trailing_blank_lines(bstring ln)
{
bstring tofind = bfromcstr(" \t\r\n");
int pos;
// find last nonspace:
pos = bninchrr(ln, blength(ln) - 1, tofind);
if (pos == BSTR_ERR) { // all spaces
bassigncstr(ln, "");
} else {
// find next newline after it
pos = bstrchrp(ln, '\n', pos);
if (pos != BSTR_ERR) {
check(bdelete(ln, pos, blength(ln) - pos) != BSTR_ERR,
"failed to delete trailing blank lines");
}
}
bdestroy(tofind);
return 0;
error:
return -1;
}
// Check to see if a block ends with a blank line, descending
// if needed into lists and sublists.
static bool ends_with_blank_line(block* block)
{
if (block->last_line_blank) {
return true;
}
if ((block->tag == list || block->tag == list_item) && block->last_child) {
return ends_with_blank_line(block->last_child);
} else {
return false;
}
}
// Break out of all containing lists
static int break_out_of_lists(block ** bptr, int line_number)
{
block * container = *bptr;
block * b = container->top;
// find first containing list:
while (b && b->tag != list) {
b = b->last_child;
}
if (b) {
while (container && container != b) {
finalize(container, line_number);
container = container->parent;
}
finalize(b, line_number);
*bptr = b->parent;
}
return 0;
}
extern int finalize(block* b, int line_number)
{
int firstlinelen;
int pos;
block* item;
block* subitem;
check(b != NULL, "finalize called on null block");
if (!b->open) {
return 0; // don't do anything if the block is already closed
}
b->open = false;
if (line_number > b->start_line) {
b->end_line = line_number - 1;
} else {
b->end_line = line_number;
}
switch (b->tag) {
case paragraph:
pos = 0;
while (bchar(b->string_content, 0) == '[' &&
(pos = parse_reference(b->string_content,
b->top->attributes.refmap))) {
bdelete(b->string_content, 0, pos);
}
if (is_blank(b->string_content, 0)) {
b->tag = reference_def;
}
break;
case indented_code:
remove_trailing_blank_lines(b->string_content);
bformata(b->string_content, "\n");
break;
case fenced_code:
// first line of contents becomes info
firstlinelen = bstrchr(b->string_content, '\n');
b->attributes.fenced_code_data.info =
bmidstr(b->string_content, 0, firstlinelen);
bdelete(b->string_content, 0, firstlinelen + 1); // +1 for \n
btrimws(b->attributes.fenced_code_data.info);
unescape(b->attributes.fenced_code_data.info);
break;
case list: // determine tight/loose status
b->attributes.list_data.tight = true; // tight by default
item = b->children;
while (item) {
// check for non-final non-empty list item ending with blank line:
if (item->last_line_blank && item->next) {
b->attributes.list_data.tight = false;
break;
}
// recurse into children of list item, to see if there are
// spaces between them:
subitem = item->children;
while (subitem) {
if (ends_with_blank_line(subitem) &&
(item->next || subitem->next)) {
b->attributes.list_data.tight = false;
break;
}
subitem = subitem->next;
}
if (!(b->attributes.list_data.tight)) {
break;
}
item = item->next;
}
break;
default:
break;
}
return 0;
error:
return -1;
}
// Add a block as child of another. Return pointer to child.
extern block* add_child(block* parent,
int block_type, int start_line, int start_column)
{
// if 'parent' isn't the kind of block that can accept this child,
// then back up til we hit a block that can.
while (!can_contain(parent->tag, block_type)) {
finalize(parent, start_line);
parent = parent->parent;
}
check(parent != NULL, "parent container cannot accept children");
block* child = make_block(block_type, start_line, start_column);
child->parent = parent;
child->top = parent->top;
if (parent->last_child) {
parent->last_child->next = child;
child->prev = parent->last_child;
} else {
parent->children = child;
child->prev = NULL;
}
parent->last_child = child;
return child;
error:
return NULL;
}
// Free a block list and any children.
extern void free_blocks(block* e)
{
block * next;
while (e != NULL) {
next = e->next;
free_inlines(e->inline_content);
bdestroy(e->string_content);
if (e->tag == fenced_code) {
bdestroy(e->attributes.fenced_code_data.info);
} else if (e->tag == document) {
free_reference_map(e->attributes.refmap);
}
free_blocks(e->children);
free(e);
e = next;
}
}
// Walk through block and all children, recursively, parsing
// string content into inline content where appropriate.
int process_inlines(block* cur, reference** refmap)
{
switch (cur->tag) {
case paragraph:
case atx_header:
case setext_header:
check(cur->string_content != NULL, "string_content is NULL");
cur->inline_content = parse_inlines(cur->string_content, refmap);
bdestroy(cur->string_content);
cur->string_content = NULL;
break;
default:
break;
}
block * child = cur->children;
while (child != NULL) {
process_inlines(child, refmap);
child = child->next;
}
return 0;
error:
return -1;
}
// Attempts to parse a list item marker (bullet or enumerated).
// On success, returns length of the marker, and populates
// data with the details. On failure, returns 0.
static int parse_list_marker(bstring ln, int pos,
struct ListData ** dataptr)
{
char c;
int startpos;
int start = 1;
struct ListData * data;
startpos = pos;
c = bchar(ln, pos);
if ((c == '*' || c == '-' || c == '+') && !scan_hrule(ln, pos)) {
pos++;
if (!isspace(bchar(ln, pos))) {
return 0;
}
data = malloc(sizeof(struct ListData));
data->marker_offset = 0; // will be adjusted later
data->list_type = bullet;
data->bullet_char = c;
data->start = 1;
data->delimiter = period;
data->tight = false;
} else if (isdigit(c)) {
pos++;
while (isdigit(bchar(ln, pos))) {
pos++;
}
if (!sscanf((char *) ln->data + startpos, "%d", &start)) {
log_err("sscanf failed");
return 0;
}
c = bchar(ln, pos);
if (c == '.' || c == ')') {
pos++;
if (!isspace(bchar(ln, pos))) {
return 0;
}
data = malloc(sizeof(struct ListData));
data->marker_offset = 0; // will be adjusted later
data->list_type = ordered;
data->bullet_char = 0;
data->start = start;
data->delimiter = (c == '.' ? period : parens);
data->tight = false;
} else {
return 0;
}
} else {
return 0;
}
*dataptr = data;
return (pos - startpos);
}
// Return 1 if list item belongs in list, else 0.
static int lists_match(struct ListData list_data,
struct ListData item_data)
{
return (list_data.list_type == item_data.list_type &&
list_data.delimiter == item_data.delimiter &&
// list_data.marker_offset == item_data.marker_offset &&
list_data.bullet_char == item_data.bullet_char);
}
// Process one line at a time, modifying a block.
// Returns 0 if successful. curptr is changed to point to
// the currently open block.
extern int incorporate_line(bstring ln, int line_number, block** curptr)
{
block* last_matched_container;
int offset = 0;
int matched = 0;
int lev = 0;
int i;
struct ListData * data = NULL;
bool all_matched = true;
block* container;
block* cur = *curptr;
bool blank = false;
int first_nonspace;
int indent;
// detab input line
check(bdetab(ln, 1) != BSTR_ERR,
"invalid UTF-8 sequence in line %d\n", line_number);
// container starts at the document root.
container = cur->top;
// for each containing block, try to parse the associated line start.
// bail out on failure: container will point to the last matching block.
while (container->last_child && container->last_child->open) {
container = container->last_child;
first_nonspace = offset;
while (bchar(ln, first_nonspace) == ' ') {
first_nonspace++;
}
indent = first_nonspace - offset;
blank = bchar(ln, first_nonspace) == '\n';
if (container->tag == block_quote) {
matched = indent <= 3 && bchar(ln, first_nonspace) == '>';
if (matched) {
offset = first_nonspace + 1;
if (bchar(ln, offset) == ' ') {
offset++;
}
} else {
all_matched = false;
}
} else if (container->tag == list_item) {
if (indent >= container->attributes.list_data.marker_offset +
container->attributes.list_data.padding) {
offset += container->attributes.list_data.marker_offset +
container->attributes.list_data.padding;
} else if (blank) {
offset = first_nonspace;
} else {
all_matched = false;
}
} else if (container->tag == indented_code) {
if (indent >= CODE_INDENT) {
offset += CODE_INDENT;
} else if (blank) {
offset = first_nonspace;
} else {
all_matched = false;
}
} else if (container->tag == atx_header ||
container->tag == setext_header) {
// a header can never contain more than one line
all_matched = false;
} else if (container->tag == fenced_code) {
// skip optional spaces of fence offset
i = container->attributes.fenced_code_data.fence_offset;
while (i > 0 && bchar(ln, offset) == ' ') {
offset++;
i--;
}
} else if (container->tag == html_block) {
if (blank) {
all_matched = false;
}
} else if (container->tag == paragraph) {
if (blank) {
container->last_line_blank =true;
all_matched = false;
}
}
if (!all_matched) {
container = container->parent; // back up to last matching block
break;
}
}
last_matched_container = container;
// check to see if we've hit 2nd blank line, break out of list:
if (blank && container->last_line_blank) {
break_out_of_lists(&container, line_number);
}
// unless last matched container is code block, try new container starts:
while (container->tag != fenced_code && container->tag != indented_code &&
container->tag != html_block) {
first_nonspace = offset;
while (bchar(ln, first_nonspace) == ' ') {
first_nonspace++;
}
indent = first_nonspace - offset;
blank = bchar(ln, first_nonspace) == '\n';
if (indent >= CODE_INDENT) {
if (cur->tag != paragraph && !blank) {
offset += CODE_INDENT;
container = add_child(container, indented_code, line_number, offset + 1);
} else { // indent > 4 in lazy line
break;
}
} else if (bchar(ln, first_nonspace) == '>') {
offset = first_nonspace + 1;
// optional following character
if (bchar(ln, offset) == ' ') {
offset++;
}
container = add_child(container, block_quote, line_number, offset + 1);
} else if ((matched = scan_atx_header_start(ln, first_nonspace))) {
offset = first_nonspace + matched;
container = add_child(container, atx_header, line_number, offset + 1);
int hashpos = bstrchrp(ln, '#', first_nonspace);
check(hashpos != BSTR_ERR, "no # found in atx header start");
int level = 0;
while (bchar(ln, hashpos) == '#') {
level++;
hashpos++;
}
container->attributes.header_level = level;
} else if ((matched = scan_open_code_fence(ln, first_nonspace))) {
container = add_child(container, fenced_code, line_number,
first_nonspace + 1);
container->attributes.fenced_code_data.fence_char = bchar(ln,
first_nonspace);
container->attributes.fenced_code_data.fence_length = matched;
container->attributes.fenced_code_data.fence_offset =
first_nonspace - offset;
offset = first_nonspace + matched;
} else if ((matched = scan_html_block_tag(ln, first_nonspace))) {
container = add_child(container, html_block, line_number,
first_nonspace + 1);
// note, we don't adjust offset because the tag is part of the text
} else if (container->tag == paragraph &&
(lev = scan_setext_header_line(ln, first_nonspace)) &&
// check that there is only one line in the paragraph:
bstrrchrp(container->string_content, '\n',
blength(container->string_content) - 2) == BSTR_ERR) {
container->tag = setext_header;
container->attributes.header_level = lev;
offset = blength(ln) - 1;
} else if (!(container->tag == paragraph && !all_matched) &&
(matched = scan_hrule(ln, first_nonspace))) {
// it's only now that we know the line is not part of a setext header:
container = add_child(container, hrule, line_number, first_nonspace + 1);
finalize(container, line_number);
container = container->parent;
offset = blength(ln) - 1;
} else if ((matched = parse_list_marker(ln, first_nonspace, &data))) {
// compute padding:
offset = first_nonspace + matched;
i = 0;
while (i <= 5 && bchar(ln, offset + i) == ' ') {
i++;
}
// i = number of spaces after marker, up to 5
if (i >= 5 || i < 1 || bchar(ln, offset) == '\n') {
data->padding = matched + 1;
if (i > 0) {
offset += 1;
}
} else {
data->padding = matched + i;
offset += i;
}
// check container; if it's a list, see if this list item
// can continue the list; otherwise, create a list container.
data->marker_offset = indent;
if (container->tag != list ||
!lists_match(container->attributes.list_data, *data)) {
container = add_child(container, list, line_number,
first_nonspace + 1);
container->attributes.list_data = *data;
}
// add the list item
container = add_child(container, list_item, line_number,
first_nonspace + 1);
container->attributes.list_data = *data;
free(data);
} else {
break;
}
if (accepts_lines(container->tag)) {
// if it's a line container, it can't contain other containers
break;
}
}
// what remains at offset is a text line. add the text to the
// appropriate container.
first_nonspace = offset;
while (bchar(ln, first_nonspace) == ' ') {
first_nonspace++;
}
indent = first_nonspace - offset;
blank = bchar(ln, first_nonspace) == '\n';
// block quote lines are never blank as they start with >
// and we don't count blanks in fenced code for purposes of tight/loose
// lists or breaking out of lists. we also don't set last_line_blank
// on an empty list item.
container->last_line_blank = (blank &&
container->tag != block_quote &&
container->tag != fenced_code &&
!(container->tag == list_item &&
container->children == NULL &&
container->start_line == line_number));
block *cont = container;
while (cont->parent) {
cont->parent->last_line_blank = false;
cont = cont->parent;
}
if (cur != last_matched_container &&
container == last_matched_container &&
!blank &&
cur->tag == paragraph &&
blength(cur->string_content) > 0) {
check(add_line(cur, ln, offset) == 0, "could not add line");
} else { // not a lazy continuation
// finalize any blocks that were not matched and set cur to container:
while (cur != last_matched_container) {
finalize(cur, line_number);
cur = cur->parent;
check(cur != NULL, "cur is NULL, last_matched_container->tag = %d",
last_matched_container->tag);
}
if (container->tag == indented_code) {
check(add_line(container, ln, offset) == 0, "could not add line");
} else if (container->tag == fenced_code) {
matched = (indent <= 3
&& bchar(ln, first_nonspace) == container->attributes.fenced_code_data.fence_char)
&& scan_close_code_fence(ln, first_nonspace,
container->attributes.fenced_code_data.fence_length);
if (matched) {
// if closing fence, don't add line to container; instead, close it:
finalize(container, line_number);
container = container->parent; // back up to parent
} else {
check(add_line(container, ln, offset) == 0, "could not add line");
}
} else if (container->tag == html_block) {
check(add_line(container, ln, offset) == 0, "could not add line");
} else if (blank) {
// ??? do nothing
} else if (container->tag == atx_header) {
// chop off trailing ###s...use a scanner?
brtrimws(ln);
int p = blength(ln) - 1;
int numhashes = 0;
// if string ends in #s, remove these:
while (bchar(ln, p) == '#') {
p--;
numhashes++;
}
if (bchar(ln, p) == '\\') {
// the last # was escaped, so we include it.
p++;
numhashes--;
}
check(bdelete(ln, p + 1, numhashes) != BSTR_ERR,
"could not delete final hashes");
check(add_line(container, ln, first_nonspace) == 0, "could not add line");
finalize(container, line_number);
container = container->parent;
} else if (accepts_lines(container->tag)) {
check(add_line(container, ln, first_nonspace) == 0, "could not add line");
} else if (container->tag != hrule && container->tag != setext_header) {
// create paragraph container for line
container = add_child(container, paragraph, line_number, first_nonspace + 1);
check(add_line(container, ln, first_nonspace) == 0, "could not add line");
} else {
log_warn("Line %d with container type %d did not match any condition:\n\"%s\"",
line_number, container->tag, ln->data);
}
*curptr = container;
}
return 0;
error:
return -1;
}
|