/usr/bin/xml_spellcheck is in xml-twig-tools 1:3.50-1.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 | #!/usr/bin/perl -w
use strict;
use XML::Twig;
use Getopt::Long;
use Pod::Usage;
use File::Temp qw{tempfile};
my $DEFAULT_SC = 'aspell -c';
my $DEFAULT_PP = 'indented';
my $DEFAULT_EXT= '.bak';
my $VERSION="0.02";
my ( $spellchecker, $ext, $attributes, $exclude_elements,
$include_elements, $pretty_print, $version, $help, $man);
GetOptions( 'spellchecker=s' => \$spellchecker,
'backup-extension=s' => \$ext,
'attributes' => \$attributes,
'exclude_elements=s' => \$exclude_elements,
'include_elements=s' => \$include_elements,
'pretty_print:s' => \$pretty_print,
'version' => \$version,
'help' => \$help,
'man' => \$man,
) or pod2usage(-verbose => 1, -exitval => -1);
pod2usage( -verbose => 1, -exitval => 0) if $help;
pod2usage( -verbose => 2, -exitval => 0) if $man;
if( $version) { print "$0 version $VERSION\n"; exit;}
# option processing
$spellchecker ||= $DEFAULT_SC;
$ext ||= $DEFAULT_EXT;
if( $exclude_elements && $include_elements)
{ die "cannot use both --exclude-elements and --include-elements\n"; }
if( defined $pretty_print and !$pretty_print)
{ $pretty_print= $DEFAULT_PP; }
my %twig_options;
my( %include_elements);
if( $exclude_elements)
{ my @exclude_elts = split /\s+/, $exclude_elements;
my %start_tag_handlers= map { $_ => \&exclude_elt } @exclude_elts;
$twig_options{start_tag_handlers}= \%start_tag_handlers;
}
if( $include_elements)
{ my @include_elts = split /\s+/, $include_elements;
my %start_tag_handlers= map { $_ => \&include_elt } @include_elts;
$twig_options{start_tag_handlers}= \%start_tag_handlers;
}
$twig_options{pretty_print}= $pretty_print if( $pretty_print);
foreach my $file (@ARGV)
{
my $id=0;
my $id2elt={}; # id => element
my( $tmp_fh, $tmp_file) = tempfile( "xml_spellcheck_XXXX",
SUFFIX => '.txt'
);
my $t= XML::Twig->new( keep_encoding =>1, %twig_options,);
$t->parsefile( $file);
foreach my $elt ($t->descendants( '#TEXT'))
{
if( (!$include_elements and !$exclude_elements)
or ($include_elements and $elt->inherit_att( '#include'))
or ($exclude_elements and !$elt->inherit_att( '#exclude'))
)
{ $id++;
process_text( $t, $elt, $id, $id2elt, $tmp_fh)
}
}
close $tmp_fh;
system( "$spellchecker $tmp_file") ==0
or die "$spellchecker $tmp_file failed: $?";
open( $tmp_fh, "<$tmp_file") or die "cannot open temp file $tmp_file: $!";
while( <$tmp_fh>)
{ chomp;
my( $id, $text)= split /:/, $_, 2;
my $wrap= $id2elt->{$id};
$text=~ s{<\\n>}{\n}g;
my $text_elt= $wrap->first_child or die "internal error 100\n";
if( $text_elt->gi eq '#PCDATA')
{ $text_elt->set_pcdata( $text); }
elsif( $text_elt->gi eq '#CDATA')
{ $text_elt->set_cdata( $text); }
else
{ die "internal error 101\n"; }
$wrap->erase;
}
close $tmp_fh;
rename( $file, "$file$ext") or die "cannot save backup file $file$ext: $!";
open( FILE, ">$file") or die "cannot save spell checked file $file: $!";
$t->print( \*FILE);
close FILE;
}
sub include_elt
{ $_->set_att( '#include' => 1) ; }
sub exclude_elt
{ $_->set_att( '#exclude' => 1) ; }
sub process_text
{ my( $t, $elt, $id, $id2elt, $tmp_fh)= @_;
my $wrap= $elt->wrap_in( '#SC');
#$wrap->set_att( '#ID' => $id);
$id2elt->{$id}= $wrap;
my $text= $elt->text;
$text=~ s{\n}{<\\n>}g;
print $tmp_fh "$id:$text\n";
}
__END__
=head1 NAME
xml_spellcheck - spellcheck XML files
=head1 SYNOPSIS
xml_spellcheck [options] <files>
=head1 DESCRIPTION
xml_spellcheck lets you spell check the content of an XML file.
It extracts the text (the content of elements and optionally of
attributes), call a spell checker on it and then recreates the
XML document.
=head1 OPTIONS
Note that all options can be abbreviated to the first letter
=over 4
=item --conf <configuration_file>
Gets the options from a configuration file. NOT IMPLEMENTED YET.
=item --spellchecker <spellchecker>
The command to use for spell checking, including any option
By default C<aspell -c> is used
=item --backup-extension <extension>
By default the original file is saved with a C<.bak> extension. This option
changes the extension
=item --attributes
Spell check attribute content. By default attribute values are NOT
spell checked. NOT YET IMPLEMENTED
=item --exclude_elements <list_of_excluded_elements>
A list of elements that should not be spell checked
=item --include_elements <list_of_included_elements>
A list of elements that should be spell checked (by default all elements
are spell checked).
C<--exclude_elements> and C<--include_elements> are mutually exclusive
=item --pretty_print <optional_pretty_print_style>
A pretty print style for the document, as defined in XML::Twig. If
the option is provided without a value then the C<indented> style is
used
=item --version
Dislay the tool version and exit
=item --help
Display help message and exit
=item --man
Display longer help message and exit
=back
=head1 EXAMPLES
=head1 BUGS
=head1 TODO
=over 4
=item --conf option
=item --attribute option
=back
=head1 PRE-REQUISITE
XML::Twig, Getopt::Long, Pod::Usage, File::Temp
XML::Twig requires XML::Parser.
=head1 SEE ALSO
XML::Twig
=head1 COPYRIGHT AND DISCLAIMER
This program is Copyright 2003 by Michel Rodriguez
This program is free software; you can redistribute it and/or modify
it under the terms of the Perl Artistic License or the GNU General
Public License as published by the Free Software Foundation either
version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MER-
CHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
Public License for more details.
If you do not have a copy of the GNU General Public License write to
the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
USA.
=head1 AUTHOR
Michel Rodriguez <mirod@xmltwig.com>
xml_spellcheck is available at http://www.xmltwig.com/xmltwig/
|