pre2html

Unnamed repository; edit this file to name it for gitweb.
git clone https://logand.com/git/pre2html.git/
Log | Files | Refs

commit 2979e095696aa23e46434354e8518e974551f328
Author: Tomas Hlavaty <tom@logand.com>
Date:   Mon,  5 Sep 2011 23:33:10 +0200

initial commit

Diffstat:
AMakefile | 5+++++
Atext2html.c | 81+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 86 insertions(+), 0 deletions(-)

diff --git a/Makefile b/Makefile @@ -0,0 +1,5 @@ +all: text2html + +text2html: text2html.o + $(CC) -o $@ $< + strip $@ diff --git a/text2html.c b/text2html.c @@ -0,0 +1,81 @@ +#include <stdlib.h> +#include <stdio.h> + +enum { + BLEN = 10240 +}; + +static int line(char *buf, char *end) { + int n = 0; + while(buf < end) { + int c = getchar(); + if(EOF == c) break; + *buf++ = c; + n++; + if('\n' == c) break; + } + return n; +} + +static int head(char *what, char *buf, char *end) { + while(*what && buf < end) { + if(*what++ != *buf++) return 0; + } + return 1; +} + +static char *find(char what, char *buf, char *end) { + for(; buf < end; buf++) + if(what == *buf) return buf; + return NULL; +} + +static void attr(char *buf, char *end) { + for(; buf < end; buf++) + switch(*buf) { + case '"': printf("&quot;"); break; + default: putchar(*buf); + } +} + +static escape(char c) { + switch(c) { + case '<': printf("&lt;"); break; + case '>': printf("&gt;"); break; + case '&': printf("&amp;"); break; + case '"': printf("&quot;"); break; + default: putchar(c); + } +} + +static void text(char *buf, char *end) { + for(; buf < end; buf++) + escape(*buf); +} + +int main() { + char buf[BLEN]; + char *end = buf + BLEN; + int n; + puts("<pre>"); + while(0 < (n = line(buf, end))) { + char *b = buf; + char *e = buf + n; + while(b < e) { + if(head("<http:", b, e) || head("<https:", b, e)) { + escape(*b++); + char *n = find('>', b, e); + printf("<a href=\""); + attr(b, n); + printf("\">"); + text(b, n); + printf("</a>"); + b = n; + escape(*b++); + } + else escape(*b++); + } + } + puts("</pre>"); + return 0; +}