Padre::Plugin::XML - L<Padre> and XML


Padre-Plugin-XML documentation Contained in the Padre-Plugin-XML distribution.

Index


Code Index:

NAME

Top

Padre::Plugin::XML - Padre and XML

Tidy XML

Top

use XML::Tidy to tidy XML

AUTHOR

Top

Fayland Lam, <fayland at gmail.com>

Heiko Jansen, <heiko_jansen@web.de>

COPYRIGHT & LICENSE

Top


Padre-Plugin-XML documentation Contained in the Padre-Plugin-XML distribution.

package Padre::Plugin::XML;

use warnings;
use strict;

our $VERSION = '0.10';

use base 'Padre::Plugin';
use Padre::Wx ();

sub padre_interfaces {
	'Padre::Plugin'   => 0.65,
	'Padre::Document' => 0.65,
}

sub registered_documents {
	'text/xml' => 'Padre::Plugin::XML::Document',
}

sub menu_plugins_simple {
	my $self = shift;
	return ('XML' => [
		Wx::gettext('Tidy XML'), sub { $self->tidy_xml },
	]);
}

sub tidy_xml {
	my ( $self ) = @_;
	
	my $main = $self->main;
	
	my $src = $main->current->text;
	my $doc = $main->current->document;

	unless ( $doc and $doc->isa('Padre::Document::XML') ) {
		$main->message( Wx::gettext("This is not a XML document!") );
		return;
	}

	my $code = ( $src ) ? $src : $doc->text_get;
	
	return unless ( defined $code and length($code) );
	
	require XML::Tidy;

	my $tidy_obj = '';
	my $string   = '';
	eval {
		$tidy_obj = XML::Tidy->new( xml => $code );
		$tidy_obj->tidy();
	
		$string = $tidy_obj->toString();
	};

	if ( ! $@ ) {
		if ( $src ) {
			$string =~ s/\A<\?xml.+?\?>\r?\n?//o;
			my $editor = $main->current->editor;
			$editor->ReplaceSelection( $string );
		} else {
			$doc->text_set( $string );
		}
	}
	else {
		$main->message( Wx::gettext("Tidying failed due to error(s):") . "\n\n" . $@ );
	}

	return;
}

sub editor_enable {
	my $self     = shift;
	my $editor   = shift;
	my $document = shift;

	if ( $document->isa('Padre::Document::XML') ) {
		$editor->SetProperty('fold.html', '1');
	}

	return 1;
}

1;
__END__