This file looks large and may slow your browser down if we attempt
to syntax highlight it, so we are showing it without any
pretty colors.
Highlight
it anyway.
| 1 |
#! /usr/bin/perl |
| 2 |
|
| 3 |
use File::Find; |
| 4 |
|
| 5 |
exit main( @ARGV ); |
| 6 |
|
| 7 |
sub main() |
| 8 |
{ |
| 9 |
my @argv = @_; |
| 10 |
|
| 11 |
print("extract all ids from svg files under directory: $argv[0]\n"); |
| 12 |
|
| 13 |
# open file and read it to array of lines |
| 14 |
open( FILE, $argv[1] ) || die( "Could not open $argv[1] for reading valid ids: $!" ); |
| 15 |
@tmp_ids = <FILE>; |
| 16 |
close(FILE) || die "Couldn't close file properly"; |
| 17 |
|
| 18 |
foreach(@tmp_ids) |
| 19 |
{ |
| 20 |
if ($_ =~ m/^([\w-]*)/) |
| 21 |
{ |
| 22 |
push(@valid_ids, $1); |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
find(\&create_id_list, $argv[0]); |
| 27 |
} |
| 28 |
|
| 29 |
sub create_id_list() |
| 30 |
{ |
| 31 |
my $filename = $_; |
| 32 |
|
| 33 |
# skip directories |
| 34 |
return unless -f; |
| 35 |
|
| 36 |
# skip non-svg files |
| 37 |
return unless rindex($filename, ".svg") == length($filename) - 4; |
| 38 |
|
| 39 |
# open file and read it to array of lines |
| 40 |
open( FILE, $filename ) || die( "Could not open $filename for reading: $!" ); |
| 41 |
@lines = <FILE>; |
| 42 |
close(FILE) || die "Couldn't close file properly"; |
| 43 |
|
| 44 |
# create one long string from the file |
| 45 |
$content = join('', @lines); |
| 46 |
|
| 47 |
my @ids; |
| 48 |
while($content =~ m/id="([^"]+)/g) |
| 49 |
{ |
| 50 |
if (grep {$_ =~ "^$1\$"} @valid_ids) |
| 51 |
{ |
| 52 |
push(@ids, $1); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
if(@ids) |
| 57 |
{ |
| 58 |
my $outputfilename = $filename.".ids"; |
| 59 |
open( OUTPUT, ">$outputfilename") || die( "Could not open $outputfilename for writing: $!" ); |
| 60 |
foreach(@ids) |
| 61 |
{ |
| 62 |
print OUTPUT "$_\n"; |
| 63 |
} |
| 64 |
close(OUTPUT) || die "Couldn't close file properly"; |
| 65 |
} else |
| 66 |
{ |
| 67 |
print "No valid ids found in $filename\n"; |
| 68 |
} |
| 69 |
|
| 70 |
return 0; |
| 71 |
} |