| 1 | package CGI::Application::Plugin::HtmlTidy; | ||
| 2 | 6 | ▶ | BEGIN { |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 6 | $CGI::Application::Plugin::HtmlTidy::VERSION = '1.05'; | |
| 4 | } | ||
| 5 | |||
| 6 | 6 | ▶ | use 5.006; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 6 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 6 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 6 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 6 | ▶ | use CGI::Application 4.01; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 6 | ▶ | use HTML::Template; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 0 | ▶ | use HTML::Tidy 1.08; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 13 | |||
| 14 | require Exporter; | ||
| 15 | our @ISA = qw(Exporter); | ||
| 16 | |||
| 17 | our @EXPORT = qw(htmltidy htmltidy_clean htmltidy_config); | ||
| 18 | |||
| 19 | sub import | ||
| 20 | { | ||
| 21 | my $c = scalar caller; | ||
| 22 | $c->add_callback( 'devpopup_report', \&htmltidy_validate ) if $c->can('devpopup'); | ||
| 23 | goto &Exporter::import; | ||
| 24 | } | ||
| 25 | |||
| 26 | sub htmltidy | ||
| 27 | { | ||
| 28 | my $self = shift; | ||
| 29 | my $opts = $self->param('htmltidy_config') || {}; | ||
| 30 | htmltidy_config( $self, %$opts ) unless $self->{ __PACKAGE__ . 'OPTIONS' }; | ||
| 31 | $self->{ __PACKAGE__ . 'HTMLTIDY' } ||= HTML::Tidy->new( $self->{ __PACKAGE__ . 'OPTIONS' } ); | ||
| 32 | } | ||
| 33 | |||
| 34 | sub htmltidy_config | ||
| 35 | { | ||
| 36 | my $self = shift; | ||
| 37 | my %opts = @_; | ||
| 38 | |||
| 39 | # if no options are supplied, use the default config file. | ||
| 40 | # otherwise, all options are passed through (and expected to be | ||
| 41 | # valid tidy-options). | ||
| 42 | if( !%opts ) { | ||
| 43 | $opts{config_file} = __find_config(); | ||
| 44 | } | ||
| 45 | |||
| 46 | $self->{ __PACKAGE__ . 'OPTIONS' } = \%opts; | ||
| 47 | } | ||
| 48 | |||
| 49 | sub htmltidy_clean | ||
| 50 | { | ||
| 51 | my ( $self, $outputref ) = @_; | ||
| 52 | return unless __check_header($self); | ||
| 53 | $$outputref = $self->htmltidy->clean($$outputref); | ||
| 54 | } | ||
| 55 | |||
| 56 | sub htmltidy_validate | ||
| 57 | { | ||
| 58 | my ( $self, $outputref ) = @_; | ||
| 59 | return unless __check_header($self); | ||
| 60 | $self->htmltidy->parse( 'why would i need to pass a file name if it isn\'t used?', $$outputref ); | ||
| 61 | if ( $self->htmltidy->messages ) | ||
| 62 | { | ||
| 63 | my @msgs; | ||
| 64 | my @output = map { { html => $_ } } split $/, $$outputref; | ||
| 65 | my ($errors, $warnings) = (0,0); | ||
| 66 | foreach ( $self->htmltidy->messages() ) | ||
| 67 | { | ||
| 68 | $_->type == TIDY_WARNING ? $warnings++ : $errors++; | ||
| 69 | push @{ $output[ $_->line - 1 ]->{messages} }, | ||
| 70 | { | ||
| 71 | type => $_->type == TIDY_WARNING ? 'warning' : 'error', | ||
| 72 | line => $_->line, | ||
| 73 | column => $_->column, | ||
| 74 | text => $_->text, | ||
| 75 | }; | ||
| 76 | } | ||
| 77 | my $t = HTML::Template->new( filename => __find_my_path() . '/validate.tmpl', die_on_bad_params => 0, cache => 1 ); | ||
| 78 | $t->param( output => \@output ); | ||
| 79 | $self->devpopup->add_report( | ||
| 80 | title => 'HTML::Tidy validation report', | ||
| 81 | summary => "$errors errors, $warnings warnings", | ||
| 82 | report => $t->output | ||
| 83 | ); | ||
| 84 | } | ||
| 85 | else | ||
| 86 | { | ||
| 87 | $self->devpopup->add_report( | ||
| 88 | title => 'HTML::Tidy validation report', | ||
| 89 | summary => "Your HTML is valid!", | ||
| 90 | ); | ||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 94 | sub __check_header | ||
| 95 | { | ||
| 96 | my $self = shift; | ||
| 97 | |||
| 98 | return unless $self->header_type eq 'header'; # don't operate on redirects or 'none' | ||
| 99 | |||
| 100 | my %props = $self->header_props; | ||
| 101 | my ($type) = grep /type/i, keys %props; | ||
| 102 | |||
| 103 | return 1 unless defined $type; # no type defaults to html, so we have work to do | ||
| 104 | |||
| 105 | return $props{$type} =~ /html/i; | ||
| 106 | } | ||
| 107 | |||
| 108 | ### find the config file | ||
| 109 | ### 1. see if we can find the package version | ||
| 110 | ### 2. fall back to /etc/tidy.conf | ||
| 111 | sub __find_config | ||
| 112 | { | ||
| 113 | my $inc = __find_my_path() . '/tidy.conf'; | ||
| 114 | return -f $inc ? $inc : '/etc/tidy.conf'; | ||
| 115 | } | ||
| 116 | |||
| 117 | sub __find_my_path | ||
| 118 | { | ||
| 119 | my $inc = $INC{'CGI/Application/Plugin/HtmlTidy.pm'}; | ||
| 120 | $inc =~ s/\.pm$//; | ||
| 121 | return $inc; | ||
| 122 | } | ||
| 123 | |||
| 124 | 1; | ||
| 125 | |||
| 126 | __END__ | ||