smu

Simple markup processor
git clone https://git.sinitax.com/codemadness/smu
Log | Files | Refs | README | LICENSE | Upstream | sfeed.txt

commit 7f666f693c45969c522075cf27c490c44a9b4d91
parent e227c9c980f115f812dde257c6959d712745738c
Author: gottox@rootkit.lan <gottox@rootkit.lan>
Date:   Tue, 11 Dec 2007 23:17:37 +0100

(semi-)proper HTML-escaping; some cleanups

Diffstat:
Mcmarkdown.c | 27++++++++++++++++++++++-----
1 file changed, 22 insertions(+), 5 deletions(-)

diff --git a/cmarkdown.c b/cmarkdown.c @@ -21,6 +21,7 @@ struct Tag { void eprint(const char *format, ...); /* Prints error and exits */ void hprint(const char *begin, const char *end); /* escapes HTML and prints it to stdout*/ +unsigned int doamp(const char *begin, const char *end); /* Parser for & */ unsigned int dolineprefix(const char *begin, const char *end); /* Parser for line prefix tags */ unsigned int dolink(const char *begin, const char *end); @@ -40,7 +41,7 @@ unsigned int dounderline(const char *begin, const char *end); void process(const char *begin, const char *end); /* Processes range between begin and end. */ Parser parsers[] = { dounderline, dolineprefix, dolist, doparagraph, - dosurround, dolink, doshortlink, doreplace }; /* list of parsers */ + dosurround, dolink, doshortlink, doamp, doreplace }; /* list of parsers */ FILE *source; unsigned int bsize = 0, nohtml = 0; struct Tag lineprefix[] = { @@ -77,6 +78,8 @@ char * replace[][2] = { { " ###\n", "\n" }, { " ##\n", "\n" }, { " #\n", "\n" }, + { " >", "&gt;" }, + { "< ", "&lt;" }, }; char * insert[][2] = { { " \n", "<br />" }, @@ -106,6 +109,20 @@ hprint(const char *begin, const char *end) { } unsigned int +doamp(const char *begin, const char *end) { + const char *p; + + if(*begin != '&') + return 0; + if(!nohtml) { + for(p = begin + 1; !strchr("; \\\n\t",*p); p++); + if(*p == ';') + return 0; + } + fputs("&amp;",stdout); + return 1; +} +unsigned int dolineprefix(const char *begin, const char *end) { unsigned int i, j, l; char *buffer; @@ -395,15 +412,15 @@ process(const char *begin, const char *end) { affected = 0; for(i = 0; i < LENGTH(parsers) && affected == 0; i++) affected = parsers[i](p, end); - if(affected == 0) { + if(affected) + p += affected; + else { if(nohtml) hprint(p,p+1); else putchar(*p); p++; } - else - p += affected; } } @@ -425,7 +442,7 @@ main(int argc, char *argv[]) { eprint("Malloc failed."); bsize = BUFFERSIZE; /* needed to properly process first line */ - strcpy(buffer,"\n"); + strcpy(buffer,"\n\n"); p = buffer+strlen(buffer); while(s = fread(p, sizeof(char),BUFFERSIZE, source)) {