#!/usr/bin/perl -w
# An exceptionally hacky script that performs the same kind of automatic
# linking of elements that is done for the SVG specifications, based
# on the definitions*.xml files in this directory.
#
# (Warning: regular expression based munging of XML ahead.)
use strict;
use utf8;
binmode(STDOUT, ":utf8");
sub loaddefs {
readdefs('definitions-SVG11.xml', 'http://www.w3.org/TR/2011/REC-SVG11-20110816/');
readdefs('definitions-filters.xml', '');
}
sub readfile {
my $fh;
my $fn = shift;
local $/;
open $fh, $fn;
binmode $fh, ':utf8';
my $s = join('', <$fh>);
return $s;
}
sub dec {
my $s = shift;
$s =~ s/\<//g;
$s =~ s/\'/>/g;
$s =~ s/\&/\&/g;
return $s;
}
sub resolve {
my $base = shift;
my $href = shift;
if ($href =~ /^http/) {
return $href;
}
return "$base$href";
}
my $htmlfn = $ARGV[0] or die;
my $html = readfile($htmlfn);
my $dfn;
my %dfns;
while ($html =~ /]*)>(.*?)<\/dfn>/gs) {
my $attrs = $1;
my $name = $2;
if ($attrs =~ /title=(?:"(.*?)"|'(.*?)')/s) {
$name = $1 || $2;
}
$dfns{$name} = 1;
}
my %attributeCategories;
my %elementCategories;
my %elements;
my %properties;
my %interfaces;
my %attributes;
my %terms;
my %commonAttributes;
sub readdefs {
my $fn = shift;
my $base = shift;
my $defs = readfile($fn);
while ($defs =~ s/ [element summary table for '$name']<$name>
";
} elsif (defined $attributes{$name}) {
if (scalar(keys(%{$attributes{$name}})) > 1) {
print STDERR "ambiguous reference '$name' to attribute; specify 'elementname/$name' instead\n";
return "$text";
} else {
my $href = join('', values(%{$attributes{$name}}));
return "‘$name‘";
}
} elsif (defined $properties{$name}) {
return "‘$name‘";
}
print STDERR "unknown element, attribute or property '$1'\n";
return "$text";
} elsif ($text =~ /^'([^ \/]*) element'$/) {
my $name = $1;
unless (defined $elements{$name}) {
print STDERR "unknown element '$1'\n";
return "$text";
}
return "<$name>
";
} elsif ($text =~ /^'([^ \/]*) attribute'$/) {
my $name = $1;
unless (defined $attributes{$name}) {
print STDERR "unknown attribute '$1'\n";
return "$text";
}
if (scalar(keys(%{$attributes{$name}})) > 1) {
print STDERR "ambiguous reference '$name attribute' to attribute; specify 'elementname/$name' instead\n";
return "$text";
} else {
my $href = join('', values(%{$attributes{$name}}));
return "$name";
}
} elsif ($text =~ /^'([^ \/]*) property'$/) {
my $name = $1;
unless (defined $properties{$name}) {
print STDERR "unknown element '$1'\n";
return "$text";
}
return "$name";
} elsif ($text =~ /^'([^ ]*)\/([^ ]*)'$/) {
my $eltname = $1;
my $attrname = $2;
unless (defined $elements{$eltname} && defined $elements{$eltname}{attributes}{$attrname}) {
print STDERR "unknown attribute '$attrname' on element '$eltname'\n";
return "$text";
}
return "$attrname";
} elsif ($text =~ /^<(.*)>$/) {
my $symname = $1;
unless (defined $terms{"<$symname>"}) {
print STDERR "unknown grammar symbol <$symname>\n";
return "<$symname>";
}
my $href = $terms{"<$symname>"};
return "<$symname>";
} else {
$text =~ s/^\s+//;
$text =~ s/\s+$//;
$text =~ s/\s/ /gs;
unless (defined $terms{$text}) {
print STDERR "unknown term '$text'\n";
return "$text";
}
return "$text";
}
}
sub elementSummary {
my $name = shift;
my $lcname = lc $name;
unless (defined $elements{$name}) {
return "';
for my $cat (@{$elements{$name}{elementcategories}}) {
$model .= "
';
}
}
my $attributes = '';
if (defined $elements{$name}{attributecategories}) {
my @others;
for my $cat (@{$elements{$name}{attributecategories}}) {
if ($cat eq 'presentation') {
$attributes .= "$attributes
";
}
my $interfaces;
if (defined $elements{$name}{interfaces}) {
$interfaces = join(', ', map { "$_" }
@{$elements{$name}{interfaces}});
} else {
$interfaces = 'None.';
}
return <
EOF
}
loaddefs();
$html =~ s{(.*?)<\/a>}{&link($1)}egs;
$html =~ s{}{&elementSummary($1)}egs;
print $html;
Name:
$name
Categories:
$cats
Content model:
$model
Attributes:
$attributes
DOM Interfaces:
$interfaces