Adding SunlightJS Syntax Highlighter to Drupal 7

Curiously looking for a decent, extensible syntax highlighting package to add to this blog I stumbled across sunlightjs which supports colorschemes, language definitions and gracefully degrades.

Simply extracting the sunlightjs sourcecode archive to your Drupal /themes folder and applying the following additions to your theme.info file will essentially make this package work on your drupal site.

stylesheets[all][] = sunlight-1.8.341/themes/sunlight.dark.css
scripts[] = sunlight-1.8.341/sunlight-min.js
scripts[] = sunlight-1.8.341/lang/sunlight.bash-min.js
scripts[] = sunlight-1.8.341/lang/sunlight.javascript-min.js
scripts[] = sunlight-1.8.341/lang/sunlight.mysql-min.js
scripts[] = sunlight-1.8.341/lang/sunlight.nginx-min.js
scripts[] = sunlight-1.8.341/lang/sunlight.php-min.js
scripts[] = sunlight-1.8.341/lang/sunlight.ruby-min.js

Lastly, you must activate sunlightjs for the content in question. For this site I essentially want syntax highlighting within the blog section of the site. So we'll write a simple module function for davidgurba.com that inserts the activation javascript from the sunlightjs documentation page for all blog pages.

/** implementation of hook_node_view() */
function dgcom_node_view($node, $view_mode, $langcode) {
  if ($node->type === 'blog_entry') {
    drupal_add_js('Sunlight.highlightAll();',
        array('type' => 'inline', 'scope' => 'footer', 'weight' => 5)
    );
  }
}

This blog post is an example of "eating your own dog food", eg this post utilizes all the code discussed herein.

2011 © Copyright David Gurba метр
Powered by Drupal