Created
September 7, 2012 09:39
-
-
Save tabletick/3664676 to your computer and use it in GitHub Desktop.
NextGen Pictures to Octopress (Fancybox/Photo-tag plugin)
This file contains 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
#!/usr/bin/perl | |
# | |
# | |
# Script converts the Wordpress NextGen images syntax in posts to | |
# Octopress markdown, Fancybox and Photo-Tag plugin syntax. | |
# | |
# Requires Text::CSV | |
# Image::Size | |
# ------------------------------------------------------------------------ | |
use Getopt::Long; # Handling parameters | |
Getopt::Long::Configure ('bundling'); | |
use Image::Size; # use Image::Magick; | |
use Text::CSV; # Handling CSV database exports | |
use File::Basename; # Filename handling | |
use POSIX; | |
use warnings; | |
# Local hash for imagedata | |
our ($imagereplacetag, %allimagedata); | |
print "\n"; | |
sub usage { # Routine prints the Syntax | |
print "Syntax: | |
script: -i|inputfile <input.markdown> | |
[-f|format o|f|t] | |
[-e|fancyboxeffect a|b|c|d] | |
-s|idsource <wp_ngg_pictures.csv> | |
-g|gallery <wp_ngg_gallery.csv> | |
[-w|fbwidth <thumbnailwidth>] | |
[-h|fbheight <thumbnailheight>] | |
[-c|content /path/to/wp-content] | |
[-t|title on|off] [-a|alttext on|off] [-o|outputfolder /path/to/output/folder] | |
[-h|help] | |
"; | |
} | |
sub help { # Subroutine prints help text | |
print "Help: | |
-i|inputfile Defines the markdown inputfile containing the old NGG syntax that needs to be converted. | |
-f|format Defines the output format: Either for Octopress (default), f for Fancybox or t for Tritargets Fancybox Plugin. | |
-e|fbeffect Specifies the Fancybox effect for the images, Values are a,b,c and d. Neccessary option when | |
parameter -f has been set to the value 'f' | |
-w|fbwidht Specifies the maximum width for the thumbnail pics for Fancybox, default is 250px. | |
-h|fbheight Specifies the maximum height for the thumbnail pics for Fancybox, default is 250px. | |
-s|idsource Defines the inputfile exported from NGG gallery, usally \"wp_ngg_pictures.csv\". | |
-g|gallery Defines the inputfile from the NGG gallery, usally \"wp_ngg_gallery.csv\" | |
-c|content* Defines the root path to the image folder for determining additional parameters like side-ratio, etc. | |
e.g.: ~/bilder when 'bilder' contains 'wp-content/' | |
-t|title* Disable/Enable to set the image title for Octopress. Default: on | |
-a|alttext* Disable/Enable to activate the alt-text for the Octopress images. Default: on. | |
-o|outputfolder* Folder for output of the changed files, Default: Screen. Folder must exist. | |
-h|help* Print out this help. | |
* Optional parameter | |
"; | |
} | |
# Sub returns Image dimensions | |
sub getImageFileData { | |
my $pathImageFile = $_[0]; | |
my $imageattribute = $_[1]; | |
($globe_x, $globe_y) = imgsize($pathImageFile); # Get the size of the Image | |
if ($imageattribute eq 'height') { | |
return $globe_y; | |
} elsif ($imageattribute eq 'width' ) { | |
return $globe_x; | |
} else { | |
print "Error, wrong parameter $imageattribute in sub 'getImageFileData'."; | |
exit 1; | |
} | |
} | |
# Routine searches the idsource files for the image id and returns | |
# filepath, galleryid, imagefilename, image titel, image date | |
# This will be used for building the Tag for Octopress later. | |
sub getImageData { | |
my $imageid = $_[0]; | |
my %getallimagedata; # Local Hash variable | |
my $csv = Text::CSV->new({sep_char => ';'}); # New Object with ; as delimiter | |
# Set Filehandle position back to the beginning - just in case (otherwise a second run would fail!) | |
seek INPUTIMAGECSV,0,0; | |
while (my $inputfile = <INPUTIMAGECSV>) { # Get the Filename and GalleryID (for the path) from the ID file | |
if ($csv->parse($inputfile)) { | |
my @columns = $csv->fields(); | |
if ( $columns[0] eq $imageid ) { # add data to hash getallimagedata | |
$getallimagedata{'imagename'} = $columns[4]; | |
$getallimagedata{'imageid'} = $columns[0]; | |
$getallimagedata{'imagegalleryid'} = $columns[3]; | |
$getallimagedata{'imagetiteltext'} = $columns[5]; | |
$getallimagedata{'imagealttext'} = $columns[7]; | |
} | |
} else { | |
my $err = $csv->error_input; | |
my $err_diag = $csv->error_diag; | |
die "Image datafile error.\n Failed to parse line: $err\n Make sure you're using utf8-encoded csv file from database export.\n$err_diag"; | |
} | |
} | |
# Set Filehandle position back to the beginning - just in case | |
seek INPUTGALLERYCSV,0,0; | |
while (my $inputfile = <INPUTGALLERYCSV>) { # Get the path from the gallery file | |
if ($csv->parse($inputfile)) { | |
my @columns = $csv->fields(); | |
if ( $columns[0] eq $getallimagedata{'imagegalleryid'} ){ | |
$getallimagedata{'imagepath'} = $columns[3]; | |
} | |
} else { | |
my $err = $csv->error_input; | |
die "Gallery datafile error.\n Failed to parse line: $err\n Make sure you're using utf8-encoded csv file from database export."; | |
} | |
} | |
return %getallimagedata; | |
} | |
# Readin all parameters. | |
# If one is unknown, print out and exit. <--- missing | |
$result = GetOptions ( | |
'i|inputfile=s' => \$inputfile, | |
's|sourceid=s' => \$idsource, | |
'g|gallery=s' => \$inputgallery, | |
'c|content=s' => \$pathimagefolder, | |
't|title=s' => \$imagetitleswitch, | |
'a|alttext=s' => \$imagealttextswitch, | |
'o|outputfolder=s' => \$pathoutputfolder, | |
'f|format=s' => \$imageoutputformat, | |
'e|fbeffect=s' => \$imagefancyboxeffect, | |
'w|fbwidth=s' => \$imageFancyboxThumbMaxWidth, | |
'h|fbheight=s' => \$imageFancyboxThumbMaxHeight | |
); | |
if (! $result ){ | |
die "Error: Problem reading parameter.\n"; | |
} | |
if ( ! defined $inputfile || ! defined $idsource || ! defined $inputgallery) { # Check the mandatory parameters | |
print "Not enough parameter specified. Abort.\n"; | |
print usage; | |
exit 1; | |
} | |
my $success = open MARKDOWNINPUT, '<:encoding(utf8)', "$inputfile"; # Open the inputfile | |
if (! $success ){ | |
print "Error: can't find file: $inputfile\n"; | |
exit 1; | |
} | |
$success = open INPUTIMAGECSV, '<:encoding(utf8)', "$idsource"; # Open idsource files | |
if (! $success ){ | |
print "Error: can't find file: $idsource\n"; | |
exit 1; | |
} | |
$success = open INPUTGALLERYCSV, '<encoding(utf8)', "$inputgallery"; # Open Gallerysource files | |
if (! $success ){ | |
print "Error: can't find file: $inputgallery\n"; | |
exit 1; | |
} | |
if ( defined $pathimagefolder && ! -d $pathimagefolder ) { # Check if image folder exists if specified | |
print "Error: Imagefolder doesn't exist!\n"; | |
exit 1; | |
} | |
if ( defined $pathoutputfolder && ! -d $pathoutputfolder ){ | |
die "Error: Outputfolder '$pathoutputfolder' doesn't exist.\n Use parameter -o|output to specify the path to a valid folder. Script stopped.\n\n"; | |
} | |
( (defined $imageFancyboxThumbMaxHeight) && ( isdigit $imageFancyboxThumbMaxHeight) && ($allimagedata{'fbthumbmaxheight'} = $imageFancyboxThumbMaxHeight) ); | |
( (defined $imageFancyboxThumbMaxWidth) && (isdigit $imageFancyboxThumbMaxWidth) && ($allimagedata{'fbthumbmaxwidth'} = $imageFancyboxThumbMaxWidth) ); | |
if ( ! $imagetitleswitch eq 'off' ) { # Set Image titel tag or not. Default: on | |
$imagetitleswitch = 'on'; | |
} | |
if ( ! $imagealttextswitch eq 'off' ) { | |
$imagealttextswitch = 'on'; | |
} | |
# Fancybox output formats and calculations. | |
if ( defined $imageoutputformat && $imageoutputformat eq "f") { | |
# Needs to get the format for the fancyboxeffect. | |
die "No Fancybox effect specified. Use parameter -e to set it to a,b,c or d." | |
unless (defined $imagefancyboxeffect); | |
if ( $imagefancyboxeffect =~ m/^[a-d]{1}$/i ) { | |
$allimagedata{'fbeffect'} = $imagefancyboxeffect; | |
} else { | |
$allimagedata{'fbeffect'} = 'a'; # Default effect | |
} | |
# Workaround since $allimagedata{fbeffect} doesn't show up in the while-loop. Wonder why | |
our $fbeffectselect = $allimagedata{'fbeffect'}; | |
} elsif ( defined $imageoutputformat && $imageoutputformat eq "t") { | |
$imageoutputformat = 't'; | |
} else { | |
$imageoutputformat = 'o'; | |
} | |
while (<MARKDOWNINPUT>) { # Read the Markdown input file ##### START #### | |
$line = $_; # Search for NGG gallery insert tags in $_ | |
while ($line =~ /(\[(?<singleimage>singlepic ([^\]])+) \])/ix) { | |
my @fields = split / /, $+{singleimage}; # Split the singlepic-string | |
foreach (@fields) { # Uses $_ | |
my @string = split /=/, $_; # $_ comes from foreach!!! | |
if ($string[0] eq "h") { # found height | |
$allimagedata{'markdownimageheight'} = $string[1]; | |
} elsif ($string[0] eq "id") { # found image id | |
%allimagedata = (%allimagedata,getImageData($string[1])); # Hash needs to be extended to keep the current values | |
} elsif ($string[0] eq "w") { # found image width | |
$allimagedata{'markdownimagewidth'} = $string[1]; | |
} elsif ($string[0] eq "float") { # found image floating parameter | |
$allimagedata{'imagefloat'} = $string[1]; | |
} | |
} | |
# If the local file is available, gather the image information and calculate missing parameters. | |
if ( -e $pathimagefolder.$allimagedata{'imagepath'}."/".$allimagedata{'imagename'} ) { | |
$allimagedata{'imagefileheight'} .= getImageFileData($pathimagefolder.$allimagedata{'imagepath'}."/".$allimagedata{'imagename'},'height'); | |
$allimagedata{'imagefilewidth'} .= getImageFileData($pathimagefolder.$allimagedata{'imagepath'}."/".$allimagedata{'imagename'},'width'); | |
} | |
# If in the original post the image dimensions have been altered and not both are available, take the available one | |
# and calulate the other one | |
if ( (! defined $allimagedata{'markdownimagewidth'}) && (defined $allimagedata{'markdownimageheight'}) && (defined $allimagedata{'imagefileheight'}) ) { | |
$allimagedata{'markdownimagewidth'} = int($allimagedata{'imagefilewidth'} / ($allimagedata{'imagefileheight'} / $allimagedata{'markdownimageheight'})); | |
} | |
if (( ! defined $allimagedata{'markdownimageheight'} ) && ( defined $allimagedata{'markdownimagewidth'} ) && ( defined $allimagedata{'imagefilewidth'} ) ) { | |
$allimagedata{'markdownimageheight'} = int($allimagedata{'imagefileheight'} / ($allimagedata{'imagefilewidth'} / $allimagedata{'markdownimagewidth'})); | |
} | |
## START THUMBNAIL CALCULATIONS #################################################### | |
# If Fancybox is the defined output, we need to calculate the thumbnails based on the information we have from the Nexgen-image string | |
# Thumbnails have max-size as used in the post, if not available set to default (250) | |
## Defaults | |
# Debug | |
#print "Incoming:\n"; | |
#print "fbthumbmaxheight: $allimagedata{'fbthumbmaxheight'}\n"; | |
#print "markdownimageheight: $allimagedata{'markdownimageheight'}\n"; | |
#print "fbthumbmaxwidth: $allimagedata{'fbthumbmaxwidth'}\n"; | |
#print "markdownimagewidth: $allimagedata{'markdownimagewidth'}\n"; | |
# If Thumbnailsize has been defined, overwrite the markdown input | |
## Width overwrites height, reset height if width available | |
((defined $allimagedata{'fbthumbmaxwidth'}) && ($allimagedata{'fbthumbmaxheight'} = '' )); | |
( (! defined $allimagedata{'fbthumbmaxwidth'}) && ($allimagedata{'fbthumbmaxwidth'} = $allimagedata{'markdownimagewidth'}) ); | |
( (! defined $allimagedata{'fbthumbmaxheight'}) && ($allimagedata{'fbthumbmaxheight'} = $allimagedata{'markdownimageheight'}) ); | |
# Write the replacement tag | |
if ( (defined $allimagedata{'fbthumbmaxheight'}) && ( ! $allimagedata{'fbthumbmaxheight'} eq '' ) ) { | |
# print "i'm here: \"$allimagedata{'fbthumbmaxheight'}\" \n"; | |
$allimagedata{'fbthumbmaxheight'} = "height=\"".$allimagedata{'fbthumbmaxheight'}."\""; | |
} else { | |
$allimagedata{'fbthumbmaxheight'} = ''; | |
} | |
if ( ( defined $allimagedata{'fbthumbmaxwidth'}) && ( ! $allimagedata{'fbthumbmaxwidth'} eq '' ) ) { | |
$allimagedata{'fbthumbmaxwidth'} = "width=\"".$allimagedata{'fbthumbmaxwidth'}."\""; | |
} else { | |
$allimagedata{'fbthumbmaxwidth'} = ''; | |
} | |
## END THUMBNAIL CALCULATIONS #################################################### | |
# Debug | |
#print "Outgoing:\n"; | |
#print "fbthumbmaxheight: $allimagedata{'fbthumbmaxheight'}\n"; | |
#print "markdownimageheight: $allimagedata{'markdownimageheight'}\n"; | |
#print "fbthumbmaxwidth: $allimagedata{'fbthumbmaxwidth'}\n"; | |
#print "markdownimagewidth: $allimagedata{'markdownimagewidth'}\n"; | |
#die; | |
# If Imagetitel or ImageAltText has been set off, switch it off! | |
if ($imagealttextswitch eq 'off') { | |
$allimagedata{'imagealttext'} = ''; | |
} else { | |
$allimagedata{'imagealttext'} = "$allimagedata{'imagename'}, $allimagedata{'imagealttext'}"; | |
} | |
if ($imagetitleswitch eq 'off') { | |
$allimagedata{'imagetiteltext'} = ''; | |
} | |
# Construct image replacement tag | |
## Standard Octopress output format | |
($imageoutputformat eq "o") && ($imagereplacetag = "{% img $allimagedata{imagefloat} $allimagedata{imagepath}/$allimagedata{imagename} $allimagedata{markdownimagewidth} $allimagedata{markdownimageheight} \"$allimagedata{imagetiteltext}\" \"$allimagedata{imagealttext}\" %}"); | |
# Fancybox output format | |
( ($imageoutputformat eq 'f') && | |
($allimagedata{'imagefloat'} = " align=\"".$allimagedata{'imagefloat'}."\"") && | |
($imagereplacetag = "<a class=\"fancybox-effects-".$fbeffectselect."\" href=\"".$allimagedata{'imagepath'}."/".$allimagedata{'imagename'}."\" title=\"".$allimagedata{'imagetiteltext'}."\"><img src=\"".$allimagedata{'imagepath'}."/".$allimagedata{'imagename'}."\" alt=\"".$allimagedata{'imagealttext'}."\"".$allimagedata{'imagefloat'}." ".$allimagedata{'fbthumbmaxheight'}." ".$allimagedata{'fbthumbmaxwidth'}."/></a>") ); | |
# Tritargets output format (photo-tag) | |
($imageoutputformat eq 't') && ($imagereplacetag = "{% photo ".$allimagedata{imagepath}."/".$allimagedata{'imagename'}." default ".$allimagedata{'imagetiteltext'}." %}"); | |
# Replace the tag | |
$line =~ s/(\[(?<singleimage>singlepic ([^\]])+) \])/$imagereplacetag/ix; | |
} | |
$OUTPUTFILE .= $line | |
} | |
# After all those changes, if an outputpath is specified, drop the file there | |
# otherwise put it on the screen | |
if ( ! defined $pathoutputfolder) { | |
print $OUTPUTFILE; | |
} else { | |
my $nameoutputfile = basename($inputfile); | |
$success = open MARKDOWOUTPUT, '>:encoding(utf8)', $pathoutputfolder.$nameoutputfile; # Open the inputfile | |
if (! $success ){ | |
die "Error: can't create file: $inputfile for output.\n"; | |
} else { | |
print MARKDOWOUTPUT $OUTPUTFILE; | |
} | |
close MARKDOWOUTPUT; | |
} | |
# Closing files | |
close MARKDOWNINPUT; | |
close INPUTIMAGECSV; | |
close INPUTGALLERYCSV; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment