Drupal syntaxhighlighter module

in

(Update: this module has been updated to D7.)

This Drupal module does program code list syntax highlighting like this:

class Person(ln : String, fn : String, s : Person)
{
    def lastName = ln;
    def firstName = fn;
    def spouse = s;
    
    def this(ln : String, fn : String) = { this(ln, fn, null); }

    def introduction() : String = 
        return "Hi, my name is " + firstName + " " + lastName +
            (if (spouse != null) " and this is my spouse, " + spouse.firstName + " " + spouse.lastName + "." 
             else ".");
}

It uses the Syntax Highlighter Javascript library to perform the highlighting on the client browser.

The module is at the Drupal Syntaxhighlighter module project page. Before installing the module, first download the latest Syntax Highlighter Javascript Library. Unzip the entire content into a sub-directory inside sites/all/libraries. You can use any directory name, in any level of nesting or even no directory. You can delete the 'src' directory from the extracted files if you want. It's not needed for normal usage. Then install the module. Refer to the module README.txt file for details.

Module configuration

Go to 'admin/settings/syntaxhighlighter' to make configuration change:

(The checkboxes and radio buttons are displayed in columns above with the Multi-column checkboxes radios module. Install and enable that module to get it display like it's here. Otherwise, the checkboxes and radio buttons are in single long column.)

The first set of checkboxes allow you to selectively enable languages. You may want to enable only the languages you need to avoid loading unnecessary Javascript files. However, choosing all should not be any problem in any case because browses cache Javascript files.

The theme radio buttons let you choose the stylesheet used by the Syntax highlighter display.

The "Tag name" setting lets you change the code markup tag to some other legal html tag if <pre> for some reason cannot be used. For example, you can use the <code> tag instead.

Enable "Legacy mode" to support old style markup format. For most users this should just be disabled. This is provided in case you have contents that use the old style Syntax highlighter markup.

Use the "Turn off Syntax Highlighter on these pages" setting to disable the Syntax highlighter to avoid compatibility problem.

The "Default expressions" setting let you enter Javascript statements to initialize the Syntax highlighter to some site wide defaults.

Visit the 'admin/help/syntaxhighlighter' page for module help.

Content markup

Markup your program listing like this:

{syntaxhighlighter OPTIONS}
// paste your code verbatim in between the markup
#include <iostream>
// etc etc etc
{/syntaxhighlighter}

where OPTIONS is a syntax highlighter options string. See the Syntax Highlighter Javascript library page for details. To highlight some C++ code:

{syntaxhighlighter brush: cpp}
// paste your code verbatim in between the markup
#include <iostream>
// etc etc etc
{/syntaxhighlighter}

and that becomes:

// paste your code verbatim in between the markup
#include <iostream>
// etc etc etc

Click to see a sample of all syntax highlighted languages.

TinyMCE Wysiwyg editor integration

This provides a Syntax Highlighter button:

that brings up the Syntax Highlighter dialog:

where you can insert code and configure the syntax highlighter property. You can see this in action on the comment form below.

See the bottom of this module's project page for detail on how to setup this.

The GeSHi Filter module also does syntax highlight. It is a server side filter, so processing is done using PHP inside Drupal on the web server. Whereas this module off loads the processing entirely to the client browser.

Demo: http://zen.hddigitalworks.com/syntax-highlighter

toggle line numbers missing ;(

elmuchacho's picture

works like described ...
ty ...
but the menu in the upper right corner on my site shows "view source","copy to clipboard","print" and "help" ...

"toggle line numbers" is missing ;(

actually i've installed version 2.1.364 of the SyntaxHighlighter ...

any ideas ?

I'm using a customized version

Alpha Epsilon Delta's picture

of syntax highlighter javascript library. The one from Alex doesn't have this feature.

Drupal Issue: wysiwyg

Visitor's picture

Hi I'm triying to install tinymce o fckeditor over Drupal 6 an d wysiwyg, the problem is that i cant see de toolbar in Chrome or IExplorer 8. I get a javascript error in wysiwyg.js.

So when I have arrive to your page I've seen that you have the same problem, i can't see the toobar in IExplorer and Chrome on your page. Do you know something about it?

Dan
(Sorry my disgusting English)

Please update to the latest version...

Alpha Epsilon Delta's picture

of syntaxhighlighter js lib  (2.1.364 as of today)

 

see http://drupal.org/node/652586

Thanks for that link, it

bed &amp; breakfast barcelona's picture

Thanks for that link, it helped me a lot.

Thank you very much for the

alışveriş's picture

Thank you very much for the excellent and useful subject.

<a

work and travel's picture

 <a href="http://www.workandtravel.net" title="Work And Travel">Work And Travel</a>

work and travel

work and travel's picture

Work and Travel ile amerikaya gidebilirsiniz.

i want add the syntaxhighlighter button to CKEditor.

song's picture

I want known , if i want add the syntaxhighlighter button to CKEditor. how can i do?

Your blog is a great one.

Cell Phone Spy's picture

Your blog is a great one. What really impresses me is that you are correctly mentioned that there are thousands of tools that are available to create a website or launch one but what matters is that you choose the right one, the one that gives you all that is actually needed Well good to read about this thanks.

Using syntax highlighter?

Jonathan's picture

 

Hi!

Thank you for your post.

I am trying to use syntax highlighter with fckeditor.

First, I'm wondering why the configuration says that I can use the PRE tag, and then in your example you use the tag {syntaxhighlighter}.

Second, whenever I try to use {syntaxhighlighter}, I get the following message:

{syntaxhighlighter} tags are not balanced: open and close tags must
match.

This is for the code:

 

if ( this_is_working() ) { echo "Test" } 

Do you know why this is?

 

~

Alpha Epsilon Delta's picture

>First, I'm wondering why the configuration says that I can use the
PRE tag, and then in your example you use the tag {syntaxhighlighter}.

The PRE tag can be use if you use the Full HTML input format, but it won't work if you use Filter HTML format. Since you will never let regular users have the Full HTML input format because that would make your side completely unsafe. So you always use some strict input format like Filtered HTML format or WYSIWYG input fomat.  However, the PRE tag is what the Syntaxhighlighter library use inside and ultimately in the end, that's what ends up in the web page when it's all said and done. Its format is something like this:

<pre class="brush:cpp;gutter:false;">

           code here...

</pre>

But the class attribute format is non-standard and the Filter HTML format will strip it out, thus preventing the syntaxhighlighter from working. The solution to this problem is the {syntaxhighlighter} filter format.

>Second, whenever I try to use {syntaxhighlighter}, I get the following message:

>{syntaxhighlighter} tags are not balanced: open and close tags must match.

You need to close your code block with

{/syntaxhighlighter}

Hi, I am wondering if you

bmalen's picture

Hi, I am wondering if you have encountered this problem. In Firefox 3.6/Windows, despite the highlighter container having overflow:auto !important, it does show a scrollbar for a long, single line of code.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

if($k=="kuldeep") { print

Friend's picture

if($k=="kuldeep") { print "Hijack"; }

 

 class Person(ln : String, fn

Friend's picture

 
class Person(ln : String, fn : String, s : Person) { def lastName = ln; def firstName = fn; def spouse = s; def this(ln : String, fn : String) = { this(ln, fn, null); } def introduction() : String = return "Hi, my name is " + firstName + " " + lastName + (if (spouse != null) " and this is my spouse, " + spouse.firstName + " " + spouse.lastName + "." else "."); }

 

Would it be okay with you if

Johnson's picture

Would it be okay with you if I linked to this page from my website? Just asking since some people don't allow linking to their sites if you don't take their permission.

Engineers who would like to have careers in management positions should seriously think about getting the PMP certification so that they can learn how to manage projects effectively. Getting the certification is a matter of passing the PMP exam which can be done with a bit of online PMP certification training.

nice post

Cell spyware's picture

This is a really good read for me. Must admit that you are one of the best bloggers I have ever read. Thanks for posting this informative article.

I am happy to find your

Clubmz e-spy's picture

I am happy to find your distinguished way of writing the post. Now you make it easy for me to understand and implement the concept. Thank you for the post.

payroll manager

payroll manager's picture

i can't see the toobar in IExplorer and Chrome on your page. Do you know something about it?

interesting post

Foil Insulation 's picture

This is a very informative article.I was looking for these things and here I found it. I am doing a project and this information is very useful me. If you are interested, but this is my duty to inform you that virtual administrative assistant a very dedicated service and can be applied anywhere you want and get results.

payroll companies

payroll companies's picture

this comp-any is very good.

loan payoff calculator

loan payoff calculator's picture

but this is my duty to inform you that virtual administrative assistant a very dedicated service and can be applied anywhere you want and get results

hello

slideshare.net's picture

When I upgraded zugec.com to Drupal 7 I had to find another syntax highlighter because the previous one that I used in Drupal 6 was GeSHi but this module hadn't been upgraded to Drupal 7 at the time. I searched around and found Syntax Highlighter module which uses the SyntaxHighlighter library.

<div class="form-item"

Friend's picture

<div class="form-item" id="edit-meta-type-forum-issues-wrapper"> <label class="option" for="edit-meta-type-forum-issues"><input type="radio" id="edit-meta-type-forum-issues" name="meta_type" value="forum-issues" class="form-radio" /> Forums & Issues</label> </div> <div class="form-item" id="edit-meta-type-group-wrapper"> <label class="option" for="edit-meta-type-group"><input type="radio" id="edit-meta-type-group" name="meta_type" value="group" class="form-radio" /> Groups</label> </div> </div></fieldset>

 

Thanks for the great tutorial

2 wired 2 tired's picture

Thanks for the great tutorial - a syntax highlighter makes code much easier to read. I had problems using this filter option with CKEditor so have disabled CKEDitor (actually, I created a new 'code' filter for blog posts with code in them) and now it works great.

 

I had a similar issue where

Motorcycle injury lawyer's picture

I had a similar issue where br tags were getting added to every line in the highlighted code. You'll have to experiment with the "Filter processing order".

Note that you must install in

nicobo's picture

Note that you must install in sites/all/libraries/syntaxhighlighter, not sites/all/libraries/syntaxhighlighter_3.0.83 as it is unzipped by default...

Yes you can use "syntaxhighlighter_3.0.83"

Alpha Epsilon Delta's picture

You can use any directory name. The module auto search and discover where the js lib is. The only directory name you cannot use is "src".

Hello its really one of the

alcoholism rehab's picture

Hello its really one of the nice blog that you have shared here.I was actually one of the similar issues like that here.Thanks a lot for the great posting here.

 

  I suffered the same issue

fire pits's picture

 

I suffered the same issue into the highlighted code and, even if I'm pretty sure there is something better to do after rewriting the SH and the Drupal Core code concerning the filters' rules, a quick solution is to simply remove the "Convert line breaks into HTML" filter in your filters configurations. Your post is good. 

 

//process.php starts here <?

Friend's picture

//process.php starts here <? $username="username"; $password="password"; $database="your_database"; $first=$_POST['first']; $last=$_POST['last']; $email=$_POST['email']; $web=$_POST['web']; $comment=$_POST['comment']; mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $query = "INSERT INTO guestbook VALUES ('','$first','$last','$email','$web','$comment')"; mysql_query($query); mysql_close(); ?> //End of Process.php //start of index.php <html> <head> <title>Guest Book</title> </head> <body> <form action="process.php" method="post"> First Name: <input type="text" name="first"><br> Last Name: <input type="text" name="last"><br> E-mail: <input type="text" name="email"><br> Web: <input type="text" name="web"><br> Comments: <textarea rows="6" name="comment" cols="27"></textarea> <input type="Submit"> </form> </body> </html> //end of index.php //start of view.php <? $username="username"; $password="password"; $database="your_database"; mysql_connect(localhost,$username,$password); mysql_select_db($database) or die( "Unable to select database"); $row1 = mysql_query("SELECT * FROM guestbook") while($row=mysql_fetch_array($row1)) { echo "$row[first_name] $row[last_name] Said<br><hr>"; echo "$row[comments]<br><hr>"; echo "You can view $row[first_name]'s website at $row[www]"; } ?> //end of view.php

 

look above,all the codes are into one line,why

I'm looking to set up an sms

sms blast's picture

I'm looking to set up an sms framework, and am trying to figure out how to do that with Drupall 6. I'd like to make it as customizable as possible, but so far can only find modules that create the link between site-email-mobil, but no way to manipulate the syntax of "look". Is there some connection I'm missing, or am I trying to find a solution to a problem that doesnt exist. I am new to communication through sms, and am ultimately hoping to find out if sms blast is a viable marketing strategy for my business. Last question, should I upgrade from Drupal 6 even if that is the only version I've used thus far? I just don't want to be behind the learning curve or have to spend too much time playing catch up.

Most websites can help you with your ISK issues

stop snoring's picture
I must say, useful information for users especailly for website designers.

Thanks

jayendra's picture

Thanks it will be very helpful.

Your blog is so rich with

Find Name By Phone Number's picture

Your blog is so rich with nice content.I think you have done a great job with your blogs.Why don't you add a translator so that people from other countries can read this awesome blog of yours.

function myFunction() {

Friend's picture

function myFunction() { //declares a function, this is named myFunction return 'John Doe'; //returns the value 'John Doe' } echo 'My name is ' . myFunction() . '!'; //outputs the text and the return variable of the //myFunction, the function is also called //the result of the output will be 'My name is John Doe!'

class Person { public

Friend's picture

class Person { public $firstName; public $lastName; public function __construct($firstName, $lastName = '') { //Optional parameter $this->firstName = $firstName; $this->lastName = $lastName; } public function greet() { return "Hello, my name is " . $this->firstName . " " . $this->lastName . "."; } public static function staticGreet($firstName, $lastName) { return "Hello, my name is " . $firstName . " " . $lastName . "."; } } $he = new Person('John', 'Smith'); $she = new Person('Sally', 'Davis'); $other = new Person('Joe'); echo $he->greet(); // prints "Hello, my name is John Smith." echo '<br />'; echo $she->greet(); // prints "Hello, my name is Sally Davis." echo '<br />'; echo $other->greet(); // prints "Hello, my name is Joe ." echo '<br />'; echo Person::staticGreet('Jane', 'Doe'); // prints "Hello, my name is Jane Doe." 

Test

Friend's picture

Hi this is java code
 
package com.scientific.calculator; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.TextView; public class CustomCalculatorActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); }
 
 

 

What really impresses me is

allopurinol 300 mg's picture

What really impresses me is that you are correctly mentioned that there are thousands of tools that are available to create a website or launch one but what matters is that you choose the right one, the one that gives you all that is actually needed Well good to read about this thanks.

I had problems using this

what is citalopram's picture

I had problems using this filter option with CKEditor so have disabled CKEDitor (actually, I created a new 'code' filter for blog posts with code in them) and now it works great.

SELECT * FROM data WHERE

Friend's picture

SELECT * FROM data WHERE ts<@ts

 

SELECT * FROM data WHERE ts<@ts

Post new comment

The content of this field is kept private and will not be shown publicly.

Navigation

User login