diff options
-rw-r--r-- | src/stasis_indexer.c | 80 |
1 files changed, 57 insertions, 23 deletions
diff --git a/src/stasis_indexer.c b/src/stasis_indexer.c index 08c8dac..7786b4d 100644 --- a/src/stasis_indexer.c +++ b/src/stasis_indexer.c @@ -231,39 +231,73 @@ struct Delivery **get_latest_deliveries(struct Delivery ctx[], size_t nelem) { int indexer_make_website(struct Delivery *ctx) { char cmd[PATH_MAX]; - char *inputs[] = { - ctx->storage.delivery_dir, "/README.md", - ctx->storage.results_dir, "/README.md", - }; + const char *pattern = "*.md"; if (!find_program("pandoc")) { fprintf(stderr, "pandoc is not installed: unable to generate HTML indexes\n"); return 0; } - for (size_t i = 0; i < sizeof(inputs) / sizeof(*inputs); i += 2) { - char fullpath[PATH_MAX]; - memset(fullpath, 0, sizeof(fullpath)); - sprintf(fullpath, "%s/%s", inputs[i], inputs[i + 1]); - if (access(fullpath, F_OK)) { + struct StrList *dirs = strlist_init(); + strlist_append(&dirs, ctx->storage.delivery_dir); + strlist_append(&dirs, ctx->storage.results_dir); + + struct StrList *inputs = NULL; + for (size_t i = 0; i < strlist_count(dirs); i++) { + if (indexer_get_files(&inputs, ctx->storage.delivery_dir, pattern)) { + SYSERROR("%s does not contain files with pattern: %s", ctx->storage.delivery_dir, pattern); + guard_strlist_free(&inputs); continue; } - // Converts a markdown file to html - strcpy(cmd, "pandoc "); - strcat(cmd, fullpath); - strcat(cmd, " "); - strcat(cmd, "-o "); - strcat(cmd, inputs[i]); - strcat(cmd, "/index.html"); - if (globals.verbose) { - puts(cmd); - } - // This might be negative when killed by a signal. - // Otherwise, the return code is not critical to us. - if (system(cmd) < 0) { - return 1; + char *root = strlist_item(dirs, i); + for (size_t x = 0; x < strlist_count(inputs); x++) { + char *filename = strlist_item(inputs, x); + char fullpath_src[PATH_MAX] = {0}; + char fullpath_dest[PATH_MAX] = {0}; + sprintf(fullpath_src, "%s/%s", root, filename); + if (access(fullpath_src, F_OK)) { + continue; + } + + // Replace *.md extension with *.html. + strcpy(fullpath_dest, fullpath_src); + char *ext = strrchr(fullpath_dest, '.'); + if (ext) { + *ext = '\0'; + } + strcat(fullpath_dest, ".html"); + + // Converts a markdown file to html + strcpy(cmd, "pandoc "); + strcat(cmd, fullpath_src); + strcat(cmd, " "); + strcat(cmd, "-o "); + strcat(cmd, fullpath_dest); + if (globals.verbose) { + puts(cmd); + } + // This might be negative when killed by a signal. + // Otherwise, the return code is not critical to us. + if (system(cmd) < 0) { + return 1; + } + if (file_replace_text(fullpath_dest, ".md", ".html", 0)) { + // inform-only + SYSERROR("%s: failed to rewrite *.md urls with *.html extension", fullpath_dest); + } + + // Link the nearest README.html to index.html + if (!strcmp(filename, "README.md")) { + char link_from[PATH_MAX] = {0}; + char link_dest[PATH_MAX] = {0}; + strcpy(link_from, "README.html"); + sprintf(link_dest, "%s/%s", root, "index.html"); + symlink(link_from, link_dest); + } } + guard_strlist_free(&inputs); } + guard_strlist_free(&dirs); return 0; } |