Root/m1r4/dsv/bomshelf

Source at commit 4bcaa5fb3c0582b95eb77a0867fabaeec3d9e32e created 12 years 22 days ago.
By Werner Almesberger, m1r4/dsv/SUPPLEMENT: fix various broken or redirected PDF links
1#!/usr/bin/perl
2
3sub csv
4{
5    my $s = $_[0];
6    my @a = ();
7
8    while (1) {
9        if ($s =~ /^"/) {
10            die unless $s =~ /^"([^"]*)"(,\s*)?/;
11            push(@a, $1);
12            $s = $';
13            return @a unless length $2;
14            
15        } elsif ($s =~ /,\s*/) {
16            push(@a, $`);
17            $s = $';
18        } else {
19            push(@a, $s);
20            return @a;
21        }
22    }
23    
24}
25
26while (<>) {
27    chop;
28    @f = &csv($_);
29    next unless $f[0] =~ /^[0-9]+$/;
30    die "duplicate part $f[3]" if defined $cr{$f[3]};
31    $f[1] =~ s/\(DNP\)//g;
32    $cr{$f[3]} = $f[1];
33    $url{$f[3]} = $f[4];
34    $dsc{$f[3]} = $f[6];
35    for (&csv($f[1])) {
36        warn "\"$_\" used for $r{$_} and $f[3]" if defined $r{$_};
37        $r{$_} = $f[3];
38    }
39}
40
41open(F, "SUPPLEMENT") || die "open SUPPLEMENT: $!";
42while (<F>) {
43    chop;
44    next if /^\s*#/;
45    next if /^\s*$/;
46    if (/^[Cc]:\s*/) {
47        undef $p if $clear;
48        $clear = 0;
49        if (defined $p) {
50            $cr{$p} = $';
51            for $c (&csv($')) {
52                warn "\"$c\" (supplement) used for $r{$c} ".
53                    "and $p" if defined $r{$c};
54                $r{$c} = $p;
55            }
56            next;
57        }
58        die "unknown component reference $'" unless defined $r{$'};
59        $p = $r{$'};
60        next;
61    }
62    if (/^[Pp]:\s*/) {
63        $p = $';
64        $clear = 0;
65        next if defined $cr{$p};
66        @a = grep { $_ =~ /^$p/ } keys %cr;
67        die "key $p* is ambiguous" if scalar @a > 1;
68        if (!@a) {
69            @a = grep { $_ =~ /$p/ } keys %cr;
70            die "key *$p* is ambiguous" if scalar @a > 1;
71            if (!@a) {
72                warn "adding $p as supplement";
73                next;
74            }
75        }
76        $p = $a[0];
77        next;
78    }
79    if (/^[Aa]:\s*/) {
80        $a{$p} .= "," if defined $a{$p};
81        $a{$p} .= $';
82        $clear = 1;
83        next;
84    }
85    if (/^[Dd]:\s*/) {
86        $url{$p} = $';
87        $clear = 1;
88        next;
89    }
90    die "don't recognize \"$_\"";
91}
92
93for (sort keys %cr) {
94    next if $url{$_} eq "";
95    print "# $dsc{$_}\n" unless $dsc{$_} eq "";
96    print "N: $_\n";
97    for $a (&csv($cr{$_})) {
98        print "A: $a\n";
99    }
100    if (defined $a{$_}) {
101        for $a (&csv($a{$_})) {
102            print "A: $a\n";
103        }
104    }
105    print "D: $url{$_}\n\n";
106}
107

Archive Download this file

Branches:
master



interactive