commit 1db68eb6d9108721555ac7195dd5b92ce1f494b5
parent 4c4faabeeecdbdbeb9af31fe1d755a43f0c55243
Author: Paco Esteban <paco@e1e0.net>
Date: Tue, 19 May 2020 20:25:25 +0200
implement write generated html to dest files
Diffstat:
M | ssnail.c | | | 83 | ++++++++++++++++++++++++++++++++++++++++++++++--------------------------------- |
1 file changed, 48 insertions(+), 35 deletions(-)
diff --git a/ssnail.c b/ssnail.c
@@ -51,6 +51,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
main(int argc, char *argv[])
@@ -100,18 +101,13 @@ main(int argc, char *argv[])
}
}
+ printf("Generating html files ... \n");
SLIST_FOREACH(ap, &head, entries) {
- printf("%s (%lld) --> %s (%lld)\n",
- ap->src_path, ap->src_mtime,
- ap->dst_path, ap->dst_mtime
- );
- /* printf("----\n%s\n----\nsize(%zd)\n", */
- /* ap->orig_content, ap->origz); */
- printf("- Title: %s\n", ap->title);
- printf("- Author: %s\n", ap->author);
- printf("- Date: %s\n", ap->date);
- printf("----\n%s\n----\nsize(%zd)\n",
- ap->html_content, ap->htmlz);
+ if (ap->src_mtime > ap->dst_mtime) {
+ printf("... %s\n", ap->dst_path);
+ if (write_html(ap) == -1)
+ err(3, "cannot write html file");
+ }
}
closedir(dirp);
@@ -179,12 +175,11 @@ gen_html(char **dest, size_t *destz,
char **title, char **author, char **date,
char *orig, size_t origz)
{
- char *head_tpl = NULL, *foot_tpl = NULL,
- *header = NULL, *footer = NULL;
struct lowdown_opts opts;
struct lowdown_metaq mq;
struct lowdown_meta *md;
int r = 0;
+ size_t orig_size = 0;
TAILQ_INIT(&mq); // create the queue(linked list) of lowdown_meta
memset(&opts, 0, sizeof(struct lowdown_opts)); // init opts to 0
@@ -204,7 +199,10 @@ gen_html(char **dest, size_t *destz,
LOWDOWN_HTML_OWASP |
LOWDOWN_HTML_SKIP_HTML;
- lowdown_buf(&opts, orig, origz, dest, destz, &mq);
+ // use strlen(orig) here because the of the \0 char that
+ // was showing up on the dest buffer.
+ // playing with origz did not work here.
+ lowdown_buf(&opts, orig, strlen(orig), dest, destz, &mq);
TAILQ_FOREACH(md, &mq, entries) {
size_t valz = (strlen(md->value) > MAX_META)
@@ -223,33 +221,48 @@ gen_html(char **dest, size_t *destz,
}
}
- /* if (strlen(title) == 0 || strlen(author) == 0 || strlen(date) == 0) */
- /* goto out; */
+ r = 1;
+
+out:
+ lowdown_metaq_free(&mq);
+
+ return r;
+}
+
+int
+write_html(struct article *ap)
+{
+ char *head_tpl = NULL, *foot_tpl = NULL,
+ *header = NULL, *footer = NULL;
+ FILE *fout;
+ int r = -1;
- /* load_from_file(&head_tpl, "_header.html"); */
- /* header = str_rep(head_tpl, "$title$", title); */
- /* header = str_rep(header, "$author$", author); */
- /* header = str_rep(header, "$date$", date); */
- /* load_from_file(&foot_tpl, "_footer.html"); */
- /* footer = str_rep(foot_tpl, "$title$", title); */
+ if (strlen(ap->title) == 0
+ || strlen(ap->author) == 0
+ || strlen(ap->date) == 0)
+ goto out;
+ load_from_file(&head_tpl, "_header.html");
+ header = str_rep(head_tpl, "$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);
- /* if ((fout = fopen(destfile, "w")) == NULL) */
- /* goto out; */
- /* fwrite(header, 1, strlen(header), fout); */
- /* fwrite(ret, 1, retsz, fout); */
- /* fwrite(footer, 1, strlen(footer), fout); */
- r = 1;
+ if ((fout = fopen(ap->dst_path, "w")) == NULL)
+ goto out;
+ fwrite(header, 1, strlen(header), fout);
+ fwrite(ap->html_content, 1, ap->htmlz, fout);
+ fwrite(footer, 1, strlen(footer), fout);
+ fclose(fout);
+ r = 0;
out:
- /* free(ret); */
- lowdown_metaq_free(&mq);
- /* free(head_tpl); */
- /* free(header); */
- /* free(footer); */
- /* free(foot_tpl); */
-
+ free(head_tpl);
+ free(header);
+ free(footer);
+ free(foot_tpl);
return r;
}