WP-Syntax tryout
Following last post on WordPress Code Snippet plugin this post will be the tryout of WP-Syntax – a code format plugin that uses GeSHi supporting a wide range of languages. That includes Perl, one of the languages I frequently use.
Unfortunately there’s no interface to easily add a code snippet. It is necessary to change to post HTML view and add HTML pre tags with attributes lang and line.
Below you can see how does a Perl piece of code looks like. Unfortunately when I change from HTML back to Visual the attribute line=”1″ is removed by WordPress.
Another problem are some characters that need be replaced by HTML entities for example less than and greater than characters. I had to manually check if the code was correctly copied and fix some of the lines.
On the positive side, this plugin allows you to have many snippet blocks within the text.
#!/usr/bin/perl -w
use MP3::Info;
use MP4::Info;
use strict;
use Getopt::Long;
use Encode qw(from_to);
use bytes;
###############
sub process {
###############
my ($src) = shift;
$src .= "/" if $src !~ /\/$/;
print "Opening $src\n";
opendir(my $dir, $src) or die "can't opendir $src: $!";
while( defined(my $file = readdir($dir))) {
next if $file =~ /^\.\.?$/;
my $fileabs = "$src$file";
if ( -d $fileabs ) {
process( $fileabs );
next;
}
next if $file !~ /mp3$|m4a$|mp4$/;
my $func=\&get_mp3tag;
if ( $file =~/m4a$|mp4$/ ) {
$func=\&get_mp4tag;
}
my $tag = $func->($fileabs) or die "No tag info";
print "File $file|$tag->{ARTIST}|$tag->{ALBUM}|$tag->{TITLE}\n";
my ($artist,$album,$title) = ($tag->{ARTIST},$tag->{ALBUM},$tag->{TITLE});
#foreach my $k ( keys %$tag ) {
# print "$k: $tag->{$k}\n";
#}
if ( $artist =~ // || $title =~ // ) {
print "SKIPPING\n";
next;
}
my ($ext) = $file =~ /\.([^\.]+)$/;
my $newname = "$artist - $title.$ext";
#if ( $album !~ // ) {
# $newname = "$artist - $album - $title.$ext";
#}
$newname =~ s/\*|\\|\///g;
$newname =~ s/ /_/g;
$newname =~ s/&/n/g;
$newname = $src . $newname;
print "$newname\n";
#from_to($newname, "utf-8", "cp1250");
#print "$newname\n";
#from_to($fileabs, "utf-8", "cp1250");
rename( $fileabs, $newname ) or warn("Unable to rename $fileabs to $newname: $!\n");
}
closedir($dir);
}
my $usage = <<END
Usage: $0 --src=<source-directory>
END
;
die($usage) if @ARGV != 1;
my $src;
GetOptions (
"src=s" => \$src,
);
if ( ! -d $src ) {
die($usage);
}
process( $src );