| 1 |
#!/usr/bin/perl |
| 2 |
#----------------------------------------------------------------------------- |
| 3 |
# $Id : odfhighlight 0.3 2010-01-11 JMG$ |
| 4 |
#----------------------------------------------------------------------------- |
| 5 |
|
| 6 |
=head1 NAME |
| 7 |
|
| 8 |
odfhighlight - search, replace and highlight text in a document |
| 9 |
|
| 10 |
=head1 SYNOPSIS |
| 11 |
|
| 12 |
odfhighlight "source.odt" "search string" -r "replacement" -o "target.odt" |
| 13 |
|
| 14 |
replaces "search string" by "replacement" in the file "source.odt", highlights |
| 15 |
each replacement with a yellow (default) backgound, then writes the resulting |
| 16 |
document as "target.odt" |
| 17 |
|
| 18 |
odfhighlight "myfile.odt" "search string" -color "green" |
| 19 |
|
| 20 |
highlights each occurrence of "search string" in "myfile.odt" with a green |
| 21 |
background color, without changing the text (without "-o" option, the changes |
| 22 |
apply to "myfile.odt" |
| 23 |
|
| 24 |
=head1 ARGUMENTS AND OPTIONS |
| 25 |
|
| 26 |
=head2 Default behaviour |
| 27 |
|
| 28 |
With the "minimal" command line, with only a filename and a string as |
| 29 |
arguments, each matching string is highlighted with a yellow background |
| 30 |
and represented with the "Standard" style. |
| 31 |
|
| 32 |
=head2 Options |
| 33 |
|
| 34 |
-e --encoding "xxxxxx" |
| 35 |
|
| 36 |
character set to use, if different from the default |
| 37 |
|
| 38 |
-r --replacement "new string" |
| 39 |
|
| 40 |
"new string" is used as a replacement for "search string" |
| 41 |
|
| 42 |
-c --color "code" |
| 43 |
|
| 44 |
an RGB color code, expressed either as the concatenation of |
| 45 |
3 comma-separated decimal values (each one in the range |
| 46 |
0..255, ex: "72,61,139" for a dark slate blue), or a 6-digit |
| 47 |
hexadecimal number, preceded by a "#" (ex: #00ff00 for green) |
| 48 |
or, if a colormap is available and known in your |
| 49 |
OpenOffice::OODoc installation, a symbolic color name (ex: |
| 50 |
"sky blue") |
| 51 |
|
| 52 |
-s --stylename "name" |
| 53 |
|
| 54 |
the name of the color style (default: "MyHighlight"); the |
| 55 |
user must provide a style name that is not already in use |
| 56 |
in the document |
| 57 |
|
| 58 |
-p --property "property=value" |
| 59 |
|
| 60 |
This option can be repeated; each occurrence gives an |
| 61 |
additional property for the highlight style (font name, size, |
| 62 |
foreground color, ...). For example, with the combination of |
| 63 |
-p 'fo:color=#ff0000' and -p 'fo:font-size=18pt', the |
| 64 |
highlighted text will be made of 18pt-sized, red characters. |
| 65 |
In order to master these options, you should have some |
| 66 |
knowledge of the Form Objects (FO) vocabulary that is used |
| 67 |
in the OpenDocument specification. |
| 68 |
|
| 69 |
-o --output "filename" |
| 70 |
-t --target "filename" |
| 71 |
|
| 72 |
an alternative filename to save the modified document, when |
| 73 |
the source document must remain unchanged |
| 74 |
|
| 75 |
=cut |
| 76 |
|
| 77 |
|
| 78 |
#----------------------------------------------------------------------------- |
| 79 |
|
| 80 |
use OpenOffice::OODoc 2.101; |
| 81 |
use Getopt::Long; |
| 82 |
|
| 83 |
#----------------------------------------------------------------------------- |
| 84 |
# getting the arguments and options |
| 85 |
|
| 86 |
my $encoding = $OpenOffice::OODoc::XPath::LOCAL_CHARSET; |
| 87 |
my $target = undef; |
| 88 |
my $replace = undef; |
| 89 |
my $color = '#ffff00'; # yellow |
| 90 |
my $stylename = 'MyHighLight'; |
| 91 |
my %properties = (); |
| 92 |
|
| 93 |
GetOptions |
| 94 |
( |
| 95 |
'encoding=s' => \$encoding, |
| 96 |
'replacement=s' => \$replace, |
| 97 |
'color=s' => \$color, |
| 98 |
'stylename=s' => \$stylename, |
| 99 |
'property=s' => \%properties, |
| 100 |
'output|target=s' => \$target |
| 101 |
); |
| 102 |
|
| 103 |
$color = odfColor($color) or die "Color code is not valid\n"; |
| 104 |
|
| 105 |
#----------------------------------------------------------------------------- |
| 106 |
|
| 107 |
my $filename = $ARGV[0]; |
| 108 |
my $search = $ARGV[1]; |
| 109 |
die "usage: odfhighlight <file> <search_string> [-options]\n" |
| 110 |
unless ($filename && $search); |
| 111 |
|
| 112 |
#----------------------------------------------------------------------------- |
| 113 |
# opening the document |
| 114 |
|
| 115 |
my $doc = odfDocument |
| 116 |
( |
| 117 |
file => $filename, |
| 118 |
local_encoding => $encoding |
| 119 |
) |
| 120 |
or die "File unavailable or non-ODF file\n"; |
| 121 |
|
| 122 |
#----------------------------------------------------------------------------- |
| 123 |
# creating the highlight style |
| 124 |
|
| 125 |
my $attr = $doc->{'opendocument'} ? |
| 126 |
'fo:background-color' : 'style:text-background-color'; |
| 127 |
$properties{$attr} = $color; |
| 128 |
|
| 129 |
$doc->createStyle |
| 130 |
( |
| 131 |
$stylename, |
| 132 |
family => 'text', |
| 133 |
properties => { %properties } |
| 134 |
) |
| 135 |
or die "Please choose another style name.\n"; |
| 136 |
|
| 137 |
#----------------------------------------------------------------------------- |
| 138 |
# searching and replacing |
| 139 |
|
| 140 |
my @list = $doc->selectElementsByContent($search, $replace); |
| 141 |
|
| 142 |
#----------------------------------------------------------------------------- |
| 143 |
# coloring |
| 144 |
|
| 145 |
if (defined $replace) |
| 146 |
{ |
| 147 |
# just in order to avoid unneeded metacharacter processing |
| 148 |
$replace =~ s/([\\\(\)\.\*\?\[\]\|\-])/\\$1/g; |
| 149 |
$search = $replace; |
| 150 |
} |
| 151 |
|
| 152 |
foreach my $element (@list) |
| 153 |
{ |
| 154 |
$doc->setSpan($element, $search, $stylename); |
| 155 |
} |
| 156 |
|
| 157 |
#----------------------------------------------------------------------------- |
| 158 |
# saving the result |
| 159 |
|
| 160 |
$doc->save($target); |
| 161 |
exit; |
| 162 |
|
| 163 |
#----------------------------------------------------------------------------- |
| 164 |
|