Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts

Sunday, May 23, 2010

An Easy SyntaxHighlighter with Blogger

So, in the last blog post, I wanted to throw down some nice code box like I've seen on other blogger pages. I found a couple tutorials out there which had you pasting information into the html layout found in layout->edit HTML in three different places.

It was a bit of a pain that I had to put this piece here and this other piece there and so on, but, I found one that doesn't require all that.

Without further adieu,
  1. Download and find a place to host the library files for Syntax HighLighter.
  2. Login to blogger; go layout -> edit HTML and add the following at the end of the file.
</div></div> <!-- end outer-wrapper -->
<link href='http://[YOUR HOST]/SyntaxHighlighter.css' rel='stylesheet' type='text/css'/>
<script src='http://[YOUR HOST]/shCore.js' type='text/javascript'/>

<script src='http://[YOUR HOST]/shBrushCpp.js' type='text/javascript'/>
<script src='http://[YOUR HOST]/shBrushCSharp.js' type='text/javascript'/>
<script src='http://[YOUR HOST]/shBrushCss.js' type='text/javascript'/>
<script src='http://[YOUR HOST]/shBrushJava.js' type='text/javascript'/>
<script src='http://[YOUR HOST]/shBrushJScript.js' type='text/javascript'/>
<script src='http://[YOUR HOST]/shBrushSql.js' type='text/javascript'/>
<script src='http://[YOUR HOST]/shBrushXml.js' type='text/javascript'/>

<script class='javascript'>
//<![CDATA[
  function FindTagsByName(container, name, Tag)
  {
      var elements = document.getElementsByTagName(Tag);
      for (var i = 0; i < elements.length; i++)
      {
          if (elements[i].getAttribute("name") == name)
          {
              container.push(elements[i]);
          }
      }
  }
  var elements = [];
  FindTagsByName(elements, "code", "pre");
  FindTagsByName(elements, "code", "textarea");

for(var i=0; i < elements.length; i++) {
if(elements[i].nodeName.toUpperCase() == "TEXTAREA") {
 var childNode = elements[i].childNodes[0];
 var newNode = document.createTextNode(childNode.nodeValue.replace(/<br\s*\/?>/gi,'\n'));
 elements[i].replaceChild(newNode, childNode);

}
else if(elements[i].nodeName.toUpperCase() == "PRE") {
 brs = elements[i].getElementsByTagName("br");
 for(var j = 0, brLength = brs.length; j < brLength; j++) {
  var newNode = document.createTextNode("\n");
  elements[i].replaceChild(newNode, brs[0]);
 }
}
}
//clipboard does not work well, no line breaks
// dp.SyntaxHighlighter.ClipboardSwf =
//"http://[YOUR HOST]/clipboard.swf";
dp.SyntaxHighlighter.HighlightAll("code");
//]]>
</script>

</body>
</html>
There are a few other language highlighting rules found in the SyntaxHighlighter library; you can add them in above as you need them, and you can take the ones out that you don't use also. Here's a list of what's in the current Syntax Highlighter 1.5.1
  • Cpp
  • C#
  • Css
  • Delphi
  • Java
  • Jscript
  • Php
  • Python
  • Ruby
  • Sql
  • Vb
  • Xml
I found this solution on Tips for software engineer. Thank you very much.