UPDATE: Support for Syntax Highlighter 2.0 + jQuery Listed + bitbucket Repository
The Syntaxhighlighter by Alex Gorbatchev is the best I have seen so far. As you see in this post, wordpress.com uses it, too.
But the required syntax is not xhtml-compliant (name on pre is forbidden).
<pre name="code" class="javascript"> // my code </pre>
In order to enable compliant xhtml I had to rewrite a small part anyway, so I just made a small jQuery-Plugin that just uses Alex’s scripts right away (thanks Alex!).
Beautify explicitely
<pre id="myCode">
<code>
// my code
</code>
</pre>
Using beautyOfCode you don’t have to decorate your html in order to enable syntax highlighting. Just call beautifyCode on any selected pre or textarea
$(function(){
$.beautyOfCode.init('clipboard.swf');
$("#myCode").beautifyCode('javascript');
});
Beautify all
If you prefer to decorate your html and then just let the highlighter take care of the rest, you may use following notation.
<pre class="code">
<code class="javascript">
// my code
</code>
</pre>
<pre class="code">
<code class="css boc-nocontrols">
body {
font-size: 2em;
}
</code>
</pre>
Make sure you have these few lines
$(function(){
$.beautyOfCode.init('clipboard.swf');
$.beautyOfCode.beautifyAll();
});
Settings
Default Settings
{
noGutter: false, // hide line numbers?
addControls: true, // copy, view plain, ...
collapse: false, // show controls with expand link
showColumns: false, // show column numbers
firstLine: 1 // start with another line number?
}
Change Settings with Javascript
$(function(){
$.beautyOfCode.init('clipboard.swf', {
// add global settings here
firstLine: 4,
collapse: true
});
// change options per listing
$("#myCode").beautifyCode('javascript', {
noGutter: true
});
});
Change Settings in Element-Decoration
Settings with in the html are added as additional classes on the code-element.
<pre class="code">
<!-- You dont have to specify default values. -->
<code class="javascript boc-firstline[3]
boc-nocontrols
boc-nogutter
boc-showcolumns
boc-collapse">
// my code
</code>
</pre>
Source Code
Notice: beautyOfCode is using Syntaxhighlighter. You need to link in SyntaxHighlighter.css, shCore.js and the brushes you want to use.
jQuery.beautyOfCode = {
initialized: false,
settings: {
// hide line numbers?
noGutter: false,
// show copy, plain, ... links
addControls: true,
// collapse to control bar. cant be used
// with addControls set to false
collapse: false,
// show column numbers
showColumns: false,
// start with another line number?
firstLine: 1
},
brushByAlias: {},
init: function (clipboardSwf, settings) {
dp.SyntaxHighlighter.ClipboardSwf = clipboardSwf;
if (settings)
jQuery.extend(jQuery.beautyOfCode.settings, settings);
if (jQuery.beautyOfCode.isInitialized)
return;
// creates a map of each registered brush by alias
jQuery.each(dp.sh.Brushes, function (i, brush) {
var aliases = brush.Aliases;
if(aliases == null)
return;
jQuery.each(aliases, function (ii, alias) {
jQuery.beautyOfCode.brushByAlias[alias] = brush;
});
});
jQuery.beautyOfCode.isInitialized = true;
},
addCssForBrush: function (brush, highlighter) {
if (brush.isCssInitialized)
return;
var headNode = $("head")[0];
if(highlighter.Style && headNode)
{
var styleNode = document.createElement('style');
styleNode.setAttribute('type', 'text/css');
if(styleNode.styleSheet) // for IE
styleNode.styleSheet.cssText = highlighter.Style;
else // for everyone else
$(styleNode).text(highlighter.Style);
headNode.appendChild(styleNode);
}
brush.isCssInitialized = true;
},
beautifyAll: function() {
jQuery("pre.code:has(code[class])").each(function (i, item) {
function getOptionValue(name, list)
{
var regex = new RegExp('^' + name + '\\[(\\w+)\\]$', 'gi');
var matches = null;
for(var i = 0; i < list.length; i++)
if((matches = regex.exec(list[i])) != null)
return matches[1];
return null;
}
var $item = jQuery(item);
var $code = $item.children("code");
var code = $code[0];
var options = code.className.split(" ");
var language = options[0];
var settings = {};
if ($code.hasClass("boc-nogutter"))
settings.noGutter = true;
if ($code.hasClass("boc-nocontrols"))
settings.addControls = false;
if ($code.hasClass("boc-showcolumns"))
settings.showColumns = true;
if ($code.hasClass("boc-collapse"))
settings.collapse = true;
var firstLine = getOptionValue("boc-firstline", options, 1);
if (firstLine)
settings.firstLine = firstLine;
$item.beautifyCode(language, settings);
});
}
};
jQuery.fn.beautifyCode = function (language, settings) {
var saveLanguage = language;
var saveSettings = settings;
// iterate all elements
this.each( function (i, item) {
var $item = jQuery(item);
var settings = jQuery.extend({}, jQuery.beautyOfCode.settings, saveSettings);
var brush = jQuery.beautyOfCode.brushByAlias[saveLanguage];
if (!brush)
return;
// instantiate brush
highlighter = new brush();
// set brush options
jQuery.extend(highlighter, settings);
jQuery.beautyOfCode.addCssForBrush(brush, highlighter);
// IE Bug?: code in pre has to be skipped
// in order to preserver line breaks.
if ($item.is("pre") && ($code = $item.children("code")))
$item.text($code.text());
highlighter.Highlight($item.html());
highlighter.source = item;
$item.replaceWith(highlighter.div);
});
}

Would love your comments!
Hi Lars.
This can help:
$(function(){
$.beautyOfCode.init(‘clipboard.swf’);
$(“pre.code”).beautifyCode(‘javascript’);
});
Thanks! Bye.
Gaston, what do you mean?
Nice
I think gaston means the problem with Firefox3, that “copy to clipboard” doesn’t work
Nice plugin. At first I thought about writing such wrapper myself, but found it by google. It would be much easier to find you, if you create an entry in official plugin database at http://plugins.jquery.com .
Thanks for the hint. I just added it. Vote for it, if you like it
http://plugins.jquery.com/project/beautyOfCode
Hi, just came across your post and started putting this code into use on a site I’m working on.
Just a quick question:
Where does ‘dp.SyntaxHighlight…’ on line 26 in your sourcecode is coming from? I’ve linked SyntaxHighlighter code, but still get an error on that line.
Which version of SyntaxHighlighter are you using here?
I think it’s version 1.5.1
Or you can also edit the shCore.js, and change the FindTagsByName function to match your needs!
Took 2 secondes for me remove the name=”"
This id the code needed that works with the latest version of SyntaxHighlighter (2.0.296)
jQuery.beautyOfCode = {
initialized: false,
settings: {
// hide line numbers?
noGutter: false,
// show copy, plain, ... links
addControls: true,
// collapse to control bar. cant be used
// with addControls set to false
collapse: false,
// show column numbers
showColumns: false,
// start with another line number?
firstLine: 1
},
brushByAlias: {},
init: function (clipboardSwf, settings) {
SyntaxHighlighter.ClipboardSwf = clipboardSwf;
if (settings)
jQuery.extend(jQuery.beautyOfCode.settings, settings);
if (jQuery.beautyOfCode.isInitialized)
return;
// creates a map of each registered brush by alias
jQuery.each(SyntaxHighlighter.brushes, function (i, brush) {
var aliases = $(brush.aliases);
if(aliases == null)
return;
jQuery.each(aliases, function (ii, alias) {
jQuery.beautyOfCode.brushByAlias[alias] = brush;
});
});
jQuery.beautyOfCode.isInitialized = true;
},
addCssForBrush: function (brush, highlighter) {
if (brush.isCssInitialized)
return;
var headNode = $("head")[0];
if(highlighter.Style && headNode)
{
var styleNode = document.createElement('style');
styleNode.setAttribute('type', 'text/css');
if(styleNode.styleSheet) // for IE
styleNode.styleSheet.cssText = highlighter.Style;
else // for everyone else
$(styleNode).text(highlighter.Style);
headNode.appendChild(styleNode);
}
brush.isCssInitialized = true;
},
beautifyAll: function() {
jQuery("pre.code:has(code[class])").each(function (i, item) {
function getOptionValue(name, list)
{
var regex = new RegExp('^' + name + '\\[(\\w+)\\]$', 'gi');
var matches = null;
for(var i = 0; i < list.length; i++)
if((matches = regex.exec(list[i])) != null)
return matches[1];
return null;
}
var $item = jQuery(item);
var $code = $item.children("code");
var code = $code[0];
var options = code.className.split(" ");
var language = options[0];
var settings = {};
if ($code.hasClass("boc-nogutter"))
settings.noGutter = true;
if ($code.hasClass("boc-nocontrols"))
settings.addControls = false;
if ($code.hasClass("boc-showcolumns"))
settings.showColumns = true;
if ($code.hasClass("boc-collapse"))
settings.collapse = true;
var firstLine = getOptionValue("boc-firstline", options, 1);
if (firstLine)
settings.firstLine = firstLine;
$item.beautifyCode(language, settings);
});
}
};
jQuery.fn.beautifyCode = function (language, settings) {
var saveLanguage = language;
var saveSettings = settings;
// iterate all elements
this.each( function (i, item) {
var $item = jQuery(item);
var settings = jQuery.extend({}, jQuery.beautyOfCode.settings, saveSettings);
var brush = jQuery.beautyOfCode.brushByAlias[saveLanguage];
if (!brush)
return;
// instantiate brush
highlighter = new brush();
// set brush options
jQuery.extend(highlighter, settings);
jQuery.beautyOfCode.addCssForBrush(brush, highlighter);
// IE Bug?: code in pre has to be skipped
// in order to preserver line breaks.
if ($item.is("pre") && ($code = $item.children("code")))
$item.text($code.text());
highlighter.highlight($item.html());
highlighter.source = item;
$item.replaceWith(highlighter.div);
});
}
BTW: My vote also goes for putting the code on http://plugins.jquery.com
Excellent work. I love it.
Is this correct?
Line 55: if(highlighter.Style && headNode)
should that not be the following:
if(highlighter.Style && headNode)
Proposition Features:
Add callbacks before and after replacement of original code by highlighter.div
I’ve made 2 changes: here are they
* add 2 settings after line 20:
l20: firstLine: 1
—————————
// beforeUpdate Callback: launch just before the content is replaced by highlighter.div,
// params: $item: jQuery item mapped element, highlighter: the original syntax highlighter object
// return false to cancel replacement
,beforeUpdate: null,
// afterUpdate Callback: launch just after the content is replaced by highlighter.div
// params: $item: jQuery item mapped element, highlighter: the original syntax highlighter object
// adterUpdate: function ($item, highlighter)
afterUpdate: null
—————————
* Replace last line which is:
$item.replaceWith(highlighter.div);
with
—————————
if( settings.beforeUpdate )
if (settings.beforeUpdate($item, highlighter) === false) return;
$item.replaceWith(highlighter.div);
if( settings.afterUpdate )
settings.afterUpdate($item, highlighter);
});
—————————
With this callbacks, i can do such things:
(Show plain text as a textarea by swapping highlighted and textarea element without opening an extra window, which i don’t really like
)
$.beautyOfCode.init(‘libs/syntaxHighlighter/clipboard.swf’, {
afterUpdate: function ($item, sh) {
// our personnalized ‘View Plain’ link
var newA = $(“View Plain“)
.css(“cursor”,”pointer”)
.click(function () {
var $bar = $(this).parents(“.bar”);
// highlighted code visible
if ($bar.next(“:visible”).length)){
var w = $(‘.tools’, sh.bar).width() – 1;
var h = $bar.next().height() – 5;
$bar.next().hide() // hide highlighted code
.next().show().css({
width: w+ ‘px’
,height: h + ‘px’}); // show and resize textarea source code
} else
$bar.next().show() // show highlighted code
.next().hide(); // hide textarea source code
});
// replace original created link by our personnalized one
$(‘.tools a:first’, sh.bar).replaceWith(newA);
// create textarea object and append it to the highlighter.div content
$(” + sh.code + ”).appendTo(sh.div);
}
});
[...] 28, 2009 by Lars Corneliussen One of my most-visited of my posts is the jQuery plugin “beautyOfCode” for the version 1.5 of Alex’ Syntax Highlighter. The main point of doing a wrapper was, that [...]
looks great, i will use it on my page
[...] In: JQuery plugins 4 Jul 2009 Go to Source [...]
[...] 3. beautyOfCode [...]
[...] 3. beautyOfCode [...]
[...] beautyOfCode: jQuery Plugin for Syntax Highlighting : In order to enable SyntaxHighlighter XHTML compliant, a small part is rewritten, and made a small jQuery-Plugin that just uses Alex’s scripts right away. Using beautyOfCode you don’t have to decorate your html in order to enable syntax highlighting. Just call beautifyCode on any selected pre or textarea. [...]
[...] 3. beautyOfCode [...]
[...] 3. beautyOfCode [...]
[...] 12. BeautyOfCode: jQuery Plugin for Syntax Highlighting [...]
[...] 12. BeautyOfCode: jQuery Plugin for Syntax Highlighting [...]
[...] 12. BeautyOfCode: jQuery Plugin for Syntax Highlighting [...]
[...] Haz clic aquí para descargar BeautyOfCode » [...]
Excellent work. Works perfeclty with the latest SH-Version! Really impressive work!
hello please help i am noob in javascript ,
can you posting the code from this site this is what i search
thanks
ich meinte hier auf der seite sieht es so schön aus und ich bekomme den code nicht hin kannst du mir sagen wie der aussehen muss
Guck dir mal die neue version an. Die ist einfacher: http://startbigthinksmall.wordpress.com/2009/05/28/beautyofcode-jquery-plugin-for-syntax-highlighter-2-0-by-alex-gorbatchev/
[...] 3. beautyOfCode [...]
[...] 3. beautyOfCode [...]
hi
i have a problem only with html code.
after asking here i do escape and the code appears correctly. but if i update the article, the html is interpreted again, and so i need to go back again and replace the .
is there a way that it will work with no problems?
for now i just do not include html anymore.
hope someone can help.
best regards
[...] 12. BeautyOfCode: jQuery Plugin for Syntax Highlighting [...]
So I am having an issue with the way the code is getting formatted.
I am using a WYSIWYG Editor called jHtmlArea. I don’t know if that has something to do with it.. you can see what it is doing on this “Fake” blog post on my development server.
http://website.somethingjavaprojects.net/Blogs/34/Preview
let me know if you have any ideas.
Your blogging engine doesn’t understand pre… instead of leaving the text formatted, it puts
for line breaks:
$.beautyOfCode.init({ brushes: ['Xml', 'JScript', 'CSharp', 'Plain', 'Php'], ready: function() { $.beautyOfCode.beautifyAll(); $("#someCode").beautifyCode('javascript', {gutter:false}); } });[...] 3. beautyOfCode [...]
[...] CLIQUEZ ICI POUR TÉLÉCHARGER [...]
[...] beautyOfCode 项目主页 | beautyOfCode 项目下载 | beautyOfCode 中文支持 [...]
thanks for the code
[...] Beauty of Code Plugin (requires JQuery). This will extend the capabilities of “Syntax Highlighter”. [...]
[...] beautyOfCode: jQuery Plugin for Syntax Highlighting (via Struggles by Lars C.) By ajbatac UPDATE: Support for Syntax Highlighter 2.0 + jQuery Listed + bitbucket Repository The Syntaxhighlighter by Alex Gorbatchev is the best I have seen so far. As you see in this post, wordpress.com uses it, too. But the required syntax is not xhtml-compliant (name on pre is forbidden). // my code In order to enable compliant xhtml I had to rewrite a small part anyway, so I just made a small jQuery-Plugin tha … Read More [...]
[...] 3. beautyOfCode [...]
[...] 3. beautyOfCode [...]
[...] 3. beautyOfCode [...]
Does anyone know how to get the ‘view source’, ‘print’ and so on dispalyed?
Thanks a lot
[...] WordPress PluginsTry one of the following highlighting plugins that fit you most:beautyOfCode: jQuery Plugin for Syntax HighlightingSyntaxHighlighter EvolvedWP-SyntaxGoogle Syntax Highlighter for WordPressFV Code HighlighterYou [...]
[...] 3. beautyOfCode [...]
[...] 3. beautyOfCode [...]
[...] 2. beautyOfCode [...]
hey good job.
I have a question:
what is the purpose of “clipboard.swf” in the .init method ???
In flash, by default, you can copy things to the users clipboard. Javascript instead will allways show a prompt asking to allow access to the clipboard. The clipboard.swf just gives access to the clipboard by wrapping flash clipboard access in a javascript api.
[...] Source + Demo [...]
[...] Beautyofcode [...]
Very Cool. Thanks!
I can’t get it work proper; I would be very thankful if you could write a demo r something so people like me could use your work:-)