summaryrefslogtreecommitdiff
path: root/src/inlines.c
blob: a43012f99d978724c6f45724a8192a2c5fe8b25d (plain)
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
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>

#include "cmark.h"
#include "html/houdini.h"
#include "utf8.h"
#include "scanners.h"
#include "inlines.h"

typedef struct InlineStack {
	struct InlineStack *previous;
	node_inl *first_inline;
	int delim_count;
	unsigned char delim_char;
} inline_stack;

typedef struct Subject {
	chunk input;
	int pos;
	int label_nestlevel;
	reference_map *refmap;
	inline_stack *emphasis_openers;
	int emphasis_nestlevel;
} subject;

static node_inl *parse_chunk_inlines(chunk *chunk, reference_map *refmap);
static node_inl *parse_inlines_while(subject* subj, int (*f)(subject*));
static int parse_inline(subject* subj, node_inl ** last);

static void subject_from_chunk(subject *e, chunk *chunk, reference_map *refmap);
static void subject_from_buf(subject *e, strbuf *buffer, reference_map *refmap);
static int subject_find_special_char(subject *subj);

static unsigned char *bufdup(const unsigned char *buf)
{
	unsigned char *new = NULL;

	if (buf) {
		int len = strlen((char *)buf);
		new = calloc(len + 1, sizeof(*new));
        if(new != NULL) {
    		memcpy(new, buf, len + 1);
        }
	}

	return new;
}

static inline node_inl *make_link_(node_inl *label, unsigned char *url, unsigned char *title)
{
	node_inl* e = calloc(1, sizeof(*e));
    if(e != NULL) {
    	e->tag = INL_LINK;
    	e->content.linkable.label = label;
	    e->content.linkable.url   = url;
    	e->content.linkable.title = title;
	    e->next = NULL;
    }
	return e;
}

inline static node_inl* make_ref_link(node_inl* label, reference *ref)
{
	return make_link_(label, bufdup(ref->url), bufdup(ref->title));
}

inline static node_inl* make_autolink(node_inl* label, chunk url, int is_email)
{
	return make_link_(label, clean_autolink(&url, is_email), NULL);
}

// Create an inline with a linkable string value.
inline static node_inl* make_link(node_inl* label, chunk url, chunk title)
{
	return make_link_(label, clean_url(&url), clean_title(&title));
}

inline static node_inl* make_inlines(int t, node_inl* contents)
{
	node_inl * e = calloc(1, sizeof(*e));
    if(e != NULL) {
    	e->tag = t;
	    e->content.inlines = contents;
    	e->next = NULL;
    }
	return e;
}

// Create an inline with a literal string value.
inline static node_inl* make_literal(int t, chunk s)
{
	node_inl * e = calloc(1, sizeof(*e));
    if(e != NULL) {
    	e->tag = t;
	    e->content.literal = s;
    	e->next = NULL;
    }
	return e;
}

// Create an inline with no value.
inline static node_inl* make_simple(int t)
{
	node_inl* e = calloc(1, sizeof(*e));
    if(e != NULL) {
    	e->tag = t;
	    e->next = NULL;
    }
	return e;
}

// Macros for creating various kinds of inlines.
#define make_str(s) make_literal(INL_STRING, s)
#define make_code(s) make_literal(INL_CODE, s)
#define make_raw_html(s) make_literal(INL_RAW_HTML, s)
#define make_linebreak() make_simple(INL_LINEBREAK)
#define make_softbreak() make_simple(INL_SOFTBREAK)
#define make_emph(contents) make_inlines(INL_EMPH, contents)
#define make_strong(contents) make_inlines(INL_STRONG, contents)

// Utility function used by free_inlines
void splice_into_list(node_inl* e, node_inl* children) {
	node_inl * tmp;
	tmp = children;
	if (!tmp) {
	    return ;
	}
	// Find last child
	while (tmp->next) {
	    tmp = tmp->next;
	}
	// Splice children into list
	tmp->next = e->next;
	e->next = children;
	return ;
}

// Free an inline list.  Avoid recursion to prevent stack overflows
// on deeply nested structures.
extern void free_inlines(node_inl* e)
{
	node_inl * next;

	while (e != NULL) {
		switch (e->tag){
		case INL_STRING:
		case INL_RAW_HTML:
		case INL_CODE:
			chunk_free(&e->content.literal);
			break;
		case INL_LINEBREAK:
		case INL_SOFTBREAK:
			break;
		case INL_LINK:
		case INL_IMAGE:
			free(e->content.linkable.url);
			free(e->content.linkable.title);
			splice_into_list(e, e->content.linkable.label);
			break;
		case INL_EMPH:
		case INL_STRONG:
		        splice_into_list(e, e->content.inlines);
			break;
		default:
			break;
		}
		next = e->next;
		free(e);
		e = next;
	}
}

// Append inline list b to the end of inline list a.
// Return pointer to head of new list.
inline static node_inl* append_inlines(node_inl* a, node_inl* b)
{
	if (a == NULL) {  // NULL acts like an empty list
		return b;
	}
	node_inl* cur = a;
	while (cur->next) {
		cur = cur->next;
	}
	cur->next = b;
	return a;
}

static void subject_from_buf(subject *e, strbuf *buffer, reference_map *refmap)
{
	e->input.data = buffer->ptr;
	e->input.len = buffer->size;
	e->input.alloc = 0;
	e->pos = 0;
	e->label_nestlevel = 0;
	e->refmap = refmap;
	e->emphasis_openers = NULL;
	e->emphasis_nestlevel = 0;

	chunk_rtrim(&e->input);
}

static void subject_from_chunk(subject *e, chunk *chunk, reference_map *refmap)
{
	e->input.data = chunk->data;
	e->input.len = chunk->len;
	e->input.alloc = 0;
	e->pos = 0;
	e->label_nestlevel = 0;
	e->refmap = refmap;
	e->emphasis_openers = NULL;
	e->emphasis_nestlevel = 0;

	chunk_rtrim(&e->input);
}

inline static int isbacktick(int c)
{
	return (c == '`');
}

static inline unsigned char peek_char(subject *subj)
{
	return (subj->pos < subj->input.len) ? subj->input.data[subj->pos] : 0;
}

static inline unsigned char peek_at(subject *subj, int pos)
{
	return subj->input.data[pos];
}

// Return true if there are more characters in the subject.
inline static int is_eof(subject* subj)
{
	return (subj->pos >= subj->input.len);
}

// Advance the subject.  Doesn't check for eof.
#define advance(subj) (subj)->pos += 1

// Take characters while a predicate holds, and return a string.
inline static chunk take_while(subject* subj, int (*f)(int))
{
	unsigned char c;
	int startpos = subj->pos;
	int len = 0;

	while ((c = peek_char(subj)) && (*f)(c)) {
		advance(subj);
		len++;
	}

	return chunk_dup(&subj->input, startpos, len);
}

// Try to process a backtick code span that began with a
// span of ticks of length openticklength length (already
// parsed).  Return 0 if you don't find matching closing
// backticks, otherwise return the position in the subject
// after the closing backticks.
static int scan_to_closing_backticks(subject* subj, int openticklength)
{
	// read non backticks
	unsigned char c;
	while ((c = peek_char(subj)) && c != '`') {
		advance(subj);
	}
	if (is_eof(subj)) {
		return 0;  // did not find closing ticks, return 0
	}
	int numticks = 0;
	while (peek_char(subj) == '`') {
		advance(subj);
		numticks++;
	}
	if (numticks != openticklength){
		return(scan_to_closing_backticks(subj, openticklength));
	}
	return (subj->pos);
}

// Parse backtick code section or raw backticks, return an inline.
// Assumes that the subject has a backtick at the current position.
static node_inl* handle_backticks(subject *subj)
{
	chunk openticks = take_while(subj, isbacktick);
	int startpos = subj->pos;
	int endpos = scan_to_closing_backticks(subj, openticks.len);

	if (endpos == 0) { // not found
		subj->pos = startpos; // rewind
		return make_str(openticks);
	} else {
		strbuf buf = GH_BUF_INIT;

		strbuf_set(&buf, subj->input.data + startpos, endpos - startpos - openticks.len);
		strbuf_trim(&buf);
		strbuf_normalize_whitespace(&buf);

		return make_code(chunk_buf_detach(&buf));
	}
}

// Scan ***, **, or * and return number scanned, or 0.
// Advances position.
static int scan_delims(subject* subj, unsigned char c, bool * can_open, bool * can_close)
{
	int numdelims = 0;
	unsigned char char_before, char_after;

	char_before = subj->pos == 0 ? '\n' : peek_at(subj, subj->pos - 1);
	while (peek_char(subj) == c) {
		numdelims++;
		advance(subj);
	}
	char_after = peek_char(subj);
	*can_open = numdelims > 0 && !isspace(char_after);
	*can_close = numdelims > 0 && !isspace(char_before);
	if (c == '_') {
		*can_open = *can_open && !isalnum(char_before);
		*can_close = *can_close && !isalnum(char_after);
	}
	return numdelims;
}

static void free_openers(subject* subj, inline_stack* istack)
{
    inline_stack * tempstack;
    while (subj->emphasis_openers != istack) {
	tempstack = subj->emphasis_openers;
	subj->emphasis_openers = subj->emphasis_openers->previous;
	subj->emphasis_nestlevel--;
	free(tempstack);
    }
}

// Parse strong/emph or a fallback.
// Assumes the subject has '_' or '*' at the current position.
static node_inl* handle_strong_emph(subject* subj, unsigned char c, node_inl **last)
{
	bool can_open, can_close;
	int numdelims;
	int useDelims;
	int openerDelims;
	inline_stack * istack;
	node_inl * inl;
	node_inl * emph;
	node_inl * inl_text;

	numdelims = scan_delims(subj, c, &can_open, &can_close);

	if (can_close)
	{
		// walk the stack and find a matching opener, if there is one
		istack = subj->emphasis_openers;
		while (true)
		{
			if (istack == NULL)
				goto cannotClose;

			if (istack->delim_char == c)
				break;

			istack = istack->previous;
		}

		// calculate the actual number of delimeters used from this closer
		openerDelims = istack->delim_count;
		if (numdelims < 3 || openerDelims < 3) {
		    useDelims = numdelims <= openerDelims ? numdelims : openerDelims;
		} else { // (numdelims >= 3 && openerDelims >= 3)
		    useDelims = numdelims % 2 == 0 ? 2 : 1;
		}

		if (istack->delim_count == useDelims)
		{
			// the opener is completely used up - remove the stack entry and reuse the inline element
			inl = istack->first_inline;
			inl->tag = useDelims == 1 ? INL_EMPH : INL_STRONG;
			chunk_free(&inl->content.literal);
			inl->content.inlines = inl->next;
			inl->next = NULL;

			// remove this opener and all later ones from stack:
                        free_openers(subj, istack->previous);
			*last = inl;
		}
		else
		{
			// the opener will only partially be used - stack entry remains (truncated) and a new inline is added.
			inl = istack->first_inline;
			istack->delim_count -= useDelims;
			inl->content.literal.len = istack->delim_count;

			emph = useDelims == 1 ? make_emph(inl->next) : make_strong(inl->next);
			inl->next = emph;

			// remove all later openers from stack:
                        free_openers(subj, istack);

			*last = emph;
		}

		// if the closer was not fully used, move back a char or two and try again.
		if (useDelims < numdelims)
		{
			subj->pos = subj->pos - numdelims + useDelims;
			return NULL;
		}

		return NULL; // make_str(chunk_literal(""));
	}

cannotClose:
	inl_text = make_str(chunk_dup(&subj->input, subj->pos - numdelims, numdelims));

	if (can_open)
	{
		istack = (inline_stack*)malloc(sizeof(inline_stack));
                if (istack == NULL) {
                  return NULL;
                }
		istack->delim_count = numdelims;
		istack->delim_char = c;
		istack->first_inline = inl_text;
		istack->previous = subj->emphasis_openers;
		subj->emphasis_openers = istack;
		subj->emphasis_nestlevel++;
	}

	return inl_text;
}

// Parse backslash-escape or just a backslash, returning an inline.
static node_inl* handle_backslash(subject *subj)
{
	advance(subj);
	unsigned char nextchar = peek_char(subj);
	if (ispunct(nextchar)) {  // only ascii symbols and newline can be escaped
		advance(subj);
		return make_str(chunk_dup(&subj->input, subj->pos - 1, 1));
	} else if (nextchar == '\n') {
		advance(subj);
		return make_linebreak();
	} else {
		return make_str(chunk_literal("\\"));
	}
}

// Parse an entity or a regular "&" string.
// Assumes the subject has an '&' character at the current position.
static node_inl* handle_entity(subject* subj)
{
	strbuf ent = GH_BUF_INIT;
	size_t len;

	advance(subj);

	len = houdini_unescape_ent(&ent,
				   subj->input.data + subj->pos,
				   subj->input.len - subj->pos
		);

	if (len == 0)
		return make_str(chunk_literal("&"));

	subj->pos += len;
	return make_str(chunk_buf_detach(&ent));
}

// Like make_str, but parses entities.
// Returns an inline sequence consisting of str and entity elements.
static node_inl *make_str_with_entities(chunk *content)
{
	strbuf unescaped = GH_BUF_INIT;

	if (houdini_unescape_html(&unescaped, content->data, (size_t)content->len)) {
		return make_str(chunk_buf_detach(&unescaped));
	} else {
		return make_str(*content);
	}
}

// Clean a URL: remove surrounding whitespace and surrounding <>,
// and remove \ that escape punctuation.
unsigned char *clean_url(chunk *url)
{
	strbuf buf = GH_BUF_INIT;

	chunk_trim(url);

	if (url->len == 0)
		return NULL;

	if (url->data[0] == '<' && url->data[url->len - 1] == '>') {
		houdini_unescape_html_f(&buf, url->data + 1, url->len - 2);
	} else {
		houdini_unescape_html_f(&buf, url->data, url->len);
	}

	strbuf_unescape(&buf);
	return strbuf_detach(&buf);
}

unsigned char *clean_autolink(chunk *url, int is_email)
{
	strbuf buf = GH_BUF_INIT;

	chunk_trim(url);

	if (url->len == 0)
		return NULL;

	if (is_email)
		strbuf_puts(&buf, "mailto:");

	houdini_unescape_html_f(&buf, url->data, url->len);
	return strbuf_detach(&buf);
}

// Clean a title: remove surrounding quotes and remove \ that escape punctuation.
unsigned char *clean_title(chunk *title)
{
	strbuf buf = GH_BUF_INIT;
	unsigned char first, last;

	if (title->len == 0)
		return NULL;

	first = title->data[0];
	last = title->data[title->len - 1];

	// remove surrounding quotes if any:
	if ((first == '\'' && last == '\'') ||
	    (first == '(' && last == ')') ||
	    (first == '"' && last == '"')) {
		houdini_unescape_html_f(&buf, title->data + 1, title->len - 2);
	} else {
		houdini_unescape_html_f(&buf, title->data, title->len);
	}

	strbuf_unescape(&buf);
	return strbuf_detach(&buf);
}

// Parse an autolink or HTML tag.
// Assumes the subject has a '<' character at the current position.
static node_inl* handle_pointy_brace(subject* subj)
{
	int matchlen = 0;
	chunk contents;

	advance(subj);  // advance past first <

	// first try to match a URL autolink
	matchlen = scan_autolink_uri(&subj->input, subj->pos);
	if (matchlen > 0) {
		contents = chunk_dup(&subj->input, subj->pos, matchlen - 1);
		subj->pos += matchlen;

		return make_autolink(
			make_str_with_entities(&contents),
			contents, 0
			);
	}

	// next try to match an email autolink
	matchlen = scan_autolink_email(&subj->input, subj->pos);
	if (matchlen > 0) {
		contents = chunk_dup(&subj->input, subj->pos, matchlen - 1);
		subj->pos += matchlen;

		return make_autolink(
			make_str_with_entities(&contents),
			contents, 1
			);
	}

	// finally, try to match an html tag
	matchlen = scan_html_tag(&subj->input, subj->pos);
	if (matchlen > 0) {
		contents = chunk_dup(&subj->input, subj->pos - 1, matchlen + 1);
		subj->pos += matchlen;
		return make_raw_html(contents);
	}

	// if nothing matches, just return the opening <:
	return make_str(chunk_literal("<"));
}

// Parse a link label.  Returns 1 if successful.
// Unless raw_label is null, it is set to point to the raw contents of the [].
// Assumes the subject has a '[' character at the current position.
// Returns 0 and does not advance if no matching ] is found.
// Note the precedence:  code backticks have precedence over label bracket
// markers, which have precedence over *, _, and other inline formatting
// markers. So, 2 below contains a link while 1 does not:
// 1. [a link `with a ](/url)` character
// 2. [a link *with emphasized ](/url) text*
static int link_label(subject* subj, chunk *raw_label)
{
	int nestlevel = 0;
	node_inl* tmp = NULL;
	int startpos = subj->pos;

	if (subj->label_nestlevel) {
		// if we've already checked to the end of the subject
		// for a label, even with a different starting [, we
		// know we won't find one here and we can just return.
		// Note:  nestlevel 1 would be: [foo [bar]
		// nestlevel 2 would be: [foo [bar [baz]
		subj->label_nestlevel--;
		return 0;
	}

	advance(subj);  // advance past [
	unsigned char c;
	while ((c = peek_char(subj)) && (c != ']' || nestlevel > 0)) {
		switch (c) {
		case '`':
			tmp = handle_backticks(subj);
			free_inlines(tmp);
			break;
		case '<':
			tmp = handle_pointy_brace(subj);
			free_inlines(tmp);
			break;
		case '[':  // nested []
			nestlevel++;
			advance(subj);
			break;
		case ']':  // nested []
			nestlevel--;
			advance(subj);
			break;
		case '\\':
			advance(subj);
			if (ispunct(peek_char(subj))) {
				advance(subj);
			}
			break;
		default:
			advance(subj);
		}
	}
	if (nestlevel == 0 && c == ']') {
		*raw_label = chunk_dup(&subj->input, startpos + 1, subj->pos - (startpos + 1));
		subj->label_nestlevel = 0;
		advance(subj);  // advance past ]
		return 1;
	} else {
		if (c == 0) {
			subj->label_nestlevel = nestlevel;
		}
		subj->pos = startpos; // rewind
		return 0;
	}
}

// Parse a link or the link portion of an image, or return a fallback.
static node_inl* handle_left_bracket(subject* subj)
{
	node_inl *lab = NULL;
	node_inl *result = NULL;
	reference *ref;
	int n;
	int sps;
	int found_label;
	int endlabel, starturl, endurl, starttitle, endtitle, endall;

	chunk rawlabel;
	chunk url, title;

	found_label = link_label(subj, &rawlabel);
	endlabel = subj->pos;

	if (found_label) {
		if (peek_char(subj) == '(' &&
		    ((sps = scan_spacechars(&subj->input, subj->pos + 1)) > -1) &&
		    ((n = scan_link_url(&subj->input, subj->pos + 1 + sps)) > -1)) {

			// try to parse an explicit link:
			starturl = subj->pos + 1 + sps; // after (
			endurl = starturl + n;
			starttitle = endurl + scan_spacechars(&subj->input, endurl);

			// ensure there are spaces btw url and title
			endtitle = (starttitle == endurl) ? starttitle :
				starttitle + scan_link_title(&subj->input, starttitle);

			endall = endtitle + scan_spacechars(&subj->input, endtitle);

			if (peek_at(subj, endall) == ')') {
				subj->pos = endall + 1;

				url = chunk_dup(&subj->input, starturl, endurl - starturl);
				title = chunk_dup(&subj->input, starttitle, endtitle - starttitle);
				lab = parse_chunk_inlines(&rawlabel, NULL);

				return make_link(lab, url, title);
			} else {
				// if we get here, we matched a label but didn't get further:
				subj->pos = endlabel;
				lab = parse_chunk_inlines(&rawlabel, subj->refmap);
				result = append_inlines(make_str(chunk_literal("[")),
							append_inlines(lab,
								       make_str(chunk_literal("]"))));
				return result;
			}
		} else {
			chunk rawlabel_tmp;
			chunk reflabel;

			// Check for reference link.
			// First, see if there's another label:
			subj->pos = subj->pos + scan_spacechars(&subj->input, endlabel);
			reflabel = rawlabel;

			// if followed by a nonempty link label, we change reflabel to it:
			if (peek_char(subj) == '[' && link_label(subj, &rawlabel_tmp)) {
				if (rawlabel_tmp.len > 0)
					reflabel = rawlabel_tmp;
			} else {
				subj->pos = endlabel;
			}

			// lookup rawlabel in subject->reference_map:
			ref = reference_lookup(subj->refmap, &reflabel);
			if (ref != NULL) { // found
				lab = parse_chunk_inlines(&rawlabel, NULL);
				result = make_ref_link(lab, ref);
			} else {
				subj->pos = endlabel;
				lab = parse_chunk_inlines(&rawlabel, subj->refmap);
				result = append_inlines(make_str(chunk_literal("[")),
							append_inlines(lab, make_str(chunk_literal("]"))));
			}
			return result;
		}
	}
	// If we fall through to here, it means we didn't match a link:
	advance(subj);  // advance past [
	return make_str(chunk_literal("["));
}

// Parse a hard or soft linebreak, returning an inline.
// Assumes the subject has a newline at the current position.
static node_inl* handle_newline(subject *subj)
{
	int nlpos = subj->pos;
	// skip over newline
	advance(subj);
	// skip spaces at beginning of line
	while (peek_char(subj) == ' ') {
		advance(subj);
	}
	if (nlpos > 1 &&
	    peek_at(subj, nlpos - 1) == ' ' &&
	    peek_at(subj, nlpos - 2) == ' ') {
		return make_linebreak();
	} else {
		return make_softbreak();
	}
}

inline static int not_eof(subject* subj)
{
	return !is_eof(subj);
}

// Parse inlines while a predicate is satisfied.  Return inlines.
extern node_inl* parse_inlines_while(subject* subj, int (*f)(subject*))
{
	node_inl* result = NULL;
	node_inl** last = &result;
	node_inl* first = NULL;
	while ((*f)(subj) && parse_inline(subj, last)) {
	    if (!first) {
		first = *last;
	    }
	}

	inline_stack* istack = subj->emphasis_openers;
	inline_stack* temp;
	while (istack != NULL) {
		temp = istack->previous;
		free(istack);
		istack = temp;
	}

	return first;
}

node_inl *parse_chunk_inlines(chunk *chunk, reference_map *refmap)
{
	subject subj;
	subject_from_chunk(&subj, chunk, refmap);
	return parse_inlines_while(&subj, not_eof);
}

static int subject_find_special_char(subject *subj)
{
	// "\n\\`&_*[]<!"
	static const int8_t SPECIAL_CHARS[256] = {
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
		0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1,
		1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

	int n = subj->pos + 1;

	while (n < subj->input.len) {
		if (SPECIAL_CHARS[subj->input.data[n]])
			return n;
		n++;
	}

	return subj->input.len;
}

// Parse an inline, advancing subject, and add it to last element.
// Adjust tail to point to new last element of list.
// Return 0 if no inline can be parsed, 1 otherwise.
static int parse_inline(subject* subj, node_inl ** last)
{
	node_inl* new = NULL;
	chunk contents;
	unsigned char c;
	int endpos;
	c = peek_char(subj);
	if (c == 0) {
		return 0;
	}
	switch(c){
	case '\n':
		new = handle_newline(subj);
		break;
	case '`':
		new = handle_backticks(subj);
		break;
	case '\\':
		new = handle_backslash(subj);
		break;
	case '&':
		new = handle_entity(subj);
		break;
	case '<':
		new = handle_pointy_brace(subj);
		break;
	case '_':
		new = handle_strong_emph(subj, '_', last);
		break;
	case '*':
		new = handle_strong_emph(subj, '*', last);
		break;
	case '[':
		new = handle_left_bracket(subj);
		break;
	case '!':
		advance(subj);
		if (peek_char(subj) == '[') {
			new = handle_left_bracket(subj);
			if (new != NULL && new->tag == INL_LINK) {
				new->tag = INL_IMAGE;
			} else {
				new = append_inlines(make_str(chunk_literal("!")), new);
			}
		} else {
			new = make_str(chunk_literal("!"));
		}
		break;
	default:
		endpos = subject_find_special_char(subj);
		contents = chunk_dup(&subj->input, subj->pos, endpos - subj->pos);
		subj->pos = endpos;

		// if we're at a newline, strip trailing spaces.
		if (peek_char(subj) == '\n') {
			chunk_rtrim(&contents);
		}

		new = make_str(contents);
	}
	if (*last == NULL) {
		*last = new;
	} else if (new) {
		append_inlines(*last, new);
		*last = new;
	}

	return 1;
}

extern node_inl* parse_inlines(strbuf *input, reference_map *refmap)
{
	subject subj;
	subject_from_buf(&subj, input, refmap);
	return parse_inlines_while(&subj, not_eof);
}

// Parse zero or more space characters, including at most one newline.
void spnl(subject* subj)
{
	bool seen_newline = false;
	while (peek_char(subj) == ' ' ||
	       (!seen_newline &&
		(seen_newline = peek_char(subj) == '\n'))) {
		advance(subj);
	}
}

// Parse reference.  Assumes string begins with '[' character.
// Modify refmap if a reference is encountered.
// Return 0 if no reference found, otherwise position of subject
// after reference is parsed.
int parse_reference_inline(strbuf *input, reference_map *refmap)
{
	subject subj;

	chunk lab;
	chunk url;
	chunk title;

	int matchlen = 0;
	int beforetitle;

	subject_from_buf(&subj, input, NULL);

	// parse label:
	if (!link_label(&subj, &lab))
		return 0;

	// colon:
	if (peek_char(&subj) == ':') {
		advance(&subj);
	} else {
		return 0;
	}

	// parse link url:
	spnl(&subj);
	matchlen = scan_link_url(&subj.input, subj.pos);
	if (matchlen) {
		url = chunk_dup(&subj.input, subj.pos, matchlen);
		subj.pos += matchlen;
	} else {
		return 0;
	}

	// parse optional link_title
	beforetitle = subj.pos;
	spnl(&subj);
	matchlen = scan_link_title(&subj.input, subj.pos);
	if (matchlen) {
		title = chunk_dup(&subj.input, subj.pos, matchlen);
		subj.pos += matchlen;
	} else {
		subj.pos = beforetitle;
		title = chunk_literal("");
	}
	// parse final spaces and newline:
	while (peek_char(&subj) == ' ') {
		advance(&subj);
	}
	if (peek_char(&subj) == '\n') {
		advance(&subj);
	} else if (peek_char(&subj) != 0) {
		return 0;
	}
	// insert reference into refmap
	reference_create(refmap, &lab, &url, &title);
	return subj.pos;
}