Drupal + WYSIWYG + tinyMCE + syntaxhighlighter REDUX
The most popular post on my site currently is an article on how to setup the tinyMCE editor in Drupal to use a syntax highlighting plugin.
Since that article, the Drupal world has moved along and my tutorial has become outdated.
Things are actually a bit easier now, however, so read on for a new version of the tutorial.
These instructions are written for the latest version of Drupal 6 ( 6.14 at the time of this writing ) but should work on a reasonably current version. There appear to be many steps, but essentially all we are doing is:
- Install wysiwyg module
- install tinyMCE library
- install syntaxhighlighter module
- install wysiwyg_preelementfix module
- install wysiwyg_syntaxhl module
- enable plugins and editor for a Input format
No editing of code is required anymore. So let's get one with it!
Get wysiwyg
Get the wysiwyg module at http://drupal.org/project/wysiwyg and follow the install instructions. Basically you're going to put it in sites/all/modules and enable it on the modules admin page.
Install tinyMCE
Go get a current version of tinyMCE ( I used the 3.2.7 main package ) from their download page. Install it in sites/all/libraries, which is the new location to install third party libraries.
You can also find this install information by visiting the drupal wysiwyg settings page on your site at admin/settings/wysiwyg. Under the 'Installation instructions' section are descriptions on how to install various editors.
Make sure that after you install tinyMCE in the libraries directory, you see the proper version in the 'Installation instructions' section on the wysiwyg settings page. It should say 'Installed' next to tinyMCE and give you the version number.
Edit Full HTML Input format
Some of these steps may not be necessary, but they worked for me. YMMV here. I edited my 'Full HTML' (at admin/settings/filters) input filter and turned off all filters ( except a filter I use to fight spam ). My filter looks like this:

The 'Hide Email addresses' is provided by the spamspan module, and is not necessary for this to work.
Install syntaxhighlighter module
Although you could get the stand-alone version, there is a drupal module for this library. You still will have to get the actual syntaxhighlighter javascript library though!
- Go get the module at their drupal.org project page.
- Unpack it in the usual location ( sites/all/modules or equivalent )
- read the README.txt file that comes with it. It tells you how to install the javascript library (not included), but essentially, you do this:
- Get the syntaxhighlighter library. Download it from http://alexgorbatchev.com/wiki/SyntaxHighlighter:Download
- Put in your libraries directory sites/all/libraries. It can be installed in sites/all/libraries/syntaxhighlighter.
- Enable the module and then visit admin/settings/syntaxhighlighter
- Choose the languages you wish to support
- Choose your theme ( I am using the default theme on my pages )
- Ensure that the tag name is set to 'pre'
- You can disable Legacy mode
- Save the settings
Get wysiwyg_preelementfix module
Apparently, tinyMCE doesn't handle <pre> elements gracefully, so there is a tinyMCE plugin to get it to work right. This plugin has been rolled up into a drupal module. Go and get the module from their project page (or install with drush or whatever) and put it in sites/all/modules.
Read the INSTALL.txt that comes with the module. It will describe how to go and get the javascript component and where to put it. Basically, you get the package from sourceforge and put it in sites/all/modules/wysiwyg_preelementfix/tinymce.
Personally, I think it should go in your libraries directory, but for the time being, do what the install doc says until the package is updated to specify otherwise.
Now just enable the module from your modules admin page.
Get wysiwyg_syntaxhl module
This module is similar to the wysiwyg_preelementfix module in that it simply exists to add a tinyMCE plugin and uses the wysiwyg module api to do it.
You can get it on its Drupal project page. Put in the normal location enable it.
BTW, I am the author of this module, so please create a an issue on the drupal project page if you have any problems.
Configure wysiwyg profile
Go back to your admin/settings/wysiwyg page. Enable the tinyMCE editor on one of your input formats. I recommend using the Full HTML filter. Click 'Edit' to configure tinyMCE. Now under buttons and plugins you should see the Syntax Highlighter option. Make sure it is selected. Additionally, my Cleanup and Output section looks like this:

Conclusion
You should now be able to insert code into your tinyMCE textareas. Note: During editing, you will only see a preformatted text box. You won't see a proper code box until you save and view the node.





Comments
Syntaxhighlighter js lib install locations
>It can be installed in sites/all/libraries/syntaxhighlighter.
This is correct but actually you can name the directory to anything and the syntaxhighlighter module will find it. For example, I use something like this:
sites/all/libraries/syntaxhighlighter_2.0.320
so I know exactly what's the version number just by looking at the directory name.
The syntaxhighlighter js lib can be installed in several locations:
1. inside the syntaxhighlight module directory
2. sites/example.com/files/
3. sites/all/libraries
The reason it goes in this order is so you can have different versions of the syntaxhighlighter js lib per site.
right
Good point.
I've done all stuff but still
I've done all stuff but still get {syntaxhighlighter brush: cpp;fontsize: 100; first-line: 1; }/** some text */ {/syntaxhighlighter} instead of syntaxhighlighter box.
Should I get pre tag in code instead of this?
No, that is what you should
No, that is what you should get. The syntaxhighlighter module should do the work of replacing the {} tags with a syntaxhighlight-ed box. Look on your input formats and see if there is a section like this on your 'Full HTML' (or whatever format you are using for your wysiwyg profile) configuration:
Make sure it is checked. I may need to update my lesson to account for that.
some errors
Thanks for you contribution.
I have a problem after I did as your instructions, there is no linefeed when i edit code via the syntax highlighter, what the problem is that?
Thanks
Do you mean you can't get a
Do you mean you can't get a newline character when editing code in the syntax highlighter popup window...so ll text you enter is on one line?
What browser are you using and what version of tinyMCE and syntaxhighlighter and wysiwyg_syntaxhl are you using?
Thanks for your reply. Yes,
Thanks for your reply.
Yes, there is no newline character in my editor.
When i have code in syntax highlighter done, then I get back to tinyMCE, and everything seems good, but when i click submit, the newline characters are gone.
here are my installed modules,
Syntax highlighter 6.x-1.19
Wysiwyg 6.x-2.0
Wysiwyg Pre Element Fix 6.x-1.0-beta1
Wysiwyg SyntaxHighlighter Plugin 6.x-1.2
IMCE Wysiwyg API bridge 6.x-1.0
Just to be complete, what
Just to be complete, what version of tinymce do you have and what browser are you testing this in?
When you say "Yes, there is no newline character in my editor" do you mean the tinyMCE editor or the syntaxhighlighter popup window?
I did experience a problem similar to this on Internet explorer in windows. When I clicked the highlight button, the popup showed up, I pasted my code in it (no problems), clicked the button.
The code appeared in my tinymce as a 'pre' element, with proper linefeeds and whitespace (no problems). Then I saved my node, but IE stripped out all the newlines (problem). The syntax highlighter box would appear around my code, but it would be all run together without newlines or tabs.
I eventually tracked this down to the IE setInnerHTML commands but couldn't work around it. In the latest version, then, I do a check to see if the browser is IE. If it is IE, I skip the step where it converts the 'pre' tags to the {} (macro) tags. This makes IE leave the 'pre' tags alone.
This worked for me, but maybe there is still a problem with some browsers. Are you using Opera? Or Chrome maybe? What OS are you on?
the behavior in my editor is
the behavior in my editor is same as your said.
When i edit them in the pop window or tinyMCE, they both look well, but when i save it, the linefeeds are gone.
Another issue is when I click 'disable rich-text', and click 'enable rich-text' to back tinyMCE, all the linefeeds have gone.
TinyMCE 3.2.7
FireFox 3.5.6
Thanks very much for your help!
Gotcha, I'll check into it
Gotcha, I'll check into it and get back to you.
I also face the same problem.
I also face the same problem. Any help would be appreciated.
Same problem here...
Same problem here...
The same problem here
Drupal 6.15
Wysiwyg 6.x-2.0
Wysiwyg Pre Element Fix 6.x-1.0-beta1
Wysiwyg SyntaxHighlighter Plugin 6.x-1.2
Syntax highlighter 6.x-1.20
TinyMCE 3.2.7
Firefox 3.5.7
1. "Insert code using Syntaxhighlighter" button and write 3 lines of code
2. Looks good in editor
3. Save
4. Looks good on page
5. Edit the same page
6. Looks good in editor
7. Do nothing and save
8. Lines collapses into one line
Works now
I reinstalled Drupal and started with your howto as first step. Now it works. So maybe some module I had before was conflicting.
I dont think it would be
I dont think it would be possible for a production site to do a complete format. So, does anyone know if it is really a module conflict, and if yes, with which module?
My suspicion is that it has
My suspicion is that it has something to do with the input filters for those particular users. At any rate, the next version will have the option to leave 'pre' tags as 'pre' tags and not reformat them automatically to the '{}' macro style tags.
The only reason I even made that option was because some people use a module called 'wysiywg filter' which has an insane number of options in it to strip out, or allow, all kinds of tags and attributes, and that module barfs on syntaxhlighter pre tags, because it doesn't recognize the brush styles in the style attribute and strips them out.
Stay tuned, I'll have the new version in a couple days.
Resolved: Same issue (code in one line)
JFYI: http://drupal.org/node/699968
Problem is solved by unchecking "Remove linebreaks" in TinyMCE profile at /admin/settings/wysiwyg
For me, disabling "Convert
For me, disabling "Convert line breaks to html" setting on the input format, and then disabling "Remove linebreaks" in the TinyMCE profile together made it work.
~
>there is no button for code/syntax_higlight
You need to go to admin/settings/wysiwyg, edit the profile(s) to enable the Syntaxhighlighter button. If you don't do this, you won't see the button.
Additonally, it *very IMPORTANT* to 1) enable the Syntax Highlighter filter in your input format. and 2) make sure to re-arrange the filters to have the Syntax Highlighter filter *after* the HTML filter or the WYSIWYG filter.
The WYSIWYG filter is the only way to have the full range of HTML tags and class/style/id and properties needed by and still keep things safe.
Works perfectly now
Thank you very much! This works perfectly for me now.
I did get caught out at first, so here are some tips for other people:
1) Make sure you have selected the 'Syntax Highlighter' option in your Administer > Site configuration > Input formats > Full HTML (or whatever format you're using) settings.
2) Make sure you don't have 'Remove linebreaks' option selected in your admin/settings/wysiwyg/profile (TinyMCE profile for your selected format) settings. This option is grouped under the 'Cleanup and output' fieldset.
Victoria
Hi, with Wysiwyg Pre Element
Hi,
with Wysiwyg Pre Element Fix 6.x-1.0-beta1 you have to install that js: preelementfix-0.3
because with the version preelementfix-0.4b2 you will have the "no newline character in the editor" bug with that verison disabling "Convert line breaks to html" setting on the input format, and then disabling "Remove linebreaks" in the TinyMCE profile don't solve the bug
I use 0.3 and still have 'no
I use 0.3 and still have 'no new line' bug - know what can happening ?
Tanx
Works like a charm! Without your guide I would never have got it going, this was just what I was looking for! Tanx!!
TinyMCE integration does not work!
I tried out this guide (along with the suggestions in the comment section), but all in vain. I didn't work for me. I can see the tinymce editor but there is no button for code/syntax_higlight. I'm using php 5.3.1, drupal 6.16, tinymce 3.3.1, wysiwyg 6.x-2.1, wysiwyg_preelementfix 6.x-1.0-beta1, wysiwyg_syntaxhl 6.x-1.2.
tinymce was gone after I installed wysiwyg_syntaxhl 6.x-1.2
I followed all the steps but when I went back to create new content the buttons of the editor were gone.
any idea where >I need to be looking to fix this.
any help is much appreciated.
thanks in advance
Marco
Remember to enable the "Pre
Remember to enable the "Pre element fix" plugin in the WYSIWYG settings admin/settings/wysiwyg for your input format.
This tutorial works just fine
This tutorial works just fine for me, but I have one little problem. I do not know how to change the font size of the code blocks I insert into the node. Changing the font size percentage in syntaxhighliter window does'n work, the size is always constant and is equal to the size of the node's body text. I want it to be smaller. Do you have any thoughts on how to do this?
Thanks ahead.
thank you very much for this
thank you very much for this tutorial.
works very well!
It Worked for me
Thank you for nice contribution!
Worked for my problem!
Had some problems when initially installing it the first time. However, on looking through the instructions given I was able to install two modules that were missing perfectly and now they are working fine. These were modules that were not installed
Syntax highlighter 6.x-1.19
Wysiwyg 6.x-2.0
This tutorial is not working for drupal 7
Hi Clifford,
I went through the complete tutorial and found that many of the modules mentioned in the tutorial are not available for Drupal-7.
1. wysiwyg_preelementfix
2. wysiwyg_syntaxhl
Could you please update the tutorial so that Drupal-7 user can also follow it.
Thanks
Abhijeet
All the code is printed in one line!
Thanks for such a detailed post, I've pretty much got this working apart from one thing.
When I insert the code using tinyMCE it looks properly formatted in the editor, when I hit save and view the page it all ends up in one long line. When I go back and edit it, the text has lost it's formatting and again is in one long line.
If I write some code within a tag without using tinyMCE, when it's saved it works perfectly. However if I then go back and switch to tinyMCE the tag and anything within it is replaced with with a single space ( ).
I've found a few people on the drupal.org website with the same problem and have tried solutions that have worked for them with no success.
Any suggestions? I'm pulling my hair out with this!
Hi there, I just followed all
Hi there,
I just followed all instructions here but got the problem of line breaks being removed from whole code block.
I experienced these:
entering a code inside syntaxhilighter pop up works well and line breaks are there after going back to tinyMCE and even by clicking on HTML button afterwards, I see the line breaks there. But as soon as disabling rich-text, the pre block is converted to {syntax...} block and line breaks are gone. so I think it is a problem in converting pre block back to syntaxhighlighter.
I have a bunch of data on website and reinstalling the whole site is not an option for me.
thanks in advance for any suggestions.
Hello I try it - but
Hello
I try it - but something going wrong - when I click Submit button on node edit - richedit switch to plain and broke the code to one line.
I looking for reason why for several hours but can't found what the hell is that.
Maybe You know that problem ?
P.S. of course any needed modules/filters are instaled and configured
Post new comment