« lib/Serge/Sync/Plugin/TranslationService/command.pm
↑ Summary
lib/Serge/Engine/Plugin/run_command.pm »
| 1 | package Serge::Engine::Plugin::serialize_csv; | ||||||||||||||
| 2 | 1 | ▶ | use parent Serge::Engine::Plugin::Base::Serializer; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 3 | |||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | |||||||||||||||
| 6 | 1 | ▶ | use Unicode::Normalize; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | |||||||||||||||
| 8 | 1 | ▶ | use Serge; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 1 | ▶ | use Serge::Util; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | |||||||||||||||
| 11 | 1 | my @field_names = qw(key context hint source target fuzzy comment); | |||||||||||||
| 12 | |||||||||||||||
| 13 | sub name { | ||||||||||||||
| 14 | 0 | ▶ | return '.CSV Serializer'; | ||||||||||||
|
Subroutine
name: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 15 | } | ||||||||||||||
| 16 | |||||||||||||||
| 17 | sub serialize { | ||||||||||||||
| 18 | 0 | ▶ | my ($self, $units, $file, $lang) = @_; | ||||||||||||
|
Subroutine
serialize: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 19 | 0 | unshift @$units, ({ | |||||||||||||
| 20 | key => 'ID', | ||||||||||||||
| 21 | context => 'Context', | ||||||||||||||
| 22 | hint => 'Hint', | ||||||||||||||
| 23 | source => 'Source', | ||||||||||||||
| 24 | target => 'Translation', | ||||||||||||||
| 25 | fuzzy => 'Needs Work', | ||||||||||||||
| 26 | comment => "Translator's Comment", | ||||||||||||||
| 27 | }); | ||||||||||||||
| 28 | 0 | return make_csv($units, \@field_names); | |||||||||||||
| 29 | } | ||||||||||||||
| 30 | |||||||||||||||
| 31 | sub deserialize { | ||||||||||||||
| 32 | 0 | ▶ | my ($self, $textref) = @_; | ||||||||||||
|
Subroutine
deserialize: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||
| 33 | 0 | my @units; | |||||||||||||
| 34 | |||||||||||||||
| 35 | 0 | $$textref =~ s/\r\n/\n/sg; # normalize line-feeds | |||||||||||||
| 36 | |||||||||||||||
| 37 | 0 | my @row; | |||||||||||||
| 38 | 0 | ▶ | while ($$textref =~ /([^,"]*?|"(?:[^"]|"")*?")([,\n])/sg) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 39 | 0 | push @row, unescape_csv_value($1); | |||||||||||||
| 40 | 0 | ▶ | if ($2 eq "\n") { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 41 | 0 | ▶ | if ($row[0] ne 'ID') { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 42 | 0 | my %h; | |||||||||||||
| 43 | 0 | @h{@field_names} = @row; | |||||||||||||
| 44 | 0 | ▶ | $h{fuzzy} = $h{fuzzy} ? 1 : 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 45 | 0 | push @units, \%h; | |||||||||||||
| 46 | } | ||||||||||||||
| 47 | 0 | @row = (); | |||||||||||||
| 48 | } | ||||||||||||||
| 49 | } | ||||||||||||||
| 50 | 0 | return \@units; | |||||||||||||
| 51 | } | ||||||||||||||
| 52 | |||||||||||||||
| 53 | sub make_csv { | ||||||||||||||
| 54 | 0 | ▶ | my ($units, $field_names) = @_; | ||||||||||||
|
Subroutine
make_csv: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||
| 55 | 0 | my $out = ''; | |||||||||||||
| 56 | 0 | foreach my $unit (@$units) { | |||||||||||||
| 57 | 0 | ▶ | $unit->{fuzzy} = $unit->{fuzzy} ? 1 : undef if ($unit->{key} ne 'ID'); # normalize the value except for the header row | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 58 | 0 | $out .= join(',', map { escape_csv_value($unit->{$_}) } @$field_names)."\n"; | |||||||||||||
| 59 | } | ||||||||||||||
| 60 | 0 | return $out; | |||||||||||||
| 61 | } | ||||||||||||||
| 62 | |||||||||||||||
| 63 | sub escape_csv_value { | ||||||||||||||
| 64 | 0 | ▶ | my $s = shift; | ||||||||||||
|
Subroutine
escape_csv_value: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||
| 65 | 0 | ▶ | if ($s =~ m/(^\s|[",\n]|\s$)/) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 66 | 0 | $s =~ s/"/""/g; | |||||||||||||
| 67 | 0 | return qq{"$s"}; | |||||||||||||
| 68 | } | ||||||||||||||
| 69 | 0 | return $s; | |||||||||||||
| 70 | } | ||||||||||||||
| 71 | |||||||||||||||
| 72 | sub unescape_csv_value { | ||||||||||||||
| 73 | 0 | ▶ | my $s = shift; | ||||||||||||
|
Subroutine
unescape_csv_value: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||
| 74 | 0 | ▶ | if ($s =~ m/^".*"$/s) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 75 | 0 | $s =~ s/^"(.*)"$/$1/se; | |||||||||||||
| 76 | 0 | $s =~ s/""/"/g; | |||||||||||||
| 77 | } | ||||||||||||||
| 78 | 0 | ▶ | $s = undef if $s eq ''; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 79 | 0 | return $s; | |||||||||||||
| 80 | } | ||||||||||||||
| 81 | |||||||||||||||
| 82 | 1 | 1; | |||||||||||||