Small improvement to [CODE] tag

Thread Starter

jrap

Joined Jun 25, 2006
1,125
Hi all,

We've added a small improvement to the [ CODE ] tag.
Code now includes line numbering, and syntax highlighting. To get the highlighting, you need to specify the language like this:

Code:
[CODE=python]
Example:

Python:
string = "Hello World"
for x in string:
    print x
The list of supported languages can be found at http://qbnz.com/highlighter/

Hope it's helpful!
 

nerdegutta

Joined Dec 15, 2009
2,684
This looks like a great feature. :)

Let's hope it will be used a lot. The line numbers could perhaps be a little more gray, I can barely see them.

Thanks!
 

tshuck

Joined Oct 18, 2012
3,534
Sounds great!

Do we use "C" for the Arduino?
Arduino FAQ: Can I program the Arduino board in C? said:
In fact, you already are; the Arduino language is merely a set of C/C++ functions that can be called from your code. Your sketch undergoes minor changes (e.g. automatic generation of function prototypes) and then is passed directly to a C/C++ compiler (avr-g++). All standard C and C++ constructs supported by avr-g++should work in Arduino. For more details, see the page on the Arduino build process.
Looks like it.
 

djsfantasi

Joined Apr 11, 2010
9,163
That's probably the best one, but since the Arduino language is only "based" on C, there may be constructs that you would think should be highlighted but that won't be.
Exactly as I expected. I could only hope otherwise. In any case, it doesn't lessen the value of this change. Thanks, jrap.
 

WBahn

Joined Mar 31, 2012
30,051
Hi all,

We've added a small improvement to the [ CODE ] tag.
Code now includes line numbering, and syntax highlighting. To get the highlighting, you need to specify the language like this:

Code:
[CODE=python]
Example:

Python:
string = "Hello World"
for x in string:
    print x
The list of supported languages can be found at http://qbnz.com/highlighter/

Hope it's helpful!
It is possible to turn line numbering off? That makes it easier to copy code from a post to try and run it. Not a biggie since you can copy it from a Reply box if needed.

A real big annoyance that needs to ge addressed (more so than code highlighting, which is undoubtedly useful in its own right) s not preserving whitespace when code is pasted into a Reply box. We tell people to properly format their code but then make it a royal pain in the ass for them to do so.
 

WBahn

Joined Mar 31, 2012
30,051
Oh, is there anyway to suppress the "Code:" header. Sometimes we use the CODE tags just because we want a monospaced font (or some other reason) and having it say "Code:" is misleading.
 

tjohnson

Joined Dec 23, 2014
611
It is possible to turn line numbering off? That makes it easier to copy code from a post to try and run it. Not a biggie since you can copy it from a Reply box if needed.
The line numbers don't get selected when I select code in a post, so I don't see a need to be able to turn them off.

By the way, I'd say this is more than just a small improvement.:p I think it's quite a measurable one, especially for those of us who frequent the Programmer's Corner.
 
Last edited:

Thread Starter

jrap

Joined Jun 25, 2006
1,125
A real big annoyance that needs to ge addressed (more so than code highlighting, which is undoubtedly useful in its own right) s not preserving whitespace when code is pasted into a Reply box. We tell people to properly format their code but then make it a royal pain in the ass for them to do so.
I wonder if this issue is OS/Browser related? I just copy & pasted code from my Sublime Text tab into a [ code ]tag and it maintained all indenting, etc.

Oh, is there anyway to suppress the "Code:" header. Sometimes we use the CODE tags just because we want a monospaced font (or some other reason) and having it say "Code:" is misleading.
It can be removed yea..but maybe if I just create a [ MONO ] BB code?
 

WBahn

Joined Mar 31, 2012
30,051
A [ MONO ] BB tag would do just fine.

I'm using IE11.

If I just Quote your original post, the extra spaces stay.

Python:
string = "Hello World"
for x in string:
    print x
And if I copy/paste the text from your code block into a new code block it keeps the spaces.

Python:
string = "Hello World"
for x in string:
    print x
But if I copy it from another application (Notepad++ in this case) it strips them down to a single space.

Python:
for (i = 0; i < 100; i++):
  if (x < 0):
  x += 42
  else:
  x *= 2
 
Last edited by a moderator:

Thread Starter

jrap

Joined Jun 25, 2006
1,125
@WBahn - Ok thanks, something I will definitely look into. If you paste the code into GMail (or any other online form really) does the whitespace maintain? What about Chrome or FF on Windows? Wonder if it's an IE issue. I can test them all out and try to get things working properly.

mono tag test - needs some padding
 

WBahn

Joined Mar 31, 2012
30,051
@WBahn - Ok thanks, something I will definitely look into. If you paste the code into GMail (or any other online form really) does the whitespace maintain? What about Chrome or FF on Windows? Wonder if it's an IE issue. I can test them all out and try to get things working properly.
It appears to be a Xenforo thing.

I had no problems pasting in text and retaining the spacing in vBulletin (now, when I submitted it the extra space would be removed unless it was within appropriate tags, but that was expected -- even though I find it to be another annoying example of the people writing the application deciding that THEY know what I want more than I possibly could). I just tested it on another forum that still uses vB and it still works like it always has.
 

wayneh

Joined Sep 9, 2010
17,498
Here's what a bit of Swift code looks like. It's not a supported language yet, but will go open source later this year.

First, just using the "code" tag inside square brackets:

Code:
    func convertP(P: Double, units: String)  -> Double {
        if units == "psi" {
            let P = P*51.71492410243892
            return P
        }
        else if units == "atm"  {
            let P = P*760.0
            return P
        return P
    }
I tried using code=swift (which is not supported) and code=Objective-C (which is listed as being supported), and these both gave identical "Unknown Language" results.

The same code worked fine when I used the demo at the GeSHi website. So I must be calling the "code=Objective-C" incorrectly. I tried various lower and uppercase combinations without any change. Using "code=C" works as follows:
C:
    func convertP(P: Double, units: String)  -> Double {
        if units == "psi" {
            let P = P*51.71492410243892
            return P
        }
        else if units == "atm"  {
            let P = P*760.0
            return P
        return P
    }
 
Last edited:

tshuck

Joined Oct 18, 2012
3,534
[...]I tried using code=swift (which is not supported) and code=Objective-C (which is listed as being supported), and these both gave identical "Unknown Language" results.

The same code worked fine when I used the demo at the GeSHi website. So I must be calling the "code=Objective-C" incorrectly. I tried various lower and uppercase combinations without any change. Using "code=C" works as follows:
C:
    func convertP(P: Double, units: String)  -> Double {
        if units == "psi" {
            let P = P*51.71492410243892
            return P
        }
        else if units == "atm"  {
            let P = P*760.0
            return P
        return P
    }
Here it is with "code=objc":
Code:
    func convertP(P: Double, units: String)  -> Double {
        if units == "psi" {
            let P = P*51.71492410243892
            return P
        }
        else if units == "atm"  {
            let P = P*760.0
            return P
        return P
    }
It looks like it doesn't support the other names listed on the GeSHi language names list.

It may just be an implementation issue with how jrap got GeSHi working on Xenforo...
 

Alec_t

Joined Sep 17, 2013
14,313
Copy and paste from post #17 above gives this, using FF on WinXP :-

func convertP(P: Double, units: String) -> Double {
if units == "psi" {
let P = P*51.71492410243892
return P
}
else if units == "atm" {
let P = P*760.0
return P
return P
}

Clearly, white space is being stripped down. But this seems to be a 2-stage process, since in the Reply box multiple white spaces were reduced to single white spaces.
 

Thread Starter

jrap

Joined Jun 25, 2006
1,125
Copy and paste from post #17 above gives this, using FF on WinXP :-

func convertP(P: Double, units: String) -> Double {
if units == "psi" {
let P = P*51.71492410243892
return P
}
else if units == "atm" {
let P = P*760.0
return P
return P
}

Clearly, white space is being stripped down. But this seems to be a 2-stage process, since in the Reply box multiple white spaces were reduced to single white spaces.
If you throw that code in a [ CODE ] tag it renders properly. If you actually view-source, the white space isn't stripped.

HTML rendering is actually stripping the whitespace



The [ code ] tag will convert whitespace to &nbsp; to retain the spacing.
 

Thread Starter

jrap

Joined Jun 25, 2006
1,125
@wayneh, fixed - thanks! "objective-c" is now supported.

Code:
func convertP(P: Double, units: String)  -> Double {
        if units == "psi" {
            let P = P*51.71492410243892
            return P
        }
        else if units == "atm"  {
            let P = P*760.0
            return P
        return P
    }
On a related note - boy do I hate Obj-C's syntax :)
 
Top