forked from viogus/eim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
table2chartbl.pl
executable file
·116 lines (89 loc) · 2.34 KB
/
table2chartbl.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
#!/usr/bin/perl -w
# table2chartbl.pl ---
# Author: Ye Wenbin <[email protected]>
# Created: 27 Apr 2008
# Version: 0.01
use warnings;
use strict;
use Getopt::Long;
use utf8;
use Pod::Usage;
my ($filter, $im, $output);
binmode STDOUT, "utf8";
GetOptions(
"filter=s" => \$filter,
"im=s" => \$im,
"output=s" => \$output,
);
if ( !defined $im ) {
pod2usage("Input method name needed!\n");
}
unless ( @ARGV ) {
pod2usage("Table files are not given!\n");
}
my %table;
foreach my $file ( @ARGV ) {
open(my $fh, "<:utf8", $file) or die "Can't open file $file: $!";
while ( <$fh> ) {
last if /^\[Table\]/;
}
while ( <$fh> ) {
my @r = split /\s+/;
foreach my $c ( @r[1..$#r] ) {
if ( length($c) == 1 ) {
if ( exists $table{$c} ) {
if ( length($r[0]) > length($table{$c}) ) {
$table{$c} = $r[0];
}
}
else {
$table{$c} = $r[0];
}
}
}
}
}
my %rtable;
foreach ( keys %table ) {
push @{$rtable{$table{$_}}}, $_;
}
$output ||= "eim-".$im."-chars.el";
open(my $fh, ">:utf8", $output) or die "Can't create file $output: $!";
print "Save table to $output...\n";
print {$fh} <<'HEADER';
;;; -*- coding: utf-8 -*-
;;;_. 字库
(eim-make-char-table '(
HEADER
foreach ( sort keys %rtable ) {
s/\\/\\\\/;
s/"/\\"/;
if ( $filter && /$filter/ ) {
next;
}
print {$fh} "(", join(' ', map { qq("$_") } $_, @{$rtable{$_}}), ")\n";
}
print {$fh} <<"FOOTER";
) eim-$im-char-table)
FOOTER
print "Done!\n"
__END__
=head1 NAME
table2chartbl.pl - 从码表中提取汉字到编码的转换表
=head1 SYNOPSIS
table2chartbl.pl [-i im -f filter -o output] tables
-i --im 输入法名称
-f --filter 对码表过滤
-o --output 输出文件
=head1 DESCRIPTION
本程序用于从 eim 的输入法码表中提取汉字的转换表。
=head1 AUTHOR
Ye Wenbin, E<lt>[email protected]<gt>
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2008 by Ye Wenbin
This program is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.8.2 or,
at your option, any later version of Perl 5 you may have available.
=head1 BUGS
None reported... yet.
=cut