Posted on Jan 13, 2009

Adding SyntaxHighlighter QuickTag Support to WordPress

Edit: The latest version of the SyntaxHighlighter plugin, called SyntaxHighlighter Evolved, now supports version 2.0 and has built-in shortcode support which is way cooler than what I did here. :-)
Continue Reading

Posted on Jan 28, 2008

Testing Perl Support for Google SyntaxHighlighter

I’ve been enjoying playing with Google’s SyntaxHighlighter. Another user came up with a Perl brush file, so I thought I’d give it a shot. One note from the author of the brush file is that -> gets converted to -> rather than being translated properly. It’s definitely the highlighter that does it, too, because when you disable the brush it shows up properly.

#!/usr/bin/perl

use strict;
use Mail::Box::Manager;

#################################
# User Variables
#
# Set $password to your password (encoded):
# . only the low four bits of each character count
# . character 0x3f gets split into 0x?3 and 0x?f,
# . fill in the ?'s with anything you like
# . man ascii =)
#

my $server = 'pop.server.name';
my $username = 'username';
my $password = 'tSvx6!f>v7V%d=fU'; # the word "ChangeMe" encoded

#################################
# Get em!
#

my $mgr = Mail::Box::Manager->new;
my $pop = $mgr->open(type => 'pop3',
username => $username,
password => decodePassword($password),
server_name => $server);

my @messages = $pop->messages;

print
"33[00;30;1m:33[00;36m:33[00;36;1m: ", scalar @messages, " messages in mailbox :33[00;36m:33[00;30;1m:33[mn";

foreach my $message (@messages) {
	my $fr = $message->get('From');
	$fr =~ s/s+< .*$//; $fr =~ s/"//g;

	printf "33#733[1m %-28.28s %-45.45s33[0mn", $fr, $message->get('Subject');
	my $lbls = $message->labels;
	while( my ($k, $v) = each %$lbls ) {
		print "key: $k, value: $v.n";
	}
}

$pop->close;

#################################
# methods
#

sub decodePassword($) {
	my ($password_split) = @_;
	my $password = "";
	my ($high, $low);

	$password_split = reverse($password_split);
	while (length($password_split) > 1) {
		$high = (ord(chop($password_split)) & 0xf) < < 4;
		$low = ord(chop($password_split)) & 0xf;
		$password .= chr($high|$low);
	}

	return $password;
}

About three years ago I was living in the shell. I had a script aliased to ‘m’ that would run a perl script for each of my IMAP accounts and show me all of the unread messages. The script had been originally written by a fellow Help Desk supervisor at UB and then given colorization support by another fellow supervisor, Doug, who has since become a perl and python guru.

The trouble then was that, while this worked for my home email server, I had to change some of the socket code to work on all of the other IMAP servers I used. That wasn’t too tough since IMAP is a relatively straightforward protocol. Getting POP support to work looked to be a lot harder, however. I couldn’t find anyone that had written something similar for me to hack, so I wrote my own.

This script above uses the perl module Mail::Box which installs straight out of CPAN. It’s probably been updated since this was written, and I don’t even know if the script still works (who still uses POP??)

Posted on Dec 13, 2007

Code Highlighting With Google’s SyntaxHighlighter

This is (mostly) a test post to see how well Google’s SyntaxHighligher works. As you hopefully see below (if your browser supports javascript), it works well. I approve!

function safe($num) {
	// there has to be a smarter way to do this!
	// make sure that $num is always above 65
	$diff = date("w",$now) + 65;
	if ($num < 65) {
		$num+=$diff;
	}

	// make sure that $num is in 65-90 or 97-122
	if (($num > 90) && ($num < 97)) {
		$num+=7;
		return $num;
	}
	elseif ($num > 122) {
		// be formulaic rather than arbitrary
		$d = $num-122; $e = $d%7; $f = $d+$e;
		$num-=$f;
		return $num;
	}
	else {
		return $num;
	}
}

I use a combination of plugins that all fight to figure out what to do with PHP soure code. In order to make this work, I used SimpleCode to translate the source into HTML entities so that Exec-PHP wouldn’t try to execute the code block.

The default font set for the code display is Consolas, and then the standard Courier New, Courier and then whatever your system’s default mono-space or serif fonts are. If you haven’t downloaded Consolas, I’d highly suggest it. Scott Hanselman wrote a couple of articles about it, including making Consolas your default Console font, which I do with reckless abandon.

SyntaxHighlighter has a couple of nifty options, such as disabling some extra controls, removing the line numbers and keeping the <pre> block collapsed by default, ideally so that you could have links to expand them as needed. I made one change to the style sheet – making the grey left-hand border 20px thinner. I thought a 45px band of nothing was a bad use of space. The highlighting loads absolutely last on the page so you, the user, don’t sit there waiting for it to work in order to begin browsing content.

Sadly, there is currently no highlighting set for Basic, else Tuesday’s post would have looked better.