forked from KanjiVG/kanjivg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKanjiVG.pm
54 lines (48 loc) · 1.12 KB
/
KanjiVG.pm
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
package KanjiVG;
use parent Exporter;
our @EXPORT_OK = qw/handle_element/;
use warnings;
use strict;
use Carp;
my $dir = "$FindBin::Bin/kanjivg";
sub find_element
{
my ($element) = @_;
if (! defined $element) {
croak "No element";
}
my $string = qr/kanjivg:element="$element"/;
my @files = <$dir/*.svg>;
my @matches;
for my $file (@files) {
open my $in, "<:encoding(utf8)", $file
or die $!;
while (<$in>) {
if (/$string/) {
push @matches, $file;
# print "$file matches.\n";
}
}
close $in or die $!;
}
return @matches;
}
sub handle_element
{
my ($element, $handle_start, $data) = @_;
my @matches = find_element ($element);
if (ref $data ne 'HASH') {
croak "Give me a hash ref";
}
my $parser = XML::Parser->new (
Handlers => {
Start => sub { &{$handle_start} ($element, $data, @_)},
},
);
for my $file (@matches) {
# print "Parsing '$file'.\n";
$data->{file} = $file;
$parser->parsefile ($file);
}
}
1;