-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert_utf8html.pl
More file actions
69 lines (63 loc) · 1.45 KB
/
Copy pathconvert_utf8html.pl
File metadata and controls
69 lines (63 loc) · 1.45 KB
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
#
# htmlファイルを変換する
# - エンコーディングを UTF-8 にする
# - charset を utf-8 に変更する
use strict;
use warnings;
use utf8;
use Encode::Guess;
use Encode 'decode';
binmode STDOUT, ':utf8';
sub ReadContentUTF8 {
my $file = shift;
my $fh;
open($fh, '<:raw', $file) or die("error :$! $file");
my $content;
while (my $line = <$fh>) {
$content .= $line;
}
close($fh);
my $enc;
my $utf8_content;
if ($content =~ /charset=utf-8/) {
# utf-8
$utf8_content = $content;
$enc = 'utf-8';
}
elsif ($content =~ /charset=iso-8859-1/) {
# 8859-1
$utf8_content = decode('iso-8859-1', $content);
$enc = 'iso-8859-1';
}
elsif ($content =~ /charset=Shift_JIS/) {
$utf8_content = decode('Shift_JIS', $content);
$enc = 'Shift_jis';
}
else {
# Shift_jis(cp932)? (or utf-8)
my $decoder = guess_encoding($content, qw/cp932 utf8/);
$enc = $decoder->name;
$utf8_content = $decoder->decode($content);
}
$utf8_content =~ s/^\x{FEFF}//; # remove BOM
return ($enc, $utf8_content);
}
my $file = shift;
my ($enc, $content) = ReadContentUTF8($file);
print "$file $enc\n";
if ($enc eq "utf-8") {
# 変換不要
exit(1);
}
my @lines = split /\n/, $content;
my $fh;
open($fh, ">:unix:encoding(utf8)", $file) or die("error :$! $file");
print $fh "\x{feff}" ; # add BOM
foreach my $line (@lines) {
$line =~ s/[\r\n]+$//;
if ($line =~ /charset=/) {
$line =~ s/charset=[\w\-]+/charset=utf-8/g;
}
print $fh $line . "\n";
}
close($fh);