ssnail

crappy and opinionated static site generator
git clone https://git.e1e0.net/ssnail.git
Log | Files | Refs | README | LICENSE

commit b84e437384d5d18cdb9e31f039535fe4c1840935
parent c80fe54bfc37aa766997bf7a999813ea4452937c
Author: Paco Esteban <paco@e1e0.net>
Date:   Sun, 24 May 2020 18:20:47 +0200

header and footer can be specified at program call

They have defaults if nothing is passed.

Diffstat:
Mssnail.c | 33+++++++++++++++++++--------------
1 file changed, 19 insertions(+), 14 deletions(-)

diff --git a/ssnail.c b/ssnail.c @@ -54,7 +54,7 @@ int build_full_path(char *, char *, char *); int load_from_file(char **, char *); void usage(); int populate_article_entry(struct article *); -int write_html(struct article *); +int write_html(struct article *, char *, char *); void articleq_free(struct listhead *); int @@ -63,10 +63,13 @@ main(int argc, char *argv[]) DIR *dirp; struct dirent *dp; int ch; - char srcdir[PATH_MAX] = "", dstdir[PATH_MAX] = "", *fbuf = NULL; + char srcdir[PATH_MAX] = "", dstdir[PATH_MAX] = "", + header_tpl[PATH_MAX] = "./_header.html", + footer_tpl[PATH_MAX] = "./_footer.html", + *fbuf = NULL; struct article *ap = NULL; - while ((ch = getopt(argc, argv, "s:d:")) != -1) { + while ((ch = getopt(argc, argv, "s:d:H:F:")) != -1) { switch (ch) { case 's': (void)strlcpy(srcdir, optarg, PATH_MAX); @@ -74,6 +77,12 @@ main(int argc, char *argv[]) case 'd': (void)strlcpy(dstdir, optarg, PATH_MAX); break; + case 'H': + (void)strlcpy(header_tpl, optarg, PATH_MAX); + break; + case 'F': + (void)strlcpy(footer_tpl, optarg, PATH_MAX); + break; default: usage(); } @@ -109,7 +118,7 @@ main(int argc, char *argv[]) SLIST_FOREACH(ap, &head, entries) { if (ap->src_mtime > ap->dst_mtime) { printf("... %s\n", ap->dst_path); - if (write_html(ap) == -1) + if (write_html(ap, header_tpl, footer_tpl) == -1) err(3, "cannot write html file"); } } @@ -246,10 +255,9 @@ out: } int -write_html(struct article *ap) +write_html(struct article *ap, char head_tpl[PATH_MAX], char foot_tpl[PATH_MAX]) { - char *head_tpl = NULL, *foot_tpl = NULL, - *header = NULL, *footer = NULL; + char *header = NULL, *footer = NULL; FILE *fout; int r = -1; @@ -258,13 +266,12 @@ write_html(struct article *ap) || strlen(ap->date) == 0) goto out; - load_from_file(&head_tpl, "_header.html"); - header = str_rep(head_tpl, "$title$", ap->title); + load_from_file(&header, head_tpl); + header = str_rep(header, "$title$", ap->title); header = str_rep(header, "$author$", ap->author); header = str_rep(header, "$date$", ap->date); - load_from_file(&foot_tpl, "_footer.html"); - footer = str_rep(foot_tpl, "$title$", ap->title); - + load_from_file(&footer, foot_tpl); + footer = str_rep(footer, "$title$", ap->title); if ((fout = fopen(ap->dst_path, "w")) == NULL) goto out; @@ -275,10 +282,8 @@ write_html(struct article *ap) r = 0; out: - free(head_tpl); free(header); free(footer); - free(foot_tpl); return r; }