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
| #!/usr/bin/env perl
use strict;
use warnings;
use autodie;
open(my $fh, "<:utf8", 'input') or die "Failed to open file: $!\n";
open my $fh1, ">:utf8", 'output.xml';
my ($entry, $gloss, $exemple);
while (<$fh> ) {
chop;
if (/^\(([^)]+)\)([^{]*)(\{(.*)\})?$/) {
print_stuff($entry, $gloss, $exemple);
($entry, $gloss, $exemple) = ($1, $2, $4);
next;
}
if (/^([^{]+)(\{(.*)\})?$/) {
$gloss .= $1;
$exemple .= $3?$3:"";
next;
}
if (/^\{(.+)\}$/) {
$exemple .= $1;
next;
}
}
print_stuff($entry, $gloss, $exemple);
close $fh1;
close $fh;
sub print_stuff {
my ($entry, $gloss, $exemple) = (shift, shift, shift);
return unless($entry);
print $fh1 "<entry form=\"$entry\">\n";
print $fh1 "\t<defs>\n";
print $fh1 "\t\t<gloss>$gloss</gloss>\n";
print $fh1 "\t\t<exemple>$exemple</exemple>\n" if ($exemple);
print $fh1 "\t</defs>\n";
print $fh1 "</entry>\n\n\n";
} |
Partager