forked from whatwg/html-build
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.pre-process-annotate-attributes.pl
88 lines (85 loc) · 3.37 KB
/
.pre-process-annotate-attributes.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/perl -w
use strict;
use v5.10.0;
my $state = undef;
my $mode = 'bored';
my %attributes = ();
my @lines = ();
my %instances = ();
while (defined($_ = <>)) {
my $line = \"$_";
push(@lines, $line);
if ($_ eq " <h3 class=\"no-num\">Attributes</h3>\n") {
$mode = 'index';
} else {
if ($mode eq 'bored') {
if ($_ eq " <dt><span data-x=\"concept-element-attributes\">Content attributes</span>:</dt>\n") {
$mode = 'element';
}
} elsif ($mode eq 'element') {
if ($_ eq " <dd><span>Global attributes</span></dd>\n") {
# ignore
} elsif ($_ =~ m!^ <dd>.*<code data-x="((?:attr-|handler-)[^"]+)">.+</code>(.*)</dd>\n$!os) {
my $key = $1;
my $notes = $2;
my $special = '';
$special = 'global' if $notes =~ m!special semantics!os;
$special = 'alt' if $notes =~ m@<!-- variant -->@os;
if (not exists $instances{$key}) {
$instances{$key} = [];
}
push(@{$instances{$key}}, { line => $line, special => $special });
} elsif ($_ =~ m/^ <!--.*-->\n$/os) {
# ignore
} elsif ($_ eq " <dd>Any other attribute that has no namespace (see prose).</dd>\n") {
# ignore
} elsif ($_ =~ m!^ <dt>!o) {
$mode = 'bored';
} else {
die "mode=$mode saw: $_";
}
} elsif ($mode eq 'index') {
if ($_ eq " </table>\n") {
$mode = 'end';
} elsif ($_ =~ m!^ <td> <code data-x="([^"]+)">[^<]*</code>;?\n$!os) {
$attributes{$1} = 1;
$mode = 'index-in';
} else {
# ignore...
}
} elsif ($mode eq 'index-in') {
if ($_ =~ m!^ <code data-x="([^"]+)">[^<]*</code>;?\n$!os) {
$attributes{$1} = 1;
} elsif ($_ =~ m@^ <td> (.+?)(?:<!--or: (.+)-->)?\n$@os) {
local $" = ', ';
my $description = $1;
my $altdescription = $2;
foreach my $key (keys %attributes) {
die "can't find instance of $key\n" unless exists $instances{$key} and scalar @{$instances{$key}};
foreach my $entry (@{$instances{$key}}) {
my $line = $entry->{line};
if ($entry->{special} eq 'global') {
$$line =~ s!(\.</dd>\n)$!: $description<\!--SPECIAL-->$1!os;
$$line =~ s!<\!--SPECIAL-->: *([^ ])!; \l$1!os;
} elsif ($entry->{special} eq 'alt') {
die "$key wants alt description but we have none" unless defined $altdescription;
$$line =~ s!(</dd>\n)$! — $altdescription$1!os;
} else {
$$line =~ s!(</dd>\n)$! — $description$1!os;
}
}
}
%attributes = ();
$mode = 'index';
} else {
die "$.: unexpected line in index: $_";
}
} else {
# ignore
}
}
}
foreach (@lines) {
$$_ =~ s/<!--SPECIAL-->//gos;
print $$_;
}