Archive

Archive for the ‘Wordpress’ Category

WP-Syntax tryout

May 31st, 2009 No comments

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 );
Categories: Wordpress

Wordpress Code Snippet plugin tryout

May 31st, 2009 No comments

Code displayed on web pages need formatting to ease reading. This post is my tryout of WordPress Code Snippet plugin.

After installation the plugin adds a code snippet box at the bottom of the Add/Edit post window and a drop down box with languages. You can select one from the following languages: HTML/XML, CSS, Javascript, PHP, SQL, C#, VB, C++, Java, Python, Ruby, Delphi.

I copy pasted some ancient C++ code and the formatting is really nice and has great syntax hightlighting.

The only problem seems to be that you can only have one snippet per post and not within text.

Categories: Wordpress