#include "config.h" #include #include #include #include #include #include "formatting.h" #include "indexer.h" /* * formatting: strip out all <>'s (by quoting them), replace * *text* with text, replace _text_ with text, * replace {pic:text} with , replace [[ ... ]] * with
..
*/ char *fetch(char*); void format(FILE *f, char *text, int flags) { char *p, *q; int newpara = 1; int didpara = 0; char *para; int bold = 0; /* 0 = not bold, 1 = *bold*, 2 = _bold_ */ int quote = 0; if (flags & FM_COOKED) { fputs(text, f); return; } if ( (flags & FM_BLOCKED) ) { para = isspace(text[0]) ? "blockquote" : "p"; fprintf(f, "<%s>\n", para); didpara=1; } for (p = text; flags & FM_ONELINE ? (*p != '\n') : (*p); ++p) { if ( (*p == '\n') && (p[1] == '\n') ) { ++p; if (!newpara) { if (didpara) fprintf(f, "\n", para); para = (p[1] == '\t' || p[1] == ' ') ? "blockquote" : "p"; fprintf(f, "<%s>\n", para); newpara = 1; didpara = 1; continue; } } newpara = 0; if (*p == '*') { if ( (bold & 1) && !isspace(p[-1]) ) { if ( !(flags & FM_STRIP) ) fputs("", f); bold &= ~1; } else if ( ((bold &1) == 0) && isalnum(p[1])) { if ( !(flags & FM_STRIP) ) fputs("", f); bold |= 1; } else putchar(*p); } else if (*p == '_') { if ( (bold & 2) && !isspace(p[-1]) ) { if ( !(flags & FM_STRIP) ) fputs("", f); bold &= ~2; } else if ( ((bold & 2) == 0) && isalnum(p[1])) { if ( !(flags & FM_STRIP) ) fputs("", f); bold |= 2; } else fputc(*p, f); } else if (*p == '\\' && 1[p]) fputc(*++p, f); else if (*p == '<' && !(isalpha(p[1]) || p[1] == '/') ) fputs( (flags & FM_STRIP) ? "?" : "<",f); else if (*p == '&' && isspace(p[1])) fputs( (flags & FM_STRIP) ? "?" : "&",f); else if (flags & FM_STRIP) { if (*p == '&') { while (*p && *p != ';') ++p; fputc('?',f); } else if (*p == '<') { while (*p && *p != '>') ++p; } else if ( 0x80 & *p) fputc('?', f); else fputc(*p, f); } else if ( (*p == '{') && (flags & FM_IMAGES) ) { int start; char *align; if (strncasecmp(p, "{pic:", 5) == 0) { start = 5; align=""; } else if (strncasecmp(p, "{left:", 6) == 0) { start = 6; align=" align=left"; } else if (strncasecmp(p, "{right:", 7) == 0) { start = 7; align=" align=right"; } else if (strncasecmp(p, "{http:", 6) == 0) { fputs("', f); if (*p) ++p; if (*p == '{') { ++p; while (*p && *p != '}') fputc(*p++, f); } else fprintf(f, "link"); fputs("", f); continue; } else { fputc(*p, f); continue; } fprintf(f, "\n", align); } else fputc(*p, f); } if (didpara) fprintf(f, "\n", para); } void byline(FILE *f, struct article *art, int with_url) { char *p; if (!art->author) return; fputs(fmt.byline.start, f); fprintf(f, "%s ", art->author); if ( with_url && (p = fetch("_ROOT")) && art->url && art->timeofday) { fprintf(f, "%s\n", p, art->url, ctime(&art->timeofday)); /* put in an edit button. */ } fputs(fmt.byline.end, f); } void subject(FILE *f, char *title) { if (title) { fputs(fmt.title.start, f); format(f, title, 0); fputs(fmt.title.end, f); } } void body(FILE *f, char *text, int flags) { fputs(fmt.body.start, f); format(f, text, flags|FM_BLOCKED); fputs(fmt.body.end, f); } void article(FILE *f, struct article *art, int flags) { if (art->body) { subject(f,art->title); if (fmt.topsig) byline(f, art, !(flags & FM_PREVIEW) ); body(f, art->body, flags); if (!fmt.topsig) byline(f, art, !(flags & FM_PREVIEW) ); } }