One of my most-visited posts is the jQuery plugin “beautyOfCode” for the version 1.5 of Alex’ Syntax Highlighter. The main point of doing a wrapper was, that version 1.5 did require invalid html code.
In the new version of Syntax Highlighter this is fixed. But still, people ask for a new version of my code, too.
I finally released a new version with some more features:
- Uses online hosting of styles, scripts and brushes. This means you only need to reference jQuery and beautyOfCode!
- Supports the new features as smart-tab, html-script, themes, line-wrapping and much more
- Still uses a code-tag inside a pre-tag as well as a more css-like configuration with classes instead of property syntaxes
Referencing the plugin and configuring brushes
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script> <script type="text/javascript" src="Scripts/jquery.beautyOfCode.js"></script> <script type="text/javascript"> $.beautyOfCode.init({ brushes: ['Xml', 'JScript', 'CSharp', 'Plain', 'Php'] }); </script>
Detection, default behaviour
By default, beautyOfCode will beautify all elements matching ‘pre.code:has(code[class])”:
<pre class="code"> <code class="javasript"> alert("Hello, World!"); </code> </pre>
Explicit
If auto detection is not desired, the plugin has to be initialized differently:
<script type="text/javascript"> $.beautyOfCode.init({ brushes: ['Xml', 'JScript'], ready: function() { $('#mycode').beautify('javascript'); } }); </script>
This would then only beautify following code:
<pre id="mycode"> <code> alert("Hello, World!"); </code> </pre>
Initialization Options
These are the default options, which can be overridden by $.jQuery.beautyOfCode.init:
settings: { // should the syntax highlighter and brushes // be loaded dynamically autoLoad: true, // the base url to alex' hosted sources // http://alexgorbatchev.com/wiki/SyntaxHighlighter:Hosting baseUrl: 'http://alexgorbatchev.com/pub/sh/2.0.320/', // the baseurl for the hosted scripts scripts: 'scripts/', // the baseurl for the hosted styles styles: 'styles/', // themes from http://alexgorbatchev.com/wiki/SyntaxHighlighter:Themes theme: 'Default', // the brushes that should be loaded - case sensitive! // http://alexgorbatchev.com/wiki/SyntaxHighlighter:Brushes brushes: ['Xml', 'JScript', 'CSharp', 'Plain'], // overrides for configurations and defaults // http://alexgorbatchev.com/wiki/SyntaxHighlighter:Configuration config: {}, defaults: {}, // function to be called, when all scripts are loaded ready: function() { jQuery.beautyOfCode.beautifyAll(); } }
Example with current version, Django-theme and no gutter
<script type="text/javascript"> $.beautyOfCode.init({ theme: 'Django', baseUrl: 'http://alexgorbatchev.com/pub/sh/current/' defaults: { gutter: false }, brushes: ['Xml', 'JScript'] }); </script>
Options
Find a reference for all options on SyntaxHighlighter Configuration.
The options can either be specified as defaults in init, on the beautify-call or as css classes.
Specify options in code:
$('#myCode').beautify('javascript', {'wrap-lines':true, 'first-line:2'});
Specify options in html:
In this case the gutter would not be shown and the lines 3 and 6 (5 and 8 in Listing) would be highlighted.
<pre class="code"> <code class="javasript boc-no-gutter boc-highlight[3,6]"> alert("Hello, World!"); function x() { if (true) x(); } </code> </pre>
Syntax for options in html:
- The flags smart-tabs, ruler, gutter, toolbar, collapse, auto-links, wrap-lines and html-script mean true, then prefixed with boc-, and false, when prefixed with boc-no-.
- class-name, first-line and tab-size are prefixed with boc- and followed by their values in square brackets. For example first-line[3] means, the gutter starts with a three.
- highlight is also prefixed with a boc- and followed by a comma-separated list of line numbers in square brackets that are to be highlighted.
Sources
Find the plugin source code and a demo page on the bitbucked beautyOfCode project page. If you like the plugin, please also vote on the jQuey plugin site.
Feel free to submit issues and patches. The code is not very well tested, since I don’t use it in any production environment today.
Pingback: beautyOfCode: jQuery Plugin for Syntax Highlighting « .Net Braindrops
hi voll cool
Hi Lars,
This has been driving me nuts, I really can’t see why this is working on my blog: http://blogs.thesitedoctor.co.uk/tim/2009/07/09/CodeGarden+09+Open+Space+Minutes+Space+2+Exception+Handling+In+Umbraco.aspx
Would you mind having a quick look?
Thanks.
Tim
Hi Tim,
sorry for answering this late.
The fixes are quite simple:
1) There is no language called javascript. 🙂 Just change language=”text/javascript” to type=”text/javascript” to make the script run.
2) The formatting of the pre-class is wrong. beautyOfCode uses a different style than syntaxhighlighter does. Use “xml xml boc-no-toolbar boc-no-auto-links” instead of “brush: xml; toolbar: false; auto-links: false;”
With those changes it ran locally, at least. Hope you get it running.
Hi ! Thanks a lot or this jQuery plugin, it does it in a fast & easy way.
By the way, I don’t have the toolbar “View plain, etc.” by default, is there is something special to do in order to have it displayed ?
My code is the folowwing :
jQuery.beautyOfCode.init({
config: { stripBrs: true},
brushes: [‘Vb’,’JScript’,’CSharp’]
});
Also, my html editor provider (Telerik RadEditor) adds automatically and in the code. The parameters stripBrs doesn’t seems to change anything, the BR are stripped anyway, but after the formatting, the code appears on 1 line only. …. Any suggestion ?
Hi Lars.
Is there a place where we can see examples of the various brushes? I can’t find one, but before I create such a page for the community, wanted to double-check.
(And I believe the 6/21 comment is just some manually-entered spam.)
Thanks for your work on the plugin,
~James
Find a overview over the different brushes here: http://alexgorbatchev.com/wiki/SyntaxHighlighter:Brushes
(deleted the comment. thanks!)
how can i configure this code + tinyMCE ?
tinyMCE formated like this :
— CODE —
—END—-
thanks
I don’t know – can’t see, how they should work together.
Pingback: Joseph Easter » jQuery accordion, syntaxhighlighter and searchSuggest added
You are using an old version of SyntaxHighlighter:
– Version used in “beautyOfCode”:
http://alexgorbatchev.com/pub/sh/2.0.320/
– New version:
http://alexgorbatchev.com/pub/sh/2.1.364/
I’m glad to see Alex clarified about the old pre tag method still working under 2.1; that was what was holding me back from upgrading.
Thank you Patrick! I updated the sources. The only thing necessary though was to change the base url.. 🙂
Hi!
I must say this is a fantastic plugin, or would be if i could get it working! I really must be doing something wrong. I keep getting an alert saying “Can’t find brush for: Php”.
this is what i’m putting in my head section:
$.beautyOfCode.init({brushes: [‘Xml’, ‘JScript’, ‘Plain’, ‘php’]});
and this is in my HTML body:
Am i missing something?
Thanks in advance!
Ash
The brushes are case-sensitive. So use php in small letters everywhere.
Hey there,
thanks for your great script. I use it for a while in my TYPO3 installation and it works great.
Until now i did this by adding plain HTML but i start building a plugin to make this more comfortable. When ive done some more testing i would like to release this extension to the TYP03 ext ter.
Your code is licensed under LGPL and Alex code under GPL, so it shouldnt be a problem to release my extension. I will add a tribute and links to you both.
So, its okay for you of i release the extension?
Thanks again and greetings from Hamburg, Germany.
Felix
@felix I’m totally ok with that! Drop me a link when you release it 🙂
hi
first thank you so much. your efforts are really appriciated.
i have tried it on only a php code for the moment, and i have one problem:
my code appears from right to left (like the page it appears on). i try to tell it with css to be ltr, but it still comes up as rtl.
what am i doing wrong?
best regards
ron
hi
i have tried putting this (i am using ckeditor):
and the paragraph does not show. i added to the brushes Html but it still does not work.
hope someone can help.
best regards
ron
hi
i manage to fix the rtl issue but i still cannot put html code.
any help please?
best regards
ron
Hi
can anybody help with displaying html code?
i still get it rendering and not as the code itself.
@ron: Example?
Off the top of my head, make sure you’ve replaced all < with < .
hi
replace this: < with this: < ?
what is there to replace then?
maybe i do not understand
best regards
You need to entity your code. Do it with htmlspecialchar in PHP or manual by using: http://www.w3schools.com/tags/ref_entities.asp
Thanks Felix; figured I should have escaped the ampersand, but when the post-submit looked fine … should have refreshed.
thank you so much it works:)
Hey, i developed a TYPO3 extension and a brush for Typoscript as well.
Please see:
http://www.felixnagel.com/blog/artikel/2010/02/07/beautyofcode-syntax-highlighter-als-typo3-extension-veroeffentlicht/
Hi
Ther is a small bug in the code, it will not work if jQuery is run in noConflict Mode.
You use the “$” on line 36 and on line 124, use “jQuery” instead and it runs perfectly.
Thanks for your great work.
Best Regards
Jürgen
Thanks. Changed! http://bitbucket.org/larscorneliussen/beautyofcode/changeset/94696f6dd85a/
jQuery.Syntax is an extremely fast and lightweight syntax highlighter. It has dynamic loading of syntax source files and integrates cleanly using CSS or modelines.
It was developed specifically to fill a gap – that is: a fast, clean, client-side syntax parser.
Please see the project homepage for more information, documentation, download, etc.
http://www.oriontransfer.co.nz/software/jquery-syntax
Is it possible to overwrite single config.strings.xyz values? It seems i have to rewrite the whole strings array. Recursive merging problem?
For anyone wanting an updated version which supports the new amazon cdn server (for those getting 404 errors), or for those using the w3c recommendation for including code, you can find a updated repository here:
http://bitbucket.org/balupton/beautyofcode
For the w3c recommendation, we use:
<code>
// your code goes here
</code>
Thanks.
Benjamin,
I love this plugin! Did you notice the new ver. 3 of SyntaxHighlighter? Any plans to include in your plugin?
I’ll have a look soon. If you need it now, you could also easily fork the code and then send a pull request. Or just send me a patch-file. 🙂
Pingback: Ten or Twenty improvements for Twenty Ten: Part 1 | Do The Right Thing
Pingback: postrational (postrational) « beautyOfCode – jQuery plugin for Syntax Highlighter 2.0 by Al... « Chat Catcher
@Lars
Is there a way to disable the notification box that pops-up when you copy the code?
Thanks!
Hi Lars,
Ich versuche mich gerade daran, eine Mehrfach-Auswahl via Single-Select einzubauen …
Mir fiel auf, das PREs und CODEs komplett umgeschrieben werden, nicht einmal weitere Klassen und IDs bleiben erhalten. Hast du da schon Erfahrung, wie sich das Einschränken lässt, um zumindest die Attribute vom PRE zu erhalten?
Wo hänge ich am sinnvollsten das Rendering der Selects an, als Erweiterung von deinem ‘ready’?
… und eine letzte Frage: Schicker als das Verwenden einzelner kompletter Blöcke PRE + CODE fände ich das automatische Erkennen mehrerer CODEs innerhalb eines PREs. Wo wäre hierzu der Code-Hebel anzusetzen?
Follow-up gerne auch per pm in E-Mail 🙂
Gute Grüße
Stefan
… rrr, dammit, es wird ja nicht einmal die Klasse der Sprache bzw. des Brushes erhalten
Code und Beispiel wäre nun vorhanden, sende es gerne auf Anfrage per Mail 🙂
Is there a tuts for blogger.com blog?
I don’t know.
Pingback: wirehack7.com
There ist a ver. 3 of SyntaxHighlighter. But your plugin doesn’t work with that… are there any chance?
Works fine for me, just need to edit the baseUrl to “http://alexgorbatchev.com/pub/sh/3.0.83/”.
can i host it self ??
when i change the base URL to my syntaxhilighter then is an error
I had some trouble with the Amazon-URL provided with this plugin, worked fine after edit the baseUrl to “http://alexgorbatchev.com/pub/sh/2.1.364/” or another version. (http://alexgorbatchev.com/SyntaxHighlighter/hosting.html)
@Jackson – Works fine for me, just need to edit the baseUrl to “http://alexgorbatchev.com/pub/sh/3.0.83/”.
@hups – You also need to edit the baseUrl to an own server and download the data from Alex: http://alexgorbatchev.com/SyntaxHighlighter/download/
I had some trouble with the Amazon-URL provided with this plugin, worked fine after edit the baseUrl to “http://alexgorbatchev.com/pub/sh/2.1.364/” or another version. (http://alexgorbatchev.com/SyntaxHighlighter/hosting.html)
Does not work anymore. I have 403 from Amazon and now a 404 from Typepad. Too bad, it was nice. Now I have to switch.
Hi,
I’m sure it is easy to fix; but i have no time. Would you like to create a pull request?
Other commented on what needed to be changed…
_
Lars
hii lars,
is it possible to give baseurl as same machine or not…??? if yes pls help..
thanks…..