diff --git a/HACKING.rst b/HACKING.rst index b31d5fe..055eae4 100644 --- a/HACKING.rst +++ b/HACKING.rst @@ -5,38 +5,47 @@ .. contents:: -How to modify templates -======================= +How to modify the template +========================== -Templates are in the ``templates/`` subdirectory of the source distribution. -They are included both as regular HTML text snippets (files ending in ``.t``) -and ready for inclusion in a C program (files ending in ``.inc``). Also -there is a small C program +The template is in the ``template.html`` file. Note that comment markers are +used to control how the ``split-template`` Awk script generates the C header +which gets ultimately included in the compiled object code. Comment markers +have the ```` format. Here ``identifier`` must be +a valid C identifier. All the text following the marker until the next +marker will be flattened into a C string. + +Regenerating the C header +~~~~~~~~~~~~~~~~~~~~~~~~~ +You will need Awk. I hope any decent implementation will do, but the GNU one +is known to work flawlessly. Just do:: + + $ awk -f template.awk template.html > template.h -Template order -~~~~~~~~~~~~~~ -01-head1 +Template identifier order +~~~~~~~~~~~~~~~~~~~~~~~~~ +t01_head1 Outputs the HTML header and must end with something like ``Index of``, because the code inserts the path of the URI just after this piece. -02-head2 +t02_head2 Outputs the rest of the header, usually will close the ```` tag opened in the previous template and add further output until the closing ```` tag. -03-body1 +t03_body1 - -04-body2 +t04_body2 - -05-list1 +t05_list1 - -06-list2 +t06_list2 - -07-body3 +t07_body3 - -08-body4 +t08_body4 - -09-foot1 +t09_foot1 - diff --git a/ngx_http_fancyindex_module.c b/ngx_http_fancyindex_module.c index 7ff7435..5e9ef16 100644 --- a/ngx_http_fancyindex_module.c +++ b/ngx_http_fancyindex_module.c @@ -22,7 +22,7 @@ #include #include #include - +#include "ngx_http_fancyindex_module.h" #if 0 @@ -179,7 +179,6 @@ ngx_module_t ngx_http_fancyindex_module = { }; -#include "templates/templates.h" static ngx_int_t diff --git a/templates/templates.h b/ngx_http_fancyindex_module.h similarity index 57% rename from templates/templates.h rename to ngx_http_fancyindex_module.h index a87a899..829f952 100644 --- a/templates/templates.h +++ b/ngx_http_fancyindex_module.h @@ -1,34 +1,14 @@ +/* + * ngx_http_fancyindex_module.h + * Copyright © 2007 Adrian Perez + * + * Distributed under terms of the BSD license. + */ -#ifndef __templates_inc__ -#define __templates_inc__ +#ifndef __ngx_http_fancyindex_module_h__ +#define __ngx_http_fancyindex_module_h__ -static const u_char t01_head1[] = -#include "01-head1.inc" -; -static const u_char t02_head2[] = -#include "02-head2.inc" -; -static const u_char t03_body1[] = -#include "03-body1.inc" -; -static const u_char t04_body2[] = -#include "04-body2.inc" -; -static const u_char t05_list1[] = -#include "05-list1.inc" -; -static const u_char t06_list2[] = -#include "06-list2.inc" -; -static const u_char t07_body3[] = -#include "07-body3.inc" -; -static const u_char t08_body4[] = -#include "08-body4.inc" -; -static const u_char t09_foot1[] = -#include "09-foot1.inc" -; +#include "template.h" #define NFI_TEMPLATE_SIZE \ ( (sizeof(t01_head1) - 1) \ @@ -61,6 +41,6 @@ static const u_char t09_foot1[] = #define nfi_maybe_cpymem_ssz(__p, __t) \ if ((__t)[0] != '\0') nfi_cpymem_ssz((__p), (__t)) -#endif /* !__templates_inc__ */ +#endif /* !__ngx_http_fancyindex_module_h__ */ /* vim:ft=c */ diff --git a/template.awk b/template.awk new file mode 100755 index 0000000..31d0779 --- /dev/null +++ b/template.awk @@ -0,0 +1,35 @@ +#! /usr/bin/awk -f +# +# Copyright © Adrian Perez +# +# Converts an HTML template into a C header suitable for inclusion. +# Take a look at the HACKING.rst file to know how to use it :-) +# +# This code is placed in the public domain. + +BEGIN { + varname = 0; + print "/* Automagically generated, do not edit! */" +} + +/^$/ { + if (varname) print ";"; + varname = $3; + print "static const u_char " varname "[] = \"\""; + next; +} + +{ + if (!varname) next; + # Order matters + gsub(/[\t\v\n\r\f\g]+/, ""); + gsub(/\\/, "\\\\"); + gsub(/"/, "\\\""); + print "\"" $0 "\"" +} + + +END { + if (varname) print ";"; +} + diff --git a/template.h b/template.h new file mode 100644 index 0000000..a4c8014 --- /dev/null +++ b/template.h @@ -0,0 +1,63 @@ +/* Automagically generated, do not edit! */ +static const u_char t01_head1[] = "" +"" +"" +"" +"" +"" +"Index of " +; +static const u_char t02_head2[] = "" +"" +"" +; +static const u_char t03_body1[] = "" +"" +"

" +; +static const u_char t04_body2[] = "" +"

" +; +static const u_char t05_list1[] = "" +"" +"" +"" +"" +"" +"" +"" +"" +"" +"" +"" +"" +"" +"" +"" +"" +"" +; +static const u_char t06_list2[] = "" +"" +"
File NameFile SizeDate
Parent Directory
" +; +static const u_char t07_body3[] = "" +; +static const u_char t08_body4[] = "" +; +static const u_char t09_foot1[] = "" +"" +"" +; diff --git a/template.html b/template.html new file mode 100644 index 0000000..1d3a76a --- /dev/null +++ b/template.html @@ -0,0 +1,53 @@ + + + + + + + Index of +<!-- var t02_head2 --> + + + + +

+ +

+ + + + + + + + + + + + + + + + + + + + +
File NameFile SizeDate
Parent Directory
+ + + + + diff --git a/templates/01-head1.inc b/templates/01-head1.inc deleted file mode 100644 index 63c409b..0000000 --- a/templates/01-head1.inc +++ /dev/null @@ -1,6 +0,0 @@ -"<" -"meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\"/>Index of " diff --git a/templates/01-head1.t b/templates/01-head1.t deleted file mode 100644 index 2f62c69..0000000 --- a/templates/01-head1.t +++ /dev/null @@ -1,18 +0,0 @@ - - - - - - Index of diff --git a/templates/02-head2.inc b/templates/02-head2.inc deleted file mode 100644 index 1608ceb..0000000 --- a/templates/02-head2.inc +++ /dev/null @@ -1 +0,0 @@ -"" diff --git a/templates/02-head2.t b/templates/02-head2.t deleted file mode 100644 index aa643e8..0000000 --- a/templates/02-head2.t +++ /dev/null @@ -1,2 +0,0 @@ - - diff --git a/templates/03-body1.inc b/templates/03-body1.inc deleted file mode 100644 index a0bf4d4..0000000 --- a/templates/03-body1.inc +++ /dev/null @@ -1 +0,0 @@ -"

" diff --git a/templates/03-body1.t b/templates/03-body1.t deleted file mode 100644 index ee48479..0000000 --- a/templates/03-body1.t +++ /dev/null @@ -1,2 +0,0 @@ - -

diff --git a/templates/04-body2.inc b/templates/04-body2.inc deleted file mode 100644 index 41afb01..0000000 --- a/templates/04-body2.inc +++ /dev/null @@ -1 +0,0 @@ -"

" diff --git a/templates/04-body2.t b/templates/04-body2.t deleted file mode 100644 index d210ccf..0000000 --- a/templates/04-body2.t +++ /dev/null @@ -1 +0,0 @@ - diff --git a/templates/05-list1.inc b/templates/05-list1.inc deleted file mode 100644 index 322797e..0000000 --- a/templates/05-list1.inc +++ /dev/null @@ -1,4 +0,0 @@ -"" diff --git a/templates/05-list1.t b/templates/05-list1.t deleted file mode 100644 index 8ccc5e1..0000000 --- a/templates/05-list1.t +++ /dev/null @@ -1,17 +0,0 @@ -
File Name" -"File SizeDate
Parent Directory
- - - - - - - - - - - - - - - - diff --git a/templates/06-list2.inc b/templates/06-list2.inc deleted file mode 100644 index 634832b..0000000 --- a/templates/06-list2.inc +++ /dev/null @@ -1 +0,0 @@ -"
File NameFile SizeDate
Parent Directory
" diff --git a/templates/06-list2.t b/templates/06-list2.t deleted file mode 100644 index da8b0a0..0000000 --- a/templates/06-list2.t +++ /dev/null @@ -1,2 +0,0 @@ - - diff --git a/templates/07-body3.inc b/templates/07-body3.inc deleted file mode 100644 index e16c76d..0000000 --- a/templates/07-body3.inc +++ /dev/null @@ -1 +0,0 @@ -"" diff --git a/templates/07-body3.t b/templates/07-body3.t deleted file mode 100644 index 8b13789..0000000 --- a/templates/07-body3.t +++ /dev/null @@ -1 +0,0 @@ - diff --git a/templates/08-body4.inc b/templates/08-body4.inc deleted file mode 100644 index e16c76d..0000000 --- a/templates/08-body4.inc +++ /dev/null @@ -1 +0,0 @@ -"" diff --git a/templates/08-body4.t b/templates/08-body4.t deleted file mode 100644 index 8b13789..0000000 --- a/templates/08-body4.t +++ /dev/null @@ -1 +0,0 @@ - diff --git a/templates/09-foot1.inc b/templates/09-foot1.inc deleted file mode 100644 index 5df4a51..0000000 --- a/templates/09-foot1.inc +++ /dev/null @@ -1 +0,0 @@ -"" diff --git a/templates/09-foot1.t b/templates/09-foot1.t deleted file mode 100644 index 7fb2bd6..0000000 --- a/templates/09-foot1.t +++ /dev/null @@ -1,2 +0,0 @@ - - diff --git a/templates/Makefile b/templates/Makefile deleted file mode 100644 index d19663d..0000000 --- a/templates/Makefile +++ /dev/null @@ -1,24 +0,0 @@ -# -# Makefile -# acastro, 2007-08-21 18:41 -# - -all_t := $(wildcard *.t) -all_inc := $(patsubst %.t,%.inc,$(all_t)) - - -all: $(all_inc) - -t2inc: t2inc.c - -.INTERMEDIATE: t2inc - -$(all_inc): t2inc - -%.inc: %.t - ./t2inc < $< > $@ - - -# vim:ft=make -# - diff --git a/templates/t2inc.c b/templates/t2inc.c deleted file mode 100644 index d5f1e87..0000000 --- a/templates/t2inc.c +++ /dev/null @@ -1,88 +0,0 @@ -/* - * t2inc.c - * Copyright (C) 2007 Adrian Perez - * - * - * About t2inc - * ~~~~~~~~~~~ - * Flattens HTML-like text to C strings. Some whitespace - * is removed to minimize output size. - * - * - * Usage - * ~~~~~ - * Compile it and use as a filter in a shell pipeline: - * - * $ cc -o t2inc t2inc.c - * $ ./t2inc < input.t > output.inc - * $ cat *.t | ./t2inc > alloutput.inc - * - * ...and so on. - * - * - * License - * ~~~~~~~ - * This code is placed in the public domain. - * - */ - -#include -#include -#include -#include -#include - -#define MAX_W 70 - -int -main(int argc, char **argv) -{ - (void) argc; /* unused */ - (void) argv; /* unused */ - - int c = 0; - int w = 0; - - putchar('"'); - while ((c = getchar()) != EOF) { - /* skip whitespace */ - if ((c != ' ') && isspace(c)) continue; - - /* take care of long lines */ - if (w++ >= MAX_W) { - printf("\"\n\""); - w = 0; - } - - /* escape non-printable characters */ - if (!isprint(c)) { - printf("\\x%02x", c); - continue; - } - -#define C(_m, _p) case _m : printf(_p); break - - switch (c) { - C('\\', "\\\\"); /* backslashes */ - C('"' , "\\\""); /* double quotes */ - default: - putchar(c); - } - } - printf("\"\n"); - - /* check whether an error was produced */ - if (ferror(stdout)) { - fprintf(stderr, "%s: error writing output, %s\n", argv[0], strerror(errno)); - fflush(stderr); - exit(EXIT_FAILURE); - } - if ((c == EOF) && ferror(stdin)) { - fprintf(stderr, "%s: error reading input, %s\n", argv[0], strerror(errno)); - fflush(stderr); - exit(EXIT_FAILURE); - } - - return EXIT_SUCCESS; -} -