From 5ee3cbd2897f96957902285d4dcbfb81ac7620ce Mon Sep 17 00:00:00 2001 From: NishiOwO Date: Tue, 10 Mar 2026 06:50:44 +0900 Subject: [PATCH] markdown-dir seems to be working --- src/action/markdown.c | 39 +++++++++++++++++++++++++++++++----- src/action/markdown_dir.c | 12 ++++------- src/taiga.h | 2 ++ src/util.c | 42 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 82 insertions(+), 13 deletions(-) diff --git a/src/action/markdown.c b/src/action/markdown.c index 58fb921..9f28bdf 100644 --- a/src/action/markdown.c +++ b/src/action/markdown.c @@ -8,6 +8,10 @@ static int in_title = 0; static int in_section = 0; static char** mapping; static int mapping_len; +static char* argin = NULL; +static char* argout = NULL; +static char* argbase = NULL; +static char* argbase2 = NULL; static void print_indent(void) { int i; @@ -219,21 +223,37 @@ static int enter_span(MD_SPANTYPE type, void* detail, void* user) { int i; char* str; - while((str = strstr(href_wd, "./")) == href_wd) href_wd = str + 2; + while((str = strstr(href_wd, "./")) == href_wd) href_wd += 2; for(i = 0; i < mapping_len; i++) { char* key = u_strdup(mapping[i]); char* value = strchr(key, '='); + char* path_abs; + char* href_abs; if(value != NULL) { value[0] = 0; value++; } - if(value != NULL && strcmp(key, href_wd) == 0) { + path_abs = u_path_combine(argbase, key); + + if(href_wd[0] == '/') { + href_abs = u_path_combine(argbase, href_wd); + } else { + href_abs = u_path_combine(argbase2, href_wd); + } + + if(value != NULL && strcmp(path_abs, href_abs) == 0) { + free(path_abs); + free(href_abs); free(href); href = encode(value, -1); + free(key); + break; } + free(path_abs); + free(href_abs); free(key); } @@ -359,9 +379,8 @@ int action_markdown(int argc, char** argv) { char* buffer; int size; MD_PARSER parser; - char* argin = NULL; - char* argout = NULL; int i; + char* s; memset(&parser, 0, sizeof(parser)); parser.flags = MD_FLAG_COLLAPSEWHITESPACE | MD_FLAG_TABLES | MD_FLAG_STRIKETHROUGH | MD_FLAG_UNDERLINE; @@ -371,6 +390,9 @@ int action_markdown(int argc, char** argv) { parser.leave_span = leave_span; parser.text = text; + argin = NULL; + argout = NULL; + argbase = NULL; mapping = NULL; mapping_len = 0; for(i = 0; i < argc; i++) { @@ -378,6 +400,8 @@ int action_markdown(int argc, char** argv) { argin = argv[i]; } else if(argout == NULL) { argout = argv[i]; + } else if(argbase == NULL) { + argbase = argv[i]; } else if(mapping == NULL) { mapping = &argv[i]; mapping_len = argc - i; @@ -385,10 +409,15 @@ int action_markdown(int argc, char** argv) { } if(argin == NULL || argout == NULL) { - fprintf(stderr, "Usage: markdown input output [linksrc=linkdest ...]\n"); + fprintf(stderr, "Usage: markdown input output [base [linksrc=linkdest ...]]\n"); return 1; } + if(argbase2 != NULL) free(argbase2); + argbase2 = u_strdup(argin); + + if((s = strrchr(argbase2, '/')) != NULL) s[0] = 0; + if((f = fopen(argin, "r")) == NULL) { fprintf(stderr, "Failed to open input\n"); return 1; diff --git a/src/action/markdown_dir.c b/src/action/markdown_dir.c index 38444ae..012830e 100644 --- a/src/action/markdown_dir.c +++ b/src/action/markdown_dir.c @@ -129,6 +129,7 @@ static int scan(const char* top, const char* path) { } arrput(argv, o); + arrput(argv, (char*)inp); for(i = 0; i < shlen(paths); i++) { char* t; @@ -140,19 +141,14 @@ static int scan(const char* top, const char* path) { t = u_strvacat(top, "../", NULL); } - if(strstr(paths[i].value, path) == paths[i].value) { - v = u_strvacat(paths[i].key + strlen(path), "=", paths[i].value + strlen(path), NULL); - arrput(argv, v); - } - - v = u_strvacat(top, paths[i].key, "=", t, paths[i].value, NULL); + v = u_strvacat(paths[i].key, "=", t, paths[i].value, NULL); arrput(argv, v); free(t); } if(action_markdown(arrlen(argv), argv)) { - for(i = 2; i < arrlen(argv); i++) free(argv[i]); + for(i = 3; i < arrlen(argv); i++) free(argv[i]); free(o); arrfree(argv); free(p); @@ -162,7 +158,7 @@ static int scan(const char* top, const char* path) { return 0; } free(o); - for(i = 2; i < arrlen(argv); i++) free(argv[i]); + for(i = 3; i < arrlen(argv); i++) free(argv[i]); arrfree(argv); } } diff --git a/src/taiga.h b/src/taiga.h index 1311cbc..7fcae8d 100644 --- a/src/taiga.h +++ b/src/taiga.h @@ -76,6 +76,8 @@ char* u_path(const char* top, const char* path); char* u_http_path(const char* top, const char* path); int u_image_size(const char* top, const char* path, char* (*path_func)(const char* top, const char* path), int* w, int* h); char* u_section_id(xl_node_t* node); +char* u_path_combine(const char* a, const char* b); +char* u_path_sanitize(const char* a); /* crc.c */ unsigned int crc32(void* input, unsigned int len); diff --git a/src/util.c b/src/util.c index 7bb1532..b842bd8 100644 --- a/src/util.c +++ b/src/util.c @@ -136,3 +136,45 @@ char* u_section_id(xl_node_t* node) { return id; } + +char* u_path_combine(const char* a, const char* b) { + char* p = u_strvacat(a, strlen(a) == 0 ? "" : "/", b, NULL); + char* r = malloc(strlen(p) + 1); + int i; + int s = 0; + + r[0] = 0; + + for(i = 0;; i++) { + if(p[i] == '/' || p[i] == 0) { + char old = p[i]; + char* path = p + s; + + p[i] = 0; + + if(strcmp(path, ".") == 0) { + } else if(strcmp(path, "..") == 0) { + char* str = strrchr(r, '/'); + + if(str != NULL) str[0] = 0; + } else if(strlen(path) > 0) { + if(r[0] == 0) { + strcpy(r, path); + } else { + strcat(r, "/"); + strcat(r, path); + } + } + + s = i + 1; + + if(old == 0) break; + } + } + + return r; +} + +char* u_path_sanitize(const char* a) { + return u_path_combine("", a); +}