summary refs log tree commit diff
diff options
context:
space:
mode:
authorWormHeamer2025-03-10 15:15:17 -0400
committerWormHeamer2025-03-10 15:15:17 -0400
commite6e0d5a53eeb969e9e04051559993db69b98cc85 (patch)
treee5ecf102cff236928876daecbb3a78b4e914a7ab
parent1f36f42f01da3c8f5733f8eb54624fdc3e26843b (diff)
replace .gmi with .html for local links properly
-rw-r--r--main.c25
1 files changed, 14 insertions, 11 deletions
diff --git a/main.c b/main.c
index 70029f8..d9cbd44 100644
--- a/main.c
+++ b/main.c
@@ -183,6 +183,7 @@ BlockList blk_gather(Str src, Arena *perm) {
 			Block *b = &blk.data[blk.len-1];
 			if (!b->lines) b->lines = lptr;
 		}
+		last = t;
 	}
 	for (size_t i = 0; i < blk.len; i++) {
 		if (blk.data[i].type == LN_NONE) {
@@ -200,7 +201,15 @@ BlockList blk_gather(Str src, Arena *perm) {
 #define Ot(a, s, b) Os(a), O(s), Os(b)
 #define Otl(a, f, b) for (Line *l = blk->lines; l; l = l->next) Ot(a, f, b)
 
-void str_cat_blk(Str *out, Block *blk, Arena *perm) {
+Str str_replace_end(Str s, Str a, Str b, Arena *m) {
+	if (!str_ends(s, a)) return s;
+	char *p = new_arr(m, char, s.n + b.n - a.n);
+	memcpy(p, s.s, s.n - a.n);
+	memcpy(p + s.n - a.n, b.s, b.n);
+	return (Str) { p, s.n + b.n - a.n };
+}
+
+void str_cat_blk(Str *out, Block *blk, Arena *perm, Arena *scratch) {
 	switch (blk->type) {
 	case LN_CODE:
 		Os("<pre><code>");
@@ -216,7 +225,9 @@ void str_cat_blk(Str *out, Block *blk, Arena *perm) {
 			Cut c = str_cut(str_trim(str_skip(l->txt, 2)), ' ');
 			Str url = c.head, txt = c.tail.n > 0 ? c.tail : c.head;
 			Os("<li><a href=");
-			str_cat_uri(out, url, perm);
+			str_cat_uri(out, str_contains(url, S("://")) ? url :
+					str_replace_end(url, S(".gmi"),
+						S(".html"), scratch), perm);
 			Ot(">", txt, "</a></li>\n");
 		}
 		Os("</ul>");
@@ -268,14 +279,6 @@ int has_image_ext(Str url) {
 		|| str_ends(url, S(".webp"));
 }
 
-Str str_replace_end(Str s, Str a, Str b, Arena *m) {
-	if (!str_ends(s, a)) return s;
-	char *p = new_arr(m, char, s.n + b.n - a.n);
-	memcpy(p, s.s, s.n - a.n);
-	memcpy(p + s.n - a.n, b.s, b.n);
-	return (Str) { p, s.n + b.n - a.n };
-}
-
 int wdoc(FILE *f, Doc **dp, Arena *perm, Arena *scratch) {
 	Str buf;
 	if (read_all(f, &buf, scratch)) return -1;
@@ -285,7 +288,7 @@ int wdoc(FILE *f, Doc **dp, Arena *perm, Arena *scratch) {
 		if (blk.data[i].type == LN_HDR1 && !d->title.s) {
 			d->title = str_trim(str_skip(blk.data[i].lines->txt, 1));
 		}
-		str_cat_blk(&d->html, &blk.data[i], perm);
+		str_cat_blk(&d->html, &blk.data[i], perm, scratch);
 	}
 	*dp = d;
 	return 0;