pull/4/head
Adrian Perez 2007-08-24 13:47:59 +02:00
parent f3cfbd01b1
commit 8d7357aa50
26 changed files with 188 additions and 224 deletions

View File

@ -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 ``<!-- var identifier -->`` 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
``<title>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 ``</title>`` tag
opened in the previous template and add further output until the closing
``</head>`` 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
-

View File

@ -22,7 +22,7 @@
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#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

View File

@ -1,34 +1,14 @@
/*
* ngx_http_fancyindex_module.h
* Copyright © 2007 Adrian Perez <adrianperez@udc.es>
*
* 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
*/

35
template.awk Executable file
View File

@ -0,0 +1,35 @@
#! /usr/bin/awk -f
#
# Copyright © Adrian Perez <adrianperez@udc.es>
#
# 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! */"
}
/^<!--[[:space:]]*var[[:space:]]+[^[:space:]]+[[:space:]]*-->$/ {
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 ";";
}

63
template.h Normal file
View File

@ -0,0 +1,63 @@
/* Automagically generated, do not edit! */
static const u_char t01_head1[] = ""
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\""
"\"http://www.w3.or/TR/xhtml1/DTD/xhtml1-strict.dtd\">"
"<html xmlns=\"http://www.w3.or/1999/xhtml\">"
"<head>"
"<style type=\"text/css\">"
"body, html {"
"backround: #fff;"
"}"
"tr.o {"
"backround: #eee;"
"}"
"th {"
"font-weiht: bold;"
"backround: #ddd;"
"}"
"</style>"
"<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\"/>"
"<title>Index of "
;
static const u_char t02_head2[] = ""
"</title>"
"</head>"
;
static const u_char t03_body1[] = ""
"<body>"
"<h1>"
;
static const u_char t04_body2[] = ""
"</h1>"
;
static const u_char t05_list1[] = ""
"<table>"
"<colroup>"
"<col width=\"55%\"/>"
"<col width=\"20%\" style=\"text-alin: riht\"/>"
"<col width=\"25%\"/>"
"</colroup>"
"<thead>"
"<tr>"
"<th>File Name</th>"
"<th>File Size</th>"
"<th>Date</th>"
"</tr>"
"</thead>"
"<tbody>"
"<tr>"
"<td colspan=\"3\"><a href=\"../\">Parent Directory</a></td>"
"</tr>"
;
static const u_char t06_list2[] = ""
"</tbody>"
"</table>"
;
static const u_char t07_body3[] = ""
;
static const u_char t08_body4[] = ""
;
static const u_char t09_foot1[] = ""
"</body>"
"</html>"
;

53
template.html Normal file
View File

@ -0,0 +1,53 @@
<!-- var t01_head1 -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style type="text/css">
body, html {
background: #fff;
}
tr.o {
background: #eee;
}
th {
font-weight: bold;
background: #ddd;
}
</style>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Index of
<!-- var t02_head2 -->
</title>
</head>
<!-- var t03_body1 -->
<body>
<h1>
<!-- var t04_body2 -->
</h1>
<!-- var t05_list1 -->
<table>
<colgroup>
<col width="55%"/>
<col width="20%" style="text-align: right"/>
<col width="25%"/>
</colgroup>
<thead>
<tr>
<th>File Name</th>
<th>File Size</th>
<th>Date</th>
</tr>
</thead>
<tbody>
<tr>
<td colspan="3"><a href="../">Parent Directory</a></td>
</tr>
<!-- var t06_list2 -->
</tbody>
</table>
<!-- var t07_body3 -->
<!-- var t08_body4 -->
<!-- var t09_foot1 -->
</body>
</html>

View File

@ -1,6 +0,0 @@
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"\"http://www.w3"
".org/TR/xhtml1/DTD/xhtml1-strict.dtd\"><html xmlns=\"http://www.w3.org/19"
"99/xhtml\"><head><style type=\"text/css\">body, html {background: #fff;}tr"
".o {background: #eee;}th {font-weight: bold;background: #ddd;}</style><"
"meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\"/><tit"
"le>Index of "

View File

@ -1,18 +0,0 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style type="text/css">
body, html {
background: #fff;
}
tr.o {
background: #eee;
}
th {
font-weight: bold;
background: #ddd;
}
</style>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Index of

View File

@ -1 +0,0 @@
"</title></head>"

View File

@ -1,2 +0,0 @@
</title>
</head>

View File

@ -1 +0,0 @@
"<body><h1>"

View File

@ -1,2 +0,0 @@
<body>
<h1>

View File

@ -1 +0,0 @@
"</h1>"

View File

@ -1 +0,0 @@
</h1>

View File

@ -1,4 +0,0 @@
"<table><colgroup><col width=\"55%\"/><col width=\"20%\" style=\"text-align:"
" right\"/><col width=\"25%\"/></colgroup><thead><tr><th>File Name</th><th>"
"File Size</th><th>Date</th></tr></thead><tbody><tr><td colspan=\"3\"><a h"
"ref=\"../\">Parent Directory</a></td></tr>"

View File

@ -1,17 +0,0 @@
<table>
<colgroup>
<col width="55%"/>
<col width="20%" style="text-align: right"/>
<col width="25%"/>
</colgroup>
<thead>
<tr>
<th>File Name</th>
<th>File Size</th>
<th>Date</th>
</tr>
</thead>
<tbody>
<tr>
<td colspan="3"><a href="../">Parent Directory</a></td>
</tr>

View File

@ -1 +0,0 @@
"</tbody></table>"

View File

@ -1,2 +0,0 @@
</tbody>
</table>

View File

@ -1 +0,0 @@
""

View File

@ -1 +0,0 @@

View File

@ -1 +0,0 @@
""

View File

@ -1 +0,0 @@

View File

@ -1 +0,0 @@
"</body></html>"

View File

@ -1,2 +0,0 @@
</body>
</html>

View File

@ -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
#

View File

@ -1,88 +0,0 @@
/*
* t2inc.c
* Copyright (C) 2007 Adrian Perez <adrianperez@udc.es>
*
*
* 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 <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include <errno.h>
#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;
}