"Ensuring Your Web Presence Contributes"Javascript code filter for Drupal![]()
Posted by admin on Thu 26 Jun 2008
Here is an explanation of how I wrote a Drupal5.n module to filter javascript code so that it displays syntax highlighted. To illustrate what it does:- /*If the code is just a single line it will display without the background div:
The filter system within Drupal is intended to make data entry into the CMS easy and safe. URL's can become clickable and malicious HTML (and rude words) can be stripped out. This js_filter module, based on the contributed codefilter module by John Wilkins, will syntax highlight Javascript code. There are already ways to do this within Drupal, the geshifilter module which uses the third-party GeSHi PHP library will syntax highlight many languages and there is also a jQuery plugin, jquery-chili-js which uses "recipes" for several languages. js_filter has no software dependencies, it is purely PHP module code. Input formats in Drupal consist of various filters. This post uses an input format I created called "Code". It uses three filters shown on the "Arrange" screen:- Of course you can include the js_filter on any input format. Setting colorsThere are only five aspects of javascript code highlighted:
The colors are set within the module's css file. span.js_comment {Symbols are not colored but displayed bold using the <strong> tag. Obviously altering the colors only requires changing the css. Writing the moduleAs there are no database tables required, there is no need for an install file, only an info file and the module. The info file looks like this: ; $Id: js_filter.info,v 1.1 2008/06/05 12:13:44 gpr Exp $Assuming you know how to create the basics of a Drupal module, I will just cover the essentials of filtering. Lets start with hook_filter(). <?php"List" and "description" don't require any explanation, however "no cache" is really useful when debugging. Normally this is set to false as Drupal caches all filtered text to save time, but having it set to true means that for every test, you know that the filter code is going to be executed instead of the result being retrieved from the cache. It is tedious to have to delete the cache_filter table records before every test. The actual filtering is done in two stages, "prepare" and "process". The prepare step converts HTML to entities, preventing any tags from being interpreted incorrectly by following filters. The process step does the text massaging. In this filter we want to color various parts of the code, so we need to parse the code to identify the bits to be colored, then wrap them inside <span> tags with a class of the appropriate type. The function which does this is js_filter_process(); <?phpNow most of this is self explanatory, the line <?php<?phpThe function _js_filter_lexer($s) is called repeatedly returning each token found. The $output string is built containing the coloring span tags according to the token. The lexer calls other smaller functions which are all pretty standard lexical scanning stuff. The complete module code (.css, .info and .module) is downloadable here.
Category(s)
|
Glastonbury had the usual mix of celebrities enjoying the hot weather and oozing festival cool. Ray Ban Wayfarer sunglasses are still incredibly popular, http://www.sale-sunglasses.net/ED_Hardy_Sunglasses.html with the likes of designer Henry Holland, T4 presenter Jameela Jamil and actresses Kate Hudson and http://www.sale-sunglasses.net/Ray.Ban_Sunglasses.html Emma Watson all seen wearing them.
Glastonbury had the usual mix of celebrities enjoying the hot weather and oozing festival cool. Ray Ban Wayfarer sunglasses are still incredibly popular, ED Hardy Sunglasses with the likes of designer Henry Holland, T4 presenter Jameela Jamil and actresses Kate Hudson and Ray Ban Sunglasses Emma Watson all seen wearing them.
I'm interested in this module because of the input filter difficulties I've been having getting various editors to work. However, I am not a programmer. Is this module's current state such that someone such as myself would be able to set up and use this effectively or is it still in the testing stages?
Not in testing stage. It is not an input filter. It is an output filter that will display content between javascript tags syntax highlighted. Hope that helps.
PS: links are filtered out of these comments!
The javascript External Links module was mostly a proof of concept when it started. But now I’ve grown to like it so much that I use it all the time. It gets points for ease of use, but it’s good to see there’s an efficient alternative for a site serious about it’s external linking.
Post new comment