Posted (Updated ) in PHP

Update 06 June 2011: I’ve contacted the current developer and he’s accepted the patch. It should be appearing in a WP-Syntax near you shortly!
Update 16 July 2011: I’ve merged Chimo’s excellent patch which adds page range support. See the updated tutorial below for usage instructions. Thanks Chimo!
Update 27 July 2011: We have liftoff! This patch is finally part of the official plugin!

I’ve used a few different WordPress syntax highlighters in my day but WP-Syntax is easily the best of them. One issue that’s bothered me with WP-Syntax for a while now, however, is lack of line highlighting support so I decided to look into adding it myself. At the time of writing I’m using WP-Syntax 0.9.9.

To add highlighting support, open up /wp-content/plugins/wp-syntax/wp-syntax.php. You’ll need to modify two functions – wp_syntax_highlight and wp_syntax_before_filter. Here’s wp_syntax_highlight:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
function wp_syntax_highlight($match)
{
    global $wp_syntax_matches;
 
    $i = intval($match[1]);
    $match = $wp_syntax_matches[$i];
 
    $language = strtolower(trim($match[1]));
    $line = trim($match[2]);
    $escaped = trim($match[3]);
 
    $code = wp_syntax_code_trim($match[5]);    if ($escaped == "true") $code = htmlspecialchars_decode($code);
 
    $geshi = new GeSHi($code, $language);
    $geshi->enable_keyword_links(false);
    do_action_ref_array('wp_syntax_init_geshi', array(&$geshi));
 
    //START LINE HIGHLIGHT SUPPORT    $highlight = array();    if ( !empty($match[4]) )    {        $highlight = strpos($match[4],',') == false ? array($match[4]) : explode(',', $match[4]); 	$h_lines = array();	for( $i=0; $i<sizeof($highlight); $i++ )	{		$h_range = explode('-', $highlight[$i]); 		if( sizeof($h_range) == 2 )			$h_lines = array_merge( $h_lines, range($h_range[0], $h_range[1]) );		else			array_push($h_lines, $highlight[$i]);	}         $geshi->highlight_lines_extra( $h_lines );    }    //END LINE HIGHLIGHT SUPPORT 
    $output = "\n<div class=\"wp_syntax\">";
 
    if ($line)
    {
        $output .= "<table><tr><td class=\"line_numbers\">";
        $output .= wp_syntax_line_numbers($code, $line);
        $output .= "</td><td class=\"code\">";
        $output .= $geshi->parse_code();
        $output .= "</td></tr></table>";
    }
    else
    {
        $output .= "<div class=\"code\">";
        $output .= $geshi->parse_code();
        $output .= "</div>";
    }
    return
 
    $output .= "</div>\n";
 
    return $output;
}

and here’s wp_syntax_before_filter:

1
2
3
4
5
6
7
8
function wp_syntax_before_filter($content)
{
    return preg_replace_callback(
        "/\s*<pre(?:lang=[\"']([\w-]+)[\"']|line=[\"'](\d*)[\"']|escaped=[\"'](true|false)?[\"']|highlight=[\"']((?:\d+[,-])*\d+)[\"']|\s)+>(.*)<\/pre>\s*/siU",        "wp_syntax_substitute",
        $content
    );
}

Updated/added lines are, of course, highlighted 🙂

Below is an example of the new functionality:

<pre lang=”php” highlight=”2″>
$foo = ‘foo’;
$bar = ‘bar’;
$foobar = ‘foobar’;
</pre>

$foo = 'foo';
    $bar = 'bar';    $foobar = 'foobar';

<pre lang=”php” highlight=”1,2,5-7″>
$foo = ‘foo’;
$bar = ‘bar’;
$foobar = ‘foobar’;

$foo = ‘foo’;
$bar = ‘bar’;
$foobar = ‘foobar’;

$foo = ‘foo’;
$bar = ‘bar’;
$foobar = ‘foobar’;
</pre>

$foo = 'foo';    $bar = 'bar';    $foobar = 'foobar';
 
    $foo = 'foo';    $bar = 'bar';    $foobar = 'foobar'; 
    $foo = 'foo';
    $bar = 'bar';
    $foobar = 'foobar';

Remember to add highlight to the list of allowed <PRE> tag arguments in your themes functions.php file. Details on doing so can be found here.

Read More »

Posted (Updated ) in Javascript

I discovered recently a little quirk with accessing a JQuery UI Dialog‘s options and methods from within the dialog itself. I was AJAXing the dialog’s contents and was having difficulty figuring out how to access the dialog’s methods. As it turns out, from the contents of the dialog you don’t use

$(this).closest('.ui-dialog').option()

which returns a javascript object, but instead use

$(this).closest('.ui-dialog-content').option()

Seeing as this post would otherwise be pretty short, let’s do a little more than simply retrieving options. Below I’ll explain not only to open a AJAX dialog from a HTML link, but also tell the dialog what element opened it. This could be useful in such instances as when you need to click a link which opens a dialog allowing you to upload a user avatar, and upon uploading, replaces the link with the avatar image.

For the lazy, you can download the complete tutorial files here.

Read More »

Posted (Updated ) in Linux

Upon downgrading back to Ubuntu 10.10 after my horrendously bad experience with 11.04, I’ve been reunited with a bug I’d previously put up with for 6 months: The trackpad on my laptop often ‘mouseups’ when dragging, resulting in windows not being dragged correctly. In addition, web browser and pidgin tabs will occasionally close on click instead of giving focus to that tab. Well here’s the fix!

Make sure the following 2 lines are In the InputClass section of /etc/X11/xorg.conf.d/50-synaptics.conf:

Option "ClickFinger3" "1"
        Option "EmulateTwoFingerMinZ" "280"

Here’s what mine looks like:

Section "InputClass"
        Identifier "touchpad catchall"
        Driver "synaptics"
        MatchIsTouchpad "on"
        MatchDevicePath "/dev/input/event*"
        Option "ClickFinger3" "1"
        Option "EmulateTwoFingerMinZ" "280"
EndSection

You’ll probably need to do a reboot for the changes to take effect.

Thanks to users egermani and ejmarkow for their respective fixes here and here.

Read More »

Posted (Updated ) in Database, Linux

Update 2011-10-12: More storage options! Google Storage, Local HDD, FTP, experimental unlimited Google Docs conversion

Have you ever wanted to ensure that even if your server dies in the most spectacular of ways, your DB is safe? Meet Cloud Database Backup. Cloud Database Backup is a little script I’ve written that allows you to quickly and easily back up your MySQL DBs to the cloud.

Currently you can back your database up to:

  • Amazon S3
  • FTP
  • Google Docs
  • Google Storage
  • Local HDD

Download the script here.

Requirements:

  • MySQL with access to INFORMATION_SCHEMA database
  • php, php5-cli (for S3 backups)
  • python2, python-gdata (for Google Docs backups)

This is an updated version of my old Automatic MySQL Database Backup script that adds support for Google Docs (which now allows uploads of any file type for non-enterprise users), the recently opened Google Storage, as well as local HDD and FTP servers. It does a MySQL dump of each DB separately, zips them all and uploads the zip to your cloud service of choice.

I’ve also added in an experimental ‘db chunker’ which uses this script to split SQL dumps into convertible sizes for Google Docs, allowing you to upload unlimited backups for free; owever due to a reproducible bug in Google Docs, I wouldn’t recommend its use (and it’s disabled by default).

To get started simply update your details in the appropriate places at the top of backup.sh and type ./backup.sh. Presto!

I’ve also included a crontab example to allow automation of backups.

Read More »

Posted (Updated ) in Uncategorized

A while back Samsung released a line of relatively cheap “EcoGreen” 2TB hard drives. Being the storage hungry IT nerd that I am, I picked up 2 of the things before finding out their firmware contains a known data write bug requiring a firmware patch best summed up with the following:

“It [the drive] will “forget” to write out its buffer if certan SMART commands are sent to the drive. This is the very definition of silent data corruption.”
source

Samsung has released a patch but it requires booting into DOS which is incredibly difficult to do on todays floppyless PCs. After a bit of googling I came across the following two solutions to get the job done:

Bootable ISO image

This is the easier solution of the two but it is also the one that didn’t work for me. Michael has created a FreeDOS image with the patch merged in. For the download and installation details, see this post.

Bootable USB image

After trying and failing the above, I settled for a bootable USB image instead. User ZDOS has written up a handy tutorial here which I’ve detailed a bit more in depth below:

  • Download the Windows 98 system files (at the top) and HP USB Disk Storage Format Tool from here and F4EG.exe from here.
  • Install the HP Format Tool and run it with administrator privileges (right click – Run as Administrator).
  • Extract the Windows 98 system files.
  • With the HP Format Tool, format your USB drive (quick format should be enough) as Fat32 making sure you use the Windows 98 system files where it offers.
  • Once done, copy F4EG.exe to your USB drive.
  • Shutdown and unplug all HDDs besides your unpatched Samsung drive
  • Boot your PC and enter the BIOS by pressing Delete during startup. Ensure that your HDDs are set to run in AHCI mode.
  • Reboot either ensuring USB-HDD is selected as your top boot device or press F12 during startup to visit the One Time Boot Device page (results may vary – check your motherboard documentation).
  • You should see a Windows 98 logo flash on the screen for a split second and then a command prompt. In the prompt type F4EG and press enter. Your drive should flash itself.

For plenty more useful information on this issue, I found this forum thread very helpful.

Read More »

Posted (Updated ) in Linux

One of my last niggling issues with Ubuntu 11.04 is, as you’d expect, another remnant of Unity carried over to the Ubuntu Classic desktop. This remnant is the notification area (or ‘Systray’ to you Windows converts) not allowing all applications access to it. Below is the line you’ll need for bringing Ubuntu back to the 21st century:

gsettings set com.canonical.Unity.Panel systray-whitelist "['all']"

Thanks to the good fellows at Web UDP8 for this little piece of magic.

PS. It appears the VLC icon STILL does not always appear. There’s a question posted on the VLC forums about this issue and it looks as though it’s due to an incompatibility between QT4 and Gnome. Stay tuned for further developments on this issue.

Read More »

Posted (Updated ) in Uncategorized

It seems firefox 4 has stupidly removed the option to save tabs on exit however there’s a way to get this very useful functionality back. Here’s how:

  1. Type about:config in the URL/Location/Address bar and press the Enter key
  2. Accept the warning message (promise to be careful)
  3. The preferences page/list will open
  4. Filter = browser.tabs.warnOnClose
  5. If its value is set to false, double-click on it (in the lower panel) to toggle its value to true
  6. Repeat steps 4 and 5 for these following 3 preferences also:
    • browser.warnOnQuit
    • browser.warnOnRestart
    • browser.showQuitWarn

Taken from mozilla support. A big thank you to user Helper7677 for this solution!

Read More »

Posted (Updated ) in Linux

Note: Also see the followup to this post: How to Make Ubuntu 11.10 More Usable

So my previous post How to Move App Menus Back Into Their Windows in Ubuntu turned out a little more bitter than even I had first intended. As a result I think it’s only fair I give a few more reasons as to why I have such a strong dislike towards Unity.

Let me preface this post by saying that I was actually quite looking forward to Unity. It looked relatively promising (although I was slightly worried about reports it wasn’t as customizable as Gnome) and after I ended up liking icons on the left when thinking I wouldn’t, I was determined to give Unity a fair go. I would also like to say that many of these issues will probably be sorted out in later bug fixes and updates – but that does not help me right here right now on my shiny new Ubuntu install a few days after release.

So here it is: a ‘by no means complete’ list of reasons using Unity is not a good idea at this point in time.

Read More »

Posted (Updated ) in Uncategorized

Due to the antics of some douchebag (IP address 109.120.34.16) I’ve had to look into blocking pages based on IP address with .htaccess files. This may come in handy to others out there so I figured I’d write up my findings.

To block everyone except specific IPs here’s what you’ll need (more info):

order deny,allow
deny from all
allow from <ip address>

To apply this restriction only to specific files, wrap the above code in the <File> tag:

<Files wp-login.php>
	order deny,allow
	deny from all
	allow from <ip address>
</Files>

Thanks to user NuclearMeltdown on the #httpd channel on Freenode for his assistance.

Read More »

Posted (Updated ) in Linux

You’ve recently upgraded to Ubuntu Natty and discovered that Unity is crap. Don’t worry – you’re in the vast majority. One of the many issues with this disastrous attempt at a shell is that it moves application menus into a global menu in the top panel. This may be useful for small screens – but for the other 99% of the market it’s just downright frustrating.

After a bit of searching I came across this post describing how to banish the global menu back to the warped hell from whence it came. You want to remove the indicator-appmenu package.

This is but one of a great many great challenge you’ll face if you decide to continue on with Unity. Good luck…and may God have mercy on your soul should you choose to stick with it.

Read More »