@@ -4,11 +4,109 @@ use strict;
44use warnings;
55use utf8 ;
66
7- use App::Asciio ;
8-
97use Module::Util qw( find_installed) ;
108use File::Basename ;
9+ use Archive::Tar ;
10+
11+ use open qw( :std :encoding(UTF-8) ) ;
12+ binmode (STDOUT ) ;
13+ binmode (STDIN ) ;
14+
15+ use App::Asciio ;
16+ my $ASCIIO_MIME_TYPE = " application/x-asciio" ;
17+
18+ # ------------------------------------------------------------------------------
19+
20+ asciio_to_text() ;
21+
22+ # ------------------------------------------------------------------------------
23+
24+ sub asciio_to_text
25+ {
26+ my ($asciio_config , $asciio ) = get_asciio() ;
27+
28+ if (-p STDIN || -f STDIN )
29+ {
30+ print STDERR " document_name: reading from STDIN\n " ;
31+
32+ undef $/ ;
33+ $asciio -> load_self($asciio -> get_decompressed_asciio(<STDIN >)) ;
34+
35+ print $asciio -> transform_elements_to_ascii_buffer() ;
36+ }
37+ else
38+ {
39+ for my $project_name ($asciio_config -> {TARGETS }-> @*)
40+ {
41+ process_file($asciio , $project_name ) ;
42+ }
43+
44+ # if(defined $asciio_config->{SCRIPT})
45+ # {
46+ # require App::Asciio::Scripting ;
47+
48+ # App::Asciio::Scripting::run_external_script($asciio, $asciio_config->{SCRIPT}) ;
49+ # }
50+ }
51+
52+ }
53+
54+ # ------------------------------------------------------------------------------
55+
56+ sub process_file
57+ {
58+ my ($asciio , $project_name ) = @_ ;
59+
60+ # save original settings. You could also use lexical typeglobs.
61+ *OLD_STDOUT = *STDOUT ;
62+ *OLD_STDERR = *STDERR ;
1163
64+ # reassign STDOUT, STDERR
65+ open my $log_fh , ' >>' , ' asciio_io.log' ;
66+ *STDOUT = $log_fh ;
67+ *STDERR = $log_fh ;
68+
69+ my $tar = Archive::Tar-> new($project_name ) ;
70+
71+ # restore STDOUT/STDERR
72+ *STDOUT = *OLD_STDOUT;
73+ *STDERR = *OLD_STDERR;
74+
75+ if ($tar )
76+ {
77+ my %documents = map { $_ => 1 } grep { $_ ne ' asciio_project' } $tar -> list_files ;
78+
79+ my $serialized_asciio_project = $tar -> get_content(' asciio_project' ) ;
80+ my $asciio_project = eval { Sereal::Decoder-> new-> decode($serialized_asciio_project ) } ;
81+
82+ if ($@ )
83+ {
84+ print STDERR " Error: deserializing '$project_name ': $@ "
85+ }
86+ else
87+ {
88+ for my $document_name ($asciio_project -> {documents }-> @*)
89+ {
90+ $asciio -> load_serialized_self($tar -> get_content($document_name )) ;
91+ print STDERR " document_name: $document_name \n " ;
92+ print $asciio -> transform_elements_to_ascii_buffer() ;
93+ }
94+ }
95+ }
96+ else
97+ {
98+ print STDERR " document_name: $project_name \n " ;
99+
100+ my $document_name = $asciio -> load_file($project_name ) ;
101+
102+ print $asciio -> transform_elements_to_ascii_buffer() ;
103+ }
104+ }
105+
106+ # ------------------------------------------------------------------------------
107+
108+ sub get_asciio
109+ {
12110my $asciio = new App::Asciio() ;
13111
14112$asciio -> {UI } = ' TUI' ;
50148 $asciio -> setup($setup_paths , { %object_override , DISPLAY_SETUP_INFORMATION_ACTION => 0 }) ;
51149 }
52150
53- if (defined $asciio_config -> {TARGETS }[0])
54- {
55- local $asciio -> {ACTION_VERBOSE } = sub { ; } ;
56- $asciio -> run_actions_by_name([' Open' , $asciio_config -> {TARGETS }[0]]) ;
57- }
58-
59-
60- if (defined $asciio_config -> {SCRIPT })
61- {
62- require App::Asciio::Scripting ;
63-
64- App::Asciio::Scripting::run_external_script($asciio , $asciio_config -> {SCRIPT }) ;
65- }
66-
67- use open qw( :std :encoding(UTF-8) ) ;
68- print $asciio -> transform_elements_to_ascii_buffer() ;
151+ return $asciio_config , $asciio ;
152+ }
69153
0 commit comments