From 5275f6b7043d399e6dc99f5ab532ac37a1f1bfda Mon Sep 17 00:00:00 2001 From: wrmr Date: Wed, 14 May 2025 21:59:24 -0500 Subject: add -R --root option to set absolute document root --- main.c | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/main.c b/main.c index cf9cc97..e65eb9d 100644 --- a/main.c +++ b/main.c @@ -23,6 +23,7 @@ typedef struct { Str hvarv[1024]; Str title; int hvarc; + Str docroot; } Options; Options opts = { 0 }; @@ -65,8 +66,7 @@ char to_xdigit(int x) { } } -void str_cat_uri(Str *s, Str uri, Arena *a) { - str_catc(s, '\'', a); +void str_cat_uri_internal(Str *s, Str uri, Arena *a) { for (isize i = 0; i < uri.n; i++) { char c = uri.s[i]; if (c == '\'' || c == '%') { @@ -77,6 +77,13 @@ void str_cat_uri(Str *s, Str uri, Arena *a) { str_catc(s, c, a); } } +} + +void str_cat_uri(Str *s, Str uri, Arena *a) { + str_catc(s, '\'', a); + if (str_starts(uri, S("/")) && opts.docroot.n > 0) + str_cat_uri_internal(s, opts.docroot, a); + str_cat_uri_internal(s, uri, a); str_catc(s, '\'', a); } @@ -499,8 +506,7 @@ int hvar_calc(Str param, Str *name, Str *val, Str filename) { usize n = 0; for (Str h = c.tail; h.n > 0; h = str_cut(h, ',').tail) n++; if (!n) return -1; - srand(str_hash(filename)); - usize j = rand() % n; + usize j = str_hash(filename) % n; usize i = 0; for (Str h = c.tail; h.n > 0; h = str_cut(h, ',').tail) { if (i == j) { @@ -526,6 +532,7 @@ void usage(const char *cmd) { " -i --inline enable inline formatting (*italics*, `code`, ---dashes, [[links]])\n" " -q --smartq enable smart quotes\n" " -t --title set the document title (does nothing without --standalone)\n" + " -R --root set a root url prepended to absolute paths\n" " -L --lang set the document's language\n", cmd, cmd); } @@ -551,8 +558,8 @@ Str html_head(Arena *m, Arena *l) { } if (opts.hvarc > 0) { - str_cat(&h, S("\n"), m); } @@ -616,7 +623,7 @@ int main(int argc, const char **argv) { Str param = { 0 }; opts.from_stdin = 1; - while ((r = arg_get(&a, "?sliqc:h:t:L:", ¶m, + while ((r = arg_get(&a, "?sliqc:h:t:L:R:", ¶m, "help", '?', "standalone", 's', "smartq", 'q', @@ -625,6 +632,7 @@ int main(int argc, const char **argv) { ":css", 'c', "link", 'l', "lang", 'L', + "root", 'R', ":hvar", 'h')) >= ARG_OK) { Arena reset = scratch; FILE *f; @@ -650,6 +658,9 @@ int main(int argc, const char **argv) { case 'L': opts.language = param; break; + case 'R': + opts.docroot = param; + break; case 't': opts.title = param; break; -- cgit 1.4.1-2-gfad0