| 1 | package Text::Ligature; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use 5.006; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 2 | ▶ | use utf8; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 2 | ▶ | use parent 'Exporter'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 2 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 2 | our $VERSION = '0.02'; | |||||||||
| 11 | 2 | our @EXPORT_OK = qw< to_ligatures from_ligatures to_ligature from_ligature >; | |||||||||
| 12 | 2 | our %EXPORT_TAGS = ( all => \@EXPORT_OK ); | |||||||||
| 13 | |||||||||||
| 14 | # TODO: remove to_ligature/from_ligature in next release | ||||||||||
| 15 | 2 | *to_ligature = \&to_ligatures; | |||||||||
| 16 | 2 | *from_ligature = \&from_ligatures; | |||||||||
| 17 | |||||||||||
| 18 | 2 | my @defaults = qw< ff fi fl ffi ffl >; | |||||||||
| 19 | |||||||||||
| 20 | 2 | my %lig_for = ( | |||||||||
| 21 | ff => 'ff', | ||||||||||
| 22 | fi => 'fi', | ||||||||||
| 23 | fl => 'fl', | ||||||||||
| 24 | ffi => 'ffi', | ||||||||||
| 25 | ffl => 'ffl', | ||||||||||
| 26 | ft => 'ſt', | ||||||||||
| 27 | st => 'st', | ||||||||||
| 28 | ); | ||||||||||
| 29 | |||||||||||
| 30 | 2 | my %chars_for = reverse %lig_for; | |||||||||
| 31 | |||||||||||
| 32 | 4 | ▶ | sub to_ligatures { | ||||||||
|
Subroutine
to_ligatures: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 33 | 4 | my ($text) = @_; | |||||||||
| 34 | |||||||||||
| 35 | 4 | ▶ | if (@_ != 1) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 36 | 2 | carp 'to_ligatures() expects one argument'; | |||||||||
| 37 | 2 | return; | |||||||||
| 38 | } | ||||||||||
| 39 | |||||||||||
| 40 | # longest token matching | ||||||||||
| 41 | 2 | for my $chars (sort { length $b <=> length $a } @defaults) { | |||||||||
| 42 | 10 | $text =~ s/$chars/$lig_for{$chars}/g; | |||||||||
| 43 | } | ||||||||||
| 44 | |||||||||||
| 45 | 2 | return $text; | |||||||||
| 46 | } | ||||||||||
| 47 | |||||||||||
| 48 | 4 | ▶ | sub from_ligatures { | ||||||||
|
Subroutine
from_ligatures: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 49 | 4 | my ($text) = @_; | |||||||||
| 50 | |||||||||||
| 51 | 4 | ▶ | if (@_ != 1) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | 2 | carp 'from_ligatures() expects one argument'; | |||||||||
| 53 | 2 | return; | |||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | 2 | for my $lig (keys %chars_for) { | |||||||||
| 57 | 14 | $text =~ s/$lig/$chars_for{$lig}/g; | |||||||||
| 58 | } | ||||||||||
| 59 | |||||||||||
| 60 | 2 | return $text; | |||||||||
| 61 | } | ||||||||||
| 62 | |||||||||||
| 63 | 2 | 1; | |||||||||
| 64 | |||||||||||
| 65 | __END__ | ||||||||||