From 5c47ef5a148f9eeaab5869add7ce5c30203fee7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Ma=C5=A1karinec?= Date: Tue, 9 May 2023 09:25:51 +0200 Subject: [PATCH] add custom content support --- stagal.c | 39 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/stagal.c b/stagal.c index 077f39c..c8d84f3 100644 --- a/stagal.c +++ b/stagal.c @@ -2,6 +2,8 @@ // public domain // by Marek Maskarinec +#include +#include #include #include #include @@ -20,12 +22,12 @@ const char *page = "img {" "width: 90vw;" - "height: 90vh;" + "height: 80vh;" "object-fit: contain;" "}" "table {" - "height: 90vh;" + "height: 80vh;" "}" "/* Custom CSS */" @@ -36,29 +38,19 @@ const char *page = "" "" "" - "" + "" "" "
<>
" + "%s" ""; static char *pagename(char *img) { char *s = calloc(sizeof(char), strlen(img) + strlen(".html") + 1); + assert(s != NULL); strcpy(s, img); - - char *p = s + strlen(img); - while (p != s) { - if (*p == '.') - break; - - --p; - } - - if (p == s) // the image has no extension - strcat(s, ".html"); - else - strcpy(p, ".html"); + strcat(s, ".html"); return s; } @@ -81,7 +73,7 @@ char *readall(char *path) { char *buf = malloc(siz + 1); buf[siz - 1] = 0; - fread(buf, 1, siz, f); + siz = fread(buf, 1, siz, f); fclose(f); return buf; @@ -92,9 +84,10 @@ extern int optind; int main(int argc, char *argv[]) { char *css = NULL; + char *custom = NULL; char ch; - while ((ch = getopt(argc, argv, "hs:")) >= 0) { + while ((ch = getopt(argc, argv, "hs:c:")) >= 0) { switch (ch) { case 's':; if (!(css = readall(optarg))) { @@ -104,11 +97,16 @@ int main(int argc, char *argv[]) { break; + case 'c': + custom = optarg; + break; + case '?': fprintf(stderr, "invalid option.\n"); case 'h': default: fprintf(stderr, "-s path - use custom css\n"); + fprintf(stderr, "-c string - add custom content bello the image\n"); return 1; } } @@ -128,12 +126,13 @@ int main(int argc, char *argv[]) { FILE *f = fopen(name, "w"); if (!f) { - fprintf(stderr, "ERROR: file %s not found\n", name); + fprintf(stderr, "stagal: %s, %s\n", name, strerror(errno)); goto fail; } - fprintf(f, page, css ? css : "", prev, argv[i], next); + fprintf(f, page, css ? css : "", + prev, argv[i], next, custom ? custom : ""); fclose(f); free(prev); -- 2.45.2