Archive

Archive for the ‘HTML’ Category

Change HTML To Lowercase For Entire Site

January 5th, 2009
3 comments
This is a great script when you need to change HTML code from uppercase to lowercase for an entire site; or at least any given directory on a site. I wrote this one a while back when a friend was in charge of fixing a site that came over from a Windows server to a Linux server. Being that Windows is not case sensitive when it comes to file names, he was going through and manually changing the HTML links <a> to lowercase to match the actual file names. This script did the entire site in a matter of seconds.

WARNING: Please backup your site first or at least use a test copy to make sure there are no issues. Also, it only converts tagged code, that is anything between a < and > so that would include HTML and probably some PHP, ASP, etc, but not any Javascript. That being said, let’s go through the code.

Line 5: Specify the directory you want to recurse.
Line 8: This is where the function is called initially. As it works through the directories it will call itself again and again.
Line 11: Start the sub (function) definition.
Line 12: Get the directory name passed in.
Line 13: Open the directory or fail trying.
Line 14: Loop through all the items in the directory.
Line 15: If it’s not pointing to the current or upper directory, keep going.
Line 16: If it’s a directory then print out what it’s doing then call the function to recurse this directory.
Line 20: It’s a file, not a directory, get the file extension.
Line 21: Make sure it’s one of the files we want to modify - add to or remove from this list if you need to.
Line 22: Print out that it’s parsing the file.
Line 23: Open the file to read it.
Line 25-28: This is where the magic happens. It reads through the file line by line and finds anything inside the <> tags and lowercases it.
Line 30-31: Write the newly changed data back to the file.



That’s it. Please feel free to use this code and modify how you’d like. I’d love to see suggestions for improvement too.

Here is the actual code to copy/paste:
#!/usr/bin/perl
use strict;

# Specify the directory
my $dir = '/home/hallamigo/Desktop/html';

# Call the function
uptolow($dir);

# The function that does the work
sub uptolow {
  my $topdir = shift;
  opendir (DIRH, $topdir) or die "Could not open: $!";
  foreach my $item (readdir DIRH) {
    if ($item ne '.' && $item ne '..') {
      if (-d "$topdir/$item") {
        print "Recursing directory: $topdir/$item\n";
        uptolow("$topdir/$item");
      } else {
        my $ext = substr("$topdir/$item", (rindex("$topdir/$item", '.') + 1));
        if ($ext eq 'htm' || $ext eq 'html' || $ext eq 'php' || $ext eq 'asp') {
          print "Parsing File: $topdir/$item\n";
          open (FILH, "$topdir/$item") or die "Could not open $!";
          my @mod;
          while (my $line = ) {
            $line =~ s/(<.+?>)/\L$1/g;
            push(@mod, $line);
          }
          close FILH;
          open (MODH, ">$topdir/$item") or die "Could not open $!";
          print (MODH @mod);
          close (MODH);
        }
      }
    }
  }
  closedir DIRH;
}

HTML, Perl , ,