From a9d975389aaf5d79d738517b98161e375e757cba Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Mon, 18 Nov 2024 09:27:35 -0500 Subject: Break down indexer into independent source files * Generate test result output * Add helper function to manage changing file extensions --- src/cli/stasis_indexer/website.c | 119 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 src/cli/stasis_indexer/website.c (limited to 'src/cli/stasis_indexer/website.c') diff --git a/src/cli/stasis_indexer/website.c b/src/cli/stasis_indexer/website.c new file mode 100644 index 0000000..c04b7f2 --- /dev/null +++ b/src/cli/stasis_indexer/website.c @@ -0,0 +1,119 @@ +#include "core.h" +#include "website.h" + +int indexer_make_website(const struct Delivery *ctx) { + if (!find_program("pandoc")) { + fprintf(stderr, "pandoc is not installed: unable to generate HTML indexes\n"); + return 0; + } + + char *css_filename = calloc(PATH_MAX, sizeof(*css_filename)); + if (!css_filename) { + SYSERROR("unable to allocate string for CSS file path: %s", strerror(errno)); + return -1; + } + + sprintf(css_filename, "%s/%s", globals.sysconfdir, "stasis_pandoc.css"); + const int have_css = access(css_filename, F_OK | R_OK) == 0; + + char pandoc_versioned_args[255] = {0}; + size_t pandoc_version = 0; + + if (!get_pandoc_version(&pandoc_version)) { + // < 2.19 + if (pandoc_version < 0x02130000) { + strcat(pandoc_versioned_args, "--self-contained "); + } else { + // >= 2.19 + strcat(pandoc_versioned_args, "--embed-resources "); + } + + // >= 1.15.0.4 + if (pandoc_version >= 0x010f0004) { + strcat(pandoc_versioned_args, "--standalone "); + } + + // >= 1.10.0.1 + if (pandoc_version >= 0x010a0001) { + strcat(pandoc_versioned_args, "-f gfm+autolink_bare_uris "); + } + + // > 3.1.9 + if (pandoc_version > 0x03010900) { + strcat(pandoc_versioned_args, "-f gfm+alerts "); + } + } + + 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++) { + const char *pattern = "*.md"; + if (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; + } + char *root = strlist_item(dirs, i); + for (size_t x = 0; x < strlist_count(inputs); x++) { + char cmd[PATH_MAX] = {0}; + 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); + gen_file_extension_str(fullpath_dest, ".html"); + + // Converts a markdown file to html + strcpy(cmd, "pandoc "); + strcat(cmd, pandoc_versioned_args); + if (have_css) { + strcat(cmd, "--css "); + strcat(cmd, css_filename); + } + strcat(cmd, " "); + strcat(cmd, "--metadata title=\"STASIS\" "); + strcat(cmd, "-o "); + strcat(cmd, fullpath_dest); + strcat(cmd, " "); + strcat(cmd, fullpath_src); + 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) { + guard_free(css_filename); + guard_strlist_free(&dirs); + 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"); + if (symlink(link_from, link_dest)) { + SYSERROR("Warning: symlink(%s, %s) failed: %s", link_from, link_dest, strerror(errno)); + } + } + } + guard_strlist_free(&inputs); + } + guard_free(css_filename); + guard_strlist_free(&dirs); + + return 0; +} -- cgit From 3336bb1399e83f10717dcad6fbbf949727a51532 Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Fri, 6 Dec 2024 08:30:26 -0500 Subject: Move pandoc code into its own function pandoc_exec() --- src/cli/stasis_indexer/website.c | 59 +++------------------------------------- 1 file changed, 4 insertions(+), 55 deletions(-) (limited to 'src/cli/stasis_indexer/website.c') diff --git a/src/cli/stasis_indexer/website.c b/src/cli/stasis_indexer/website.c index c04b7f2..cf5f3c2 100644 --- a/src/cli/stasis_indexer/website.c +++ b/src/cli/stasis_indexer/website.c @@ -2,11 +2,6 @@ #include "website.h" int indexer_make_website(const struct Delivery *ctx) { - if (!find_program("pandoc")) { - fprintf(stderr, "pandoc is not installed: unable to generate HTML indexes\n"); - return 0; - } - char *css_filename = calloc(PATH_MAX, sizeof(*css_filename)); if (!css_filename) { SYSERROR("unable to allocate string for CSS file path: %s", strerror(errno)); @@ -16,34 +11,6 @@ int indexer_make_website(const struct Delivery *ctx) { sprintf(css_filename, "%s/%s", globals.sysconfdir, "stasis_pandoc.css"); const int have_css = access(css_filename, F_OK | R_OK) == 0; - char pandoc_versioned_args[255] = {0}; - size_t pandoc_version = 0; - - if (!get_pandoc_version(&pandoc_version)) { - // < 2.19 - if (pandoc_version < 0x02130000) { - strcat(pandoc_versioned_args, "--self-contained "); - } else { - // >= 2.19 - strcat(pandoc_versioned_args, "--embed-resources "); - } - - // >= 1.15.0.4 - if (pandoc_version >= 0x010f0004) { - strcat(pandoc_versioned_args, "--standalone "); - } - - // >= 1.10.0.1 - if (pandoc_version >= 0x010a0001) { - strcat(pandoc_versioned_args, "-f gfm+autolink_bare_uris "); - } - - // > 3.1.9 - if (pandoc_version > 0x03010900) { - strcat(pandoc_versioned_args, "-f gfm+alerts "); - } - } - struct StrList *dirs = strlist_init(); strlist_append(&dirs, ctx->storage.delivery_dir); strlist_append(&dirs, ctx->storage.results_dir); @@ -71,29 +38,11 @@ int indexer_make_website(const struct Delivery *ctx) { strcpy(fullpath_dest, fullpath_src); gen_file_extension_str(fullpath_dest, ".html"); - // Converts a markdown file to html - strcpy(cmd, "pandoc "); - strcat(cmd, pandoc_versioned_args); - if (have_css) { - strcat(cmd, "--css "); - strcat(cmd, css_filename); - } - strcat(cmd, " "); - strcat(cmd, "--metadata title=\"STASIS\" "); - strcat(cmd, "-o "); - strcat(cmd, fullpath_dest); - strcat(cmd, " "); - strcat(cmd, fullpath_src); - 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) { - guard_free(css_filename); - guard_strlist_free(&dirs); - return 1; + // Convert markdown to html + if (pandoc_exec(fullpath_src, fullpath_dest, have_css ? css_filename : NULL, "STASIS")) { + msg(STASIS_MSG_L2 | STASIS_MSG_WARN, "Unable to convert %s\n", fullpath_src); } + if (file_replace_text(fullpath_dest, ".md", ".html", 0)) { // inform-only SYSERROR("%s: failed to rewrite *.md urls with *.html extension", fullpath_dest); -- cgit From 9788c4ffe8a75d1a10ff0bdc5d382b5e7f1cd70b Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Fri, 6 Dec 2024 08:31:35 -0500 Subject: Scan the directories listed in `dirs` instead of just the delivery directory --- src/cli/stasis_indexer/website.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'src/cli/stasis_indexer/website.c') diff --git a/src/cli/stasis_indexer/website.c b/src/cli/stasis_indexer/website.c index cf5f3c2..9fa7b57 100644 --- a/src/cli/stasis_indexer/website.c +++ b/src/cli/stasis_indexer/website.c @@ -18,11 +18,12 @@ int indexer_make_website(const struct Delivery *ctx) { struct StrList *inputs = NULL; for (size_t i = 0; i < strlist_count(dirs); i++) { const char *pattern = "*.md"; - if (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); + char *dirpath = strlist_item(dirs, i); + if (get_files(&inputs, dirpath, pattern)) { + SYSERROR("%s does not contain files with pattern: %s", dirpath, pattern); continue; } + char *root = strlist_item(dirs, i); for (size_t x = 0; x < strlist_count(inputs); x++) { char cmd[PATH_MAX] = {0}; -- cgit From 4da928cada36fcabf4f0c9138ade0beef7bf92e4 Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Fri, 6 Dec 2024 08:31:56 -0500 Subject: Take the basename of the file immediately from the StrList item --- src/cli/stasis_indexer/website.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'src/cli/stasis_indexer/website.c') diff --git a/src/cli/stasis_indexer/website.c b/src/cli/stasis_indexer/website.c index 9fa7b57..55f0c45 100644 --- a/src/cli/stasis_indexer/website.c +++ b/src/cli/stasis_indexer/website.c @@ -26,8 +26,7 @@ int indexer_make_website(const struct Delivery *ctx) { char *root = strlist_item(dirs, i); for (size_t x = 0; x < strlist_count(inputs); x++) { - char cmd[PATH_MAX] = {0}; - char *filename = strlist_item(inputs, x); + char *filename = path_basename(strlist_item(inputs, x)); char fullpath_src[PATH_MAX] = {0}; char fullpath_dest[PATH_MAX] = {0}; sprintf(fullpath_src, "%s/%s", root, filename); -- cgit