#! /usr/bin/gawk -f
# Last edited on 2008-02-04 18:24:53 by stolfi
BEGIN{
usage = ( ARGV[0] " [ -v title=STRING ] < INDEXFILE > INDEXPAGE" );
abort = -1;
# Reads from stdin a list of lines of the form KEY IMGDIR,
# sorted by KEY. Assigns an integer page number PAGE to each
# distinct KEY. Then writes to stdout a master HTML index with
# all keys, and creates two files for each PAGE:
#
# "htitle/PAGE" with the corresponding KEY, and
# "entries/PAGE" with the corresponding IMGDIRs, one per line.
#
# In the "htitle/PAGE" file, the KEY has its braces removed,
# "_" replaced by " ", and the characters <"'> quoted as in HTML.
if (title == "")
{ title = "Image index"; }
else
{ title = iso_to_html(title); }
pg = 0;
okey = "";
# Print header:
printf "\n";
printf "
\n";
printf "%s", title;
printf "\n";
printf "\n";
printf "\n";
printf "%s
", title;
printf "\n";
printf "\n";
}
(abort >= 0) { exit abort; }
/^[ \011]*([#]|$)/ { next; }
/./{
if (NF != 2) { data_error(("bad format")); }
key = $1;
img = $2;
if(key != okey)
{ if (pg > 0) { close(entryfile); }
pg++; pg = sprintf("%05d",pg);
entryfile = ("entries/" pg);
titlefile = ("htitle/" pg);
okey = key;
pgtitle = key;
gsub(/[{}]/, "", pgtitle);
gsub(/[_]/, " ", pgtitle);
pgtitle = iso_to_html(pgtitle);
printf " - %s\n", pg, pgtitle;
printf "%s\n", pgtitle > titlefile;
close(titlefile);
}
printf "%s/p.png\n", img > entryfile;
}
END{
if (abort >= 0) { exit abort; }
if (pg > 0) { close(entryfile); }
printf "
\n";
printf "\n";
printf "\n";
close("/dev/stdout");
}
function iso_to_html(str)
{
gsub(/&/, "\\&", str); # "\\"
gsub(/"/, "\\"", str); # "\\"
gsub(/, "\\<", str); # "\\"
gsub(/>/, "\\>", str); # "\\"
return str;
}
function data_error(f,n,msg)
{
printf "line %d: %s\n", FNR, msg > "/dev/stderr";
abort = 1;
exit 1
}