| 1 | package Perlanet::Trait::Tidy; | ||||||||||
| 2 | |||||||||||
| 3 | 11 | ▶ | use 5.10.0; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 11 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 11 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 11 | ▶ | use Moose::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 11 | ▶ | use namespace::autoclean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 11 | ▶ | use Encode; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 11 | ▶ | use HTML::T5; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | =head1 NAME | ||||||||||
| 14 | |||||||||||
| 15 | Perlanet::Trait::Tidy - run posts through HTML::T5 (an HTML::Tidy replacement) | ||||||||||
| 16 | |||||||||||
| 17 | =head1 SYNOPSIS | ||||||||||
| 18 | |||||||||||
| 19 | my $perlanet = Perlanet->new_with_traits( | ||||||||||
| 20 | traits => [ 'Perlanet::Trait::Tidy' ] | ||||||||||
| 21 | ); | ||||||||||
| 22 | |||||||||||
| 23 | $perlanet->run; | ||||||||||
| 24 | |||||||||||
| 25 | =head1 DESCRIPTION | ||||||||||
| 26 | |||||||||||
| 27 | Before a post is added to the aggregated feed, it will be run through | ||||||||||
| 28 | HTML::T5. | ||||||||||
| 29 | |||||||||||
| 30 | =head2 Configuring | ||||||||||
| 31 | |||||||||||
| 32 | To configure the HTML::T5 instance, you should override the C<_build_tidy> | ||||||||||
| 33 | method. This method takes no input, and returns a HTML::T5 instance. | ||||||||||
| 34 | |||||||||||
| 35 | =head1 ATTRIBUTES | ||||||||||
| 36 | |||||||||||
| 37 | =head2 tidy | ||||||||||
| 38 | |||||||||||
| 39 | An instance of L<HTML::T5> used to tidy the feed entry contents | ||||||||||
| 40 | before outputting. For default settings see source.. | ||||||||||
| 41 | |||||||||||
| 42 | =cut | ||||||||||
| 43 | |||||||||||
| 44 | 11 | has 'tidy' => ( | |||||||||
| 45 | is => 'ro', | ||||||||||
| 46 | lazy_build => 1 | ||||||||||
| 47 | ); | ||||||||||
| 48 | |||||||||||
| 49 | 4 | ▶ | sub _build_tidy { | ||||||||
|
Subroutine
_build_tidy: called CC=1 SCAR=0.0
| |||||||||||
| 50 | 4 | my $self = shift; | |||||||||
| 51 | 4 | my %tidy = ( | |||||||||
| 52 | doctype => 'omit', | ||||||||||
| 53 | output_xhtml => 1, | ||||||||||
| 54 | wrap => 0, | ||||||||||
| 55 | alt_text => '', | ||||||||||
| 56 | break_before_br => 0, | ||||||||||
| 57 | char_encoding => 'raw', | ||||||||||
| 58 | tidy_mark => 0, | ||||||||||
| 59 | show_body_only => 1, | ||||||||||
| 60 | preserve_entities => 1, | ||||||||||
| 61 | show_warnings => 0, | ||||||||||
| 62 | ); | ||||||||||
| 63 | |||||||||||
| 64 | 4 | my $tidy = HTML::T5->new(\%tidy); | |||||||||
| 65 | 4 | $tidy->ignore( type => TIDY_WARNING ); | |||||||||
| 66 | |||||||||||
| 67 | 4 | return $tidy; | |||||||||
| 68 | } | ||||||||||
| 69 | |||||||||||
| 70 | 7 | ▶ | around 'clean_html' => sub { | ||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.2
| |||||||||||
| 71 | 7 | my $orig = shift; | |||||||||
| 72 | 7 | my $self = shift; | |||||||||
| 73 | 7 | my ($html) = @_; | |||||||||
| 74 | |||||||||||
| 75 | 7 | ▶ | warn __PACKAGE__, '::clean_html' if $ENV{PERLANET_DEBUG}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 76 | |||||||||||
| 77 | 7 | $html = $self->$orig($html); | |||||||||
| 78 | |||||||||||
| 79 | 7 | ▶ | my $clean = $self->tidy->clean(utf8::is_utf8($html) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 80 | ? $html | ||||||||||
| 81 | : decode('utf8', $html)); | ||||||||||
| 82 | |||||||||||
| 83 | 7 | return $clean; | |||||||||
| 84 | 11 | }; | |||||||||
| 85 | |||||||||||
| 86 | =head1 AUTHOR | ||||||||||
| 87 | |||||||||||
| 88 | Oliver Charles, <oliver.g.charles@googlemail.com> | ||||||||||
| 89 | Dave Cross, <dave@perlhacks.com> | ||||||||||
| 90 | |||||||||||
| 91 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 92 | |||||||||||
| 93 | Copyright (c) 2010-2023 by Magnum Solutions Ltd. | ||||||||||
| 94 | |||||||||||
| 95 | This library is free software; you can redistribute it and/or modify | ||||||||||
| 96 | it under the same terms as Perl itself, either Perl version 5.10.0 or, | ||||||||||
| 97 | at your option, any later version of Perl 5 you may have available. | ||||||||||
| 98 | |||||||||||
| 99 | =cut | ||||||||||
| 100 | |||||||||||
| 101 | 11 | 1; | |||||||||