| 1 |
#!/usr/bin/perl |
| 2 |
#----------------------------------------------------------------------------- |
| 3 |
# $Id : odfsearch 0.3 2010-01-11 JMG$ |
| 4 |
#----------------------------------------------------------------------------- |
| 5 |
|
| 6 |
=head1 NAME |
| 7 |
|
| 8 |
odfsearch - Text selection and copy from one document to another using callback |
| 9 |
|
| 10 |
=head1 SYNOPSIS |
| 11 |
|
| 12 |
This sample script extracts the content of every text element (paragraph, |
| 13 |
header, list item) in a source document matching a given pattern (string or |
| 14 |
regex), and appends it as a new paragraph to a target document. The target |
| 15 |
document must exist. |
| 16 |
|
| 17 |
The target document must exist (it can be an empty document or a template). |
| 18 |
|
| 19 |
usage : odfsearch target_file source_file "search_string" |
| 20 |
|
| 21 |
=cut |
| 22 |
|
| 23 |
use OpenOffice::OODoc 2.101; |
| 24 |
|
| 25 |
our $count = 0; |
| 26 |
|
| 27 |
#----------------------------------------------------------------------------- |
| 28 |
# callback function executed each time a matching element is found while |
| 29 |
# the selectElementsByContent method is running. |
| 30 |
# the last argument is the selected element; it's preceded by |
| 31 |
# application-specific arguments |
| 32 |
|
| 33 |
sub append_text |
| 34 |
{ |
| 35 |
my ($dst, $stylename, $element) = @_; |
| 36 |
$count++; |
| 37 |
# convert the given element to flat text |
| 38 |
# we use getText as a class method; here we don't need/know |
| 39 |
# the context of the source document |
| 40 |
my $text = OpenOffice::OODoc::Text->getText($element); |
| 41 |
# append as a new paragraph to target document |
| 42 |
$dst->appendParagraph(text => $text, style => $stylename); |
| 43 |
return undef; |
| 44 |
} |
| 45 |
|
| 46 |
#----------------------------------------------------------------------------- |
| 47 |
# main program |
| 48 |
|
| 49 |
# get the command line arguments |
| 50 |
my $target_file = $ARGV[0]; |
| 51 |
my $source_file = $ARGV[1]; |
| 52 |
my $search_string = $ARGV[2]; |
| 53 |
my $stylename = $ARGV[3] || 'Text body'; |
| 54 |
|
| 55 |
die "Usage : odfsearch target_file source_file search_string\n" |
| 56 |
unless ($target_file && $source_file && $search_string); |
| 57 |
|
| 58 |
# create the 2 Document instances |
| 59 |
print "Opening the target file $target_file...\n"; |
| 60 |
my $target_doc = odfDocument(file => $target_file) |
| 61 |
or die "$target_file is not available\n"; |
| 62 |
print "Opening the source file $source_file...\n"; |
| 63 |
my $source_doc = odfDocument(file => $source_file) |
| 64 |
or die "$source_file is not available\n"; |
| 65 |
|
| 66 |
# the main processing takes place here : |
| 67 |
# the search method invocation, with search string and reference to |
| 68 |
# the procedure to be executed to process each selected element. |
| 69 |
# The selected element is automatically passed as an argument to the |
| 70 |
# callback, following explicit (optional) arguments provided by |
| 71 |
# the application (here $source_doc, $target_doc, $stylename) |
| 72 |
print "Selecting the content...\n"; |
| 73 |
$source_doc->selectElementsByContent |
| 74 |
( |
| 75 |
$search_string, # filter expression |
| 76 |
\&append_text, # callback function reference |
| 77 |
$target_doc, $stylename # callback function arguments |
| 78 |
); |
| 79 |
|
| 80 |
# append a conclusion |
| 81 |
my $report = "$count text elements from $source_file " . |
| 82 |
"have been selected, converted in paragraphs " . |
| 83 |
"and appended to $target_file. Good reading."; |
| 84 |
$target_doc->appendParagraph(text => $report, style => $stylename); |
| 85 |
|
| 86 |
# save the changes and leave |
| 87 |
print "Saving the target document...\n"; |
| 88 |
$target_doc->save; |
| 89 |
print "Job complete\n"; |
| 90 |
exit; |
| 91 |
|
| 92 |
#----------------------------------------------------------------------------- |