commit f34ed785bd5ed318fa98d3dfcf468d1658de4e46
parent 14d6d23ffbd0c40277b27797b99f224caa8adaa9
Author: gearsix <gearsix@tuta.io>
Date: Wed, 30 Jun 2021 20:10:58 +0100
Merge branch 'master' of https://notabug.org/gearsix/txt2html
Diffstat:
1 file changed, 9 insertions(+), 21 deletions(-)
diff --git a/txt2html.c b/txt2html.c
@@ -44,7 +44,7 @@ int main(int argc, char **argv)
struct node *n = txt2html(text);
while(n != NULL) {
if (n->buf != NULL)
- printf(n->buf);
+ printf("%02x='%s'\n", n->type, n->buf);
n = n->next;
}
@@ -171,41 +171,28 @@ struct node *newnode(struct node *prev, uint8_t tag, struct node *n)
n->prev = prev;
n->type = tag;
switch(tag) {
- case OPEN+H1:
- n->buf = malloc(5);
- strncat(n->buf, "<h1>", 5);
- break;
- case OPEN+H2:
- n->buf = malloc(5);
- strncat(n->buf, "<h2>", 5);
- break;
- case OPEN+P:
- n->buf = malloc(4);
- strncat(n->buf, "<p>", 4);
- break;
+ case OPEN+H1: n->buf = "<h1>\0"; break;
+ case OPEN+H2: n->buf = "<h2>\0"; break;
+ case OPEN+P: n->buf = "<p>\0"; break;
case CLOSE+H1:
if (prev != NULL && prev->type == H1)
writebuf(prev, EOF);
- n->buf = malloc(7);
- strncat(n->buf, "</h1>\n", 7);
+ n->buf = "</h1>\n\0";
break;
case CLOSE+H2:
if (prev != NULL && prev->type == H2)
writebuf(prev, EOF);
- n->buf = malloc(7);
- strncat(n->buf, "</h2>\n", 7);
+ n->buf = "</h2>\n\0";
break;
case CLOSE+P:
if (prev != NULL && prev->type == P)
writebuf(prev, EOF);
- n->buf = malloc(6);
- strncat(n->buf, "</p>\n", 6);
+ n->buf = "</p>\n\0";
break;
case OPEN+BR+CLOSE:
if (prev != NULL && prev->type == P)
writebuf(prev, EOF);
- n->buf = malloc(7);
- strncat(n->buf, "<br/>\n", 7);
+ n->buf = "<br/>\n\0";
break;
default:
break;
@@ -223,6 +210,7 @@ void writebuf(struct node *n, int c)
static char buf[BUFSIZ];
if (len+1 == BUFSIZ || c == EOF) {
+ buf[len++] = '\0';
n->buf = (pag == 0) ? malloc(len) : realloc(n->buf, strlen(n->buf) + len);
memmove(n->buf, buf, len);
++pag;