954,089 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Syntax language can be highlighted

Hey guys! Just what you've been waiting for ... You can now specify a language within your code bbcode to use syntax highlighting just like in our code snippet library.

For example ... Just do [code=c] ...

printf("Hello World\n");


Additionally, you can use the noparse bbcode when you don't want bbcode to be parsed. For example ...[b]Hi[/b] ... notice that [b] wasn't parsed into bold despite a matching close tag and no funky spaces thrown in.

cscgal
The Queen of DaniWeb
Administrator
19,421 posts since Feb 2002
Reputation Points: 1,474
Solved Threads: 229
 

Excellent!!!

If I may humbly suggest a few visual suggestions, though:

#include <stdio.h>
#include <ctype.h>

char* Word[]= { "Alpha ",   "Bravo ",   "Charlie ", "Delta ",   "Echo ",    
                "Foxtrot ", "Golf ",    "Hotel ",   "India ",   "Juliet ",  
                "Kilo ",    "Lima ",    "Mike ",    "November ","Oscar ",   
                "Papa ",    "Quebec ",  "Romeo ",   "Sierra ",  "Tango ",   
                "Uniform ", "Victor ",  "Whiskey ", "X=ray ",   "Yankee ",  
                "Zulu "
              };

int main()     // This is a comment
{
    char  inbuf[200];
    char *p;
    int   i;
    
    printf("Enter: ");
    fgets(inbuf, 200, stdin);
    
    p = inbuf;
    while (*p)
    {
        if (isupper(*p))
        {
            i = *p - 'A';
            printf("%s", Word[i]);
        }
        else
        if (islower(*p))
        {
            i = *p - 'a';
            printf("%s", Word[i]);
        }
        else
        {
            putchar(*p);
        }
        p++;
    }
    
    return 1;
}

To follow more closely what I've seen:comments in green
"Strings" and 'characters' in blue
Numbers in dark blue
Keywords in bold purple (no underline)
All else is black
C/C++ functions don't need to be colored, but if they are, use a dark color like maybe red

All the colors should be of a similar intensity. The switch from the keyword-yellow to function-underline/blue is too striking.

<strong> #include</strong> <stdio.h>
<strong> #include</strong> <ctype.h>

<strong> char</strong>* Word[]= { "Alpha ",   "Bravo ",   "Charlie ", "Delta ",   "Echo ",    
                "Foxtrot ", "Golf ",    "Hotel ",   "India ",   "Juliet ",  
                "Kilo ",    "Lima ",    "Mike ",    "November ","Oscar ",   
                "Papa ",    "Quebec ",  "Romeo ",   "Sierra ",  "Tango ",   
                "Uniform ", "Victor ",  "Whiskey ", "X=ray ",   "Yankee ",  
                "Zulu "
              };

<strong>int </strong>main()   // This is a comment
{
    <strong>char  </strong>inbuf[200];
    <strong>char </strong>*p;
    <strong>int   </strong>i;
    
    printf("Enter: ");
    fgets(inbuf, 200, stdin);
    
    p = inbuf;
    <strong>while </strong>(*p)
    {
        <strong>if </strong>(isupper(*p))
        {
            i = *p - 'A';
            printf("%s", Word[i]);
        }
        <strong>else</strong>
        if (islower(*p))
        {
            i = *p - 'a';
            printf("%s", Word[i]);
        }
        else
        {
            putchar(*p);
        }
        p++;
    }
    
    <strong>return </strong>1;
}


I also notice in the[code=c] section, there is no formatting displayed in the edit window. I assume that's just a glitch, but I thought I'd at least point it out, just in case.

Also, is there a possibility to close up the gap between lines just a tad -- at least as a test? I'd like to see a little less space and a little more code. Instead of 1.5 spacing maybe 1.25.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,492 posts since May 2006
Reputation Points: 3,348
Solved Threads: 943
 

Or how about 1.00...

But WaltP's colors are all wrong. Yours are better. (Mine are the best. No you can't have them :P)

I know! Let's through obscure languages at the feature!

factorial 0 = 1
factorial n = n * factorial (n - 1)


It doesn't look like you have Haskell highlighting yet.

((call/cc call/cc)
 (lambda (f)
   (display "Hello, world!")
   (newline)
   (f f)))


Well at least you have Scheme. But the highlighting is broken, since it treats display and newline like keywords, when they're just built-in procedures.

Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 176
 

I would like to see this:

&#91;CODE=text&#93;This is text&#91;/CODE&#93;

And this:

&#91;CODE=toggled&#93;This text is allready toggled&#91;/CODE&#93;

And of course, a list of supported syntaxes on theHelp with Code Tags page.

Soren Werk
Newbie Poster
3 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: