HTML-Truncate
view release on metacpan or search on metacpan
lib/HTML/Truncate.pm view on Meta::CPAN
cannot be known until the given HTML is parsed.
Side-effect: clears any L</chars> that has been set.
=cut
sub percent {
my ( $self, $percent ) = @_;
return unless $self->{_percent} or $percent;
return sprintf("%d%%", 100 * $self->{_percent})
unless $percent;
my ( $temp_percent ) = $percent =~ /^(100|[1-9]?[0-9])\%$/;
$temp_percent and $temp_percent != 0
or croak "Specified percent is invalid '$percent' -- 1\% - 100\%";
$self->{_chars} = undef; # no conflict allowed
$self->{_percent} = $1 / 100;
}
=item B<ellipsis>
Set/get. Ellipsis in this case means --
The omission of a word or phrase necessary for a complete
syntactical construction but not necessary for understanding.
http://www.answers.com/topic/ellipsis
What it will probably mean in most real applications is "read more."
The default is C<…> which if the utf8 flag is true will render
as a literal ellipsis, C<chr(8230)>.
The reason the default is C<…> and not "..." is this is meant
for use in HTML environments, not plain text, and "..." (dot-dot-dot)
is not typographically correct or equivalent to a real horizontal
ellipsis character.
=cut
sub ellipsis {
my $self = shift;
if ( @_ )
{
$self->{_ellipsis} = shift;
}
elsif ( $self->utf8_mode() )
{
return HTML::Entities::decode($self->{_ellipsis});
}
else
{
return $self->{_ellipsis};
}
}
=item B<truncate>
It returns the truncated XHTML if asked for a return value.
my $truncated = $ht->truncate($html);
It will truncate the string in place if no return value is expected
(L<wantarray> is not defined).
$ht->truncate($html);
print $html;
Also can be called with inline arguments-
print $ht->truncate( $html,
$chars_or_percent,
$ellipsis );
No arguments are strictly required. Without HTML to operate upon it
returns undef. The two optional arguments may be preset with the
methods L</chars> (or L</percent>) and L</ellipsis>.
Valid nesting of tags is required (alla XHTML). Therefore some old
HTML habits like E<lt>pE<gt> without a E<lt>/pE<gt> are not supported
and may cause a fatal error. See L</repair> for help with badly formed
HTML.
Certain tags are omitted by default from the truncated output.
=over 4
=item * Skipped tags
These will not be included in truncated output by default.
<head>...</head> <script>...</script> <form>...</form>
<iframe></iframe> <title>...</title> <style>...</style>
<base/> <link/> <meta/>
=item * Tags allowed to self-close
See L<emptyElement|HTML::Tagset/emptyElement> in L<HTML::Tagset>.
=back
=cut
sub _chars_or_percent {
my ( $self, $which ) = @_;
if ( $which =~ /\%\z/ )
{
$self->percent($which);
}
else
{
$self->chars($which);
}
}
sub truncate {
my $self = shift;
$self->{_raw_html} = \$_[0];
shift || return;
( run in 0.972 second using v1.01-cache-2.11-cpan-119454b85a5 )