-
Notifications
You must be signed in to change notification settings - Fork 67
/
eep-index.pl
executable file
·410 lines (374 loc) · 8.93 KB
/
eep-index.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#! /usr/bin/perl
##
## Scan headers of all EEPs and fill in the index
## in the tagged places. Preprocess Markdown.
##
## Copyright 2010: Erlang/OTP, Raimo Niskanen
## This document has been placed in the public domain.
#
#
# Argument: EEP_dir/Index_file.suffix
#
# Expand these markdown constructs into HTML tables on output:
#
# [Prefix] : <$me:>
# [Table Caption]: <$me:index/col1/col2...>
#
require 5.008_000;
use strict;
use warnings;
my $me = $0;
$me =~ s| .* / ||x;
$me =~ s| [.] .* $||x;
my $index = shift;
die "Missing argument: index file to fill in" unless defined $index;
$index =~ m|^ ((?:.* /)?) .*? ((?:[.] .*)*) $|x;
my $dir = $1;
my $suffix = $2;
my $lf = "\n";
my $type_suffix = ' EEP';
my $status_suffix = ' proposal';
my @type =
('Standards Track' => 'S',
'Process' => 'P',
);
my @status =
('Active' => '',
'Draft' => '',
'Accepted' => 'A',
'Rejected' => 'R',
'Replaced' => 'P',
'Deferred' => 'D',
'Final' => 'F',
);
# Index lists
my @file = ();
my @tag = ();
my @owner = ();
# Data tables
my (%eep, %num, %status, %type, %title, %owner); # $file indexed
my (%tag, %desc); # $status indexed
my (%email, %author); # $owner indexed
# Mapping of index name in HTML table spec to array variable
my %index =
('file' => \@file,
'tag' => \@tag,
'owner' => \@owner,
);
# Mapping of HTML table column name to table variable
my %table =
('status' => \%status,
'num' => \%num,
'title' => \%title,
'owner' => \%owner,
'tag' => \%tag,
'description' => \%desc,
'email' => \%email,
'author' => \%author,
);
# Mapping of EEP Type:
my %type_map;
while (@type) {
my ($type, $tag);
($type, $tag, @type) = @type;
my $t = lc $type;
$type_map{$t} = $tag;
$tag{$t} = $tag;
$desc{$t} = $type.$type_suffix;
push @tag, $t;
}
@type = undef;
# Mapping of EEP Status:
my %status_map;
while (@status) {
my ($status, $tag);
($status, $tag, @status) = @status;
my $s = lc $status;
$status_map{$s} = $tag;
$tag{$s} = $tag;
$desc{$s} = $status.$status_suffix;
push @tag, $s unless $tag eq '';
}
@status = undef;
my @warnings = ();
# These set_* subroutines actually get their argument in $_
my $file; # heavily used hidden parameter
sub set_eep {
unless (m|^ \s* \d+ \s* $|x) {
push @warnings, "File $file: 'eep: $_' not decimal!";
}
if (defined $num{$file}) {
push
@warnings,
"File $file: 'eep: $num{$file}' already seen in $num{$file}!";
}
push @file, $file;
$eep{$file} = $_;
$num{$file} = "[$_][EEP $_]";
}
sub set_title {
if (m| (?<!\\) \`\` |x) {
push
@warnings,
"File $file: 'title:' contains double backtick!";
}
$title{$file} = "\`\` $_ \`\`";
}
sub set_author {
my $author;
my $email = '';
my @authors = split m|(?<=\S) \s* , \s* (?=\S)|x;
if ($authors[0] =~ m|^ (.*?) \s* (\< .* \>) \s* $|x) {
($author, $email) = ($1, $2);
if ($email =~ m| (?<!\\) \`\` |x) {
push
@warnings,
"File $file: 'title:' email contains double backtick!";
}
$email = "\`\` $email \`\`";
} else {
$author = $authors[0];
}
$owner{$file} = $author;
if (defined $author{$author}) {
if ($email ne $email{$author}) {
push
@warnings,
"File $file: 'author:' email $email ne $email{$author}!";
}
} else {
push @owner, $author;
$author{$author} = $author;
$email{$author} = $email;
}
}
sub set_status {
m|^ ([^/]*) (/? [^\s]*) \s* ([^;]*)|x;
my ($status, $tag, $desc) = ($1, $2, $3);
my $s = $status_map{lc $status};
unless (defined $s) {
push @warnings, "File $file: 'status: $status' illegal!";
}
$status{$file} = $s.$tag;
if ($tag ne '') {
if (defined $tag{$tag}) {
if ($desc ne $desc{$tag}) {
push
@warnings,
"File $file: 'status:' tag $desc ne $desc{$tag}!";
}
} else {
push @tag, $tag;
$tag{$tag} = $tag;
$desc{$tag} = $desc;
}
}
}
sub set_type {
my $type = $_;
my $t = $type_map{lc $type};
unless (defined $t) {
push @warnings, "File $file: 'type: $type' illegal!";
}
$type{$file} = $t;
}
# Mapping of EEP header tag to handler set_* function
my %set =
('eep' => \&set_eep,
'title' => \&set_title,
'version' => 0,
'last-modified' => 0,
'author' => \&set_author,
'discussions-to' => 0,
'status' => \&set_status,
'type' => \&set_type,
'content-type' => 0,
'requires' => 0,
'created' => 0,
'erlang-version' => 0,
'post-history' => sub {},
'replaces' => 0,
'replaced-by' => 0,
);
sub check_erlang_version {
my $version = $_;
if (defined($version)) {
push @warnings, "File $file: 'version: $version' illegal!"
unless $version =~ /OTP_R[0-9]+[AB]([-][0-9])?/ || $version =~ /OTP-([0-9]+)[.]/;
} elsif ($status{$file} =~ /^F\//) {
push @warnings, "File $file: 'version: EEPs in status Final must have an Erlang-Version!";
}
}
# Mapping of EEP header tag to handler check_* function
my %check =
('eep' => 0,
'title' => 0,
'version' => 0,
'last-modified' => 0,
'author' => 0,
'discussions-to' => 0,
'status' => 0,
'type' => 0,
'content-type' => 0,
'requires' => 0,
'created' => 0,
'erlang-version' => \&check_erlang_version,
'post-history' => 0,
'replaces' => 0,
'replaced-by' => 0,
);
sub store_key {
my ($hash, $key, $value) = @_;
unless (defined $set{$key}) {
push
@warnings,
"File $file: '$key:' unknown header - file skipped!";
}
if ($set{$key} || $check{$key}) {
if (defined $$hash{$key}) {
push
@warnings,
"File $file: '$key:' double header - file skipped!";
return 0;
}
$$hash{$key} = $value;
}
return 1;
}
$\ = $lf;
open INDEX, '<', $index or die "Can't open $index: $!";
# Gather info from headers
#
opendir DIR, $dir or die "Can't open directory $dir: $!";
while ($file = readdir DIR) {
next if $file =~ m|^ [.]{1,2} $|x;
next unless $file =~ m|$suffix$|;
open EEP, '<', $dir.$file or next;
my (%hdr, $key, $value);
my $ws; # leading whitespace
LINE: while (<EEP>) {
chomp;
if (defined $ws) {
if (m|^$ws(\s.*)|) {
# same leading whitespace as previous line - concatenate
$value .= $1;
next LINE;
} else {
$ws = undef;
}
}
my $line = $_;
#
if (defined $key) {
# we now have a complete header in $key, $value
# check it and store in %hdr
$key = lc($key);
if ($key =~ m|^ eep \s+ (\d+) $|x) {
# special treatment of EEP headline
store_key \%hdr, 'eep', $1
or last LINE;
store_key \%hdr, 'title', $value
or last LINE
} else {
store_key \%hdr, $key, $value
or last LINE;
}
$key = undef;
$value = undef;
}
#
if ($line =~ m|(^\s*)([^:]+):\s*(.*)|) {
# header line
($ws, $key, $value) = ($1, $2, $3);
} elsif ($line =~ m|\s* \* \s* \* \s* \* [\s\*]*|x
or $line =~ m|\s* - \s* - \s* - [-\s]*|x) {
# horizontal rule
next LINE;
} elsif ($line =~ m|^\s*$|) { # blank line
# end of headers - process them all
next LINE unless defined($hdr{'eep'}); # still missing?
foreach (keys %set) {
if ($set{$_} and !(defined $hdr{$_})) {
push
@warnings,
"File $file: '$_:' missing header - file skipped!";
last LINE;
}
}
# call handler for all headers
while (($key, $_) = each %hdr) {
&{$set{$key}} if $set{$key};
}
# call checks for all headers
while (($key, $_) = each %hdr) {
&{$check{$key}} if $check{$key};
}
last LINE;
} else {
push
@warnings,
"File $file: line '$line' illegal header - file skipped!";
last LINE;
}
}
close EEP;
}
# post-processing pre-printing
foreach (@file) {
$status{$_} = $type{$_}.$status{$_};
}
@file = sort @file;
@owner = sort @owner;
# Table printer
#
sub table {
my ($caption, $ix, @cols) = @_;
$ix = $index{lc $ix};
return unless defined $ix;
print "<TABLE border='1' summary='$caption'>";
print "<CAPTION><STRONG>$caption</STRONG></CAPTION>";
foreach (@cols) {
print "<TH align='left'>$_</TH>";
}
foreach my $i (@$ix) {
print "<TR>";
foreach (@cols) {
my $td = $table{lc $_}{$i};
if (defined $td) {
print "<TD align='left'>$td</TD>";
} else {
print "<TD align='left'> </TD>";
}
}
print "</TR>";
}
print "</TABLE>";
}
# Traverse index file
#
while (<INDEX>) {
chomp;
if (m|^ [[] (.*) []] : \s* $me : (.+)? $|x) {
if (defined $2) {
# [Table Caption]: <$me:index/col1/col2...>
&table($1, split(m|/|x, $2));
} else {
# [Prefix] : <$me:>
foreach (@file) {
print "[EEP $eep{$_}]: $_";
print " \"$1$eep{$_}: $title{$_}, $owner{$_}\"";
}
}
last unless(<INDEX>);
} else {
print;
}
}
# Warnings appear at the end of the generated Markdown page
#
if (@warnings) {
print "${lf}${lf}${lf}----${lf}Warnings${lf}--------${lf}";
foreach (sort @warnings) {
print " $_";
}
}