summary refs log tree commit diff
path: root/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'main.c')
-rw-r--r--main.c76
1 files changed, 47 insertions, 29 deletions
diff --git a/main.c b/main.c
index d0f6e04..e749d58 100644
--- a/main.c
+++ b/main.c
@@ -369,13 +369,15 @@ int hvar_calc(Str param, Str *name, Str *val, Str filename) {
 
 void usage(const char *cmd) {
 	fprintf(stderr, "usage: %s -?\n"
-			"       %s [-s] [-c FILE] [-t TITLE] [-h NAME:ARG1[,ARG2,ARG3...]] [FILES...]\n"
+			"       %s [-s] [[-l] -c FILE] [-t TITLE] [-h NAME:ARG1[,ARG2,ARG3...]] [FILES...]\n"
 			"\n"
 			" -? --help         show this help text\n"
 			" -s --standalone   prefix with html metadata\n"
 			" -h --hvar         define a css variable (--name) with a random value,\n"
 			"                   selected by a hash of the document's title\n"
 			" -c --css          embed the given file within a <style> element\n"
+			" -l --link         when combined with --css, link to an external stylehseet\n"
+			"                   instead of reading from a file locally\n"
 			" -t --title        set the document title (does nothing without --standalone)\n",
 			cmd, cmd);
 }
@@ -384,6 +386,7 @@ typedef struct {
 	int standalone;
 	int from_stdin;
 	Str stylesheet;
+	int csslink;
 	Str hvarv[1024];
 	Str title;
 	int hvarc;
@@ -393,43 +396,54 @@ Str html_head(Options *o, Arena *m, Arena *l) {
 	Str h = S("<!DOCTYPE html>\n"
 			"<meta charset=utf-8>\n"
 			"<meta name=viewport content='width=device-width,initial-scale=1'>\n");
+
 	if (o->title.s) {
 		str_cat(&h, S("<title>"), m);
 		str_cat_html(&h, o->title, m);
 		str_cat(&h, S("</title>\n"), m);
 	}
-	if (o->stylesheet.s) {
-		FILE *f = fopen(str_to_cstr(o->stylesheet, m), "r/o");
-		if (!f) {
-			str_putf(o->stylesheet, stderr);
-			fprintf(stderr, ": %s\n", strerror(errno));
-			exit(1);
-		}
+
+	if (o->hvarc > 0) {
 		str_cat(&h, S("<style>\n"), m);
-		if (o->hvarc > 0) {
-			str_cat(&h, S(":root {"), m);
-			for (int i = 0; i < o->hvarc; i++) {
-				Str name, val;
-				if (hvar_calc(o->hvarv[i], &name, &val, o->title)) {
-					fprintf(stderr, "invalid argument given to --hvar\n");
-					exit(1);
-				}
-				str_cat(&h, S("--"), m);
-				str_cat(&h, name, m);
-				str_catc(&h, ':', m);
-				str_cat(&h, val, m);
-				str_catc(&h, ';', m);
+		str_cat(&h, S(":root {"), m);
+		for (int i = 0; i < o->hvarc; i++) {
+			Str name, val;
+			if (hvar_calc(o->hvarv[i], &name, &val, o->title)) {
+				fprintf(stderr, "invalid argument given to --hvar\n");
+				exit(1);
 			}
-			str_cat(&h, S("}\n"), m);
-		}
-		Str css;
-		if (read_all(f, &css, l)) {
-			fprintf(stderr, "failed to read stylesheet: %s\n", strerror(errno));
-			exit(1);
+			str_cat(&h, S("--"), m);
+			str_cat(&h, name, m);
+			str_catc(&h, ':', m);
+			str_cat(&h, val, m);
+			str_catc(&h, ';', m);
 		}
-		str_cat(&h, css, m);
+		str_cat(&h, S("}\n"), m);
 		str_cat(&h, S("</style>\n"), m);
 	}
+
+	if (o->stylesheet.s) {
+		if (o->csslink) {
+			str_cat(&h, S("<link rel='stylesheet' href="), m);
+			str_cat_uri(&h, o->stylesheet, m);
+			str_cat(&h, S(">"), m);
+		} else {
+			FILE *f = fopen(str_to_cstr(o->stylesheet, m), "r/o");
+			if (!f) {
+				str_putf(o->stylesheet, stderr);
+				fprintf(stderr, ": %s\n", strerror(errno));
+				exit(1);
+			}
+			Str css;
+			if (read_all(f, &css, l)) {
+				fprintf(stderr, "failed to read stylesheet: %s\n", strerror(errno));
+				exit(1);
+			}
+			str_cat(&h, S("<style>"), m);
+			str_cat(&h, css, m);
+			str_cat(&h, S("</style>"), m);
+		}
+	}
 	return h;
 }
 
@@ -448,11 +462,12 @@ int main(int argc, const char **argv) {
 	Str param = { 0 };
 	opts.from_stdin = 1;
 
-	while ((r = arg_get(&a, "?sc:h:t:", &param,
+	while ((r = arg_get(&a, "?slc:h:t:", &param,
 					"help", '?',
 					"standalone", 's',
 					":title", 't',
 					":css", 'c',
+					"link", 'l',
 					":hvar", 'h')) >= ARG_OK) {
 		Arena reset = scratch;
 		FILE *f;
@@ -466,6 +481,9 @@ int main(int argc, const char **argv) {
 		case 'c':
 			opts.stylesheet = param;
 			break;
+		case 'l':
+			opts.csslink = 1;
+			break;
 		case 't':
 			opts.title = param;
 			break;