| 1 | #!/usr/bin/perl | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Cwd; | ||
| 7 | use IO::Dir; | ||
| 8 | use IO::File; | ||
| 9 | use Getopt::Std; | ||
| 10 | use File::Spec::Functions qw(catfile no_upwards rel2abs splitdir); | ||
| 11 | |||
| 12 | #---------------------------------------------------------------------- | ||
| 13 | # Main | ||
| 14 | |||
| 15 | my %opt; | ||
| 16 | getopts('r', \%opt); | ||
| 17 | my $folder = shift(@ARGV) or getcwd(); | ||
| 18 | die ("$folder is not a directory") unless -e $folder && -d $folder; | ||
| 19 | |||
| 20 | if ($opt{r}) { | ||
| 21 | revert_files($folder); | ||
| 22 | } else { | ||
| 23 | alter_files($folder); | ||
| 24 | } | ||
| 25 | |||
| 26 | #---------------------------------------------------------------------- | ||
| 27 | # Alter the files in a folder | ||
| 28 | |||
| 29 | sub alter_files { | ||
| 30 | my ($folder) = @_; | ||
| 31 | my @files = find_files($folder); | ||
| 32 | |||
| 33 | foreach my $file (@files) { | ||
| 34 | my $text = slurp_file($file); | ||
| 35 | backup_file($file, $text); | ||
| 36 | |||
| 37 | $text = alter_text($text); | ||
| 38 | write_file($file, $text); | ||
| 39 | } | ||
| 40 | |||
| 41 | return; | ||
| 42 | } | ||
| 43 | |||
| 44 | #---------------------------------------------------------------------- | ||
| 45 | # Alter the file names in a file | ||
| 46 | |||
| 47 | sub alter_text { | ||
| 48 | my ($text) = @_; | ||
| 49 | |||
| 50 | $text =~ s/src="([^"]*)"/'src="' . alter_url($1) . '"'/ge; | ||
| 51 | $text =~ s/href="([^"]*)"/'href="' . alter_url($1) . '"'/ge; | ||
| 52 | $text =~ s/url\('([^']*)'\)/'url(\'' . alter_url($1) . '\')'/ge; | ||
| 53 | |||
| 54 | return $text; | ||
| 55 | } | ||
| 56 | |||
| 57 | #---------------------------------------------------------------------- | ||
| 58 | # Modify a url to point at the file's new location | ||
| 59 | |||
| 60 | sub alter_url { | ||
| 61 | my ($url) = @_; | ||
| 62 | |||
| 63 | if ($url !~ /:/) { | ||
| 64 | my @path = split(/\//, $url); | ||
| 65 | $url = pop(@path); | ||
| 66 | } | ||
| 67 | |||
| 68 | return $url; | ||
| 69 | } | ||
| 70 | |||
| 71 | #---------------------------------------------------------------------- | ||
| 72 | # Create a backup copy of the original file | ||
| 73 | |||
| 74 | sub backup_file { | ||
| 75 | my ($file, $text) = @_; | ||
| 76 | $file .= '~'; | ||
| 77 | |||
| 78 | write_file($file, $text); | ||
| 79 | } | ||
| 80 | |||
| 81 | #---------------------------------------------------------------------- | ||
| 82 | # Find files to modify | ||
| 83 | |||
| 84 | sub find_files { | ||
| 85 | my ($folder) = @_; | ||
| 86 | |||
| 87 | my @files; | ||
| 88 | my $dd = IO::Dir->new($folder); | ||
| 89 | while (defined (my $file = $dd->read())) { | ||
| 90 | next unless no_upwards($file); | ||
| 91 | my $file = catfile($folder, $file); | ||
| 92 | |||
| 93 | if (-d $file) { | ||
| 94 | push(@files, find_files($file)); | ||
| 95 | } else { | ||
| 96 | push(@files, $file) if text_file($file); | ||
| 97 | } | ||
| 98 | } | ||
| 99 | |||
| 100 | return @files; | ||
| 101 | } | ||
| 102 | |||
| 103 | #---------------------------------------------------------------------- | ||
| 104 | # Alter the files in a folder | ||
| 105 | |||
| 106 | sub revert_files { | ||
| 107 | my ($folder) = @_; | ||
| 108 | my @files = find_files($folder); | ||
| 109 | |||
| 110 | foreach my $file (@files) { | ||
| 111 | my $old_file = "$file~"; | ||
| 112 | rename($old_file, $file) if -e $old_file; | ||
| 113 | } | ||
| 114 | |||
| 115 | return; | ||
| 116 | } | ||
| 117 | |||
| 118 | #---------------------------------------------------------------------- | ||
| 119 | # Read the file into a single string | ||
| 120 | |||
| 121 | sub slurp_file { | ||
| 122 | my ($path) = @_; | ||
| 123 | |||
| 124 | local $/; | ||
| 125 | my $fd = IO::File->new($path, 'r'); | ||
| 126 | die "Couldn't read file ($path): $!\n" unless $fd; | ||
| 127 | |||
| 128 | my $text = <$fd>; | ||
| 129 | return $text; | ||
| 130 | } | ||
| 131 | |||
| 132 | #---------------------------------------------------------------------- | ||
| 133 | # Test if a file is a text file | ||
| 134 | |||
| 135 | sub text_file { | ||
| 136 | my ($file) = @_; | ||
| 137 | |||
| 138 | for my $ext (qw(html css)) { | ||
| 139 | return 1 if $file =~ /\.$ext$/; | ||
| 140 | } | ||
| 141 | |||
| 142 | return; | ||
| 143 | } | ||
| 144 | |||
| 145 | #---------------------------------------------------------------------- | ||
| 146 | # Read the file into a single string | ||
| 147 | |||
| 148 | sub write_file { | ||
| 149 | my ($file, $text) = @_; | ||
| 150 | |||
| 151 | my $fd = IO::File->new($file, 'w'); | ||
| 152 | die "Couldn't write file ($file): $!\n" unless $fd; | ||
| 153 | |||
| 154 | print $fd $text; | ||
| 155 | close($fd); | ||
| 156 | } | ||
| 157 | |||
| 158 | =encoding utf-8 | ||
| 159 | |||
| 160 | =head1 NAME | ||
| 161 | |||
| 162 | flatten.pl - Modify web files so they are in a single flat directory | ||
| 163 | |||
| 164 | =head1 SYNOPSIS | ||
| 165 | |||
| 166 | perl flatten.pl directory | ||
| 167 | |||
| 168 | =head1 DESCRIPTION | ||
| 169 | |||
| 170 | This script modifies the contents of a directory containing web files so that the | ||
| 171 | relative urls they contain point to files in the same directory.It's assumed | ||
| 172 | that the files have already been copied into a single directory before this | ||
| 173 | script is run. | ||
| 174 | |||
| 175 | =head1 FLAGS | ||
| 176 | |||
| 177 | This script supports a single command line flag: | ||
| 178 | |||
| 179 | =over 4 | ||
| 180 | |||
| 181 | =item -r | ||
| 182 | |||
| 183 | Before modifying the file the script creates a backup copy whose name is | ||
| 184 | the same with an appended ~ character. If the -r flag is on the command | ||
| 185 | line, instead of modifying the files, it restores the original file by | ||
| 186 | copying the old version over the modified version. This script for | ||
| 187 | developers of this code and not for end users. | ||
| 188 | |||
| 189 | =back | ||
| 190 | |||
| 191 | =head1 LICENSE | ||
| 192 | |||
| 193 | Copyright (C) Bernie Simon. | ||
| 194 | |||
| 195 | This library is free software; you can redistribute it and/or modify | ||
| 196 | it under the same terms as Perl itself. | ||
| 197 | |||
| 198 | =head1 AUTHOR | ||
| 199 | |||
| 200 | Bernie Simon E<lt>bernie.simon@gmail.comE<gt> | ||
| 201 | |||
| 202 | =cut |