Syntax Highlighter Plugin for Zend Framework
I'm a big fan of Alex Gorbatchev's javascript-based Syntax Highlighter. With this plugin for Zend Framework, it's easy to add syntax highlighing to any page and only load the css required for the languages being highlighted.
The nice thing about the syntax highlighter is that it supports various "brushes" for different languages. I tend to use the PHP, Javascript, and SQL brushes often, but many pages do not need any syntax highlighting at all.
I wrote this plugin to detect which brushes I need and only load the javascript / css as necessary. If I have a page that doesn't need any syntax highlighting, this plugin prevents my server from serving javascript / css that isn't required.
To use this plugin in your own ZF-powered website, you'll need to copy the plugin into your site's plugins directory and rename the class according to your site's specifications. Then you need to install the javascript library in public/library/syntaxhighlighter. Finally, register the plugin with your front controller.
Source
<?php
/**
* Enables syntax highlighting for code blocks.
*
* @uses Zend
* @uses _Controller_Plugin_Abstract
* @author Hector Virgen <hector@virgentech.com>
* @copyright Copyright (c) 2009-2010, Virgen Technologies
* @category Application
* @package Plugins
*/
class Default_Plugin_SyntaxHighlighter extends Zend_Controller_Plugin_Abstract
{
/**
* Scans the response for code syntax and adds necessary javascript/css
* to enable syntax highlighting.
*
* @param Zend_Controller_Request_Abstract $request
*/
public function postDispatch(Zend_Controller_Request_Abstract $request)
{
if (!$request->isDispatched()) {
return;
}
// Prevent this plugin from running again.
Zend_Controller_Front::getInstance()->unregisterPlugin($this);
// Search the response for code syntax blocks.
$response = $this->getResponse();
$html = $response->getBody();
$dom = new Zend_Dom_Query($html);
$results = $dom->query('pre[class*="brush"], script[type="syntaxhighlighter"]');
if (!count($results)) return;
// Determine which brushes (languages) are used.
$brushes = array();
$regex = '/brush:[\s]*([a-z]+)/';
foreach ($results as $result) {
$class = $result->getAttribute('class');
if (!$class) continue;
$matches = array();
preg_match_all($regex, $class, $matches);
if (isset($matches[1][0])) {
$brush = $matches[1][0];
if ($brush == 'js' OR $brush == 'javascript') {
$brush = 'jScript';
}
$brushes[$brush] = ucfirst($brush);
}
}
// Return early if no brushes were found.
if (empty($brushes)) return;
// Get view to add CSS and JS
$view = Zend_Controller_Front::getInstance()
->getParam('bootstrap')
->getResource('view');
// Add CSS
$headLink = $view->getHelper('headLink');
$headLink->appendStylesheet('/library/syntaxhighlighter/styles/shCore.css');
$headLink->appendStylesheet('/library/syntaxhighlighter/styles/shThemeVirgen.css');
// Add JS
$headScript = $view->getHelper('headScript');
$headScript->appendFile("/library/syntaxhighlighter/scripts/shCore.js"); // core
$headScript->appendFile("/library/syntaxhighlighter/scripts/shBrushXml.js"); // required
foreach ($brushes as $brush) {
$headScript->appendFile("/library/syntaxhighlighter/scripts/shBrush{$brush}.js");
}
$headScript->appendScript("
jQuery(function($)
{
SyntaxHighlighter.config.clipboardSwf = '/library/syntaxhighlighter/scripts/clipboard.swf';
SyntaxHighlighter.all();
});
");
}
}
