... | ... |
@@ -20,6 +20,11 @@ |
20 | 20 |
;time_zone=+10:30 |
21 | 21 |
;time_zone=-4:00 |
22 | 22 |
|
23 |
+;;; Suspects encoding list for source code character encoding (default:UTF-8) |
|
24 |
+;;; set comma separated encoding list if your source code is different from UTF-8. |
|
25 |
+;;; encoding name follow Perl encoding API. |
|
26 |
+;encoding_suspects=cp932,utf8 |
|
27 |
+ |
|
23 | 28 |
[admin] |
24 | 29 |
;;; If you forget admin password, |
25 | 30 |
;;; set this value to 1 and access /reset-password page. |
... | ... |
@@ -56,6 +56,12 @@ sub startup { |
56 | 56 |
} |
57 | 57 |
$git->bin($git_bin); |
58 | 58 |
|
59 |
+ # Encoding suspects list for Git |
|
60 |
+ my $encoding_suspects |
|
61 |
+ = $conf->{basic}{encoding_suspects} ||= 'utf8'; |
|
62 |
+ $encoding_suspects = [split /,/, $encoding_suspects] unless ref $encoding_suspects eq 'ARRAY'; |
|
63 |
+ $git->encoding_suspects($encoding_suspects); |
|
64 |
+ |
|
59 | 65 |
# Repository Manager |
60 | 66 |
my $manager = Gitprep::Manager->new(app => $self); |
61 | 67 |
weaken $manager->{app}; |
... | ... |
@@ -3,6 +3,7 @@ use Mojo::Base -base; |
3 | 3 |
|
4 | 4 |
use Carp 'croak'; |
5 | 5 |
use Encode qw/encode decode/; |
6 |
+use Encode::Guess; |
|
6 | 7 |
use Fcntl ':mode'; |
7 | 8 |
use File::Basename qw/basename dirname/; |
8 | 9 |
use File::Copy 'move'; |
... | ... |
@@ -13,6 +14,7 @@ use POSIX 'floor'; |
13 | 14 |
# Attributes |
14 | 15 |
has 'bin'; |
15 | 16 |
has default_encoding => 'UTF-8'; |
17 |
+has 'encoding_suspects'; |
|
16 | 18 |
has 'rep_home'; |
17 | 19 |
has text_exts => sub { ['txt'] }; |
18 | 20 |
has 'time_zone_second'; |
... | ... |
@@ -181,7 +183,7 @@ sub blame { |
181 | 183 |
my $blame_line; |
182 | 184 |
my $max_author_time; |
183 | 185 |
my $min_author_time; |
184 |
- while (my $line = $self->_dec(scalar <$fh>)) { |
|
186 |
+ while (my $line = $self->_dec_guess(scalar <$fh>)) { |
|
185 | 187 |
chomp $line; |
186 | 188 |
|
187 | 189 |
if ($blame_line) { |
... | ... |
@@ -258,7 +260,7 @@ sub blob { |
258 | 260 |
|
259 | 261 |
# Format lines |
260 | 262 |
my $lines =[]; |
261 |
- while (my $line = $self->_dec(scalar <$fh>)) { |
|
263 |
+ while (my $line = $self->_dec_guess(scalar <$fh>)) { |
|
262 | 264 |
chomp $line; |
263 | 265 |
push @$lines, $line; |
264 | 266 |
} |
... | ... |
@@ -287,7 +289,7 @@ sub blob_diffs { |
287 | 289 |
open my $fh, '-|', @cmd |
288 | 290 |
or croak('Open self-diff-tree failed'); |
289 | 291 |
my @diff_tree; |
290 |
- while (my $line = $self->_dec(scalar <$fh>)) { |
|
292 |
+ while (my $line = $self->_dec_guess(scalar <$fh>)) { |
|
291 | 293 |
chomp $line; |
292 | 294 |
push @diff_tree, $line if $line =~ /^:/; |
293 | 295 |
last if $line =~ /^\n/; |
... | ... |
@@ -320,7 +322,7 @@ sub blob_diffs { |
320 | 322 |
); |
321 | 323 |
open my $fh, '-|', @cmd |
322 | 324 |
or croak('Open self-diff-tree failed'); |
323 |
- my @lines = map { $self->_dec($_) } <$fh>; |
|
325 |
+ my @lines = map { $self->_dec_guess($_) } <$fh>; |
|
324 | 326 |
close $fh; |
325 | 327 |
my ($lines, $diff_info) = $self->parse_blob_diff_lines(\@lines); |
326 | 328 |
my $blob_diff = { |
... | ... |
@@ -1672,6 +1674,20 @@ sub _chop_str { |
1672 | 1674 |
} |
1673 | 1675 |
} |
1674 | 1676 |
|
1677 |
+sub _dec_guess { |
|
1678 |
+ my ($self, $str) = @_; |
|
1679 |
+ |
|
1680 |
+ my $enc = Encode::Guess->guess($str, @{$self->encoding_suspects}); |
|
1681 |
+ # fallback default encoding if multile guess result |
|
1682 |
+ # http://perl-users.jp/articles/advent-calendar/2009/casual/10.html |
|
1683 |
+ $enc = $self->default_encoding unless ref $enc; |
|
1684 |
+ |
|
1685 |
+ my $new_str; |
|
1686 |
+ eval { $new_str = decode($enc, $str) }; |
|
1687 |
+ |
|
1688 |
+ return $@ ? $str : $new_str; |
|
1689 |
+} |
|
1690 |
+ |
|
1675 | 1691 |
sub _dec { |
1676 | 1692 |
my ($self, $str) = @_; |
1677 | 1693 |
|