| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | package Unicode::NoFancyPants; | ||
| 4 | |||
| 5 | use utf8; | ||
| 6 | use common::sense; | ||
| 7 | use Text::Unidecode; | ||
| 8 | use Unicode::Normalize qw(NFC); | ||
| 9 | our(@EXPORT)=qw(dropFancyPants); | ||
| 10 | use base qw(Exporter); | ||
| 11 | |||
| 12 | our $VERSION = '0.01'; | ||
| 13 | =encoding utf8 | ||
| 14 | |||
| 15 | =head1 NAME | ||
| 16 | |||
| 17 | Unicode::NoFancyPants - Remove fancy Unicode characters, replacing them with ASCII equivalents. | ||
| 18 | |||
| 19 | =head1 SYNOPSIS | ||
| 20 | |||
| 21 | use Unicode::NoFancyPants; | ||
| 22 | |||
| 23 | my $ascii_text = Unicode::NoFancyPants::dropFancyPants("Hello, “world”!"); | ||
| 24 | print $ascii_text; # Output: Hello, "world"! :-) | ||
| 25 | |||
| 26 | # Or, with the exported function: | ||
| 27 | use Unicode::NoFancyPants qw(dropFancyPants); | ||
| 28 | my @ascii_texts = dropFancyPants("Fancy String 1", "Fancy String 2"); | ||
| 29 | print join("\n", @ascii_texts); | ||
| 30 | |||
| 31 | =head1 DESCRIPTION | ||
| 32 | |||
| 33 | This module provides a function, C<dropFancyPants>, that takes Unicode text and returns a version with "fancy" characters replaced by their closest ASCII equivalents. It handles: | ||
| 34 | |||
| 35 | * Fancy quotes and dashes. | ||
| 36 | * Emoji (converting them to basic ASCII smileys where possible). | ||
| 37 | * Other Unicode symbols (using L<Text::Unidecode>). | ||
| 38 | |||
| 39 | =head1 FUNCTIONS | ||
| 40 | |||
| 41 | =head2 dropFancyPants($text | @texts) | ||
| 42 | |||
| 43 | Takes either a single Unicode string or a list of Unicode strings and returns the corresponding ASCII-converted string(s). | ||
| 44 | |||
| 45 | =over 4 | ||
| 46 | |||
| 47 | =item $text | ||
| 48 | |||
| 49 | A Unicode string to be processed. | ||
| 50 | |||
| 51 | =item @texts | ||
| 52 | |||
| 53 | A list of Unicode strings to be processed. | ||
| 54 | |||
| 55 | =back | ||
| 56 | |||
| 57 | =head1 EXAMPLES | ||
| 58 | |||
| 59 | use Unicode::NoFancyPants qw(dropFancyPants); | ||
| 60 | |||
| 61 | my $result = dropFancyPants("This is a test—with “fancy” quotes! 😊"); | ||
| 62 | print $result; # Output: This is a test-with "fancy" quotes! :-) | ||
| 63 | |||
| 64 | my @results = dropFancyPants("String 1: 😃", "String 2: 😞"); | ||
| 65 | print join("\n", @results); # Output: String 1: :-D\nString 2: :-( | ||
| 66 | |||
| 67 | =head1 DEPENDENCIES | ||
| 68 | |||
| 69 | * L<Unicode::Normalize> | ||
| 70 | * L<Text::Unidecode> | ||
| 71 | |||
| 72 | =head1 AUTHOR | ||
| 73 | |||
| 74 | Nobody <nobody-spam-me@turing-trust.com> | ||
| 75 | |||
| 76 | =head1 LICENSE | ||
| 77 | |||
| 78 | Artistic License 2.0 | ||
| 79 | |||
| 80 | =cut | ||
| 81 | |||
| 82 | use Unicode::Normalize; | ||
| 83 | use Text::Unidecode; | ||
| 84 | |||
| 85 | sub dropFancyPants { | ||
| 86 | local(@_)=@_; | ||
| 87 | for(@_) { | ||
| 88 | if(ref($_) eq 'ARRAY'){ | ||
| 89 | @$_=dropFancyPants(@$_); | ||
| 90 | next; | ||
| 91 | }; | ||
| 92 | $_ = NFC( $_ ); | ||
| 93 | |||
| 94 | # Replace fancy quotes and dashes | ||
| 95 | s/([\x{2018}\x{2019}\x{201B}])/'/g; # single quotes | ||
| 96 | s/([\x{201C}\x{201D}\x{201E}\x{201F}])/"/g; # double quotes | ||
| 97 | s/[\x{2013}\x{2014}]/-/g; # dashes | ||
| 98 | s/\x{2026}/.../g; #ellipsis | ||
| 99 | |||
| 100 | # Basic smiley replacements (expand this as needed) | ||
| 101 | s/\x{1F600}/:-)/g; # Grinning Face | ||
| 102 | s/\x{1F601}/:-D/g; # Grinning Face with Smiling Eyes | ||
| 103 | s/\x{1F602}/:D/g; # Face with Tears of Joy | ||
| 104 | s/\x{1F603}/:-D/g; # Smiling Face with Open Mouth | ||
| 105 | s/\x{1F604}/:-D/g; # Smiling Face with Open Mouth and Smiling Eyes | ||
| 106 | s/\x{1F605}/:-)/g; # Smiling Face with Open Mouth and Cold Sweat | ||
| 107 | s/\x{1F606}/:-D/g; # Smiling Face with Open Mouth and Tightly-Closed Eyes | ||
| 108 | s/\x{1F607}/O:-)/g; # Smiling Face with Halo | ||
| 109 | s/\x{1F608}/>:-\]/g; # Smiling Face with Horns | ||
| 110 | s/\x{1F609}/;-\)/g; # Winking Face | ||
| 111 | s/\x{1F60a}/:-)/g; # shit that got forgot | ||
| 112 | s/\x{1F60b}/1F60b:-)/g; | ||
| 113 | s/\x{1F60c}/1F60c:-)/g; | ||
| 114 | s/\x{1F60d}/1F60d:-)/g; | ||
| 115 | s/\x{1F60d}/1F60d:-)/g; | ||
| 116 | s/\x{1F60f}/1F60f:-)/g; | ||
| 117 | s/\x{1F610}/:-|/g; # Neutral Face | ||
| 118 | s/\x{1F611}/:-\|/g; # Expressionless Face | ||
| 119 | s/\x{1F612}/:-\(/g; # Unamused Face | ||
| 120 | s/\x{1F613}/:-\(/g; # Downcast Face with Sweat | ||
| 121 | s/\x{1F614}/:-\(/g; # Pensive Face | ||
| 122 | s/\x{1F615}/:-\(/g; # Confused Face | ||
| 123 | s/\x{1F616}/:-\(/g; # Confounded Face | ||
| 124 | s/\x{1F617}/\)-:/g; # Kissing Face | ||
| 125 | s/\x{1F618}/:-*/g; # Face Blowing a Kiss | ||
| 126 | s/\x{1F619}/:-*/g; # Kissing Face with Smiling Eyes | ||
| 127 | s/\x{1F61A}/:-*/g; # Kissing Face with Closed Eyes | ||
| 128 | s/\x{1F61B}/:-P/g; # Face with Stuck-out Tongue | ||
| 129 | s/\x{1F61C}/:-P/g; # Winking Face with Stuck-out Tongue | ||
| 130 | s/\x{1F61D}/:-P/g; # Face with Stuck-out Tongue and Tightly-Closed Eyes | ||
| 131 | s/\x{1F61E}/:-\(/g; # Disappointed Face | ||
| 132 | s/\x{1F61F}/:-\(/g; # Worried Face | ||
| 133 | s/\x{1F620}/>:-\(/g; # Angry Face | ||
| 134 | s/\x{1F621}/>:-\(/g; # Pouting Face | ||
| 135 | s/\x{1F622}/:'-\(/g; # Crying Face | ||
| 136 | s/\x{1F623}/>:-\(/g; # Persevering Face | ||
| 137 | s/\x{1F624}/>:-\(/g; # Face with Look of Triumph | ||
| 138 | s/\x{1F625}/:-\(/g; # Disappointed but Relieved Face | ||
| 139 | s/\x{1F626}/:-\(/g; # Frowning Face with Open Mouth | ||
| 140 | s/\x{1F627}/:-\(/g; # Anguished Face | ||
| 141 | s/\x{1F628}/:-O/g; # Fearful Face | ||
| 142 | s/\x{1F629}/:-\(/g; # Weary Face | ||
| 143 | s/\x{1F62A}/:-Z/g; # Sleepy Face | ||
| 144 | s/\x{1F62B}/>:-\(/g; # Tired Face | ||
| 145 | s/\x{1F62C}/>:-\(/g; # Face with Open Mouth and Cold Sweat | ||
| 146 | s/\x{1F62D}/:'-\(/g; # Loudly Crying Face | ||
| 147 | s/\x{1F62E}/:-O/g; # Face with Open Mouth | ||
| 148 | s/\x{1F62F}/:-O/g; # Hushed Face | ||
| 149 | s/\x{1F630}/:-O/g; # Face with Open Mouth and Cold Sweat | ||
| 150 | s/\x{1F631}/:-O/g; # Face Screaming in Fear | ||
| 151 | s/\x{1F632}/:-O/g; # Astonished Face | ||
| 152 | s/\x{1F633}/:-O/g; # Flushed Face | ||
| 153 | s/\x{1F634}/:-Z/g; # Sleeping Face | ||
| 154 | s/\x{1F635}/:-X/g; # Dizzy Face | ||
| 155 | s/\x{1F636}/:-X/g; # Face Without Mouth | ||
| 156 | s/\x{1F637}/:-X/g; # Face with Medical Mask | ||
| 157 | |||
| 158 | # Use Text::Unidecode for the rest | ||
| 159 | $_ = unidecode($_); | ||
| 160 | }; | ||
| 161 | local($")=""; | ||
| 162 | return wantarray ? @_ : "@_"; | ||
| 163 | } | ||
| 164 | 1; | ||
| 165 | #my $test_string = "Hello, “world”!\n | ||
| 166 | #😊 This is a test—with fancy quotes and em dashes…\n"; | ||
| 167 | #print dropFancyPants($test_string), "\n"; |