diff options
author | Bas Zoetekouw <bas> | 2008-11-22 15:49:07 +0000 |
---|---|---|
committer | Bas Zoetekouw <bas> | 2008-11-22 15:49:07 +0000 |
commit | 13d21e67ab89920172cb47e2603db88caf445ba8 (patch) | |
tree | 52b5127acdb542f25cce3efdfb575df3d87d44fc /i18nwwwfix.pl | |
parent | a60ed078ffa5cbaf5d605234580ce2e650c9d9c8 (diff) |
Clean up i18nwwwfix.pl
CVS version numbers
i18nwwwfix.pl: 1.2 -> 1.3
Diffstat (limited to 'i18nwwwfix.pl')
-rwxr-xr-x | i18nwwwfix.pl | 224 |
1 files changed, 145 insertions, 79 deletions
diff --git a/i18nwwwfix.pl b/i18nwwwfix.pl index 73d58d1c935..39ce3602960 100755 --- a/i18nwwwfix.pl +++ b/i18nwwwfix.pl @@ -1,4 +1,4 @@ -#!/usr/bin/perl -w +#!/usr/bin/perl # This script enables the creation of copies of the www site # in only two languages. Default language (see $DEFAULT) @@ -20,7 +20,7 @@ # (for Spanish users) # 1.- wget -o debian.log -m -k http://www.es.debian.org # 2.- (go to the dir created by wget, in our case www.es.debian.org) -# 3.- perl intcopy.pl -p es +# 3.- perl intcopy.pl -p es # # NOTES: # 1.- Customize the URL and -p option to fix for your closest mirror and @@ -39,120 +39,186 @@ use Getopt::Std; use IO::File; +use Cwd; +use File::Copy; + +use strict; +use warnings; + +my %opts; +getopts('vp:d:', \%opts); + +my $POST = $opts{'p'}; +my $DEFAULT = "en"; #Default language is english (en) +my $INVALID_DIRS = '^\.|\.\.|CVS|\.svn|.git$'; + +my $current_dir = $opts{'d'} || getcwd; +my $verbose = $opts{'v'}; + +fixDirectory($current_dir); -getopts('vp:d:'); -$POST = $opt_p; -$DEFAULT = "en"; #Default language is english (en) -$INVALID_DIRS="^\\.|\\.\\.|CVS\$"; -if (!$opt_d) { - $current_dir=`pwd`; -} else { - $current_dir=$opt_d; -} -chomp $current_dir; -fixDirectory ($current_dir); exit 0; -sub fixDirectory + +sub fixDirectory { my ($directory) = @_; my $dir = new IO::File; - opendir ($dir, $directory) || die ("I cannot read $directory: $!"); - while ( $file = readdir ($dir) ) + opendir ($dir, $directory) || die ("I cannot read $directory: $!\n"); + while ( my $file = readdir ($dir) ) { - print STDERR "Checking $file\n" if $opt_v; - if ( -d "${directory}/${file}" and ! -l "${directory}/${file}" ) - { - if ( $file =~ /$INVALID_DIRS/ ) - { print STDERR "Not a valid dir: $file \n" if $opt_v; } - else - { fixDirectory ("${directory}/${file}"); } + next if $file eq '.' or $file eq '..'; + + warn "Checking $file\n" if $verbose; + + if ( -d "${directory}/${file}" and not -l "${directory}/${file}" ) + { + if ( $file =~ /$INVALID_DIRS/ ) + { + warn "Not a valid dir: $file \n" if $verbose; + } + else + { + fixDirectory ("${directory}/${file}"); + } } else - { fix_html_file (${directory},"${directory}/${file}") if $file =~ /.html?$/ ; } + { + fix_html_file (${directory},"${directory}/${file}") if $file =~ /.html?$/ ; + } } # del while } #de la subrutina + sub fix_html_file { -# This is a html file + # This is a html file my ($directory,$file) = @_; - print "Opening the file $file.\n" if $opt_v; - open (FICHERO, "<${file}") || die ("Cannot open ${file} : $!"); - open (NEWFICHERO, ">${file}.bak") || die ("I cannot create a backup of${file} : $!"); - while ( $line =<FICHERO>) { + + warn "Opening the file $file.\n" if $verbose; + + open (FICHERO, "<${file}") or die ("Cannot open ${file} : $!\n"); + open (NEWFICHERO, ">${file}.bak") or die ("I cannot create a backup of ${file} : $!\n"); + + while ( my $line =<FICHERO>) + { chomp $line; -# Here we must check: -# 1.- the href ends in .$post.html and $POST = $post and if not -# cancel the href (remove the tag) -# 2.- if the href does not end in $post.html and $POST.html exists -# make it point there -# 3.- if the href does not end in $post.html and $POST.html does not -# exist then link to .en.html (english version) - - my $newline =""; - my $endofline =""; + + # Here we must check: + # 1.- the href ends in .$post.html and $POST = $post and if not + # cancel the href (remove the tag) + # 2.- if the href does not end in $post.html and $POST.html exists + # make it point there + # 3.- if the href does not end in $post.html and $POST.html does not + # exist then link to .en.html (english version) + + my $newline = ""; + my $endofline = ""; + while ( $line =~ m/A HREF=\"(.*?)\"/gi ) - { + { my $old_ref = $1; my $new_ref = $old_ref; $newline = $newline.$`; $endofline = $'; - if ( islocalreference($old_ref) ) { - print "Checking reference $old_ref\n" if $opt_v; - if ( $old_ref =~ /\/$/ ) { - # This is a directory... check if the file exists - print "Fixing directory reference $old_ref\n" if $opt_v; - $new_ref = $old_ref."index.".$POST.".html" if ( -f "${directory}/${old_ref}/index.$POST.html" ); - $new_ref = $old_ref."index.".$DEFAULT.".html" if ( $new_ref eq $old_ref && -f "${directory}/${old_ref}/index.$DEFAULT.html" ); - $new_ref = $old_ref."index.html" if ( $new_ref eq $old_ref && -f "${directory}/${old_ref}/index.html" ); + + if ( islocalreference($old_ref) ) + { + warn "Checking reference $old_ref\n" if $verbose; + if ( $old_ref =~ /\/$/ ) + { + # This is a directory... check if the file exists + warn "Fixing directory reference $old_ref\n" if $verbose; + + if ( -f "${directory}/${old_ref}/index.$POST.html" ) + { + $new_ref = $old_ref."index.".$POST.".html"; + } + if ( $new_ref eq $old_ref + and -f "${directory}/${old_ref}/index.$DEFAULT.html" ) + { + $new_ref = $old_ref."index.".$DEFAULT.".html"; + } + if ( $new_ref eq $old_ref and -f "${directory}/${old_ref}/index.html" ) + { + $new_ref = $old_ref."index.html"; + } } - elsif ( $old_ref =~ /(.*?)\.(.*?)\.html$/ ) { - # This one uses does *not* use content negotiation... - print "Fixing HTML reference $old_ref\n" if $opt_v; - $base = $1; - $ending = $2; - $new_ref = $base.".".$POST.".html" if ( -f "${directory}/${base}.$POST.html" ); - $new_ref = $base.".".$DEFAULT.".html" if ( "$new_ref eq $old_ref && -f ${directory}/${base}.$DEFAULT.html" ); + elsif ( $old_ref =~ /(.*?)\.(.*?)\.html$/ ) + { + # This one uses does *not* use content negotiation... + warn "Fixing HTML reference $old_ref\n" if $verbose; + + my $base = $1; + my $ending = $2; + + if ( -f "${directory}/${base}.$POST.html" ) + { + $new_ref = $base.".".$POST.".html"; + } + if ( $new_ref eq $old_ref && -f "${directory}/${base}.$DEFAULT.html" ) + { + $new_ref = $base.".".$DEFAULT.".html"; + } } elsif ( $old_ref !~ /([\w-]+)\.([\w-]+)$/ ) { - print "Fixing Content Negotiation reference $old_ref\n" if $opt_v; - # This one uses *does* use content negotiation... - # Check as above but also move around files - $new_ref = $old_ref.".".$POST.".html" if ( -f "${directory}/${old_ref}.$POST.html" ); - $new_ref = $old_ref.".".$DEFAULT.".html" if ( "$new_ref eq $old_ref && -f ${directory}/${old_ref}.$DEFAULT.html" ); - $new_ref = $old_ref.".html" if ( "$new_ref eq $old_ref && -f ${directory}/${old_ref}.html" ); - if ( "$new_ref eq $old_ref && -f ${directory}/${old_ref}" ) { - $new_ref = $old_ref.".html"; -# `mv ${directory}/${old_ref} ${directory}/${old_ref}.html`; - - } + warn "Fixing Content Negotiation reference $old_ref\n" if $verbose; + + # This one uses *does* use content negotiation... + # Check as above but also move around files + if ( -f "${directory}/${old_ref}.$POST.html" ) + { + $new_ref = $old_ref.".".$POST.".html"; + } + if ( "$new_ref eq $old_ref && -f ${directory}/${old_ref}.$DEFAULT.html" ) + { + $new_ref = $old_ref.".".$DEFAULT.".html"; + } + if ( "$new_ref eq $old_ref && -f ${directory}/${old_ref}.html" ) + { + $new_ref = $old_ref.".html" + } + if ( "$new_ref eq $old_ref && -f ${directory}/${old_ref}" ) + { + $new_ref = $old_ref.".html"; + } } - } -# After checking if $old_ref =/= $new_ref then substitute - $newline .= "A HREF=\"".$new_ref."\""; - print "Fixed reference $old_ref to $new_ref\n" if ( $opt_v && $new_ref ne $old_ref) ; + + # After checking if $old_ref =/= $new_ref then substitute + $newline .= qq{A HREF="$new_ref"}; + if ( $verbose and $new_ref ne $old_ref) + { + warn "Fixed reference $old_ref to $new_ref\n"; + } } - $newline .= $endofline; + $newline .= $endofline; $newline = $line if $newline eq ""; - print "Changing $line to $newline\n" if $opt_v; + + warn "Changing $line to $newline\n" if $verbose; print NEWFICHERO $newline; print NEWFICHERO "\n"; } close FICHERO; close NEWFICHERO; + unlink $file; - `mv $file.bak $file`; + move("$file.bak", $file) + or die("Couldn't move `$file.bak' to `$file': $!\n"); } -sub islocalreference { -# Cheks if a reference points to a local resource, +# Checks if a reference points to a local resource, # i.e. it is not in (http|ftp|gopher):// form +sub islocalreference +{ my ($reference) = @_; - if ($reference !~ /:\/\// ) { - print "Local reference: $reference\n" if $opt_v; + if ($reference !~ /:\/\// ) + { + warn "Local reference: $reference\n" if $verbose; return 1; - } else { - return 0 ; } + } + return; } + +__END__ + |