Playing with Crayon

With the rebirth of Binary Templar, I knew I would need a way to publish code to my posts. Syntax highlighting was obviously a must, and ease of use was very far and away at the top of my list. I tested a few plugins, most based on GeSHi, before stumbling upon Crayon.

Crayon has a TON of features but boils down to a plugin that does its job and does it damn well. Once I played around with the numerous options for defaults, I finally got things to where adding code to a post is a single mouse click away inside the Visual Editor.

// This just echoes the chosen line, we'll position it later
function hello_dolly() {
	$chosen = hello_dolly_get_lyric();
	echo "<p id='dolly'>$chosen</p>";
}

// Now we set that function up to execute when the admin_notices action is called
add_action( 'admin_notices', 'hello_dolly' );

I can customize the look and feel if I want on a per-use basis, but why mess with a good thing? Out of the box it supports line numbers, syntax highlighting (there are easily 20 themes to choose from, just like most development IDE’s, and you have the ability to quickly create your own as well), copy abilities – I’m having a hard time finding features that it doesn’t have. Oh, and it even has the ability to highlight particular lines if you wish, as well.

// We need some CSS to position the paragraph
function dolly_css() {
	// This makes sure that the positioning is also good for right-to-left languages
	$x = is_rtl() ? 'left' : 'right';

	echo "
	<style type='text/css'>
	#dolly {
		float: $x;
		padding-$x: 15px;
		padding-top: 5px;		
		margin: 0;
		font-size: 11px;
	}
	</style>
	";
}

add_action( 'admin_head', 'dolly_css' );

I’m looking forward to playing with Crayon more as I continue, but if anyone out there has other suggestions for a plugin that they would recommend as an alternative then leave a note in the comments!

 

Leave a Reply