JavaScript: How to make code-viewer: Power of replace
Want to turn this boring code-view to something more exciting?
[html]:
#include "stdio.h"
//main function
int main(int argc, char *argv[]) {
printf("Hello world\n");
return 0;
}
How do you like this?
Here we have code highlighting, line tracking and line numbers:
#include "stdio.h"
//main function
int main(int argc, char *argv[]) {
printf("Hello world\n");
return 0;
}
All you need is just one line of javascript.[html]:
<PRE id=codeview style='border:1px solid black;background:#FFFFCC;'>
#include "stdio.h"
//main function
int main(int argc, char *argv[]) {
printf("Hello world\n");
return 0;
}
</PRE>
[javascript] (it's realy only one long line):
<SCRIPT>
document.getElementById('codeview').innerHTML=
"<OL style=margin-top:0px;margin-bottom:0px;>"+
("\n"+document.getElementById('codeview').innerHTML).
replace(/\t/g, " ").
replace(/ /g, " ").
replace(RegExp("(\"[^\"]*\")","g"), "<font color=green>$1</font>").
replace(RegExp("//([^\n]*)","g"), "<font color=red>//$1</font>").
replace(RegExp("(\\n| |#|\\()(int|return|include|char)", "g"),
"$1<font color=blue>$2</font>").
replace(RegExp("\n","g"), "<LI style=background:white; "+
"onmouseover=this.style.background='#EEEEFF' "+
"onmouseout=this.style.background='white'>") +
"</OL>";
</SCRIPT>
You should understand, this is just an example. Syntax highlighting will not work with any source code. At the moment there are a lot of solution. For example:

Cool ;)
Posted by Sila Kayo on August 20, 2008 at 05:23 PM GMT+03:00 #
Thanks )
Posted by ahot on August 21, 2008 at 12:02 PM GMT+03:00 #