Click to See Complete Forum and Search --> : colorize code
brown monkey
Aug 3rd, 2004, 10:42 PM
any idea of colorizing code? such example is the <pre class=csharp>blah blah</pre> blah blah is colorize under the syntax of c#. i think there should be a pre.csharp{...}. and i think it's regex. any example is greatly appreciated. thanks in advance. :)
visualAd
Aug 4th, 2004, 07:17 AM
You would use egular expressions. Syntax highlighting is best done on the server side using perl or PHP. If you do a google search you'll find loads of examples.
For php the preg_replace() (http://www.php.net/preg_replace) fuunction will be useful.
brown monkey
Aug 4th, 2004, 08:14 PM
thanks mate. i'll do that. but i hope i will know the javascript version cause i'm just on the free servers mate and they only allow html on it so i had to do this on javascript. :D hope i can get some info on the net. if i get some, i'll post. thanks a lot. ;)
brown monkey
Aug 5th, 2004, 05:05 AM
i've got this so far. only the reserve words and the comment. anyone has some better regex, please share. thanks
<style>
#csharp{
font:9pt courier new;
}
</style>
<script language="javascript">
window.onload=function(){
var s=document.getElementById('csharp').innerHTML;
var re=/(\b(abstract|as|base|bool|break|byte|case|catch|char|checked|class|const|continue|decimal|default|de legate|do|double|else|enum|event|explicit|extern|false|finally|fixed|float|for|foreach|goto|if|impli cit|in|int|interface|internal|is|lock|long|namespace|new|null|object|operator|out|override|partial|p arams|private|protected|public|readonly|ref|return|sbyte|sealed|short|sizeof|stackalloc|static|strin g|struct|switch|this|throw|true|try|typeof|uint|ulong|unchecked|unsafe|ushort|using|virtual|volatile |void|where|while|yield)\b)/g;
s=s.replace(re,"<span style='color:blue;'>$1</span>");
s=s.replace(/((\/\*[^""]*\*\/)|(\/\/.*))/g,"<span style='color:green;'>$1</span>");
document.getElementById('csharp').innerHTML=s;
}
</script>
<pre id='csharp'>
/*
* Author: brown monkey
* Date written: 5 July 2004
* Definition: no definition
*/
using System;
namespace WindowsApplication100
{
/// <summary>
/// Summary description for Class1.
/// <summary>
public class Class1
{
public Class1()
{
//
// TODO: Add constructor logic here
//
}
static void Main()
{
Console.WriteLine("hello world");
}
}
}
</pre>
:)
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.