in reply to Copy html tag and replace umlauts with alternate spellings
Here's a minimal solution using HTML::Parser. It would be worthwhile and instructive to use Unicode::Normalize as well, but if we're just twiddling umlauts, this is good enough. (Still, you'll want to check the output carefully...):
That's set up to work as a "stdin - stdout filter" -- in other words, it's strictly a command line process, and the usage is supposed to be: script_name < input.html > output.html#!/usr/bin/perl use strict; use HTML::Parser; # set up a hash containing the umlauted characters and their replaceme +nts: my %replace = ( "\xC4" => 'Ae', "\xCF" => 'Ie', "\xD6" => 'Oe', "\xDC" => 'Ue', "\xE4" => 'ae', "\xEF" => 'ie', "\xF6" => 'oe', "\xFC" => 'ue', ); my $um = join '', keys %replace; binmode STDIN, ':utf8'; binmode STDOUT, ':utf8'; $/ = undef; my $input = <>; my $output = ''; my $p = HTML::Parser->new( api_version => 3, start_h => [ \&fix_umlaut, 'tagname, attr, +text' ], default_h => [ \©, 'text' ], ); $p->empty_element_tags( 1 ); $p->parse( $input ); print $output; sub fix_umlaut { my ( $tagname, $attr, $text ) = @_; $output .= $text; if ( $tagname eq 'idx:orth' and $$attr{value} =~ /[$um]/ ) { $text =~ s/([$um])/$replace{$1}/g; $output .= $text; # repeat the tag with modified umlauts } } sub copy { $output .= $_[0]; }
The HTML::Parser man page is well worth studying.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Copy html tag and replace umlauts with alternate spellings
by Anonymous Monk on Mar 27, 2011 at 20:30 UTC | |
by graff (Chancellor) on Mar 30, 2011 at 21:11 UTC | |
by Anonymous Monk on Mar 27, 2011 at 20:36 UTC |