Created
July 25, 2011 17:29
-
-
Save dbb/1104650 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- add_pics_to_html_11.pl.orig 2011-07-25 13:12:25.000000000 -0400 | |
+++ add_pics_to_html_11.pl 2011-07-25 13:26:43.000000000 -0400 | |
@@ -4,6 +4,11 @@ | |
# lots of help by telemachus and dbbolton too | |
# license: GPL-3 | |
+## all my comments start with a double hash | |
+## -dbb | |
+ | |
+## general tips: | |
+## 1. use perltidy | |
#--------------------------------------------------# | |
# USE | |
@@ -12,48 +17,50 @@ | |
use autodie; | |
use File::Copy; | |
use File::Path qw(make_path remove_tree); | |
-#--------------------------------------------------# | |
- | |
+#--------------------------------------------------# | |
#--------------------------------------------------# | |
# VARIABLES - Change localhost to a real host name or IP number for public access. | |
-my $work_dir = "$ENV{HOME}/public_html/gallery"; | |
-my $pictures_dir = "pictures"; | |
+## if domain is going to vary, why not make a variable? | |
+my $domain = "localhost"; | |
+my $work_dir = "$ENV{HOME}/public_html/gallery"; | |
+my $pictures_dir = "pictures"; | |
my $thumbnails_dir = "thumbnails"; | |
-my $gallery_html = "gallery.html"; | |
+my $gallery_html = "gallery.html"; | |
+ | |
# if /var/www/username exists as symlink to /home/username/public_html: | |
#my $url = "http://localhost/$ENV{USER}/gallery"; | |
# if mod userdir is enabled: | |
-my $url = "http://localhost/\~$ENV{USER}/gallery"; | |
+## use the new domain var | |
+my $url = "http://${domain}/\~$ENV{USER}/gallery"; | |
-chdir ("$work_dir/$pictures_dir"); | |
-my @picture_folders = glob("*"); | |
-chdir ("$work_dir/$thumbnails_dir"); | |
-my @thumbnail_folders = glob("*"); | |
-chdir ("$work_dir"); | |
+## all the chdir calls are unnecessary | |
+my @picture_folders = glob "$work_dir/$pictures_dir/*"; | |
+my @thumbnail_folders = glob "$work_dir/$thumbnails_dir/*"; | |
+chdir("$work_dir"); | |
+## after this, you put $chdir in front of every path, so there's no use | |
+## in using chdir in the first place. | |
#--------------------------------------------------# | |
- | |
- | |
#--------------------------------------------------# | |
# First of all: make a BACKUP | |
if ( -f $gallery_html ) { | |
- copy("$work_dir/$gallery_html","$work_dir/$gallery_html.orig"); | |
+## you're already in $work_dir, so why bother typing it in the path? | |
+ copy( "$work_dir/$gallery_html", "$work_dir/$gallery_html.orig" ); | |
} | |
-#--------------------------------------------------# | |
- | |
- | |
-# OPEN for WRITING | |
-open(my $out, ">", "$work_dir/$gallery_html"); | |
#--------------------------------------------------# | |
+# OPEN for WRITING | |
+open( my $out, ">", "$work_dir/$gallery_html" ); | |
+#--------------------------------------------------# | |
#--------------------------------------------------# | |
# WRITE HEADER | |
+## why are you putting a newline at the beginning of the line? confusing. | |
my $header = << "EOF"; | |
<!doctype html> | |
\n<html> | |
@@ -68,13 +75,12 @@ | |
<p> Just some shots. </p></br> | |
EOF | |
- | |
+## you're using \n as well as blank lines. go with one or the other. | |
# Print the header to file. | |
print $out $header; | |
-#--------------------------------------------------# | |
- | |
+#--------------------------------------------------# | |
#--------------------------------------------------# | |
@@ -82,73 +88,80 @@ | |
# Do the Voodoo: | |
# for each folder in gallery/{thumbnails,pictures} | |
# make a heading, and add the thumbnails/pictures to our gallery | |
-foreach my $folder (@picture_folders) { | |
- chdir ("$work_dir/$pictures_dir/$folder"); | |
- my @pictures = glob("*"); | |
- | |
- say $out "\n</br></br>\n<h2>$folder</h2>"; | |
- | |
- # Create the folder for the thumbnails if it doesn't exist | |
- unless ( -d "$work_dir/$thumbnails_dir/$folder" ) { | |
-# say "Creating directory: $work_dir/$thumbnails_dir/$folder"; | |
-# system "mkdir -p $work_dir/$thumbnails_dir/$folder"; | |
- make_path("$work_dir/$thumbnails_dir/$folder", { | |
- verbose => 1, | |
- }); | |
- | |
- } | |
- | |
- foreach my $pic (@pictures) { | |
- # create thumbnail if it doesn't already exist | |
- if ( -f "$work_dir/$thumbnails_dir/$folder/$pic" ) { | |
- my $line = "<a href=\"$url/$pictures_dir/$folder/$pic\"><img src=\"$url/$thumbnails_dir/$folder/$pic\" alt=\"Desciption\"></a>"; | |
- say $out $line; | |
- } | |
- else { | |
- say "Adding thumbnail for $folder/$pic"; | |
- system "convert -resize 145x145 $work_dir/$pictures_dir/$folder/$pic $work_dir/$thumbnails_dir/$folder/$pic" ; | |
- my $line = "<a href=\"$url/$pictures_dir/$folder/$pic\"><img src=\"$url/$thumbnails_dir/$folder/$pic\" alt=\"Desciption\"></a>"; | |
- say $out $line; | |
- } | |
- } | |
+for my $folder (@picture_folders) { | |
+## again, you don't need to chdir in order to glob. | |
+## if you don't believe me, try: | |
+## perl -le '@a=glob"/etc/*"; print for @a;' | |
+ my @pictures = glob("$work_dir/$pictures_dir/$folder/*"); | |
+ | |
+ say $out "\n</br></br>\n<h2>$folder</h2>"; | |
+ | |
+ # Create the folder for the thumbnails if it doesn't exist | |
+ unless ( -d "$work_dir/$thumbnails_dir/$folder" ) { | |
+ | |
+ # say "Creating directory: $work_dir/$thumbnails_dir/$folder"; | |
+ # system "mkdir -p $work_dir/$thumbnails_dir/$folder"; | |
+ make_path( "$work_dir/$thumbnails_dir/$folder", { verbose => 1, } ); | |
+ | |
+ } | |
+ | |
+ for my $pic (@pictures) { | |
+ | |
+ # create thumbnail if it doesn't already exist | |
+ if ( -f "$work_dir/$thumbnails_dir/$folder/$pic" ) { | |
+## use the catenate operator . to make long lines reasable: | |
+ my $line = | |
+ "<a href=\"$url/$pictures_dir/$folder/$pic\">" | |
+ . "<img src=\"$url/$thumbnails_dir/$folder/$pic\"" | |
+ . " alt=\"Desciption\"></a>"; | |
+ say $out $line; | |
+ } | |
+ else { | |
+ say "Adding thumbnail for $folder/$pic"; | |
+ system "convert -resize 145x145 $work_dir/$pictures_dir/" | |
+ . "$folder/$pic $work_dir/$thumbnails_dir/$folder/$pic"; | |
+ my $line = | |
+ "<a href=\"$url/$pictures_dir/$folder/$pic\">" | |
+ . "<img src=\"$url/$thumbnails_dir/$folder/$pic\"" | |
+ . " alt=\"Desciption\"></a>"; | |
+ say $out $line; | |
+ } | |
+ } | |
} | |
# Clean up orphaned thumbnail folders and files | |
-foreach my $folder (@thumbnail_folders) { | |
- chdir ("$work_dir/$thumbnails_dir/$folder"); | |
- my @thumbnails = glob("*"); | |
- | |
-# Remove orphaned thumbnail folders | |
-# If there aren't any orphaned folders, then go through and remove | |
-# orphaned thumbnail files. | |
- unless ( -d "$work_dir/$pictures_dir/$folder" ) { | |
-# say "Removing directory: $$work_dir/thumbnails_dir/$folder"; | |
-# system "rm -r $work_dir/$thumbnails_dir/$folder"; | |
- chdir ("$work_dir"); | |
- remove_tree("$work_dir/$thumbnails_dir/$folder", { | |
- verbose => 1, | |
- }); | |
- } | |
- else { | |
- # Remove orphaned thumbnail files | |
- foreach my $pic (@thumbnails) { | |
- unless ( -f "$work_dir/$pictures_dir/$folder/$pic" ) { | |
-# say "Removing thumbnail for $folder/$pic"; | |
-# system (`rm "$work_dir/$thumbnails_dir/$folder/$pic"`); | |
-# # remove_tree works for files, too. | |
- remove_tree("$work_dir/$thumbnails_dir/$folder/$pic", { | |
- verbose => 1, | |
- }); | |
- } | |
- } | |
- } | |
+for my $folder (@thumbnail_folders) { | |
+## unnecessary chdir again | |
+ my @thumbnails = glob("$work_dir/$thumbnails_dir/$folder/*"); | |
+ | |
+ # Remove orphaned thumbnail folders | |
+ # If there aren't any orphaned folders, then go through and remove | |
+ # orphaned thumbnail files. | |
+ unless ( -d "$work_dir/$pictures_dir/$folder" ) { | |
+ | |
+ # say "Removing directory: $$work_dir/thumbnails_dir/$folder"; | |
+ # system "rm -r $work_dir/$thumbnails_dir/$folder"; | |
+ chdir("$work_dir"); | |
+ remove_tree( "$work_dir/$thumbnails_dir/$folder", { verbose => 1, } ); | |
+ } | |
+ else { | |
+ | |
+ # Remove orphaned thumbnail files | |
+ for my $pic (@thumbnails) { | |
+ unless ( -f "$work_dir/$pictures_dir/$folder/$pic" ) { | |
+ | |
+ # say "Removing thumbnail for $folder/$pic"; | |
+ # system (`rm "$work_dir/$thumbnails_dir/$folder/$pic"`); | |
+ # # remove_tree works for files, too. | |
+ remove_tree( "$work_dir/$thumbnails_dir/$folder/$pic", | |
+ { verbose => 1, } ); | |
+ } | |
+ } | |
+ } | |
} | |
#--------------------------------------------------# | |
- | |
- | |
- | |
#--------------------------------------------------# | |
# FOOTER | |
my $footer = << "EOF"; | |
@@ -161,11 +174,11 @@ | |
# PRINT the FOOTER | |
print $out $footer; | |
-#--------------------------------------------------# | |
- | |
+#--------------------------------------------------# | |
#--------------------------------------------------# | |
# DONE | |
close $out; | |
+ | |
#--------------------------------------------------# |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment