/* matchticks() -- match a certain # of ticks, and if that fails * match the largest subset of those ticks. * * if a subset was matched, modify the passed in * # of ticks so that the caller (text()) can * appropriately process the horrible thing. */ static int matchticks(MMIOT *f, int *ticks) { int size, tick, c; int subsize=0, subtick=0; for (size = *ticks; (c=peek(f,size)) != EOF; ) { if ( c == '`' ) if ( (tick=nrticks(size,f)) == *ticks ) return size; else { if ( tick > subtick ) { subsize = size; subtick = tick; } size += tick; } else size++; } if ( subsize ) { *ticks = subtick; return subsize; } return 0; } /* matchticks */
/* 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 */
/* process a body of text encased in some sort of tick marks. If it * works, generate the output and return 1, otherwise just return 0 and * let the caller figure it out. */ static int tickhandler(MMIOT *f, int tickchar, int minticks, spanhandler spanner) { int endticks, size; int tick = nrticks(0, tickchar, f); if ( (tick >= minticks) && (size = matchticks(f,tickchar,tick,&endticks)) ) { if ( endticks < tick ) { size += (tick - endticks); tick = endticks; } shift(f, tick); (*spanner)(f,size); shift(f, size+tick-1); return 1; } return 0; }
static void text(MMIOT *f) { int c, j; int rep; int smartyflags = 0; while (1) { if ( (f->flags & AUTOLINK) && isalpha(peek(f,1)) && !tag_text(f) ) maybe_autolink(f); c = pull(f); if (c == EOF) break; if ( smartypants(c, &smartyflags, f) ) continue; switch (c) { case 0: break; case 3: Qstring(tag_text(f) ? " " : "<br/>", f); break; case '>': if ( tag_text(f) ) Qstring(">", f); else Qchar(c, f); break; case '"': if ( tag_text(f) ) Qstring(""", f); else Qchar(c, f); break; case '!': if ( peek(f,1) == '[' ) { pull(f); if ( tag_text(f) || !linkylinky(1, f) ) Qstring("![", f); } else Qchar(c, f); break; case '[': if ( tag_text(f) || !linkylinky(0, f) ) Qchar(c, f); break; #if SUPERSCRIPT /* A^B -> A<sup>B</sup> */ case '^': if ( (f->flags & (STRICT|INSIDE_TAG)) || isthisspace(f,-1) || isthisspace(f,1) ) Qchar(c,f); else { char *sup = cursor(f); int len = 0; Qstring("<sup>",f); while ( !isthisspace(f,1+len) ) { ++len; } shift(f,len); ___mkd_reparse(sup, len, 0, f); Qstring("</sup>", f); } break; #endif case '_': #if RELAXED_EMPHASIS /* Underscores don't count if they're in the middle of a word */ if ( !(f->flags & STRICT) && isthisalnum(f,-1) && isthisalnum(f,1) ) { Qchar(c, f); break; } #endif case '*': /* Underscores & stars don't count if they're out in the middle * of whitespace */ if ( isthisspace(f,-1) && isthisspace(f,1) ) { Qchar(c, f); break; } /* else fall into the regular old emphasis case */ if ( tag_text(f) ) Qchar(c, f); else { for (rep = 1; peek(f,1) == c; pull(f) ) ++rep; Qem(f,c,rep); } break; case '`': if ( tag_text(f) ) Qchar(c, f); else { int size, tick = nrticks(0, f); if ( size = matchticks(f, &tick) ) { shift(f, tick); codespan(f, size-tick); shift(f, size-1); } else { Qchar(c, f); Qcopy(tick-1, f); } } break; case '\\': switch ( c = pull(f) ) { case '&': Qstring("&", f); break; case '<': Qstring("<", f); break; case '>': case '#': case '.': case '-': case '+': case '{': case '}': case ']': case '!': case '[': case '*': case '_': case '\\':case '(': case ')': case '`': Qchar(c, f); break; default: Qchar('\\', f); if ( c != EOF ) shift(f,-1); break; } break; case '<': if ( !maybe_tag_or_link(f) ) Qstring("<", f); break; case '&': j = (peek(f,1) == '#' ) ? 2 : 1; while ( isthisalnum(f,j) ) ++j; if ( peek(f,j) != ';' ) Qstring("&", f); else Qchar(c, f); break; default: Qchar(c, f); break; } } /* truncate the input string after we've finished processing it */ S(f->in) = f->isp = 0; } /* text */