ssnail

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

commit a8d0c7cfea03e98cbb6c5b8f0177e55e2e26c2f6
parent f6268f26f81207ec80260488671e29669399460b
Author: Paco Esteban <paco@e1e0.net>
Date:   Wed, 24 Jun 2020 17:25:51 +0200

move all stuff related to populate an article to populate_article

Where it should have been since the begining.

Diffstat:
Mssnail.c | 89++++++++++++++++++++++++++++++++++++++++++-------------------------------------
1 file changed, 47 insertions(+), 42 deletions(-)

diff --git a/ssnail.c b/ssnail.c @@ -56,8 +56,9 @@ SLIST_HEAD(listhead, article) head; __dead static void usage(void); static void articleq_free(struct listhead *); +static struct article *populate_article_entry(char *, char *); + static const struct ssnail_error *process_dir(char *, char *, int); -static const struct ssnail_error *populate_article_entry(struct article *); static const struct ssnail_error *write_html(struct article *, char *, char *); static int gen_html(struct article *); @@ -306,38 +307,63 @@ out: return error; } -static const struct ssnail_error * -populate_article_entry(struct article *ap) +static struct article * +populate_article_entry(char *src_path, char *dst_path) { - const struct ssnail_error *error = NULL; - struct stat st; - int len = 0; + struct article *ap = malloc(sizeof(struct article)); + struct stat st; + int len = 0; - if (stat(ap->src_path, &st) == -1) { - error = ssnail_error_from_errno("src_path"); - goto out; + if (ap == NULL) { + goto error; } + + ap->src_path = strndup(src_path, PATH_MAX); + if (ap->src_path == NULL) + goto error; + + char *fbuf = NULL; + fbuf = str_rep(dst_path, ".md", ".html"); + + ap->dst_path = strndup(fbuf, PATH_MAX); + if (ap->dst_path == NULL) + goto error; + + if (stat(ap->src_path, &st) == -1) + goto error; ap->src_mtime = st.st_mtim.tv_sec; if (stat(ap->dst_path, &st) == -1) { /* dst file does not have to exist */ - if (errno != ENOENT) { - error = ssnail_error_from_errno("dst_path"); - goto out; - } + if (errno != ENOENT) + goto error; ap->dst_mtime = 0; } else { ap->dst_mtime = st.st_mtim.tv_sec; } - if ((ap->origz = load_from_file(&ap->orig_content, ap->src_path)) == 0) { - error = ssnail_error_from_errno("load orig"); - goto out; - } + if ((ap->origz = load_from_file(&ap->orig_content, ap->src_path)) == 0) + goto error; if (gen_html(ap) != 0) - error = ssnail_error_msg(2, "gen_html"); + goto error; out: - return error; + free(fbuf); + fbuf = NULL; + return ap; +error: + free(fbuf); + fbuf = NULL; + if (ap->src_path != NULL) + free(ap->src_path); + if (ap->dst_path != NULL) + free(ap->dst_path); + /* if (ap->orig_content != NULL) */ + /* free(ap->orig_content); */ + /* if (ap->html_content != NULL) */ + /* free(ap->html_content); */ + if (ap != NULL) + free(ap); + return NULL; } static const struct ssnail_error * @@ -376,32 +402,11 @@ process_dir(char *src, char *dst, int force) if (dp->d_type == DT_REG) { if (strcmp(get_filename_ext(src_path), "md") == 0) { - a = malloc(sizeof(struct article)); + a = populate_article_entry(src_path, dst_path); if (a == NULL) { - error = ssnail_error_from_errno("malloc a"); - goto out; - } - - a->src_path = strndup(src_path, PATH_MAX); - if (a->src_path == NULL) { - error = ssnail_error_from_errno("malloc str_path"); - goto out; - } - - char *fbuf = NULL; - fbuf = str_rep(dst_path, ".md", ".html"); - - a->dst_path = strndup(fbuf, PATH_MAX); - if (a->dst_path == NULL) { - error = ssnail_error_from_errno("malloc dst_path"); - free(fbuf); + error = ssnail_error_from_errno("populate"); goto out; } - free(fbuf); - - error = populate_article_entry(a); - if (error) - goto out; SLIST_INSERT_HEAD(&head, a, entries); } else { error = copy_file(src_path, dst_path, force);