App-Guiio

 view release on metacpan or  search on metacpan

setup/import_export/perl.pl  view on Meta::CPAN


register_import_export_handlers 
	(
	pod => 
		{
		IMPORT => \&import_pod,
		EXPORT => \&export_pod,
		},
		
	pl => 
		{
		IMPORT => \&import_pod,
		EXPORT => \&export_pod,
		},
		
	pm => 
		{
		IMPORT => \&import_pod,
		EXPORT => \&export_pod,
		},
	) ;

#----------------------------------------------------------------------------------------------------------------------------

sub import_pod
{
my ($self, $file)  = @_ ;

my ($base_name, $path, $extension) = File::Basename::fileparse($file, ('\..*')) ;
my $file_name = $base_name . $extension ;

my ($base64_data, $header, $footer) = get_base64_data($file_name) ;

my $decoded_base64 = MIME::Base64::decode($base64_data);
my $self_to_resurect =  decompress($decoded_base64) ;

my $VAR1 ;
my $resurected_self =  eval $self_to_resurect ;
die $@ if $@ ;

return($resurected_self, $file, {HEADER => $header, FOOTER => $footer}) ;
}

sub get_base64_data
{
=pod
find all guiio sections
select one section
extract section
remove diagram and padding
regenerate base 64 string
=cut

my ($file_name) = @_ ;

my ($header, $footer) = ('', '') ;

eval "use Pod::Select ; use Pod::Text;" ;
die $@ if $@ ;

open INPUT, '<', $file_name or die "get_base64_data: Can't open '$file_name'!\n" ;
open my $out, '>', \my $all_pod or die "Can't redirect to scalar output: $!\n";

my $parser = new Pod::Select();
$parser->parse_from_filehandle(\*INPUT, $out);

$all_pod .= '=cut' ; #add the =cut taken away by above parsing

my @guiio_pods ;

while($all_pod =~ /(^=.*?(?=\n=))/smg)
	{
	my $section = $1 ;
	if($section =~ s/^=for guiio\s*//i) 
		{
		push @guiio_pods, "=for guiio $section" ;
		last ;
		}
	}

#todo: handle files without guiio section
#todo: handle files with multiple guiio sections

my $guiio_section = $guiio_pods[0] ;
my @guiio_lines = split "\n", $guiio_section ;
my $guiio_header = shift @guiio_lines ;

#~ use Data::TreeDumper ;
#~ print DumpTree \@guiio_lines, 'guiio_lines' ;

my $whole_file = read_file($file_name) ;

if($whole_file =~ /(.*)$guiio_header.*?(\n=.*)/sm)
	{
	($header, $footer) = ($1, $2) ;
	}
else
	{
	die "get_base64_data: Can't find the text we just extracted!" ;
	}
	
my ($for, $guiio, $width, $name) = split ' ', $guiio_header ;

my $base64 = '' ;

for my $guiio_line (@guiio_lines)
	{
	substr($guiio_line, 0, $width + $BASE64_HEADER_SIZE + 1, '') ; # strip to base64
	$base64 .= $guiio_line . "\n" ;
	}

return ($base64, $header, $footer) ;
}

#----------------------------------------------------------------------------------------------------------------------------

sub export_pod
{
my ($self, $elements_to_save, $file, $data)  = @_ ;

my ($base_name, $path, $extension) = File::Basename::fileparse($file, ('\..*')) ;
my $file_name = $base_name . $extension ;

my @ascii_representation = $self->transform_elements_to_ascii_array() ;
my $longest_line =  max( map{length} @ascii_representation) ;



( run in 1.468 second using v1.01-cache-2.11-cpan-98e64b0badf )