File Coverage

blib/lib/Padre/Plugin/XML.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             package Padre::Plugin::XML;
2            
3 1     1   35768 use warnings;
  1         42  
  1         42  
4 1     1   7 use strict;
  1         2  
  1         56  
5            
6             our $VERSION = '0.10';
7            
8 1     1   6 use base 'Padre::Plugin';
  1         6  
  1         655  
9 1     1   424 use Padre::Wx ();
  0            
  0            
10            
11             sub padre_interfaces {
12             'Padre::Plugin' => 0.65,
13             'Padre::Document' => 0.65,
14             }
15            
16             sub registered_documents {
17             'text/xml' => 'Padre::Plugin::XML::Document',
18             }
19            
20             sub menu_plugins_simple {
21             my $self = shift;
22             return ('XML' => [
23             Wx::gettext('Tidy XML'), sub { $self->tidy_xml },
24             ]);
25             }
26            
27             sub tidy_xml {
28             my ( $self ) = @_;
29            
30             my $main = $self->main;
31            
32             my $src = $main->current->text;
33             my $doc = $main->current->document;
34            
35             unless ( $doc and $doc->isa('Padre::Document::XML') ) {
36             $main->message( Wx::gettext("This is not a XML document!") );
37             return;
38             }
39            
40             my $code = ( $src ) ? $src : $doc->text_get;
41            
42             return unless ( defined $code and length($code) );
43            
44             require XML::Tidy;
45            
46             my $tidy_obj = '';
47             my $string = '';
48             eval {
49             $tidy_obj = XML::Tidy->new( xml => $code );
50             $tidy_obj->tidy();
51            
52             $string = $tidy_obj->toString();
53             };
54            
55             if ( ! $@ ) {
56             if ( $src ) {
57             $string =~ s/\A<\?xml.+?\?>\r?\n?//o;
58             my $editor = $main->current->editor;
59             $editor->ReplaceSelection( $string );
60             } else {
61             $doc->text_set( $string );
62             }
63             }
64             else {
65             $main->message( Wx::gettext("Tidying failed due to error(s):") . "\n\n" . $@ );
66             }
67            
68             return;
69             }
70            
71             sub editor_enable {
72             my $self = shift;
73             my $editor = shift;
74             my $document = shift;
75            
76             if ( $document->isa('Padre::Document::XML') ) {
77             $editor->SetProperty('fold.html', '1');
78             }
79            
80             return 1;
81             }
82            
83             1;
84             __END__