Skip to content

Instantly share code, notes, and snippets.

@tabletick
Created September 4, 2012 12:47
Show Gist options
  • Save tabletick/3620859 to your computer and use it in GitHub Desktop.
Save tabletick/3620859 to your computer and use it in GitHub Desktop.
NGG Gallery to Fancybox
#!/usr/bin/perl
#
# Script converts Wordpress NGG Gallery Syntax in posts into
# Fancybox Gallery syntax
#
# RequiresText:CSV
# Image::Size
# ------------------------------------------------------------------------
use Getopt::Long;
Getopt::Long::Configure ('bundling');
use Image::Size;
use Text::CSV;
use File::Basename;
use POSIX;
use warnings;
# Local hashes for data
our (%allgallerydata);
our ($htmloutput);
our $OUTPUTFILE;
print "\n";
sub usage { # Routine prints the Syntax
print "Syntax:
script: -i|inputfile <input.markdown>
-g|gallery <galleryfile>
-i|idsources <picturefile>
-o|outputfolder /path/to/output/folder
-r|removepath stringtoremovefrompath
-a|addpath stringtoaddtopath
-f|format f|t
";
}
sub help { # Subroutine prints help text
print "Help:
-i|inputfile Defines the markdown inputfile containing the old NGG Syntax that needs to be converted.
-g|gallery Defines the inputfile from the NGG gallery, usally \"wp_ngg_gallery.csv\"
-s|idsource Defines the inputfile from the NGG gallery, usuall \"wp_ngg_pictures.csv\"
-r|removepath Defines the string to remove from the image path (good when you need to move the image-source somewhere else.), e.g. \"http://example.com/images\" <-- removes that domain from the path.
-a|addpath Defines a string to add at the beginning of the image-path
-o|outputfolder Markdown outputfolder
-f|format Defines format, either fancybox(f) or tritarget(t)
";
}
# # # # Start # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Reading the script parameters
our $inputpathremove;
$result = GetOptions (
'i|inputfile=s' => \$inputmarkdownfile,
'g|gallery=s' => \$inputgalleryfile,
's|idsource=s' => \$inputpicturefile,
'o|outputfolder=s' => \$pathoutputfolder,
'r|removepath=s' => \$inputpathremove,
'a|addpath=s' => \$inputpathadd,
'f|format=s' => \$inputformat
);
die "Error: Problem reading parameter. Abort.\n"
unless ($result);
help() && die " => Error: not enough parameter specified. Abort.\n"
unless (defined $inputmarkdownfile && defined $inputgalleryfile && defined $inputpicturefile);
my $success = open MARKDOWNINPUT, '<:encoding(utf8)', "$inputmarkdownfile"; #Open the input file
die "Error: Can't find input file $inputmarkdownfile. Abbort.\n"
unless ($success);
$success = open GALLERYINPUT, '<:encoding(utf8)', "$inputgalleryfile";
die "Error: Can't read the input file $inputgalleryfile. Abort.\n"
unless ($success);
$success = open PICTUREINPUT, '<:encoding(utf8)', "$inputpicturefile";
die "Error: Can't read the input file $inputpicturefile. Abort.\n"
unless ($success);
if ( ! defined $inputformat || ! $inputformat eq 't') {
our $galleryformat = 'f';
} elsif ($inputformat eq 't') {
our $galleryformat = 't';
} else {
our $galleryformat = 'f';
}
$allgallerydata{'galleryidfilepath'} = $inputgalleryfile;
# SUB for reading gallery file
sub getGalleryData {
my $galleryid = $_[0];
my %getallgallerydata;
my $csv = Text::CSV->new({sep_char => ';'});
# Reset filehandle position - you never know ;)
seek GALLERYINPUT,0,0;
while (my $galleryinputline = <GALLERYINPUT>) {
if ($csv->parse($galleryinputline)) {
my @columns = $csv->fields();
if ( $columns[0] eq $galleryid) {
$getallgallerydata{'galleryname'} = $columns[2];
$getallgallerydata{'galleryfolderpath'} = $columns[3];
$getallgallerydata{'gallerydescription'} = $columns[5];
return %getallgallerydata;
}
}
}
}
# Read image file ids from a gallery
sub getallimageids {
my $csv = Text::CSV->new({sep_char => ';'});
my @getallimageids;
my $galleryid = $_[0];
seek PICTUREINPUT,0,0; # Reset Reader for PICTUREINPUT
while (my $pictureinputfile = <PICTUREINPUT>) {
# Read CSV Data
$csv->parse($pictureinputfile);
my @columns = $csv->fields();
if ( $columns[3] eq $galleryid ) {
push(@getallimageids,"$columns[0]");
}
}
return @getallimageids;
}
# Read image file data from picture file
sub getallimagedata {
my $csv = Text::CSV->new({sep_char => ';'});
my $imageid = $_[0];
my %getallimagedata;
seek PICTUREINPUT,0,0; # Reset reader for PICTUREINPUT
while (my $pictureinputfile = <PICTUREINPUT> ) {
$csv->parse($pictureinputfile);
my @columns = $csv->fields();
if ( $columns[0] eq $imageid ) {
$getallimagedata{'image_filename'} = $columns[4];
$getallimagedata{'image_description'} = $columns[5];
$getallimagedata{'image_alttext'} = $columns[6];
return %getallimagedata;
}
}
}
# Reading the markdown input file and starting script
while (<MARKDOWNINPUT>) {
if ($_ =~ /(\[(?<nggallery>nggallery ([^\]])+) \])/ix) {
my @fields = split / /, $+{nggallery};
foreach (@fields) {
my @string = split /=/, $_;
if ($string[0] eq 'id') {
$allgallerydata{'galleryid'} = $string[1];
%allgallerydata = (%allgallerydata,getGalleryData($allgallerydata{'galleryid'}));
# Alter imagepath from console parameters
$allgallerydata{'galleryfolderpath'} =~ s/$inputpathremove//
unless (! defined $inputpathremove);
$allgallerydata{'galleryfolderpath'} =~ s/^/$inputpathadd/
unless (! defined $inputpathadd);
# Go Through the image file and get the image data
# First: Get the involved Image Ids
my @imageids = getallimageids($allgallerydata{'galleryid'});
# Adjust output format
if ($galleryformat eq 'f') {
foreach $imageid (@imageids) {
#grep the information from the picture file
my %imagedata = getallimagedata ($imageid);
# Build HTML output
$htmloutput .= "\n<a class=\"fancybox\" rel=\"group1\" href=\"".$allgallerydata{'galleryfolderpath'}."/".$imagedata{'image_filename'}."\"><img src=\"".$allgallerydata{'galleryfolderpath'}."/".$imagedata{'image_filename'}."\" alt=\"".$imagedata{'image_alttext'}."\" width=\"150\"/></a>";
}
} elsif ($galleryformat eq 't') {
$htmloutput = "{% gallery %}\n";
foreach $imageid (@imageids) {
#grep the information from the picture file
my %imagedata = getallimagedata ($imageid);
# 2012-09-04
# This output does work, but the plugin breaks currently when urls are used with http:// in front of it.1
$htmloutput .= $allgallerydata{'galleryfolderpath'}."/".$imagedata{'image_filename'}.": ".$imagedata{'image_alttext'}."\n";
}
$htmloutput .= "{% endgallery %}\n";
}
}
}
# Replace the tag
$_ =~ s/(\[(?<nggallery>nggallery ([^\]])+) \])/$htmloutput/ix;
}
$OUTPUTFILE .= $_;
}
# Output
if ( ! defined $pathoutputfolder ) {
print $OUTPUTFILE;
} else {
my $nameoutputfile = basename($inputmarkdownfile);
$success = open MARKDOWNOUTPUT, '>:encoding(utf8)',$pathoutputfolder.$nameoutputfile; #open the inputfile
if ( ! $success ){
die "Error: can't create file: $inputmarkdownfile for output.\n";
} else {
print MARKDOWNOUTPUT $OUTPUTFILE;
}
close MARKDOWNOUTPUT;
}
# # Tasks
# 1. Parameter -r doesn't work as expected.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment