Results 1 to 4 of 4

Thread: colorize code

  1. #1

    Thread Starter
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552

    colorize code

    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.

  2. #2
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906
    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() fuunction will be useful.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  3. #3

    Thread Starter
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    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. hope i can get some info on the net. if i get some, i'll post. thanks a lot.

  4. #4

    Thread Starter
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    i've got this so far. only the reserve words and the comment. anyone has some better regex, please share. thanks
    PHP Code:
    <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|delegate|do|double|else|enum|event|explicit|extern|false|finally|fixed|float|for|foreach|goto|if|implicit|in|int|interface|internal|is|lock|long|namespace|new|null|object|operator|out|override|partial|params|private|protected|public|readonly|ref|return|sbyte|sealed|short|sizeof|stackalloc|static|string|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
    {
       
    /// &lt;summary&gt;
       /// Summary description for Class1.
       /// &lt;summary&gt;
       
    public class Class1
       
    {
          public 
    Class1()
          {
             
    //
             // TODO: Add constructor logic here
             //
          
    }
          static 
    void Main()
          {
             
    Console.WriteLine("hello world");
          }
       }
    }
    </
    pre

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width