A few weeks ago I set out to redesign my blog, and in the process, one thing I wanted to accomplish was creating a way to present code in a “pretty print” kind of way on the webpage. My 4 requirements were:
- monospace font
- syntax highlighting
- include line numbers as needed
- optionally be valid html and semantically correct
Luckily for me I found a pretty sweet wordpress plugin, highlight source pro, built around the GeSHI library which did most of these things for me. I thought it was pretty cool that it used a wordpress filter to transform the code upon request, rather than transforming prior to db insert. However there were a couple downsides I saw:
- GeSHI mangles the html so badly that it is no where close to semantically correct.
- It did this for both the webpage and syndication feed, but I only wanted the webpage.
There isn’t a lot I can do about number one, unless I really want to dig into the GeSHI library, and since it validates I’ve decided to leave it alone for now. However it was pretty easy to solve number two and here is how I did that.
First, you need to know that highlight source pro plugin uses a wordpress filter on ‘the_content‘ to transform your code markup into it’s “pretty print” format for display. The function for this filter is called hsp_clean_and_codify() and is located in the file: wordpress/wp-content/plugins/highlight-source-pro/highlight_source_pro.php.
So, then open wordpress/wp-content/plugins/highlight-source-pro/highlight_source_pro.php and locate the hsp_clean_and_codify() function. Then you want to modify the opening lines of the function to check if the request’s query string includes ‘feed=’, if so return and avoid transforming the html markup into the GeSHI pretty print format. ie:
-
function hsp_clean_and_codify($subject)
-
{
-
// do not want the funky markup in the rss feed, its not pretty.
-
// so if the request contains a 'feed=' in the query string then
-
// ignore it
-
if(preg_match("/feed=/", $_SERVER['QUERY_STRING'])){
-
return $subject;
-
}
-
-
// original code below here
-
-
$original= $subject;
Now, anytime a request comes for a feed, the response will not include the GeSHI generated markup.
In the future it would be nice to see wordpress include separate filters for ‘the_content’ and perhaps ‘the_feed_content’ or possibly add an argument to the add_filter function which specifies what type of requests this filter should be applied on.
