#!/usr/local/bin/perl
# Extract entries from the CSS properties database
# The database has the following format:
# name;; values;; initial-value;; applies-to;; 
# inherited;; percentages;; media;; computed value
# Special characters: 
#  1) Translate "*" into the words "all elements"
#  1.a) Translate "XX" into "not defined for shorthand properties"
#  2) "<name>" means the value is defined elsewhere. Translate to
#     <span class="value-inst-name">&lt;name&gt;</span>
#  3) "<'name'>" means the set of values is the same as for the
#      property with the same name. Translate to
#     <span class="propinst-name">&lt;'name'&gt;</span>     
#
# Ian Jacobs - ij@w3.org (Based on work by Arnaud Le Hors)
# $Id: pextr,v 1.31 2006-10-09 18:55:53 ihickson Exp $
#

$PROGNAME = substr($0, rindex($0, "/") + 1);

if (!$ARGV[0]) {
    print "Usage: $PROGNAME file [tgt_dir]\n";
    exit 1;
}

# copy file in memory
if (!open(input, $ARGV[0])) {
    print "$PROGNAME Error: Cannot open file: $ARGV[0]\n";
    exit 1;
}
$buf = "";
while (<input>) {
    $buf .= $_;
}
close(input);

$path = "";
if ($ARGV[1]) {
    $path = "$ARGV[1]/";
}

# regexps
$sp = "[ \t\n]*";		# whitespace
$patt = "$sp(.*?)$sp";
$blockst = "[/][*]";
$blocket = "[*][/]";

sub format_property {
my $name = $_[0]; 
my $values = $_[1]; 
my $init = $_[2];
my $applies = $_[3];
my $inherited = $_[4];
my $percentages = $_[5];
my $media = $_[6];
my $compval = $_[7];
#Do next three lines in src files to get headings right.
print output "<div class=\"propdef\">\n";
print output "<dl><dt>\n";
@names = split(/[, \t\n]+/, $name);
$comma = "";
foreach $n (@names) {
  print output "$comma<span class=\"index-def\" title=\"'$n'\"><a name=\"propdef-$n\" class=\"propdef-title\"><strong>'$n'</strong></a></span>";
  $comma = ", ";
}
print output "\n<dd>\n";
print output "<table class=\"propinfo\" cellspacing=0 cellpadding=0>\n";

# We use cellspacing/cellpadding until more browsers
# handle tables correctly.
&format_values($values);
&format_init($init);
&format_applies($applies);
&format_inherited($inherited);
&format_percentages($percentages);
&format_media($media);
&format_compval($compval);
print output "</table>\n";
print output "</dl>\n";
print output "</div>\n\n";
}  

sub clean {
$_[0] =~ s/<([^>']*)>/<span class=\"value-inst-$1\">&lt;$1&gt;<\/span>/g;
$_[0] =~ s/<[']([^']*)[']>/<span class=\"propinst-$1\">&lt;'$1'&gt;<\/span>/g;
$_[0] =~ s/\{([^\}]*)\}([^{]*)\{\}/<a href=\"$1\">$2<\/a>/g;
return $_[0];
}

# Include an nbsp before TD since CSS not used to
# align tables yet.
sub format_values {
$values = clean($_[0]);
$values =~ s/inherit/<span class=\"value-inst-inherit\">inherit<\/span>/g;
print output "<tr valign=baseline>";
print output "<td><em>Value:</em>&nbsp;&nbsp;";
print output "<td>$values\n";
}

sub format_init {
#Note that in the case of properties, <> are removed in text.
$init = clean($_[0]);
$init =~ s/XX/not defined for shorthand properties/g;
print output "<tr valign=baseline>";
print output "<td><em>Initial:</em>&nbsp;&nbsp;";
print output "<td>$init\n";
}

sub format_applies {
#Note that in the case of properties, <> are removed in text.
$applies = clean($_[0]);
$applies =~ s/^\*$/all elements/g;
print output "<tr valign=baseline>";
print output "<td><em>Applies to:</em>&nbsp;&nbsp;";
print output "<td>$applies\n";
}

sub format_inherited {
$inherited = clean($_[0]);
print output "<tr valign=baseline>";
print output "<td><em>Inherited:</em>&nbsp;&nbsp;";
print output "<td>$inherited\n";
}

sub format_percentages {
#Note that in the case of properties, <> are removed in text.
$perc = clean($_[0]);
print output "<tr valign=baseline>";
print output "<td><em>Percentages:</em>&nbsp;&nbsp;";
print output "<td>$perc\n";
}

sub format_media {
    $fmedia = $_[0];
    $fmedia =~ s/visual/<a href=\"media.html#visual-media-group\" class=\"noxref\">visual<\/a>/;
    $fmedia =~ s/aural/<a href=\"aural.html#aural-media-group\" class=\"noxref\">aural<\/a>/;
    $fmedia =~ s/tactile/<a href=\"media.html#tactile-media-group\" class=\"noxref\">tactile<\/a>/;
    $fmedia =~ s/continuous/<a href=\"media.html#continuous-media-group\" class=\"noxref\">continuous<\/a>/;
    $fmedia =~ s/paged/<a href=\"media.html#paged-media-group\" class=\"noxref\">paged<\/a>/;
    $fmedia =~ s/grid/<a href=\"media.html#grid-media-group\" class=\"noxref\">grid<\/a>/;
    $fmedia =~ s/interactive/<a href=\"media.html#interactive-media-group\" class=\"noxref\">interactive<\/a>/;
    $fmedia =~ s/static/<a href=\"media.html#static-media-group\" class=\"noxref\">static<\/a>/;
    $fmedia =~ s/all/<a href=\"media.html#all-media-group\" class=\"noxref\">all<\/a>/;
    print output "<tr valign=baseline>";
    print output "<td><em>Media:</em>&nbsp;&nbsp;";
    print output "<td>$fmedia\n";
}

sub format_compval {
$values = clean($_[0]);
$values =~ s/inherit/<span class=\"value-inst-inherit\">inherit<\/span>/g;
print output "<tr valign=baseline>";
print output "<td><em>Computed&nbsp;value:</em>&nbsp;&nbsp;";
print output "<td>$values\n";
}


# extract every possible block
$_ = $buf;
while (/$blockst(?:.*?)$blocket/s) {
  $placeholder = $';
  my $h = $&;
  if ($h =~ /$blockst$patt;;$patt;;$patt;;$patt;;$patt;;$patt;;$patt;;$patt$blocket/s) {
    # Set parameters first (before any modifications)
    my $props = $1;
    my $values = $2;
    my $init = $3;
    my $applies = $4;
    my $inherited = $5;
    my $percentage = $6;
    my $media = $7;
    my $compval = $8;
    my @names = split(/[, \t\n]+/, $props);
    my $name = $names[0];
    $output = "$path$name.srb";
    print "\textracting $output\n";
    open(output, "> $output");
    &format_property($props, $values, $init, $applies, $inherited,
		     $percentage, $media, $compval);
    close(output);
  } else {
    warn "Incorrect syntax for property definition: $h\n";
  }
  $_ = $placeholder;
}
