| 1 | package Serge::Engine::Plugin::Transform::case; | ||||||||||
| 2 | 1 | ▶ | use parent Serge::Engine::Plugin::Base::Transform; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | |||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | sub name { | ||||||||||
| 7 | 0 | ▶ | return 'Case normalization plugin'; | ||||||||
|
Subroutine
name: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 8 | } | ||||||||||
| 9 | |||||||||||
| 10 | sub filter_key { | ||||||||||
| 11 | 0 | ▶ | my ($self, $s) = @_; | ||||||||
|
Subroutine
filter_key: not called CC=1 SCAR=0.6
Pod
undocumented
| |||||||||||
| 12 | |||||||||||
| 13 | # Suppress `Operation "lc" returns its argument for UTF-16 surrogate 0xNNNN` warning | ||||||||||
| 14 | # for the `lc()` call below; use 'utf8' instead of a more appropriate 'surrogate' pragma | ||||||||||
| 15 | # since the latter is not available in until Perl 5.14 | ||||||||||
| 16 | 1 | ▶ | no warnings 'utf8'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | |||||||||||
| 18 | 0 | return lc($s); # lowercase the string | |||||||||
| 19 | } | ||||||||||
| 20 | |||||||||||
| 21 | sub transform { | ||||||||||
| 22 | 0 | ▶ | my ($self, $s, $target_string, $lang) = @_; | ||||||||
|
Subroutine
transform: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||
| 23 | |||||||||||
| 24 | 0 | $target_string =~ s/<.*?>//g; | |||||||||
| 25 | |||||||||||
| 26 | 0 | ▶ | if (lc($target_string) eq $target_string) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 27 | # for German, always return the original string with unmodified case, | ||||||||||
| 28 | # because in German all nouns start with the uppercase letter | ||||||||||
| 29 | 0 | ▶ | if ($lang =~ m/^(de|de-.*)$/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 30 | 0 | return $s; | |||||||||
| 31 | } else { | ||||||||||
| 32 | 0 | return lc($s); | |||||||||
| 33 | } | ||||||||||
| 34 | } | ||||||||||
| 35 | |||||||||||
| 36 | 0 | ▶ | if (uc($target_string) eq $target_string) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 37 | 0 | return uc($s); | |||||||||
| 38 | } | ||||||||||
| 39 | |||||||||||
| 40 | 0 | ▶ | if (sc($target_string) eq $target_string) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | # for German, uppercase only the first letter while leaving | ||||||||||
| 42 | # all others intact | ||||||||||
| 43 | 0 | ▶ | if ($lang =~ m/^(de|de-.*)$/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 44 | 0 | return sc_delicate($s); | |||||||||
| 45 | } else { | ||||||||||
| 46 | 0 | return sc($s); | |||||||||
| 47 | } | ||||||||||
| 48 | } | ||||||||||
| 49 | |||||||||||
| 50 | # we don't want to apply title case anymore, since | ||||||||||
| 51 | # it is not widely used outside the English language | ||||||||||
| 52 | #if (tc($target_string) eq $target_string) { | ||||||||||
| 53 | # return tc($s); | ||||||||||
| 54 | #} | ||||||||||
| 55 | |||||||||||
| 56 | # TODO: do the same but in XML-aware fashion, by skipping the tags | ||||||||||
| 57 | # ... | ||||||||||
| 58 | |||||||||||
| 59 | # TODO: do the same but skip the %PLACEHOLDERS% | ||||||||||
| 60 | # ... | ||||||||||
| 61 | |||||||||||
| 62 | # TODO: do the same but skip the {PLACEHOLDERS} | ||||||||||
| 63 | # ... | ||||||||||
| 64 | |||||||||||
| 65 | # TODO: do the same but skip the {{PLACEHOLDERS}} | ||||||||||
| 66 | # ... | ||||||||||
| 67 | |||||||||||
| 68 | # TODO: do the same but skip the {{{PLACEHOLDERS}}} | ||||||||||
| 69 | # ... | ||||||||||
| 70 | |||||||||||
| 71 | # TODO: do the same but skip the sfprinf-style strings | ||||||||||
| 72 | # ... | ||||||||||
| 73 | |||||||||||
| 74 | # ... | ||||||||||
| 75 | # ... | ||||||||||
| 76 | |||||||||||
| 77 | 0 | return $s; | |||||||||
| 78 | } | ||||||||||
| 79 | |||||||||||
| 80 | # Uppercase the first letter, lowercase everything else. | ||||||||||
| 81 | # Note: not an object's method | ||||||||||
| 82 | sub sc { | ||||||||||
| 83 | 0 | ▶ | my $s = lc(shift); | ||||||||
|
Subroutine
sc: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 84 | 0 | $s = uc(substr($s, 0, 1)) . substr($s, 1); | |||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | # Uppercase the first letter, leave everything else intact | ||||||||||
| 88 | # Note: not an object's method | ||||||||||
| 89 | sub sc_delicate { | ||||||||||
| 90 | 0 | ▶ | my $s = shift; | ||||||||
|
Subroutine
sc_delicate: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 91 | 0 | $s = uc(substr($s, 0, 1)) . substr($s, 1); | |||||||||
| 92 | } | ||||||||||
| 93 | |||||||||||
| 94 | # Apply title case (uppercase the first letter of each word, lowercase everything else). | ||||||||||
| 95 | # Note: not an object's method | ||||||||||
| 96 | sub tc { | ||||||||||
| 97 | 0 | ▶ | my $s = lc(shift); | ||||||||
|
Subroutine
tc: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 98 | 0 | $s =~ s/(\W)(\w)/$1.uc($2)/sge; | |||||||||
| 99 | 0 | $s =~ s/^(\W*)(\w)/$1.uc($2)/sge; | |||||||||
| 100 | 0 | return $s; | |||||||||
| 101 | } | ||||||||||
| 102 | |||||||||||
| 103 | 1 | 1; | |||||||||