kzys (owner)

Revisions

gist: 3821 Download_button fork
public
Public Clone URL: git://gist.github.com/3821.git
Embed All Files: show embed
splitvcf.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
# Originally written by ono_matope
# http://d.hatena.ne.jp/ono_matope/20080713#1215974472
 
use strict;
use warnings;
use utf8;
use Encode;
use Unicode::Japanese;
binmode(STDOUT, 'encoding(utf8)');
 
sub read_categories {
    my ($path) = @_;
 
    my %result;
    open(my $file, '<:encoding(shiftjis)', $path);
 
    my $category;
    my $buffer;
    while (my $line = <$file>){
        $line =~ s/\r\n$//g;
 
        if ($line =~ /^X-GN:(.*)$/) {
            $category = $1;
        }
        elsif ($line =~ /^SORT\-STRING:([^\x20]+)\x20([^\x20]+)$/) {
            $line = "X-PHONETIC-LAST-NAME:$1\nX-PHONETIC-FIRST-NAME:$2";
        }
        elsif ($line =~ /^N:([^\s]+)\s([^\s;]+);;;;/) {
            $line = "N:$1;$2;;;";
        }
        else {
            $line =~ s/^SORT\-STRING/X-PHONETIC-LAST-NAME/;
            $line =~ s/^X\-GNO.*//;
            $line =~ s/^X\-REDUCTION.*//;
            $line =~ s/^CLASS.*//;
        }
 
        if ($line) {
            $buffer .= Unicode::Japanese->new("$line\n")->h2zKana->getu;
        }
 
        if ($line =~ /^END:VCARD$/) {
            $result{ $category } .= "$buffer\n";
            $buffer = '';
        }
    }
 
    close($file);
    return %result;
}
 
my $path = shift;
my %cards_of = read_categories($path);
 
foreach my $category (keys %cards_of){
    open(my $file, '>:encoding(shiftjis)', "$category.vcf");
    print $file $cards_of{ $category };
    close($file);
}