Quote Panel

26 Mar

Yesterday while looking at the new blog header I decided to have a hand at adding a quote mechanism. I have always been fond of the hilarious tongue in check The Tao Of Programming series – the first self titled book being best by far. My initial notion was to put it in the footer which seemed vacuous. Puzzled I gave up for a few hours and wandered to a tangential stream of thought. Later, while talking with my wife just before bed an idea hit me like a ton of bricks.  Why not put a sliding quote panel in the header animated by jQuery? Nearly sick with excitement I “uhm”-ed my way through the rest of the talk with my wife and then stole off the first chance to get to a computer to hammer out some initial code before the ephemeral concept evaporated from my mind.

Quotes Collection, a WordPress plugin, looked great but after playing with it for five minutes warts appeared. For starters it smartly used a database table, but there were no tools baked in for bulk import.  Secondly, the plugin UI was badly munging anything I bothered to manually insert into the database. However, the plugin provided an easy to use and feature rich PHP function for dropping the quotes into HTML. I decided to keep the plugin for displaying the quotes and hack a perl script to do the bulk database quote insert.

Fortunately, the quotes themselves were easy enough to obtain and due to a lapsed copyright available for the taking. After copy and pasting them into vim and staring at one line for nearly three minutes fatigue settled in. I made a few conceptual mental notes for the morrow and went to bed.

Early today I finished the edit of the quotes file, ttop.dat, in a vim editor. The tedium of manually stripping poor double quote characters, blank lines, and extra spaces raced by with a rested mind. I settled on using ‘—’ as a delimiter between all quotes as most contained multiple lines knowing that perl’s input record separator would permit parsing pliability. Once the quote file edit was complete I hacked up the perl script to slurp and parse the file and insert each quote to the database. After a few trail runs to ferret out the bugs the quotes were neatly tucked away in the database.

#!/usr/bin/perl

use strict;
use warnings;

use DBI;

# insert the new uirs into the db
sub db_insert_quotes {
    my $quote_sref = shift;
    my $dbh = shift;

    my $sql = "INSERT INTO wp_quotescollection (quote, author, source, tags, visible, time_added, time_updated)
               VALUES (?, 'Geoffrey James', 'The Tao Of Programming', 'TTOP', 'yes', NOW(), NOW())";
    my $sth = $$dbh->prepare($sql);
    $sth->bind_param(1, $$quote_sref);
    $sth->execute;
}

# Connect to the database.
my $dbh = DBI->connect("DBI:mysql:database=DATABASE;host=localhost",
                       "USER", "PASSWORD",
                       {'RaiseError' => 1});

$/ = "---\n";
while (<>) {
    chomp;
    chop;
    my $quote = $_;
    #db_insert_quotes(\$quote, \$dbh);
}

exit;

A few months back I vaguely recalled reading a jQuery article with a great simple sliding panel demo. After grabbing a few images and applying a dollop or two of CSS, Javascript, and HTML my panel was functional. Kuler‘s color insights came to the rescue again saving site visitors from my train wreck artistic spasms.

This site enhancement was a joy to work on. I hope visitors are enthused about it as I am.

No comments yet

Leave a Reply

Powered by WP Hashcash