App-Asciio

 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 asciio 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 @asciio_pods ;

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

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

my $asciio_section = $asciio_pods[0] ;
my @asciio_lines = split "\n", $asciio_section ;
my $asciio_header = shift @asciio_lines ;

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

my $whole_file = read_file($file_name) ;

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

my $base64 = '' ;

for my $asciio_line (@asciio_lines)
	{
	substr($asciio_line, 0, $width + $BASE64_HEADER_SIZE + 1, '') ; # strip to base64
	$base64 .= $asciio_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 0.679 second using v1.01-cache-2.11-cpan-ceb78f64989 )