App-Basis-ConvertText2-UtfTransform

 view release on metacpan or  search on metacpan

README.md  view on Meta::CPAN

# App-Basis-ConvertText2-UtfTransform

A number of popular websites (eg twitter) do not allow the use of HTML to create
bold/italic font effects or perform smily transformations

However we can simulate this with some clever transformations of plain ascii text
into UTF8 codes which are a different font and so effectively create the same effect.

We have transformations for flip (reverses the string and flips upside down,
bold, italic, bubbles,script and leet.

We can only transform A-Z a-z 0-9 and ? ! ,

I have only implemented a small set of smilies, ones that I am likely to use.

## Formatting

* flip     <f>text</f>      upside down and reversed
* bold     <b>text</b>
* italic   <i>text</i>
* bubbles  <o>text</o>
* script   <s>text</s>
* leet     <l>text</l>      LeetSpeak

## Smilies

| smilie                                    | symbol      |
|----------------------------+--------------+-------------|
| <3. :heart:                               | heart       |

bin/utftx  view on Meta::CPAN

#!/usr/bin/env perl
# PODNAME: utftx - Convert ascii text into UTF8 to simulate text formatting
# ABSTRACT: Convert ascii text into UTF8 to simulate text formatting. Perform smilies transformations

=head1 NAME

utftx

=head1 SYNOPSIS

    > utftx "text string with <b>bold</b> and <i>italic</i> and <l>Leet speak</l>"

    to get full help use
    > utftx --help   

=head1 DESCRIPTION

Convert ascii text into UTF8 to simulate text formatting

Note: You cannot embed one format within another, so you cannot have bold script, or bold italic.

=cut

# (c) kevin Mulholland, moodfarm@cpan.org
# this code is released under the Perl Artistic License

use 5.14.0;
use strict;
use warnings;
use App::Basis;

lib/App/Basis/ConvertText2/UtfTransform.pm  view on Meta::CPAN


App::Basis::ConvertText2::UtfTransform

=head1 SYNOPSIS

    use 5.10.0 ;
    use strict ;
    use warnings ;
    use App::Basis::ConvertText2::UtfTransform

    my $string = "<b>bold text</b> 
        <i>italic text</i>
        <f>flipped upside down text and reversed</f>
        <l>Some Leet speak</l>
        <o>text in bubbles</o>
        <s>script text</s>
        <l>are you leet</l>" ;

    say utf_transform( $string) ;

    my $smile = ":beer: is food!  :) I <3 :cake: ;)" ;

    say uttf_smilies( $smile ) ;

=head1 DESCRIPTION

A number of popular websites (eg twitter) do not allow the use of HTML to create
bold/italic font effects or perform smily transformations

However we can simulate this with some clever transformations of plain ascii text
into UTF8 codes which are a different font and so effectively create the same effect.

We have transformations for flip (reverses the string and flips upside down,
bold, italic, bubbles and leet.

We can transform A-Z a-z 0-9 and ? ! ,

I have only implemented a small set of smilies, ones that I am likely to use

=head1 Note

You cannot embed one format within another, so you cannot have bold script, or 
bold italic.

=head1 See Also 

L<http://txtn.us/>

=head1 Functions

=over 4

=cut

lib/App/Basis/ConvertText2/UtfTransform.pm  view on Meta::CPAN

    "5" => "5",
    "6" => "6",
    "7" => "7",
    "8" => "8",
    "9" => "9",
    "?" => "\x{00BF}",
    "!" => "\x{00A1}",
    "," => ",",
);

my %bold = (
    "A" => "\x{1D400}",
    "B" => "\x{1D401}",
    "C" => "\x{1D402}",
    "D" => "\x{1D403}",
    "E" => "\x{1D404}",
    "F" => "\x{1D405}",
    "G" => "\x{1D406}",
    "H" => "\x{1D407}",
    "I" => "\x{1D408}",
    "J" => "\x{1D409}",

lib/App/Basis/ConvertText2/UtfTransform.pm  view on Meta::CPAN

    "5" => "5",
    "6" => "6",
    "7" => "7",
    "8" => "8",
    "9" => "9",
    "?" => "?",
    "!" => "!",
    "," => ",",
);

# mathematical bold script capital and small
# http://www.fileformat.info/info/unicode/category/Lu/list.htm
# http://www.fileformat.info/info/unicode/category/Ll/list.htm

my %script = (
    "A" => "\x{1d4d0}",
    "B" => "\x{1d4d1}",
    "C" => "\x{1d4d2}",
    "D" => "\x{1d4d3}",
    "E" => "\x{1d4d4}",
    "F" => "\x{1d4d5}",

lib/App/Basis/ConvertText2/UtfTransform.pm  view on Meta::CPAN

    ":ghost:"      => "\x{1f47b}",    # ghost
    ":skull:"      => "\x{1f480}",    # skull
    ":time:"       => "\x{231a}",     # time, watch face
    ":hourglass:"  => "\x{231b}",     # hourglass
);

my $smiles = join( '|', map { quotemeta($_) } keys %smilies );

my %code_map = (
    f => \%flip,
    b => \%bold,
    i => \%italic,
    o => \%bubbles,
    s => \%script,
);

# ----------------------------------------------------------------------------
# regexp replace function
sub _transform {
    my ( $code, $string ) = @_;
    my $transform = 1;

lib/App/Basis/ConvertText2/UtfTransform.pm  view on Meta::CPAN

# ----------------------------------------------------------------------------

=item utf_transform

transform A-ZA-z0-9!?, into UTF8 forms suitable for websites that do not allow
HTML codes for these

we use the following psuedo HTML elements

    flip     <f>text</f>      upside down and reversed
    bold     <b>text</b>
    italic   <i>text</i>
    bubbles  <o>text</o>
    script   <s>text</s>
    leet     <l>text</l>      LeetSpeak

B<Parameters>  

incoming string    

B<Returns>

t/01_basic.t  view on Meta::CPAN


=cut

use v5.10;
use strict;
use warnings;
use Test::More tests => 8;

BEGIN { use_ok('App::Basis::ConvertText2::UtfTransform'); }

my $string = "<b>bold text</b> 
<i>italic text</i>
<f>flipped upside down text</f>
<s>script text</s>
<o>bubles text</o>
<l>Leet Speak</l>" ;

my $new =  utf_transform( $string) ;
ok( $new !~ /<b>/, 'bold has been replaced') ;
ok( $new !~ /<i>/, 'italic has been replaced') ;
ok( $new !~ /<l>/, 'leet has been replaced') ;
ok( $new !~ /<o>/, 'bubbles has been replaced') ;
ok( $new !~ /<s>/, 'script has been replaced') ;

my $smile = ":beer: <3" ;
$new = utf_smilies( $smile) ;
ok( $new !~ /:beer:/, 'beer has been replaced') ;
ok( $new !~ /<3/, 'heart has been replaced') ;



( run in 0.532 second using v1.01-cache-2.11-cpan-c333fce770f )