bpaul-webgen

html generator for bpaul.xyz
Log | Files | Refs | README

commit 4068d88f411c38ea73db2b4e5871a311c86cfa50
parent 0dea7100b0dabcbe8458ad1f913a194fa7ad7e97
Author: benjamin paul <bpaul@bpaul.xyz>
Date:   Thu,  5 Aug 2021 21:54:28 +1000

ok writes to file now!

Diffstat:
Mmain.c | 114++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------------------
1 file changed, 81 insertions(+), 33 deletions(-)

diff --git a/main.c b/main.c @@ -6,6 +6,34 @@ #include <sys/stat.h> #include <unistd.h> +const char start_html[] = "<html>" + "<head>" + "<meta charset=\"UTF-8\">" + "<style>" + "a{" + "color:inherit;" + "outline:0;" + "text-decoration:inherit;" + "}" + "" + "body{" + "font-family:monospace;" + "color:white;" + "background-color:black;" + "}" + "" + "pre{" + "font-family:monospace;" + "}" + "</style>" + "</head>" + "<body>" + "<pre>\n"; + +const char end_html[] = "</pre>" + "</body>" + "</html>\n"; + struct links_entry { char *href; int minx, miny; @@ -15,9 +43,10 @@ struct links_entry { size_t line_count(const char *buf) { const char *ptr = buf; - size_t cnt = 0; + size_t cnt = 0; while (*ptr != '\0') { - if (*ptr == '\n') cnt++; + if (*ptr == '\n') + cnt++; ptr++; } return cnt; @@ -34,13 +63,13 @@ fsize(FILE *f) { char * add_path(const char *dir, const char *name) { - size_t len = strlen(dir) + strlen(name) + 1; - char *buf = malloc(len); - memcpy(buf, dir, len); + size_t len = strlen(dir) + strlen(name) + 1; + char *buf = malloc(len); + memcpy(buf, dir, len); - strcat(buf, name); + strcat(buf, name); - return buf; + return buf; } void @@ -59,25 +88,31 @@ iterate(DIR *d, const char *curdir) { } /* Skip dotfiles */ - if (entry->d_name[0] == '.') { goto skip; } + if (entry->d_name[0] == '.') { + goto skip; + } if (entry->d_type == DT_REG) { /* Only want files without file extensions */ - if (strrchr(entry->d_name, '.') != NULL) { goto skip; } + if (strrchr(entry->d_name, '.') != NULL) { + goto skip; + } - char *buf = add_path(curdir, entry->d_name); + char *buf = add_path(curdir, entry->d_name); char *link = add_path(buf, ".links"); char *html = add_path(buf, ".html"); /* Only want files which have a .links file */ - if (access(link, F_OK) != 0) { goto skip; } + if (access(link, F_OK) != 0) { + goto skip; + } /* Read the .links file */ char *links_buf; size_t len; FILE *linkf = fopen(link, "r"); - len = fsize(linkf); - links_buf = malloc(len); + len = fsize(linkf); + links_buf = malloc(len); fread(links_buf, 1, len, linkf); fclose(linkf); @@ -86,8 +121,7 @@ iterate(DIR *d, const char *curdir) { entry_cnt = line_count(links_buf); /* Store each entry in the .links file into this struct array */ - struct links_entry *entries = - malloc(sizeof(struct links_entry) * entry_cnt); + struct links_entry *entries = malloc(sizeof(struct links_entry) * entry_cnt); for (size_t i = 0; i < entry_cnt; i++, links_buf = NULL) { entries[i].href = strtok(links_buf, " \n"); @@ -98,36 +132,50 @@ iterate(DIR *d, const char *curdir) { } /* Read ascii file and add links when needed */ - FILE *f = fopen(buf, "r"); - char *c = malloc(1); - len = fsize(f); - int x=1, y=1; + FILE *f = fopen(buf, "r"); + FILE *htmlf = fopen(html, "w"); + char *c = malloc(1); + len = fsize(f); + int x = 1, y = 1; int opened = 0; + + /* Print html */ + fprintf(htmlf, start_html); for (size_t j = 0; j < len; j++) { fread(c, 1, 1, f); for (size_t i = 0; i < entry_cnt; i++) { - if (y >= entries[i].miny - && y <= entries[i].maxy - && x == entries[i].minx) { - printf("<a href=\"%s\">", entries[i].href); + /* If there is either a start or end of a link, print it */ + if (y >= entries[i].miny && y <= entries[i].maxy + && x == entries[i].minx) { + fprintf(htmlf, "<a href=\"%s\">", entries[i].href); opened = i; - } else if (y >= entries[i].miny - && y <= entries[i].maxy - && x == entries[i].maxx+1) { - printf("</a>"); + } else if (y >= entries[i].miny && y <= entries[i].maxy + && x == entries[i].maxx + 1) { + fprintf(htmlf, "</a>"); opened = 0; } } x++; if (*c == '\n') { + /* If a link was opened but not closed, there was not enough + * space to put the close part. + * Add more space so that it can be closed properly */ if (opened) { - while (x <= entries[opened].maxx+1) { putchar(' '); x++; } - printf("</a>"); + while (x <= entries[opened].maxx + 1) { + fputc(' ', htmlf); + x++; + } + fprintf(htmlf, "</a>"); } - x=1; y++; + x = 1; + y++; } - putchar(*c); + fputc(*c, htmlf); } + fprintf(htmlf, end_html); + + fclose(f); + fclose(htmlf); free(buf); free(link); @@ -142,8 +190,8 @@ iterate(DIR *d, const char *curdir) { DIR *newd; if ((newd = opendir((buf))) == NULL) { - perror("opendir"); - return; + perror("opendir"); + return; } /* Iterate */