Files
fsfe-website/tools/preproc.pl
allegreg 3dc0b7a28d initial import
svn path=/trunk/; revision=1794
2001-12-10 18:55:17 +00:00

131 lines
4.3 KiB
Perl

#! /usr/bin/perl -w
# Guillaume Allègre <gallegre@april.org>, dec. 2001
# use File::Find;
my $InFmt="pxhtml";
my $OutFmt="xhtml";
my $FinalFmt="html";
my $InFile="", $OutFile="", $FinalFile="";
my $preproc="preproc";
my $usage_text="$preproc <file>.$InFmt \n Generate .$OutFmt file from .$InFmt, managing language selection.\n\n";
my %language=( de=>"Deutsch",
en=>"English",
es=>"Español",
fr=>"Français",
it=>"Italiano",
# nl=>"Netherlands",
pt=>"Português" );
# Main
my $langcodes = join(" ",sort(keys(%language)));
# ************** Checkings
if ($#ARGV < 0 ) {
die ($usage_text);
}
$FinalFile = $OutFile = $InFile = $ARGV[0];
if ( $InFile =~ /\w+\.(\w\w)\.$InFmt$/ ) # checks if filename respects foo.la.$InFmt
{ if (! exists ($language{$1}) )
{ die ("$1 : unknown language. Choose among <$langcodes>.\n" );}
}
else
{ die ("$InFile : incorrect filename format. Must be <smth>.<lang>.pxhtml"); }
my $lang=$1;
open (INFILE, $InFile) || die ("Impossible to read $InFile : $!\n");
@lines=<INFILE>;
@htmllines=grep(/<html lang=/, @lines); # checks if lang in filename = lang in <html> tag
if (@htmllines != 1) { die "No <html lang=...> declaration.\n";}
if ($htmllines[0] !~ /<html lang="(\w\w)"/ )
{ die "Inconsistent declaration : $htmllines[0].\n";}
$htmllang=$1;
if ( $htmllang ne $lang) {die ("<html> language $htmllang differs from filename language $lang.\n"); }
print ("$1 : $language{$1} : OK \n"); # all is correct ; we can process
# ***************** Parsing special tags
$OutFile =~ s/$InFmt/$OutFmt/; # post- preprocessor format (.xhtml ?)
$FinalFile =~ s/$InFmt/$FinalFmt/; # final format (html)
open (OUTFILE, ">$OutFile");
my $count=0;
foreach $line (@lines) # loop on the lines
{
$count++;
# **** process <plang:langselection> tag
if ($line =~ /(.*)<plang:langselection>(.*)/)
{ print OUTFILE "$1 \n";
print OUTFILE "<!-- Autogenerated by $preproc from $InFile -->\n";
print OUTFILE "\t<center>\n";
print OUTFILE "\t[\n";
$sep="";
foreach $otherlang (sort(keys(%language)))
{ $otherfile_h=$FinalFile; $otherfile_h =~ s/$lang/$otherlang/;
$otherfile_p=$InFile; $otherfile_p =~ s/$lang/$otherlang/;
if ($otherlang ne $lang && -f $otherfile_p)
{ $stag="<a href=\"$otherfile_h\">";
$etag="</a>";
}
else {$stag=""; $etag="";}
print OUTFILE "\t$sep$stag$language{$otherlang}$etag\n";
$sep="| ";
}
print OUTFILE "\t]\n";
print OUTFILE "\t</center>\n";
print OUTFILE "$2";
}
# **** process <plang:internallink href="" lnlang="" fblang="" name=""> tag
elsif ($line =~ /(.*)<plang:internallink(.*?)>(.*)/)
{ print OUTFILE "$1";
$arguments=$2; $remaining=$3;
# print "ARGUMENTS : $arguments\n";
if ( $arguments =~ /href=\"(.*?)\"/ ) # argument href="..." (mandatory)
{ $href=$1; }
else
{ die "argument href is mandatory in <plang:internallink> tag, $InFile, line $count\n";}
if ( $arguments =~ /name=\"(.*?)\"/ ) # argument name="..." (mandatory)
{ $name=$1; }
else
{ die "argument name is mandatory in <plang:internallink> tag, $InFile, line $count\n";}
if ( $arguments =~ /lnlang=\"(.*?)\"/ ) # argument lnlang="..."
{ $lnlang=$1; }
else
{ $lnlang=$lang;}
if ( $arguments =~ /fblang=\"(.*?)\"/ ) # argument fnlang="..."
{ $fblang=$1; }
else
{ $fblang="en";}
$otherfile_h=$href . "." . $lnlang . "." . $FinalFmt;
$otherfile_p=$href . "." . $lnlang . "." . $InFmt;
if (-f $otherfile_h || -f $otherfile_p) # the lnlang version exists
{ $stag="<a href=\"$otherfile_h\">";
$etag=" ($lnlang)</a>";
}
else
{ $otherfile_h=$href . "." . $fblang . "." . $FinalFmt;
$otherfile_p=$href . "." . $fblang . "." . $InFmt;
if (-f $otherfile_h || -f $otherfile_p) # the fallback lang version exists
{ $stag="<a href=\"$otherfile_h\">";
$etag=" ($fblang)</a>";
}
else
{ $stag=$etag="";}
}
print OUTFILE "$stag$name$etag <!-- preproc req=$lnlang fb=$fblang -->";
print OUTFILE "$remaining \n";
}
# **** normal line
else
{ print OUTFILE $line; }
}
close (OUTFILE);