source: gsdl/trunk/bin/script/replace_srcdoc_with_html.pl@ 15121

Last change on this file since 15121 was 15121, checked in by ak19, 14 years ago

New perl script replace_srcdoc_with_html.pl allows users to replace a source document in their collection with a Greenstone-generated html version of it when they rightclick in GLI's collection folder view. The script can also be run from the command line.

  • Property svn:executable set to *
File size: 6.4 KB
Line 
1#!/usr/bin/perl -w
2
3
4BEGIN {
5 die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
6 unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
7 unshift (@INC, "$ENV{'GSDLHOME'}/perllib/plugins");
8}
9
10use strict;
11no strict 'subs'; # allow barewords (eg STDERR) as function arguments
12no strict 'refs'; # allow filehandles to be variables and vice versa
13
14use encodings;
15use printusage;
16use parse2;
17use FileHandle;
18
19my $arguments =
20 [
21 { 'name' => "language",
22 'desc' => "{scripts.language}",
23 'type' => "string",
24 'reqd' => "no",
25 'hiddengli' => "yes" },
26 { 'name' => "plugin",
27 'desc' => "{srcreplace.plugin}",
28 'type' => "string",
29 'reqd' => "yes",
30 'hiddengli' => "yes"},
31 { 'name' => "verbosity",
32 'desc' => "{import.verbosity}",
33 'type' => "int",
34 'range' => "0,",
35 'deft' => "1",
36 'reqd' => "no",
37 'modegli' => "4" },
38 # Do not remove the following option, it's a flag for generating the xml of the options
39 # It WILL be used!
40 { 'name' => "xml", # run with -xml, the output generated should be valid XML. Used from GLI
41 'desc' => "",
42 'type' => "flag",
43 'reqd' => "no",
44 'hiddengli' => "yes" }
45 ];
46
47my $options = { 'name' => "replace_srcdoc_with_html.pl",
48 'desc' => "{srcreplace.desc}",
49 'args' => $arguments };
50
51
52sub main
53{
54 my ($language, $plugin, $verbosity);
55
56 my $xml = 0;
57
58 my $hashParsingResult = {};
59
60 # parse the options
61 my $intArgLeftinAfterParsing = parse2::parse(\@ARGV,$arguments,$hashParsingResult,"allow_extra_options");
62
63 # If parse returns -1 then something has gone wrong
64 if ($intArgLeftinAfterParsing == -1)
65 {
66 &PrintUsage::print_txt_usage($options, "{srcreplace.params}");
67 die "\n";
68 }
69
70 foreach my $strVariable (keys %$hashParsingResult)
71 {
72 eval "\$$strVariable = \$hashParsingResult->{\"\$strVariable\"}";
73 }
74
75 # If $language has been specified, load the appropriate resource bundle
76 # (Otherwise, the default resource bundle will be loaded automatically)
77 if ($language && $language =~ /\S/) {
78 &gsprintf::load_language_specific_resource_bundle($language);
79 }
80
81 if ($xml) {
82 &PrintUsage::print_xml_usage($options);
83 print "\n";
84 return;
85 }
86
87 # There should be one arg left after parsing (the filename)
88 # Or the user may have specified -h, in which case we output the usage
89 if($intArgLeftinAfterParsing != 1 || (@ARGV && $ARGV[0] =~ /^\-+h/))
90 {
91 &PrintUsage::print_txt_usage($options, "{srcreplace.params}");
92 die "\n";
93 }
94
95 # The filename of the document to be replaced is the first value
96 # that remains after the options have been parsed out
97 my $filename = $ARGV[0];
98 if (!defined $filename || $filename !~ /\w/) {
99
100 &PrintUsage::print_txt_usage($options, "{srcreplace.params}");
101 print STDERR "You need to specify a filename";
102 die "\n";
103 }
104 # check that file exists
105 if (!-e $filename) {
106 print STDERR "File $filename doesn't exist...\n";
107 die "\n";
108 }
109 # check required options
110 if (!defined $plugin || $plugin !~ /\w/) {
111 &PrintUsage::print_txt_usage($options, "{srcreplace.params}");
112 print STDERR "You need to specify a plugin";
113 die "\n";
114 }
115
116 # ConvertToPlug.pm's subclasses should be available here through GLI,
117 # but in cmdline version, these should be supplied
118 my $plugobj;
119 require "$plugin.pm";
120 eval ("\$plugobj = new $plugin()");
121 die "$@" if $@;
122
123 # ...and initialize it
124 $plugobj->init(1, "STDERR", "STDERR");
125
126 # find the import directory, where we want to create it in. This is where the file
127 # passed as parameter by GLI is located.
128
129 # derive tmp filename from input filename
130 my ($tailname, $import_dir, $suffix)
131 = &File::Basename::fileparse($filename, "\\.[^\\.]+\$");
132
133 # Use the plugin's tmp_area_convert_file function to avoid duplicating code.
134 # This method returns the name of the output file. In the case of Word docs,
135 # if converted with windows_scripting a "filename_files" folder might have been
136 # created for associated files. Same situation when using wvware with gsConvert.pl.
137 # (When old gsConvert.pl was used, wvware created no separate directory, instead files
138 # associated with the html generated would be at the same level in the tmp folder
139 # where the output file was created.) Now it's the same no matter whether wvware
140 # or windows_scripting did the conversion of the Word doc to html.
141 my $output_filename = $plugobj->tmp_area_convert_file("html", $filename);
142 # my $output_filename = $plugobj->tmp_area_convert_file($output_ext, $input_filename, $textref);
143
144 if(!defined $output_filename || (!-e $output_filename)) { # if no output html file was created, then error message
145 print STDERR "***replace_srcdoc_with_html.pl: no output file created for $filename ***\n";
146 die "No html file created for $filename. Replacement did not take place\n"; # Program NEEDS to die here,
147 # else the error that occurred is not transmitted to GLI and it thinks instead that execution was fine
148 #return 0; # error code 0 for false <- NO, needs to die, not return!
149 }
150 #else
151
152 # now, find out what to move:
153 # it may be a single file, or, if it is a word doc, it may also have an image folder
154 # which has the name "filename-without-extension_files"
155 my ($tmp_name, $tmp_dir, $ext) = &File::Basename::fileparse($output_filename, "\\.[^\\.]+\$");
156
157 if($import_dir ne $tmp_dir) { # output file is not in the same folder
158 my $assoc_folder = &util::filename_cat($tmp_dir, $tmp_name."_files");
159
160 # use util to move it all to the import directory where the original file is located
161 #&util::mv($output_filename, &util::filename_cat($import_dir, $tmp_name.$ext)); # doesn't work
162 #&util::mv($output_filename, $import_dir); # doesn't work
163 &util::cp($output_filename, $import_dir);
164 &util::rm($output_filename);
165 if(defined $assoc_folder) { # move any associated folders too
166 print STDERR "****Folder for associated files is $assoc_folder\n";
167 #&util::mv($assoc_folder, $import_dir); # doesn't work for me
168 &util::cp_r($assoc_folder, $import_dir);
169 &util::rm_r($assoc_folder);
170 }
171 } # else output file was created in the same folder as the input file
172 # this happens for txt files processed by TEXTPlug
173 # in this case, there will be no image folder AND no moving of output html file is necessary
174
175 # Now we can remove the source doc permanently (there are no assocdirs for source doc)
176 &util::rm($filename);
177
178}
179&main(@ARGV);
Note: See TracBrowser for help on using the repository browser.