« blib/lib/App/Asciio/Actions/Colors.pm
↑ Summary
blib/lib/App/Asciio/Text/Asciio/stripes/editable_exec_box.pm »
| 1 | |||
| 2 | use strict ; | ||
| 3 | use warnings ; | ||
| 4 | use utf8 ; | ||
| 5 | |||
| 6 | use Compress::Bzip2 qw(:all :utilities :gzip); | ||
| 7 | use MIME::Base64 (); | ||
| 8 | |||
| 9 | use File::Slurp ; | ||
| 10 | use Data::Dumper ; | ||
| 11 | use List::Util qw(max); | ||
| 12 | use File::Basename ; | ||
| 13 | |||
| 14 | use App::Asciio ; | ||
| 15 | use App::Asciio::String ; | ||
| 16 | |||
| 17 | my $BASE64_HEADER = (' ' x 120) . '#asciio' ; | ||
| 18 | my $BASE64_HEADER_SIZE = length($BASE64_HEADER) ; | ||
| 19 | |||
| 20 | #---------------------------------------------------------------------------------------------------------------------------- | ||
| 21 | |||
| 22 | register_import_export_handlers | ||
| 23 | ( | ||
| 24 | pod => | ||
| 25 | { | ||
| 26 | IMPORT => \&import_pod, | ||
| 27 | EXPORT => \&export_pod, | ||
| 28 | }, | ||
| 29 | |||
| 30 | pl => | ||
| 31 | { | ||
| 32 | IMPORT => \&import_pod, | ||
| 33 | EXPORT => \&export_pod, | ||
| 34 | }, | ||
| 35 | |||
| 36 | pm => | ||
| 37 | { | ||
| 38 | IMPORT => \&import_pod, | ||
| 39 | EXPORT => \&export_pod, | ||
| 40 | }, | ||
| 41 | ) ; | ||
| 42 | |||
| 43 | #---------------------------------------------------------------------------------------------------------------------------- | ||
| 44 | |||
| 45 | sub import_pod | ||
| 46 | { | ||
| 47 | my ($self, $file) = @_ ; | ||
| 48 | |||
| 49 | my ($base_name, $path, $extension) = File::Basename::fileparse($file, ('\..*')) ; | ||
| 50 | my $file_name = $base_name . $extension ; | ||
| 51 | |||
| 52 | my ($base64_data, $header, $footer) = get_base64_data($file_name) ; | ||
| 53 | |||
| 54 | my $decoded_base64 = MIME::Base64::decode($base64_data); | ||
| 55 | my $self_to_resurect = decompress($decoded_base64) ; | ||
| 56 | |||
| 57 | my $VAR1 ; | ||
| 58 | my $resurected_self = eval $self_to_resurect ; | ||
| 59 | die $@ if $@ ; | ||
| 60 | |||
| 61 | return($resurected_self, $file, {HEADER => $header, FOOTER => $footer}) ; | ||
| 62 | } | ||
| 63 | |||
| 64 | sub get_base64_data | ||
| 65 | { | ||
| 66 | =pod | ||
| 67 | find all asciio sections | ||
| 68 | select one section | ||
| 69 | extract section | ||
| 70 | remove diagram and padding | ||
| 71 | regenerate base 64 string | ||
| 72 | =cut | ||
| 73 | |||
| 74 | my ($file_name) = @_ ; | ||
| 75 | |||
| 76 | my ($header, $footer) = ('', '') ; | ||
| 77 | |||
| 78 | eval "use Pod::Select ; use Pod::Text;" ; | ||
| 79 | die $@ if $@ ; | ||
| 80 | |||
| 81 | open INPUT, '<', $file_name or die "get_base64_data: Can't open '$file_name'!\n" ; | ||
| 82 | open my $out, '>', \my $all_pod or die "Can't redirect to scalar output: $!\n"; | ||
| 83 | |||
| 84 | my $parser = new Pod::Select(); | ||
| 85 | $parser->parse_from_filehandle(\*INPUT, $out); | ||
| 86 | |||
| 87 | $all_pod .= '=cut' ; #add the =cut taken away by above parsing | ||
| 88 | |||
| 89 | my @asciio_pods ; | ||
| 90 | |||
| 91 | while($all_pod =~ /(^=.*?(?=\n=))/smg) | ||
| 92 | { | ||
| 93 | my $section = $1 ; | ||
| 94 | if($section =~ s/^=for asciio\s*//i) | ||
| 95 | { | ||
| 96 | push @asciio_pods, "=for asciio $section" ; | ||
| 97 | last ; | ||
| 98 | } | ||
| 99 | } | ||
| 100 | |||
| 101 | #todo: handle files without asciio section | ||
| 102 | #todo: handle files with multiple asciio sections | ||
| 103 | |||
| 104 | my $asciio_section = $asciio_pods[0] ; | ||
| 105 | my @asciio_lines = split "\n", $asciio_section ; | ||
| 106 | my $asciio_header = shift @asciio_lines ; | ||
| 107 | |||
| 108 | #~ use Data::TreeDumper ; | ||
| 109 | #~ print DumpTree \@asciio_lines, 'asciio_lines' ; | ||
| 110 | |||
| 111 | my $whole_file = read_file($file_name) ; | ||
| 112 | |||
| 113 | if($whole_file =~ /(.*)$asciio_header.*?(\n=.*)/sm) | ||
| 114 | { | ||
| 115 | ($header, $footer) = ($1, $2) ; | ||
| 116 | } | ||
| 117 | else | ||
| 118 | { | ||
| 119 | die "get_base64_data: Can't find the text we just extracted!" ; | ||
| 120 | } | ||
| 121 | |||
| 122 | my ($for, $asciio, $width, $name) = split ' ', $asciio_header ; | ||
| 123 | |||
| 124 | my $base64 = '' ; | ||
| 125 | |||
| 126 | for my $asciio_line (@asciio_lines) | ||
| 127 | { | ||
| 128 | substr($asciio_line, 0, $width + $BASE64_HEADER_SIZE + 1, '') ; # strip to base64 | ||
| 129 | $base64 .= $asciio_line . "\n" ; | ||
| 130 | } | ||
| 131 | |||
| 132 | return ($base64, $header, $footer) ; | ||
| 133 | } | ||
| 134 | |||
| 135 | #---------------------------------------------------------------------------------------------------------------------------- | ||
| 136 | |||
| 137 | sub export_pod | ||
| 138 | { | ||
| 139 | my ($self, $elements_to_save, $file, $data) = @_ ; | ||
| 140 | |||
| 141 | my ($base_name, $path, $extension) = File::Basename::fileparse($file, ('\..*')) ; | ||
| 142 | my $file_name = $base_name . $extension ; | ||
| 143 | |||
| 144 | my @ascii_representation = $self->transform_elements_to_ascii_array() ; | ||
| 145 | my $longest_line = max( map{unicode_length($_)} @ascii_representation) ; | ||
| 146 | |||
| 147 | my $compressed_self = compress($self->serialize_self() . '$VAR1 ;') ; | ||
| 148 | |||
| 149 | my $base64 =MIME::Base64::encode($compressed_self, '') ; | ||
| 150 | my $base64_chunk_size = int((length($base64) / @ascii_representation) + 1) ; | ||
| 151 | |||
| 152 | if($self->{CREATE_BACKUP} && -e $file) | ||
| 153 | { | ||
| 154 | use File::Copy; | ||
| 155 | copy($file,"$file.bak") or die "export_pod: Copy failed while making backup copy: $!"; | ||
| 156 | } | ||
| 157 | |||
| 158 | open POD, ">:encoding(utf8)", $file_name or die "export_pod: can't open file '$file_name'!\n"; | ||
| 159 | |||
| 160 | print POD $data->{HEADER} || '' ; | ||
| 161 | print POD "=for asciio $longest_line $base_name\n\n" ; | ||
| 162 | |||
| 163 | for my $diagram_line (@ascii_representation) | ||
| 164 | { | ||
| 165 | my $padding = ' ' x ($longest_line - unicode_length($diagram_line)) ; | ||
| 166 | my $base64_chunk = substr($base64, 0, $base64_chunk_size, '') || '' ; | ||
| 167 | |||
| 168 | print POD ' ' , $diagram_line, $padding, $BASE64_HEADER, $base64_chunk, "\n" | ||
| 169 | } | ||
| 170 | |||
| 171 | print POD $data->{FOOTER} || "\n=cut\n\n"; | ||
| 172 | close POD ; | ||
| 173 | |||
| 174 | return $file ; | ||
| 175 | } | ||
| 176 | |||
| 177 |