:
# Insert a table of properties into an HTML file
#
# $1 = HTML file to insert into, replacing "<!--KEY-->"
# $2 (optional) = key to replace, default "properties"
# stdin = tab-separated database of properties
#
# Assumes stdin conforms to the output of propdeftotab, which should be close to
# ~P~Value:~V~Initial:~I~Applies to:~A~Inherited:~I~Percentages:~P~Media:~M
# where ~ is a tab.
#
# Author: Bert Bos
# Created: 14 Nov 2000
# Version: $Id: tabtoproptable,v 1.7 2012-10-09 12:27:47 bbos Exp $


# usage -- print usage message and exit
function usage
{
  echo "$1 HTML-file [key]" >&2
  exit 1
}


TMP1=/tmp/tabtopropdeftable1-$$
TMP2=/tmp/tabtopropdeftable2-$$
trap "rm $TMP1 $TMP2" 0

if [ "$1" == "" ]; then usage ${0##*/}; else html="$1"; shift; fi
if [ "$1" == "" ]; then key="properties"; else key="$1"; shift; fi

if [ "$key" == "properties" ]; then
  cat >$TMP2 <<'EOD'
BEGIN {
  FS = "\t";

  # The expected structure.
  #
  fieldno["nam"] = 3;
  label["nam"] = "Name";
  pat["nam"] = "^ *[Nn]ame: *$";

  fieldno["val"] = 5;
  label["val"] = "Value";
  pat["val"] = "^ *[Vv]alue: *$";

  fieldno["ini"] = 7;
  label["ini"] = "Initial";
  pat["ini"] = "^ *[Ii]nitial: *$";

  fieldno["app"] = 9;
  label["app"] = "Applies to";
  pat["app"] = "^ *[Aa]pplies( | |&nbsp;)[Tt]o: *$";

  fieldno["inh"] = 11;
  label["inh"] = "Inherited";
  pat["inh"] = "^ *[Ii]nherited: *$";

  fieldno["ani"] = 13;
  label["ani"] = "Animatable";
  pat["ani"] = "^ *[Aa]nimatable: *$";

  fieldno["per"] = 15;
  label["per"] = "Percentages";
  pat["per"] = "^ *[Pp]ercentages: *$";

  fieldno["med"] = 17;
  label["med"] = "Media";
  pat["med"] = "^ *[Mm]edia: *$";

  fieldno["com"] = 19;
  label["com"] = "Computed value";
  pat["com"] = "^ *[Cc]omputed( | |&nbsp;)[Vv]alue: *$";

  fieldno["can"] = 21;
  label["can"] = "Canonical order";
  pat["can"] = "^ *[Cc]anonical( | |&nbsp;)[Oo]rder: *$";
}

{
  # Find the fields in the input line.
  #
  delete x;
  for (i = 1; i < NF; i++)
    for (k in pat)
      if ($i ~ pat[k]) {i++; x[k] = $i; break}

  # Print a TR with the information we found. (Could be empty...)
  #
  name = x["nam"];
  gsub(/, */, "</span>, <span class=\"property\">", name);
  name = "<span class=\"property\">" name "</span>";
  printf "<tr><th>%s</th>", name;
  printf "<td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td>",
    x["val"], x["ini"], x["app"], x["inh"], x["per"], x["med"];
  printf "</tr>\n";

  # Check that the property definition has the expected
  # structure. Only report the first error found.
  #
  for (k in fieldno) {
    if (!(k in x)) {
      printf "Warning: (%s): Missing \"%s\" field\n",
        x["nam"], label[k] >"/dev/stderr";
      break;
    }
    if ($(fieldno[k]) !~ pat[k]) {
      printf "Warning: (%s): Found \"%s\" instead of \"%s\"\n",
        x["nam"], $(fieldno[k]), label[k] >"/dev/stderr";
      break;
    }
  }
}
EOD
elif [ "$key" == "descriptors" ]; then
  cat >$TMP2 <<'EOD'
BEGIN {
  FS = "\t";

  # The expected structure.
  #
  fieldno["nam"] = 3;
  label["nam"] = "Name";
  pat["nam"] = "^ *[Nn]ame: *$";

  fieldno["val"] = 5;
  label["val"] = "Value";
  pat["val"] = "^ *[Vv]alue: *$";

  fieldno["ini"] = 7;
  label["ini"] = "Initial";
  pat["ini"] = "^ *[Ii]nitial: *$";

  fieldno["per"] = 9;
  label["per"] = "Percentages";
  pat["per"] = "^ *[Pp]ercentages: *$";

  fieldno["med"] = 11;
  label["med"] = "Media";
  pat["med"] = "^ *[Mm]edia: *$";
}

{
  # Find the fields in the input line.
  #
  delete x;
  for (i = 1; i < NF; i++)
    for (k in pat)
      if ($i ~ pat[k]) {i++; x[k] = $i; break}

  # Print a TR with the information we found. (Could be empty...)
  #
  name = x["nam"];
  gsub(/, */, "</span>, <span class=\"descriptor\">", name);
  name = "<span class=\"descriptor\">" name "</span>";
  printf "<tr><th>%s</th>", name;
  printf "<td>%s</td><td>%s</td><td>%s</td><td>%s</td>",
    x["val"], x["ini"], x["per"], x["med"];
  printf "</tr>\n";

  # Check that the descriptor definition has the expected
  # structure. Only report the first error found.
  #
  for (k in fieldno) {
    if (!(k in x)) {
      printf "Warning: (%s): Missing \"%s\" field\n",
        x["nam"], label[k] >"/dev/stderr";
      break;
    }
    if ($(fieldno[k]) !~ pat[k]) {
      printf "Warning: (%s): Found \"%s\" instead of \"%s\"\n",
        x["nam"], $(fieldno[k]), label[k] >"/dev/stderr";
      break;
    }
  }
}
EOD
else
  echo "${0##*/}: key must be \"properties\" or \"descriptors\"" >&2
  exit 1
fi

if [ "$key" == "properties" ]; then
  labels="<th>Property</th><th>Values</th><th>Initial</th>"
  labels="$labels<th>Applies&nbsp;to</th><th>Inh.</th>"
  labels="$labels<th>Percentages</th><th>Media</th>"
else
  labels="<th>Descriptor</th><th>Value</th><th>Initial</th>"
  labels="$labels<th>Percentages</th><th>Media</th>"
fi

echo "<!--begin-${key}-->" >$TMP1
echo "<table class=proptable>" >>$TMP1
echo "<thead>" >>$TMP1
echo "<tr>${labels}</tr>" >>$TMP1
echo "</thead>" >>$TMP1
echo "<tbody>" >>$TMP1
awk -f $TMP2 $2 | sort -bdf >>$TMP1
echo "</tbody>" >>$TMP1
echo "</table>" >>$TMP1
echo "<!--end-${key}-->" >>$TMP1

# First insert newlines before and after the comment, then replace the
# comment by the file we just generated:

sed \
  -e 's/<!--[ 	]*'$key'[ 	]*-->/\
&\
/' \
  -e 's/<!--[ 	]*begin[ -]'$key'[ 	]*-->/\
&/' \
  -e 's/<!--[ 	]*end[ -]'$key'[ 	]*-->/&\
/' "$html" | \
  sed \
  -e '/<!--[ 	]*begin[ -]'$key'[ 	]*-->/r '$TMP1 \
  -e '/<!--[ 	]*'$key'[ 	]*-->/r '$TMP1 \
  -e '/<!--[ 	]*begin[ -]'$key'[ 	]*-->/,/<!--[ 	]*end[ -]'$key'[ 	]*-->/d' \
  -e '/<!--[ 	]*'$key'[ 	]*-->/d'

# Local variables:
# mode: ksh
# End:
