-
Notifications
You must be signed in to change notification settings - Fork 156
/
generate.c
2105 lines (1742 loc) · 43.4 KB
/
generate.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* markdown: a C implementation of John Gruber's Markdown markup language.
*
* Copyright (C) 2007 Jessica L Parsons.
* The redistribution terms are provided in the COPYRIGHT file that must
* be distributed with this source code.
*/
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
#include "config.h"
#include "cstring.h"
#include "markdown.h"
#include "amalloc.h"
typedef int (*stfu)(const void*,const void*);
typedef void (*spanhandler)(MMIOT*,int);
/* forward declarations */
static void text(MMIOT *f);
static Paragraph *display(Paragraph*, MMIOT*);
/* externals from markdown.c */
int __mkd_footsort(Footnote *, Footnote *);
/*
* push text into the generator input buffer
*/
static void
push(char *bfr, int size, MMIOT *f)
{
while ( size-- > 0 )
EXPAND(f->in) = *bfr++;
}
/*
* push a character into the generator input buffer
*/
static void
pushc(char c, MMIOT *f)
{
EXPAND(f->in) = c;
}
/* look <i> characters ahead of the cursor.
*/
static inline int
peek(MMIOT *f, int i)
{
i += (f->isp-1);
return (i >= 0) && (i < S(f->in)) ? (unsigned char)T(f->in)[i] : EOF;
}
/* pull a byte from the input buffer
*/
static inline unsigned int
pull(MMIOT *f)
{
return ( f->isp < S(f->in) ) ? (unsigned char)T(f->in)[f->isp++] : EOF;
}
/* return a pointer to the current position in the input buffer.
*/
static inline char*
cursor(MMIOT *f)
{
return T(f->in) + f->isp;
}
static inline int
isthisspace(MMIOT *f, int i)
{
int c = peek(f, i);
if ( c == EOF )
return 1;
if ( c & 0x80 )
return 0;
return isspace(c) || (c < ' ');
}
static inline int
isthisalnum(MMIOT *f, int i)
{
int c = peek(f, i);
return (c != EOF) && isalnum(c);
}
static inline int
isthisnonword(MMIOT *f, int i)
{
return isthisspace(f, i) || ispunct(peek(f,i));
}
/* return/set the current cursor position
* (when setting the current cursor position we also need to flush the
* last character written cache)
*/
#define mmiotseek(f,x) ((f->isp = x), (f->last = 0))
#define mmiottell(f) (f->isp)
/* move n characters forward ( or -n characters backward) in the input buffer.
*/
static void
shift(MMIOT *f, int i)
{
if (f->isp + i >= 0 )
f->isp += i;
}
/* Qchar()
*/
static void
Qchar(int c, MMIOT *f)
{
block *cur;
if ( S(f->Q) > 0 )
cur = &T(f->Q)[S(f->Q)-1];
else {
cur = &EXPAND(f->Q);
memset(cur, 0, sizeof *cur);
cur->b_type = bTEXT;
}
EXPAND(cur->b_text) = c;
}
/* Qstring()
*/
static void
Qstring(char *s, MMIOT *f)
{
while (*s)
Qchar(*s++, f);
}
/* Qwrite()
*/
static void
Qwrite(char *s, int size, MMIOT *f)
{
while (size-- > 0)
Qchar(*s++, f);
}
/* Qprintf()
*/
static void
Qprintf(MMIOT *f, char *fmt, ...)
{
char bfr[80];
va_list ptr;
va_start(ptr,fmt);
vsnprintf(bfr, sizeof bfr, fmt, ptr);
va_end(ptr);
Qstring(bfr, f);
}
/* Qanchor() prints out a suitable-for-id-tag version of a string
*/
static void
Qanchor(char *name, MMIOT *f)
{
mkd_string_to_anchor(name, strlen(name), (mkd_sta_function_t)Qchar, f, 1, f);
}
/* Qem()
*/
static void
Qem(MMIOT *f, char c, int count)
{
block *p = &EXPAND(f->Q);
memset(p, 0, sizeof *p);
p->b_type = (c == '*') ? bSTAR : bUNDER;
p->b_char = c;
p->b_count = count;
memset(&EXPAND(f->Q), 0, sizeof(block));
}
/* generate html from a markup fragment
*/
void
___mkd_reparse(char *bfr, int size, mkd_flag_t* flags, MMIOT *f, char *esc)
{
MMIOT sub;
struct escaped e;
___mkd_initmmiot(&sub, f->footnotes);
COPY_FLAGS(sub.flags, f->flags);
if ( flags )
ADD_FLAGS(&sub.flags, flags);
sub.cb = f->cb;
sub.ref_prefix = f->ref_prefix;
if ( esc ) {
sub.esc = &e;
e.up = f->esc;
e.text = esc;
}
else
sub.esc = f->esc;
push(bfr, size, &sub);
pushc(0, &sub);
S(sub.in)--;
text(&sub);
___mkd_emblock(&sub);
Qwrite(T(sub.out), S(sub.out), f);
/* inherit the last character printed from the reparsed
* text; this way superscripts can work when they're
* applied to something embedded in a link
*/
f->last = sub.last;
___mkd_freemmiot(&sub, f->footnotes);
}
/*
* check the escape list for special cases
*/
static int
escaped(MMIOT *f, char c)
{
struct escaped *thing = f->esc;
while ( thing ) {
if ( strchr(thing->text, c) )
return 1;
thing = thing->up;
}
return 0;
}
/*
* write out a url, escaping problematic characters
*/
static void
puturl(char *s, int size, MMIOT *f, int display)
{
unsigned char c;
if ( size && s[0] == '<' && s[size-1] == '>' ) {
/* urls encased in <> need to have the <>'s removed */
s++;
size -= 2;
}
while ( size-- > 0 ) {
c = *s++;
if ( c == '\\' && size-- > 0 ) {
c = *s++;
if ( !( ispunct(c) || isspace(c) ) )
Qchar('\\', f);
}
if ( c == '&' )
Qstring("&", f);
else if ( c == '<' )
Qstring("<", f);
else if ( c == '"' )
Qstring("%22", f);
else if ( isalnum(c) || ispunct(c) || (display && isspace(c)) )
Qchar(c, f);
else if ( c == MKD_EOLN ) /* untokenize hard return */
Qstring(" ", f);
else
Qprintf(f, "%%%02X", c);
}
}
/* advance forward until the next character is not whitespace
*/
static int
eatspace(MMIOT *f)
{
int c;
for ( ; ((c=peek(f, 1)) != EOF) && isspace(c); pull(f) )
;
return c;
}
/* (match (a (nested (parenthetical (string.)))))
*/
static int
parenthetical(int in, int out, MMIOT *f)
{
int size, indent, c;
for ( indent=1,size=0; indent; size++ ) {
if ( (c = pull(f)) == EOF )
return EOF;
else if ( (c == '\\') && (peek(f,1) == out || peek(f,1) == in) ) {
++size;
pull(f);
}
else if ( c == in )
++indent;
else if ( c == out )
--indent;
}
return size ? (size-1) : 0;
}
/* extract a []-delimited label from the input stream.
*/
static int
linkylabel(MMIOT *f, Cstring *res)
{
char *ptr = cursor(f);
int size;
if ( (size = parenthetical('[',']',f)) != EOF ) {
T(*res) = ptr;
S(*res) = size;
return 1;
}
return 0;
}
/* see if the quote-prefixed linky segment is actually a title.
*/
static int
linkytitle(MMIOT *f, char quote, Footnote *ref)
{
int whence = mmiottell(f);
char *title = cursor(f);
char *e;
register int c;
while ( (c = pull(f)) != EOF ) {
e = cursor(f);
if ( c == quote ) {
if ( (c = eatspace(f)) == ')' ) {
T(ref->title) = 1+title;
S(ref->title) = (e-title)-2;
return 1;
}
}
}
mmiotseek(f, whence);
return 0;
}
/* extract a =HHHxWWW size from the input stream
*/
static int
linkysize(MMIOT *f, Footnote *ref)
{
int height=0, width=0;
int whence = mmiottell(f);
int c;
if ( isspace(peek(f,0)) ) {
pull(f); /* eat '=' */
for ( c = pull(f); isdigit(c); c = pull(f))
width = (width * 10) + (c - '0');
if ( c == 'x' ) {
for ( c = pull(f); isdigit(c); c = pull(f))
height = (height*10) + (c - '0');
if ( isspace(c) )
c = eatspace(f);
if ( (c == ')') || ((c == '\'' || c == '"') && linkytitle(f, c, ref)) ) {
ref->height = height;
ref->width = width;
return 1;
}
}
}
mmiotseek(f, whence);
return 0;
}
/* extract a <...>-encased url from the input stream.
* (markdown 1.0.2b8 compatibility; older versions
* of markdown treated the < and > as syntactic
* sugar that didn't have to be there. 1.0.2b8
* requires a closing >, and then falls into the
* title or closing )
*/
static int
linkybroket(MMIOT *f, int image, Footnote *p)
{
int c;
int good = 0;
T(p->link) = cursor(f);
for ( S(p->link)=0; (c = pull(f)) != '>'; ++S(p->link) ) {
/* pull in all input until a '>' is found, or die trying.
*/
if ( c == EOF )
return 0;
else if ( (c == '\\') && ispunct(peek(f,2)) ) {
++S(p->link);
pull(f);
}
}
c = eatspace(f);
/* next nonspace needs to be a title, a size, or )
*/
if ( ( c == '\'' || c == '"' ) && linkytitle(f,c,p) )
good=1;
else if ( image && (c == '=') && linkysize(f,p) )
good=1;
else
good=( c == ')' );
if ( good ) {
if ( peek(f, 1) == ')' )
pull(f);
___mkd_tidy(&p->link);
}
return good;
} /* linkybroket */
/* extract a (-prefixed url from the input stream.
* the label is either of the format `<link>`, where I
* extract until I find a >, or it is of the format
* `text`, where I extract until I reach a ')', a quote,
* or (if image) a '='
*/
static int
linkyurl(MMIOT *f, int image, Footnote *p)
{
int c;
int mayneedtotrim=0;
if ( (c = eatspace(f)) == EOF )
return 0;
if ( c == '<' ) {
pull(f);
if ( !is_flag_set(&f->flags, MKD_1_COMPAT) )
return linkybroket(f,image,p);
mayneedtotrim=1;
}
T(p->link) = cursor(f);
for ( S(p->link)=0; (c = peek(f,1)) != ')'; ++S(p->link) ) {
if ( c == EOF )
return 0;
else if ( (c == '"' || c == '\'') && linkytitle(f, c, p) )
break;
else if ( image && (c == '=') && linkysize(f, p) )
break;
else if ( (c == '\\') && ispunct(peek(f,2)) ) {
++S(p->link);
pull(f);
}
pull(f);
}
if ( peek(f, 1) == ')' )
pull(f);
___mkd_tidy(&p->link);
if ( mayneedtotrim && (S(p->link) > 0) && (T(p->link)[S(p->link)-1] == '>') )
--S(p->link);
return 1;
}
/* prefixes for <automatic links>
*/
static struct _protocol {
char *name;
int nlen;
} protocol[] = {
#define _aprotocol(x) { x, (sizeof x)-1 }
_aprotocol( "https:" ),
_aprotocol( "http:" ),
_aprotocol( "news:" ),
_aprotocol( "ftp:" ),
#undef _aprotocol
};
#define NRPROTOCOLS (sizeof protocol / sizeof protocol[0])
static int
isautoprefix(char *text, int size)
{
int i;
struct _protocol *p;
for (i=0, p=protocol; i < NRPROTOCOLS; i++, p++)
if ( (size >= p->nlen) && strncasecmp(text, p->name, p->nlen) == 0 )
return 1;
return 0;
}
/*
* all the tag types that linkylinky can produce are
* defined by this structure.
*/
typedef struct linkytype {
char *pat;
int szpat;
char *link_pfx; /* tag prefix and link pointer (eg: "<a href="\"" */
char *link_sfx; /* link suffix (eg: "\"" */
int WxH; /* this tag allows width x height arguments */
char *text_pfx; /* text prefix (eg: ">" */
char *text_sfx; /* text suffix (eg: "</a>" */
mkd_flag_t flags; /* reparse flags */
int kind; /* tag is url or something else? */
#define IS_URL 0x01
} linkytype;
static linkytype imaget = { 0, 0, "<img src=\"", "\"",
1, " alt=\"", "\" />", { { [MKD_NOIMAGE] = 1,
[MKD_TAGTEXT] = 1,
[MKD_ALT_AS_TITLE] = 1 } }, IS_URL };
static linkytype linkt = { 0, 0, "<a href=\"", "\"",
0, ">", "</a>", { {[MKD_NOLINKS] = 1} }, IS_URL };
/*
* pseudo-protocols for [][];
*
* id: generates <a id="link">tag</a>
* class: generates <span class="link">tag</span>
* raw: just dump the link without any processing
*/
static linkytype specials[] = {
{ "id:", 3, "<span id=\"", "\"", 0, ">", "</span>", {0}, 0 },
{ "raw:", 4, 0, 0, 0, 0, 0, { { [MKD_NOHTML] = 1 } }, 0 },
{ "lang:", 5, "<span lang=\"", "\"", 0, ">", "</span>", {0}, 0 },
{ "abbr:", 5, "<abbr title=\"", "\"", 0, ">", "</abbr>", {0}, 0 },
{ "class:", 6, "<span class=\"", "\"", 0, ">", "</span>", {0}, 0 },
} ;
#define NR(x) (sizeof x / sizeof x[0])
/* see if t contains one of our pseudo-protocols.
*/
static linkytype *
pseudo(Cstring t)
{
int i;
linkytype *r;
for ( i=0, r=specials; i < NR(specials); i++,r++ ) {
if ( (S(t) > r->szpat) && (strncasecmp(T(t), r->pat, r->szpat) == 0) )
return r;
}
return 0;
}
/* print out the start of an `img' or `a' tag, applying callbacks as needed.
*/
static void
printlinkyref(MMIOT *f, linkytype *tag, char *link, int size)
{
char *edit;
if ( is_flag_set(&f->flags, IS_LABEL) )
return;
Qstring(tag->link_pfx, f);
if ( tag->kind & IS_URL ) {
if ( f->cb && f->cb->e_url.func && (edit = (*f->cb->e_url.func)(link, size, f->cb->e_url.data)) ) {
puturl(edit, strlen(edit), f, 0);
if ( f->cb->e_url.free )
(*f->cb->e_url.free)(edit, strlen(edit), f);
}
else
puturl(link + tag->szpat, size - tag->szpat, f, 0);
}
else {
mkd_flag_t tagtext;
mkd_init_flags(&tagtext);
set_mkd_flag(&tagtext, MKD_TAGTEXT);
___mkd_reparse(link + tag->szpat, size - tag->szpat, &tagtext, f, 0);
}
Qstring(tag->link_sfx, f);
if ( f->cb && f->cb->e_flags.func && (edit = (*f->cb->e_flags.func)(link, size, f->cb->e_flags.data)) ) {
Qchar(' ', f);
Qstring(edit, f);
if ( f->cb->e_flags.free )
(*f->cb->e_flags.free) (edit, strlen(edit), f);
}
} /* printlinkyref */
/* helper function for php markdown extra footnotes; allow the user to
* define a prefix tag instead of just `fn`
*/
static char *
p_or_nothing(MMIOT *p)
{
return p->ref_prefix ? p->ref_prefix : "fn";
}
/* php markdown extra/daring fireball style print footnotes
*/
static int
extra_linky(MMIOT *f, Cstring text, Footnote *ref)
{
if ( ref->fn_flags & REFERENCED )
return 0;
if ( is_flag_set(&f->flags, IS_LABEL) )
___mkd_reparse(T(text), S(text), &(linkt.flags), f, 0);
else {
ref->fn_flags |= REFERENCED;
ref->refnumber = ++ f->footnotes->reference;
Qprintf(f, "<sup id=\"%sref:%d\"><a href=\"#%s:%d\" rel=\"footnote\">%d</a></sup>",
p_or_nothing(f), ref->refnumber,
p_or_nothing(f), ref->refnumber, ref->refnumber);
}
return 1;
} /* extra_linky */
/* check a url (or fragment) to see that it begins with a known good
* protocol (or no protocol at all)
*/
static int
safelink(Cstring link)
{
char *p, *colon;
if ( T(link) == 0 ) /* no link; safe */
return 1;
p = T(link);
if ( (colon = memchr(p, ':', S(link))) == 0 )
return 1; /* no protocol specified: safe */
if ( !isalpha(*p) ) /* protocol/method is [alpha][alnum or '+.-'] */
return 1;
while ( ++p < colon )
if ( !(isalnum(*p) || *p == '.' || *p == '+' || *p == '-') )
return 1;
return isautoprefix(T(link), S(link));
}
/* print out a linky (or fail if it's Not Allowed)
*/
static int
linkyformat(MMIOT *f, Cstring text, int image, Footnote *ref)
{
linkytype *tag;
static mkd_flag_t tagtext = { {[MKD_TAGTEXT] = 1} };
if ( image )
tag = &imaget;
else if ( tag = pseudo(ref->link) ) {
if ( is_flag_set(&f->flags, MKD_NO_EXT) || is_flag_set(&f->flags, MKD_STRICT)
|| is_flag_set(&f->flags, MKD_SAFELINK) )
return 0;
}
else if ( is_flag_set(&f->flags, MKD_SAFELINK) && !is_flag_set(&f->flags, MKD_STRICT)
&& !safelink(ref->link) )
/* if MKD_SAFELINK, only accept links that are local or
* a well-known protocol
*/
return 0;
else
tag = &linkt;
if ( ANY_FLAGS(&f->flags, &tag->flags) )
return 0;
if ( is_flag_set(&f->flags, IS_LABEL) )
___mkd_reparse(T(text), S(text), &(tag->flags), f, 0);
else if ( tag->link_pfx ) {
printlinkyref(f, tag, T(ref->link), S(ref->link));
if ( tag->WxH ) {
if ( ref->height ) Qprintf(f," height=\"%d\"", ref->height);
if ( ref->width ) Qprintf(f, " width=\"%d\"", ref->width);
}
if ( S(ref->title) || (is_flag_set(&f->flags, MKD_ALT_AS_TITLE) && is_flag_set(&tag->flags, MKD_ALT_AS_TITLE)) ) {
Qstring(" title=\"", f);
if ( S(ref->title) )
___mkd_reparse(T(ref->title), S(ref->title), &tagtext, f, 0);
else
___mkd_reparse(T(text), S(text), &tagtext, f, 0);
Qchar('"', f);
}
Qstring(tag->text_pfx, f);
___mkd_reparse(T(text), S(text), &(tag->flags), f, 0);
Qstring(tag->text_sfx, f);
}
else
Qwrite(T(ref->link) + tag->szpat, S(ref->link) - tag->szpat, f);
return 1;
} /* linkyformat */
/*
* process embedded links and images
*/
static int
linkylinky(int image, MMIOT *f)
{
int start = mmiottell(f);
Cstring name;
Footnote key, *ref;
int status = 0;
int extra_footnote = 0;
CREATE(name);
memset(&key, 0, sizeof key);
if ( linkylabel(f, &name) ) {
if ( peek(f,1) == '(' ) {
pull(f);
if ( linkyurl(f, image, &key) )
status = linkyformat(f, name, image, &key);
}
else {
int goodlink, implicit_mark = mmiottell(f);
if ( is_flag_set(&f->flags, MKD_EXTRA_FOOTNOTE)
&& !is_flag_set(&f->flags, MKD_STRICT)
&& (!image)
&& S(name)
&& T(name)[0] == '^' ) {
extra_footnote = 1;
goodlink = 1;
}
else {
if ( isspace(peek(f,1)) )
pull(f);
if ( peek(f,1) == '[' ) {
pull(f); /* consume leading '[' */
goodlink = linkylabel(f, &key.tag);
}
else {
/* new markdown implicit name syntax doesn't
* require a second []
*/
mmiotseek(f, implicit_mark);
goodlink = !is_flag_set(&f->flags, MKD_1_COMPAT);
}
}
if ( goodlink ) {
if ( !S(key.tag) ) {
DELETE(key.tag);
T(key.tag) = T(name);
S(key.tag) = S(name);
}
if ( ref = bsearch(&key, T(f->footnotes->note),
S(f->footnotes->note),
sizeof key, (stfu)__mkd_footsort) ) {
if ( extra_footnote )
status = extra_linky(f,name,ref);
else
status = linkyformat(f, name, image, ref);
}
}
}
}
DELETE(name);
___mkd_freefootnote(&key);
if ( status == 0 )
mmiotseek(f, start);
return status;
}
/* write a character to output, doing text escapes ( & -> &,
* > -> > < -> < )
*/
static void
cputc(int c, MMIOT *f)
{
switch (c) {
case '&': Qstring("&", f); break;
case '>': Qstring(">", f); break;
case '<': Qstring("<", f); break;
default : Qchar(c, f); break;
}
}
/*
* convert an email address to a string of nonsense
*/
static void
mangle(char *s, int len, MMIOT *f)
{
while ( len-- > 0 ) {
#if DEBIAN_GLITCH
Qprintf(f, "&#%02d;", *((unsigned char*)(s++)) );
#else
Qstring("&#", f);
Qprintf(f, COINTOSS() ? "x%02x;" : "%02d;", *((unsigned char*)(s++)) );
#endif
}
}
/* nrticks() -- count up a row of tick marks
*/
static int
nrticks(int offset, int tickchar, MMIOT *f)
{
int tick = 0;
while ( peek(f, offset+tick) == tickchar ) tick++;
return tick;
} /* nrticks */
/* matchticks() -- match a certain # of ticks, and if that fails
* match the largest subset of those ticks.
*
* if a subset was matched, return the # of ticks
* that were matched.
*/
static int
matchticks(MMIOT *f, int tickchar, int ticks, int *endticks)
{
int size, count, c;
int subsize=0, subtick=0;
*endticks = ticks;
for (size = 0; (c=peek(f,size+ticks)) != EOF; size ++) {
if ( (c == tickchar) && ( count = nrticks(size+ticks,tickchar,f)) ) {
if ( count == ticks )
return size;
else if ( count ) {
if ( (count > subtick) && (count < ticks) ) {
subsize = size;
subtick = count;
}
size += count;
}
}
}
if ( subsize ) {
*endticks = subtick;
return subsize;
}
return 0;
} /* matchticks */
/* code() -- write a string out as code. The only characters that have
* special meaning in a code block are * `<' and `&' , which
* are /always/ expanded to < and &
*/
static void
code(MMIOT *f, char *s, int length)
{
int i,c;
for ( i=0; i < length; i++ )
if ( (c = s[i]) == MKD_EOLN) /* expand back to 2 spaces */
Qstring(" ", f);
else if ( c == '\\' && (i < length-1) && escaped(f, s[i+1]) )
cputc(s[++i], f);
else
cputc(c, f);
} /* code */
/* delspan() -- write out a chunk of text, blocking with <del>...</del>
*/
static void
delspan(MMIOT *f, int size)
{
Qstring("<del>", f);
___mkd_reparse(cursor(f)-1, size, NULL, f, 0);
Qstring("</del>", f);
}
/*
* LaTeXspan() -- write out a chunk of text as a section of (unmangled)
* input for a LaTeX preprocessor
*/
static void
LaTeXspan(MMIOT *f, int size)
{
Qchar('$', f);
if ( size > 0 )
code(f, cursor(f)-1, size);
Qchar('$', f);
}
/* codespan() -- write out a chunk of text as code, trimming one
* space off the front and/or back as appropriate.
*/
static void
codespan(MMIOT *f, int size)
{
int i=0;
if ( size > 1 && peek(f, size-1) == ' ' ) --size;
if ( peek(f,i) == ' ' ) ++i, --size;
Qstring("<code>", f);
code(f, cursor(f)+(i-1), size);
Qstring("</code>", f);
} /* codespan */
/* before letting a tag through, validate against
* MKD_NOLINKS and MKD_NOIMAGE
*/
static int
forbidden_tag(MMIOT *f)
{
int c = toupper(peek(f, 1));
if ( is_flag_set(&f->flags, MKD_NOHTML) )
return 1;
if ( c == 'A' && is_flag_set(&f->flags, MKD_NOLINKS) && !isthisalnum(f,2) )
return 1;
if ( c == 'I' && is_flag_set(&f->flags, MKD_NOIMAGE)
&& strncasecmp(cursor(f)+1, "MG", 2) == 0
&& !isthisalnum(f,4) )
return 1;
return 0;
}
/* Check a string to see if it looks like a mail address
* "looks like a mail address" means alphanumeric + some
* specials, then a `@`, then alphanumeric + some specials,
* but with a `.`
*/
static int
maybe_address(char *p, int size)