CCCP-HTML-Truncate

 view release on metacpan or  search on metacpan

lib/CCCP/HTML/Truncate.pm  view on Meta::CPAN

            };
            # skip body
            if ($parent->isSameNode($body)) {
                $_->unbindNode();
            } else {
                my @childs = $parent->findnodes($parent->nodePath.'//child::text()');
                $#childs > 0 ? $_->unbindNode() : $parent->unbindNode();                
            }
        }
    };
    
    my $ret = $body->html();
    $ret =~ s/^<body(.*?)>(<p>)?|(<\/p>)?<\/body>$//igm;
    return $ret; 
}

1;
__END__
=encoding utf-8

=head1 NAME

B<CCCP::HTML::Truncate> - truncate html with html-entities.

I<Version 0.04>

=head1 SYNOPSIS
    
    CCCP::HTML::Truncate;
    
    my $str = "<div>Тут могут быть <b>&mdash; разные entities и &quot; всякие</b> и,\n\n незакрытые теги <div> bla ... bla";
    
    print CCCP::HTML::Truncate->truncate($str,20);
    # <div>Тут могут быть <b>— раз...</b></div>
    
    print CCCP::HTML::Truncate->truncate($str,20,'...конец');
    # <div>Тут могут быть <b>— раз...конец</b></div>
    
=head1 DESCRIPTION

Truncate html string. 
Correct job with html entities.
Validate truncated html.

=head1 METHODS

=head3 truncate($str,$length,$elips)

Class method.
Return truncated html string.

=head1 PACKAGE VARIABLES

=head3 $CCCP::HTML::Truncate::enc

Charset for source html.
Default 'utf-8'.

=head1 BENCHMARK

	Benchmark: timing 10000 iterations of CCCP::HTML::Truncate, HTML::Truncate...
	CCCP::HTML::Truncate:  4 wallclock secs ( 4.55 usr +  0.00 sys =  4.55 CPU) @ 2197.80/s (n=10000)
	HTML::Truncate:        5 wallclock secs ( 4.86 usr +  0.00 sys =  4.86 CPU) @ 2057.61/s (n=10000)
	
	Benchmark: timing 25000 iterations of CCCP::HTML::Truncate, HTML::Truncate...
	CCCP::HTML::Truncate: 12 wallclock secs (11.37 usr +  0.00 sys = 11.37 CPU) @ 2198.77/s (n=25000)
	HTML::Truncate:       12 wallclock secs (12.12 usr +  0.01 sys = 12.13 CPU) @ 2061.01/s (n=25000)


=head1 WARNING

Version oldest 0.04 is DEPRECATED 

=head1 SEE ALSO

C<XML::LibXML>, C<Encode>

=head1 AUTHOR

Ivan Sivirinov

=cut



( run in 0.470 second using v1.01-cache-2.11-cpan-96521ef73a4 )