diff options
-rw-r--r-- | main.c | 42 |
1 files changed, 39 insertions, 3 deletions
diff --git a/main.c b/main.c index 27746d6..7830adf 100644 --- a/main.c +++ b/main.c @@ -24,6 +24,7 @@ typedef struct { Str title; int hvarc; Str docroot; + Str header_file, footer_file; } Options; Options opts = { 0 }; @@ -550,12 +551,13 @@ int hvar_calc(Str param, Str *name, Str *val, Str filename) { void usage(const char *cmd) { fprintf(stderr, "usage: %s -?\n" - " %s [-silq] [-L LANG] [-c FILE] [-t TITLE] [-h NAME:ARG1[,ARG2,ARG3...]] [FILES...]\n" + " %s [OPTIONS] [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" + " (format: NAME:ARG1[,ARG2,ARG3...])\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" @@ -563,6 +565,8 @@ void usage(const char *cmd) { " -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" + " -H --header prepend the given file before the document\n" + " -F --footer append the given file after the document\n" " -L --lang set the document's language\n", cmd, cmd); } @@ -639,6 +643,26 @@ Str html_tail(Arena *m, Arena *l) { return h; } +void put_file(Str filename, Arena *scratch) { + Arena t = *scratch; + const char *path = str_to_cstr(filename, scratch); + FILE *f = fopen(path, "r/o"); + if (!f) { + fprintf(stderr, "failed to open file %s: %s\n", path, + strerror(errno)); + return; + } + Str buf; + if (read_all(f, &buf, scratch)) { + fprintf(stderr, "failed to read file %s: %s\n", path, + strerror(errno)); + return; + } + str_put(buf); + fclose(f); + *scratch = t; +} + #define countof(x) (sizeof(x) / sizeof(*x)) int main(int argc, const char **argv) { (void)argc; @@ -653,7 +677,7 @@ int main(int argc, const char **argv) { Str param = { 0 }; opts.from_stdin = 1; - while ((r = arg_get(&a, "?sliqc:h:t:L:R:", ¶m, + while ((r = arg_get(&a, "?sliqc:h:t:L:R:H:F:", ¶m, "help", '?', "standalone", 's', "smartq", 'q', @@ -663,7 +687,9 @@ int main(int argc, const char **argv) { "link", 'l', "lang", 'L', "root", 'R', - ":hvar", 'h')) >= ARG_OK) { + ":hvar", 'h', + "header", 'H', + "footer", 'F')) >= ARG_OK) { Arena reset = scratch; FILE *f; switch (r) { @@ -701,6 +727,12 @@ int main(int argc, const char **argv) { } opts.hvarv[opts.hvarc++] = param; break; + case 'H': + opts.header_file = param; + break; + case 'F': + opts.footer_file = param; + break; default: if (str_eql(param, S("-"))) { if (wdoc(stdin, &doc, &perm, &scratch)) { @@ -757,11 +789,15 @@ int main(int argc, const char **argv) { str_put(S("\n")); } + if (opts.header_file.n) put_file(opts.header_file, &scratch); + while (doc) { str_put(doc->html); doc = doc->next; } + if (opts.footer_file.n) put_file(opts.footer_file, &scratch); + str_put(html_tail(&perm, &scratch)); return 0; |