/* * process embedded links and images */ static int linkylinky(int image, MMIOT *f) { int start = mmiottell(f); Cstring name; Footnote key, *ref; int status = 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 ( 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 = !(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), S(*f->footnotes), sizeof key, (stfu)__mkd_footsort) ) status = linkyformat(f, name, image, ref); else if ( f->flags & IS_LABEL ) status = linkyformat(f, name, image, &imaget); } } } DELETE(name); ___mkd_freefootnote(&key); if ( status == 0 ) mmiotseek(f, start); return status; }
/* look up (or construct) a footnote from the [xxx] link * at the head of the stream. */ static int linkykey(int image, Footnote *val, MMIOT *f) { Footnote *ret; Cstring mylabel; memset(val, 0, sizeof *val); if ( (T(val->tag) = linkylabel(f, &S(val->tag))) == 0 ) return 0; eatspace(f); switch ( pull(f) ) { case '(': /* embedded link */ if ( (T(val->link) = linkyurl(f,&S(val->link))) == 0 ) return 0; if ( image && !linkysize(f, &val->height, &val->width) ) return 0; T(val->title) = linkytitle(f, &S(val->title)); return peek(f,0) == ')'; case '[': /* footnote link */ mylabel = val->tag; if ( (T(val->tag) = linkylabel(f, &S(val->tag))) == 0 ) return 0; if ( !S(val->tag) ) val->tag = mylabel; ret = bsearch(val, T(*f->footnotes), S(*f->footnotes), sizeof *val, (stfu)__mkd_footsort); if ( ret ) { val->tag = mylabel; val->link = ret->link; val->title = ret->title; val->height = ret->height; val->width = ret->width; return 1; } } return 0; }