-
-
Save jackbaty/1973678 to your computer and use it in GitHub Desktop.
Convert wordpress style caption tags to octopress image tags
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 | |
use strict; | |
my $script_name = $ARGV[0]; | |
my $file_contents = ""; | |
while(<>) { | |
if($_ =~ /\[caption.*/) { | |
my %imageData; | |
my $line = $_; | |
# url | |
my $url; | |
if($line =~ /\(http.*\/(\d\d\d\d)\/(\d\d)\/(.*)\)\[/){ | |
$url = $imageData{'img'} = $script_name . "/" . $3; | |
$url =~ s/\.markdown//g | |
} | |
my $imgInfo; | |
# Get image information - Comment this one out if you don't have ImageMagick | |
$imgInfo = `identify ../images/posts/$url`; | |
# width | |
if($line =~ /width=\"(\d+)\"/){ | |
$imageData{'width'} = $1; | |
} else { | |
if($imgInfo =~ /\s+(\d+)x\d+\s+/) { | |
$imageData{'width'} = $1; | |
} | |
} | |
# height | |
if($line =~ /height=\"(\d+)\"/){ | |
$imageData{'height'} = $1; | |
} else { | |
if($imgInfo =~ /\s+\d+x(\d+)\s+/) { | |
$imageData{'height'} = $1; | |
} | |
} | |
# align | |
if($line =~ /align=\"align(\w+)\"/){ | |
$1 = 'center' if($1 eq 'none'); | |
$imageData{'align'} = $1; | |
} | |
# caption | |
if($line =~ /caption=\"(.+)\"/){ | |
$imageData{'caption'} = $1; | |
} | |
$file_contents .= '{% img '; | |
$file_contents .= ' ' . $imageData{'align'} . ' '; | |
$file_contents .= '/images/posts/' . $imageData{'img'}. ' '; | |
if(!$imageData{'width'}) { | |
$file_contents .= ' %}' . "\n"; | |
next; | |
} | |
$file_contents .= ' ' . $imageData{'width'} . ' '; | |
if(!$imageData{'height'}) { | |
$file_contents .= ' %}' . "\n"; | |
next; | |
} | |
$file_contents .= ' ' . $imageData{'height'} . ' '; | |
if(!$imageData{'caption'}) { | |
$file_contents .= ' %}' . "\n"; | |
next; | |
} | |
$file_contents .= '"' . $imageData{'caption'} . '" "' . $imageData{'caption'} . '" ' if($imageData{'caption'}); | |
$file_contents .= ' %}' . "\n"; | |
} else { | |
$file_contents .= $_; | |
} | |
} | |
open FILE, ">" . $script_name; | |
print FILE $file_contents; | |
close FILE; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment