shayla

a simple yet fast static site builder
Log | Files | Refs | Submodules | README | LICENSE | git clone https://git.ne02ptzero.me/git/shayla

commit cbec05b205c16fd59a5bc9ac2304ddb3b2c13bbd
parent 289e123762b428a8223cd3a1d45e81f891569372
Author: Louis Solofrizzo <lsolofrizzo@online.net>
Date:   Fri, 28 Sep 2018 10:10:39 +0200

Dump: Now dumping the website and generating the index

This commit introduces full website HTML dumping, with route and such.
I've added a sort by time in order to generate the index correctly,
which is now done.

Signed-off-by: Louis Solofrizzo <lsolofrizzo@online.net>

Diffstat:
MCMakeLists.txt | 1+
Mmain.c | 4++++
Mposts.c | 14++++++++++++++
Ashtml.c | 155+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ashtml.h | 27+++++++++++++++++++++++++++
5 files changed, 201 insertions(+), 0 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt @@ -45,6 +45,7 @@ add_executable(${SDOW_NAME} main.c list.c dir.c pool.c + shtml.c ./sundown/src/autolink.c ./sundown/src/buffer.c ./sundown/src/markdown.c diff --git a/main.c b/main.c @@ -20,6 +20,7 @@ #include <posts.h> #include <inttypes.h> #include <pool.h> +#include <shtml.h> #include <errno.h> enum { @@ -389,6 +390,9 @@ int main(int ac, const char **av) if (!create_final_directories_and_copy(&ctx)) goto end; + if (!dump_site(&ctx)) + goto end; + ret = 0; end: free_args(options, COUNT_OF(options)); diff --git a/posts.c b/posts.c @@ -73,6 +73,19 @@ static void post_parse_callback(void *data, uint8_t num) } } +static int file_ts_cmp(void *priv, struct list_head *a, struct list_head *b) +{ + file_t *one = list_entry(a, file_t, list); + file_t *two = list_entry(b, file_t, list); + + if (one->ts > two->ts) + return -1; + else if (one->ts == two->ts) + return 0; + + return 1; +} + bool posts_parse_all(list_head_t *head) { file_t *ptr; @@ -95,5 +108,6 @@ bool posts_parse_all(list_head_t *head) PROGRESS_DONE(); markdown_context_cleanup(); + list_sort(NULL, head, &file_ts_cmp); return !parse_error; } diff --git a/shtml.c b/shtml.c @@ -0,0 +1,155 @@ +/** + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. +*/ + +#include <shtml.h> +#include <stdio.h> +#include <pool.h> + +#define html(str, ...) fprintf(fd, str, ##__VA_ARGS__); +static const shayla_t *ctx = NULL; +static bool dump_error = false; + +static void html_head(FILE *fd, const char *title) +{ + file_t *ptr; + + html("<!DOCTYPE HTML>"); + html("<html>"); + html("<head>") { + html("<meta charset='utf-8'>") + html("<meta name='viewport' content='width=device-width, initial-scale=1'>"); + html("<title>%s</title>", title); + + if (ctx->favicon != NULL) + { + html("<link rel='shortcut icon' type='image/png' href='%sfavicon.ico'/>", + ctx->root); + } + + list_for_each_entry(ptr, &ctx->styles, list) + { + html("<link rel='stylesheet' href='%sstyles/%s'>", ctx->root, + get_filename(ptr->path)); + } + } html("</head>"); + html("<body>"); +} + +static void html_end(FILE *fd) +{ + html("</body>"); + html("</html>"); +} + +static void dump_post(FILE *fd, file_t *ptr) +{ + html_head(fd, ptr->title); + + fwrite(ctx->header->ib->data, 1, ctx->header->ib->unit, fd); + html("<div id='content'>") { + fwrite(ptr->ob->data, 1, ptr->ob->size, fd); + } html("</div>"); + fwrite(ctx->footer->ib->data, 1, ctx->footer->ib->unit, fd); + + html_end(fd); +} + +static inline FILE *open_dump_file(const char *file) +{ + FILE *fd; + char path[PATH_MAX] = { 0 }; + + snprintf(path, sizeof(path), "%s/%s.html", ctx->dest, file); + fd = fopen(path, "w+"); + if (fd == NULL) + { + PROGRESS_WARNING("Could not open %s", path); + dump_error = true; + } + + return fd; +} + +static void post_dump_callback(void *data) +{ + file_t *ptr = data; + FILE *fd; + + fd = open_dump_file(ptr->route); + if (fd == NULL) + return ; + + PROGRESS_UPDATE("Generating %s", ptr->route); + dump_post(fd, ptr); + + fclose(fd); +} + +static void index_build_callback(void *unused) +{ + file_t *ptr; + FILE *fd; + + UNUSED_ARG(unused); + fd = open_dump_file("index"); + + if (fd == NULL) + return ; + + PROGRESS_UPDATE("Generating index.html"); + html_head(fd, ctx->title); + + fwrite(ctx->header->ib->data, 1, ctx->header->ib->unit, fd); + fwrite(ctx->intro->ib->data, 1, ctx->intro->ib->unit, fd); + html("<div id='content'>") { + html("<ul>"); + list_for_each_entry(ptr, &ctx->posts, list) + { + if (!ptr->is_listed) + continue; + + html("<li><a href='%s%s.html' title='%s'>%s</a></li>", + ctx->root, ptr->route, ptr->summary, ptr->title); + } + } html("</div>"); + html("</ul>"); + fwrite(ctx->footer->ib->data, 1, ctx->footer->ib->unit, fd); + + html_end(fd); + fclose(fd); +} + +bool dump_site(const shayla_t *shayla) +{ + file_t *ptr; + + ctx = shayla; + + SET_PREFIX("Posts:"); + PROGRESS_BEGIN(); + pool_run(); + + list_for_each_entry(ptr, &ctx->posts, list) + { + pool_add_job(&post_dump_callback, ptr); + } + + pool_add_job(&index_build_callback, NULL); + + pool_wait(); + PROGRESS_DONE(); + + return !dump_error; +} diff --git a/shtml.h b/shtml.h @@ -0,0 +1,27 @@ +/** + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. +*/ + +#ifndef SHTML_H +# define SHTML_H + +# include <shayla.h> +# include <stdbool.h> + +/*! + * \brief Dump the html posts, and generate the index + */ +bool dump_site(const shayla_t *ctx); + +#endif /* SHTML_H */