|
|
Usage
Placing the code
Place your code on the page and surround it with <pre> tag. Set name attribute to code and class attribute to one of the language aliases you wish to use.
<pre name="code" class="c-sharp"> ... some code here ... </pre>
NOTE: One important thing to watch out for is opening triangular bracket <. It must be replaced with an HTML equivalent of < in all cases. Failure to do won't break the page, but might break the source code displayed.
An alternative to <pre> is to use <textarea> tag. There are no problems with < character in that case. The main problem is that it doesn't look as good as <pre> tag if for some reason JavaScript didn't work (in RSS feed for example).
<textarea name="code" class="c#" cols="60" rows="10"> ... some code here ... </textarea>
Extended configuration
There's a way to pass a few configuration options to the code block. It's done via colon separated arguments.
<pre name="code" class="html:collapse"> ... some code here ... </pre>
Making it work
Finally, to get the whole thing to render properly on the page, you have to add JavaScript to the page.
<link type="text/css" rel="stylesheet" href="css/SyntaxHighlighter.css"></link>
<script language="javascript" src="js/shCore.js"></script>
<script language="javascript" src="js/shBrushCSharp.js"></script>
<script language="javascript" src="js/shBrushXml.js"></script>
<script language="javascript">
dp.SyntaxHighlighter.ClipboardSwf = '/flash/clipboard.swf';
dp.SyntaxHighlighter.HighlightAll('code');
</script>For optimal result, place this code at the very end of your page. Check HighlightAll for more details about the function.
Sign in to add a comment

Can't get it to work in any way, shape or form :(
Can you get included samples working? Please give more details.
This code :
dp.SyntaxHighlighter?.ClipboardSwf? = '/flash/clipboard.swf'; dp.SyntaxHighlighter?.HighlightAll('code');
must been in onload function like this :
<script language="javascript"> window.onload = function () {
} </script>This code :
dp.SyntaxHighlighter?.ClipboardSwf? = '/flash/clipboard.swf'; dp.SyntaxHighlighter?.HighlightAll('code');
must been in onload function like this :
<script language="javascript"> window.onload = function () {
} </script>This code :
dp.SyntaxHighlighter.ClipboardSwf = '/flash/clipboard.swf'; dp.SyntaxHighlighter.HighlightAll('code');must been in onload function like this :
<script language="javascript"> window.onload = function () { dp.SyntaxHighlighter.ClipboardSwf = '/flash/clipboard.swf'; dp.SyntaxHighlighter.HighlightAll('code'); } </script>Awesome piece of code. Worked like a charm the first time.
Thanks Alex
mark...
Awesome piece of code. Worked like a charm the first time.
Thanks Alex
mark...
Very good, but I have some problem to make XHTML 1.1 valid pages. The problem is in <pre> tag: name is not allowed anymore for this tag. And <textarea> needs to be in a form...
Any clue?
.BoB.
Excelent! Works flawlessly! 5 stars! :D
My 1st try (took less than 5 minutes):
<html> <body>
<textarea name="code" class="html" cols="60" rows="10">
BEGIN --- This is the example code:
<link type="text/css" rel="stylesheet" href="Styles/SyntaxHighlighter.css"></link> <script language="javascript" src="Scripts/shCore.js"></script> <script language="javascript" src="Scripts/shBrushCSharp.js"></script> <script language="javascript" src="Scripts/shBrushXml.js"></script> <script language="javascript"> dp.SyntaxHighlighter?.ClipboardSwf? = 'Scripts/clipboard.swf'; dp.SyntaxHighlighter?.HighlightAll('code'); </script>
END --- This is the example code:
</textarea>
<link type="text/css" rel="stylesheet" href="Styles/SyntaxHighlighter.css"></link> <script language="javascript" src="Scripts/shCore.js"></script> <script language="javascript" src="Scripts/shBrushCSharp.js"></script> <script language="javascript" src="Scripts/shBrushXml.js"></script> <script language="javascript"> dp.SyntaxHighlighter?.ClipboardSwf? = 'Scripts/clipboard.swf'; dp.SyntaxHighlighter?.HighlightAll('code'); </script>
</body> </html>
Nice, some problems with <br /> but nice.
i soooooo.... like it!
i sooooo... like it
i sooooo... like it
i sooooo... like it
A really great piece of work! Marvelous! Just added it to my blog.
I found the last 2 lines to be problematic, especially if the Scripts take too long to download. It kept throwing this 'dp is undefined' error. It looked like the last script fragment namely -
<script language="javascript"> dp.SyntaxHighlighter.ClipboardSwf = '/flash/clipboard.swf'; dp.SyntaxHighlighter.HighlightAll('code'); </script>used to get initiated even before the other scripts were downloaded. So I moved it to another file called shInit.js and it looks like this -
var attempts = 0; var timerHandle = setTimeout("checkIfHilighterLoaded()", 350); function checkIfHilighterLoaded(){ try{ dp.SyntaxHighlighter.ClipboardSwf = '/flash/clipboard.swf'; dp.SyntaxHighlighter.HighlightAll('code'); clearTimeout(timerHandle); } catch(e){ clearTimeout(timerHandle); if(attempts < 25){ timerHandle = setTimeout("checkIfHilighterLoaded()", 350); } attempts++; } }So, instead of the inline JavaScript?? I have this -
<script language="javascript" src="js/shInit.js"></script>
Well robccsilva's example posted on Aug 29, 2007 worked well, but the following and all other attempts I've tried just fail. Firebug reveals no errors, so I'm not sure what is wrong. Any ideas:
<textarea name="code" class="java" cols="45" rows="10"> public class HelloWorld? {
} </textarea>
Is anyone able to use this on blogger? it looks to me like i need to upload a bunch of (primarily javascript) files but i dont think i can do that with blogger? anyone have any ideas?
Where should I write this piece of code in wordpress?
<link type="text/css" rel="stylesheet" href="css/SyntaxHighlighter.css"></link> <script language="javascript" src="js/shCore.js"></script> <script language="javascript" src="js/shBrushCSharp.js"></script> <script language="javascript" src="js/shBrushXml.js"></script> <script language="javascript"> dp.SyntaxHighlighter?.ClipboardSwf? = '/flash/clipboard.swf'; dp.SyntaxHighlighter?.HighlightAll('code'); </script>
Is it in the header.php in the theme or somewhere else? Please ...
It doesn't work,man;-( I swear i did nothing wrong.I think i need a sample or more details.
I found that syntax highlighting only worked if these two lines <script class="javascript"> dp.SyntaxHighlighter?.ClipboardSwf? = '/flash/clipboard.swf'; dp.SyntaxHighlighter?.HighlightAll('code'); </script> are placed AFTER the closing </pre> tag
How to get syntax highlighting in Blogger with SyntaxHighlighter?
http://morten.lyhr.dk/2007/12/how-to-get-syntax-highlighting-in.html
<pre name="code" class="php"> $test = "syntax highlighter"; echo $test; </pre>
When language is php,"empty" of the function is displayed double.
example: emptyempty($example)
Hi, why don;t you use <code> element instead?
which is better .... your or from ,,, http://shjs.sourceforge.net/
Worked first time like a charm! thanks for this!
I have my own language file, and am trying to get the following to work: (this relates to my drumputer scripting language at http://www.drumputer.com)
SET KEYWORD1=VALUE1;SET KEYWORD2=VALUE2; LET VARx=VALUE
LET and SET are keywords (as is KEYWORDx) and get hilighted okay. I want to hilight the VARx part. "VARx" can be any string without spaces or an =.
I think I figured it out:
This has been added in my "Brush" file's RegexList?: { regex: new RegExp?('^LET\\s+([a-zA-Z\\-\\d\\]+)=.+', 'gmi'), css: 'vars', submatchIndex: 1, disallow: keywords } //Note the "submatchIndex" property and "disallow"
//And I redefined the following two items from shCore.js (but left shCore alone): //--------- dp.sh.Highlighter.prototype.ProcessRegexList? = function() {
} //--------- dp.sh.Highlighter.prototype.GetMatches? = function(regex, css, submatchIndex, disallowList) {}
// I have two special cases that this takes care of; SET INTRO=intropatternname SET TRANSITION=transitionpatternname RAW INTRO=4/4,15,15,15,25,25 RAW TRANSITION=4/4,15,15,15,25,25 In which case the SET INTRO/TRANSITION would have been handled already
Anyway, it's been an interesting day working through this. And I didn't have to alter any of the main code (but did have to co-opt it, I guess, and adapt a copy) All the above changes are in the customized Brush file
David! This is killer! Worked perfect. Thanks, Steve
let f1 and f2 be two input files of unknown length whose records are integers if the records in both files are in ascending order, the files can be merged to produce a third file f3 whose records are likewise in ascending order. the following algorithm shows how this can be done without using arrys or other costly internal data structures
funtion merge (file f1, file f2, file f3)
while(not eof(f1) or not eof(f2)) end-whileend-funtion //it's merge program #include<iostream> using namespace std; const int MAX = 100; int main() {
}took me a while to figure this out. In order to get the script working, you have to put the js declaration right before the closing </body> tag.
Cool script but the instructions leave much to be desired!
one two three
This is WICKED!
I have one problem though, my escape chars get lost (\).
ie. the following code:
<?php echo "Carlos said \"loving this script\""; ?>
renders as:
<?php echo "Carlos said "loving this script""; ?>
which will cause a parse error.
I suspect the solution is to use a special char or string in place of the \ and dynamically replace it from within the js.
I'll post when I find the solution.
regarding on Comment by joomlers, Dec 19, 2007 about function empty() does anyone knows how to fix this? coz I also experience it.
thanks
that worked great...thank you! :)
This is unbelievably wonderful, awesome and works perfectly for me, thank you very much. :)
in ff - cool, but doesn't work in opera
not working.
If you're one to pay attention to standards & DOCTYPEs, you're probably cringing at the instruction to add a "name" attribute to your <pre/> elements.
Actually, if you look at the source "shCore.js" there's a comment about this which is kinda funny.
"// for some reason IE doesn't find <pre/> by name, however it does see them just fine by tag name..."
Ironicly enough that's probably got somthing to do with "name" not being a valid attribute of the <pre/> element. IE is actually behaving as it should in this case.
"dp.sh.HighlightAll" really should be rewritten so invalid attributes aren't required. I plan to do just that for my implementation of this otherwise awesome script, just wanted to leave this note for any of the scripts developers who happen to read these comments. :)
Hello,
Excellent work, does it work with tinymce? If so how to configure that?
Thanks
Great Piece ......!!!
I used in my site..... www.certification4career.com
@mr.joebert I agree - I was trying to use this with SharePoint?, which strips the name attribute from the pre tag. I modified HighlightAll to look for a compound class name instead (like "xml code", or "css code"). That said, it still failed to work...
To get it works on tinymce
http://blog.daemon.com.au/go/blog-post/getting-tinymce-to-play-nice-with-dp-syntaxhighlighter
Hello, I have written a plugin for tinymce you can find here
http://weblogs.asp.net/nawaf/archive/2008/04/06/syntaxhighlighter-plugin-for-tinymce-wysiwyg-editor.aspx
how to use in mediawiki
Hello again,
I have upgraded the plug-in to work on tinymce 3.x, you can find it here
http://weblogs.asp.net/nawaf/archive/2008/04/10/syntaxhighlighter-plug-in-for-tinymce-3-x-wysiwyg-editor.aspx
Hope this helps.
klein.stephane said: This code :
dp.SyntaxHighlighter.ClipboardSwf = '/flash/clipboard.swf'; dp.SyntaxHighlighter.HighlightAll('code');must been in onload function like this :
<script language="javascript">window.onload = function () { dp.SyntaxHighlighter.ClipboardSwf = '/flash/clipboard.swf'; dp.SyntaxHighlighter.HighlightAll('code');} </script>maybe not necessary, put the code:
<script language="javascript"> dp.SyntaxHighlighter.ClipboardSwf = '/flash/clipboard.swf'; dp.SyntaxHighlighter.HighlightAll('code'); </script>after your pre or textarea will be fine.
I admit that I'm eccentric upfront and I don't like to place script tags outside of the head element when I don't have to. Rather than place the scripts at the bottom of the page, how about placing the defer attribute on the script element?
<script language="javascript" defer="defer"> dp.SyntaxHighlighter.ClipboardSwf = '/scripts/clipboard.swf'; dp.SyntaxHighlighter.BloggerMode(); dp.SyntaxHighlighter.HighlightAll('code',true,false,false,1,false); </script>in the <head> I put
And at the botom of the page
And its not working:I can't get the following to work. I know all the files are referred to correctly, but I did mess w/ the structure to make it simpler. (the CSS file is called syntax-highlighter.css and is in the same folder as this code... all the js's and the swf are in syntax/ which is in the same folder as this code)
What am I doing wrong?
{{{<html> <head>
</head><body>
</body> </html>}}}Sorry... I meant the js's and swf are in the folder 'scripts'.
Javascript should be linked that way: <script type="text/javascript" src="/synhighlighter/shBrushXml.js"></script>
In other words the "language" attribute is deprecated and does not replace the "type" attribute. Using the "language" attribute only may lead to un expected behaviour (non-working JS): <script language="javascript" src="/synhighlighter/shBrushXml.js"></script>
To use SyntaxHighligter? with SPIP. You can try the plugin : http://www.touv.fr/spip.php?article141
I think this needs a bit of a re-think in order to work with XHTML pages, you're not allowed to use name as that's been replaced by ID, and you can only have one ID which would limit this to one use per page (little use for tutorials). I think it needs to use the class attribute if possible...
However, visually it looks great!