/* 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 ( f->flags & IS_LABEL ) return; Qstring(tag->link_pfx, f); if ( tag->kind & IS_URL ) { if ( f->cb && f->cb->e_url && (edit = (*f->cb->e_url)(link, size, f->cb->e_data)) ) { puturl(edit, strlen(edit), f, 0); if ( f->cb->e_free ) (*f->cb->e_free)(edit, f->cb->e_data); } else puturl(link + tag->szpat, size - tag->szpat, f, 0); } else ___mkd_reparse(link + tag->szpat, size - tag->szpat, MKD_TAGTEXT, f, 0); Qstring(tag->link_sfx, f); if ( f->cb && f->cb->e_flags && (edit = (*f->cb->e_flags)(link, size, f->cb->e_data)) ) { Qchar(' ', f); Qstring(edit, f); if ( f->cb->e_free ) (*f->cb->e_free)(edit, f->cb->e_data); } } /* printlinkyref */
/* print out a linky (or fail if it's Not Allowed) */ static int linkyformat(MMIOT *f, Cstring text, int image, Footnote *ref) { linkytype *tag; if ( image ) tag = &imaget; else if ( tag = pseudo(ref->link) ) { if ( f->flags & (NO_PSEUDO_PROTO|SAFELINK) ) return 0; } else if ( (f->flags & SAFELINK) && T(ref->link) && (T(ref->link)[0] != '/') && !isautoprefix(T(ref->link)) ) /* if SAFELINK, only accept links that are local or * a well-known protocol */ return 0; else tag = &linkt; if ( f->flags & tag->flags ) return 0; if ( tag->link_pfx ) { Qstring(tag->link_pfx, f); if ( tag->kind & IS_URL ) { if ( f->base && T(ref->link) && (T(ref->link)[tag->szpat] == '/') ) puturl(f->base, strlen(f->base), f, 0); puturl(T(ref->link) + tag->szpat, S(ref->link) - tag->szpat, f, 0); } else ___mkd_reparse(T(ref->link) + tag->szpat, S(ref->link) - tag->szpat, INSIDE_TAG, f); Qstring(tag->link_sfx, f); if ( tag->WxH && ref->height && ref->width ) { Qprintf(f," height=\"%d\"", ref->height); Qprintf(f, " width=\"%d\"", ref->width); } if ( S(ref->title) ) { Qstring(" title=\"", f); ___mkd_reparse(T(ref->title), S(ref->title), INSIDE_TAG, f); Qchar('"', f); } Qstring(tag->text_pfx, f); ___mkd_reparse(T(text), S(text), tag->flags, f); 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); Footnote link; linkytype *tag; if ( !linkykey(image, &link, f) ) { mmiotseek(f, start); return 0; } if ( image ) tag = &imaget; else if ( (f->flags & NO_PSEUDO_PROTO) || (tag = extratag(link.link)) == 0 ) tag = &linkt; if ( f->flags & tag-> flags ) { mmiotseek(f, start); return 0; } if ( tag->link_pfx ) { Qstring(tag->link_pfx, f); if ( f->base && (T(link.link)[tag->szpat] == '/') ) puturl(f->base, strlen(f->base), f); puturl(T(link.link) + tag->szpat, S(link.link) - tag->szpat, f); Qstring(tag->link_sfx, f); if ( tag->WxH && link.height && link.width ) { Qprintf(f," height=\"%d\"", link.height); Qprintf(f, " width=\"%d\"", link.width); } if ( S(link.title) ) { Qstring(" title=\"", f); reparse(T(link.title), S(link.title), INSIDE_TAG, f); Qchar('"', f); } Qstring(tag->text_pfx, f); reparse(T(link.tag), S(link.tag), tag->flags, f); Qstring(tag->text_sfx, f); } else Qwrite(T(link.link) + tag->szpat, S(link.link) - tag->szpat, f); return 1; }
/* The size-length token at cursor(f) is either a mailto:, an * implicit mailto:, one of the approved url protocols, or just * plain old text. If it's a mailto: or an approved protocol, * linkify it, otherwise say "no" */ static int process_possible_link(MMIOT *f, int size) { int address= 0; int mailto = 0; char *text = cursor(f); if ( f->flags & DENY_A ) return 0; if ( (size > 7) && strncasecmp(text, "mailto:", 7) == 0 ) { /* if it says it's a mailto, it's a mailto -- who am * I to second-guess the user? */ address = 1; mailto = 7; /* 7 is the length of "mailto:"; we need this */ } else address = maybe_address(text, size); if ( address ) { Qstring("<a href=\"", f); if ( !mailto ) { /* supply a mailto: protocol if one wasn't attached */ mangle("mailto:", 7, f); } mangle(text, size, f); Qstring("\">", f); mangle(text+mailto, size-mailto, f); Qstring("</a>", f); return 1; } else if ( isautoprefix(text) ) { Qstring("<a href=\"", f); puturl(text,size,f, 0); Qstring("\">", f); puturl(text,size,f, 1); Qstring("</a>", f); return 1; } return 0; } /* process_possible_link */
/* a < may be just a regular character, the start of an embedded html * tag, or the start of an <automatic link>. If it's an automatic * link, we also need to know if it's an email address because if it * is we need to mangle it in our futile attempt to cut down on the * spaminess of the rendered page. */ static int maybe_tag_or_link(MMIOT *f) { char *text; int c, size, i; int maybetag=1, maybeaddress=0; int mailto; if ( f->flags & INSIDE_TAG ) return 0; for ( size=0; ((c = peek(f,size+1)) != '>') && !isspace(c); size++ ) { if ( ! (c == '/' || isalnum(c) || c == '~') ) maybetag=0; if ( c == '@' ) maybeaddress=1; else if ( c == EOF ) return 0; } if ( size == 0 ) return 0; if ( maybetag || (size >= 3 && strncmp(cursor(f), "!--", 3) == 0) ) { Qstring(forbidden_tag(f) ? "<" : "<", f); while ( ((c = peek(f, 1)) != EOF) && (c != '>') ) cputc(pull(f), f); return 1; } if ( f->flags & DENY_A ) return 0; text = cursor(f); shift(f, size+1); for ( i=0; i < SZAUTOPREFIX; i++ ) if ( strncasecmp(text, autoprefix[i], strlen(autoprefix[i])) == 0 ) { Qstring("<a href=\"", f); puturl(text,size,f); Qstring("\">", f); puturl(text,size,f); Qstring("</a>", f); return 1; } if ( maybeaddress ) { Qstring("<a href=\"", f); if ( (size > 7) && strncasecmp(text, "mailto:", 7) == 0 ) mailto = 7; else { mailto = 0; /* supply a mailto: protocol if one wasn't attached */ mangle("mailto:", 7, f); } mangle(text, size, f); Qstring("\">", f); mangle(text+mailto, size-mailto, f); Qstring("</a>", f); return 1; } shift(f, -(size+1)); return 0; } /* maybe_tag_or_link */