HTML.pm untested

filter: Statementstmt 0.0% 0 / 88 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 150 CC 13 SCAR 29.3
CC
13
Cov
0%
tidy_html
42.0
html_lint
30.0
validate_html
20.0
CRAP 18.7
1 package Padre::Plugin::HTML;
2 BEGIN {
3 $Padre::Plugin::HTML::VERSION = '0.14';
4 }
5  
6 # ABSTRACT: HTML Support for Padre
7  
8 use warnings;
9 use strict;
10  
11 use base 'Padre::Plugin';
12 use Padre::Wx ();
13  
14 sub padre_interfaces {
15 'Padre::Plugin' => 0.47, 'Padre::Document' => 0.47,;
16 }
17  
18 sub registered_documents {
19 'text/html' => 'Padre::Plugin::HTML::Document',;
20 }
21  
22 sub menu_plugins_simple {
23 my $self = shift;
24 return (
25 Wx::gettext('HTML') => [
26 Wx::gettext('Tidy HTML'),
27 sub { $self->tidy_html },
28 Wx::gettext('HTML Lint'),
29 sub { $self->html_lint },
30 Wx::gettext('Validate HTML'),
31 sub { $self->validate_html },
32 Wx::gettext('Docs') => [
33 Wx::gettext('HTML 4.01 Specification'),
34 sub { Padre::Wx::launch_browser('http://www.w3.org/TR/html401/'); },
35 ],
36 ]
37 );
38 }
39  
40 sub validate_html {
41 my ($self) = @_;
42 my $main = $self->main;
43  
44 my $doc = $main->current->document;
45 my $code = $doc->text_get;
46  
47 unless ( $code and length($code) ) {
48 Wx::MessageBox( 'No Code', 'Error', Wx::wxOK | Wx::wxCENTRE, $main );
49 }
50  
51 require WebService::Validator::HTML::W3C;
52 my $v = WebService::Validator::HTML::W3C->new( detailed => 1 );
53  
54 if ( $v->validate_markup($code) ) {
55 if ( $v->is_valid ) {
56 $self->_output("HTML is valid\n");
57 } else {
58 my $error_text = "HTML is not valid\n";
59 foreach my $error ( @{ $v->errors } ) {
60 $error_text .= sprintf( "%s at line %d\n", $error->msg, $error->line );
61 }
62 $self->_output($error_text);
63 }
64 } else {
65 my $error_text = sprintf( "Failed to validate the code: %s\n", $v->validator_error );
66 $self->_output($error_text);
67 }
68 }
69  
70 sub _output {
71 my ( $self, $text ) = @_;
72 my $main = $self->main;
73  
74 $main->show_output(1);
75 $main->output->clear;
76 $main->output->AppendText($text);
77 }
78  
79 sub tidy_html {
80 my ($self) = @_;
81 my $main = $self->main;
82  
83 my $src = $main->current->text;
84 my $doc = $main->current->document;
85 return unless $doc;
86 my $code = ($src) ? $src : $doc->text_get;
87  
88 return unless ( defined $code and length($code) );
89  
90 require HTML::Tidy;
91 my $tidy = HTML::Tidy->new;
92  
93 my $cleaned_code = $tidy->clean($code);
94  
95 my $text;
96 for my $message ( $tidy->messages ) {
97 $text .= $message->as_string . "\n";
98 }
99  
100 $text = 'OK' unless ( length($text) );
101 $self->_output($text);
102  
103 if ($src) {
104 my $editor = $main->current->editor;
105 $editor->ReplaceSelection($cleaned_code);
106 } else {
107 $doc->text_set($cleaned_code);
108 }
109 }
110  
111 sub html_lint {
112 my ($self) = @_;
113 my $main = $self->main;
114  
115 my $src = $main->current->text;
116 my $doc = $main->current->document;
117 return unless $doc;
118 my $code = ($src) ? $src : $doc->text_get;
119  
120 return unless ( defined $code and length($code) );
121  
122 require HTML::Lint;
123 my $lint = HTML::Lint->new;
124  
125 $lint->parse($code);
126  
127 my $text;
128 my $error_count = $lint->errors;
129  
130 foreach my $error ( $lint->errors ) {
131 $text .= $error->as_string . "\n";
132 }
133  
134 $text = 'OK' unless ( length($text) );
135 $self->_output($text);
136 }
137  
138 1;
139  
140  
141 =pod
142  
143 =head1 NAME
144  
145 Padre::Plugin::HTML - HTML Support for Padre
146  
147 =head1 VERSION
148  
149 version 0.14
150  
151 =head1 Validate HTML
152  
153 use L<WebService::Validator::HTML::W3C> to validate the HTML
154  
155 =head1 Tidy HTML
156  
157 use L<HTML::Tidy> to tidy HTML
158  
159 =head1 HTML Lint
160  
161 use L<HTML::Lint> to ?????
162  
163 =head1 AUTHORS
164  
165 =over 4
166  
167 =item *
168  
169 Fayland Lam <fayland@gmail.com>
170  
171 =item *
172  
173 Ahmad M. Zawawi <ahmad.zawawi@gmail.com>
174  
175 =back
176  
177 =head1 COPYRIGHT AND LICENSE
178  
179 This software is copyright (c) 2010 by Fayland Lam.
180  
181 This is free software; you can redistribute it and/or modify it under
182 the same terms as the Perl 5 programming language system itself.
183  
184 =cut
185  
186  
187 __END__