Small improvement to [CODE] tag

Discussion in 'Feedback and Suggestions' started by jrap, Jul 8, 2015.

  1. jrap

    Thread Starter Administrator

    Jun 25, 2006
    961
    200
    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 (Text):
    1.  
    2. [CODE=python]
    3.  
    Example:

    Code (Python):
    1.  
    2. string = "Hello World"
    3. for x in string:
    4.     print x
    5.  
    The list of supported languages can be found at http://qbnz.com/highlighter/

    Hope it's helpful!
     
  2. nerdegutta

    Moderator

    Dec 15, 2009
    2,515
    785
    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!
     
  3. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    2,796
    831
    Sounds great!

    Do we use "C" for the Arduino?
     
  4. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    I noticed GeSHi in the articles and was wondering when we might see it in the forums... Thanks!
     
  5. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    Looks like it.
     
  6. WBahn

    Moderator

    Mar 31, 2012
    17,716
    4,788
    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.
     
  7. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    2,796
    831
    Exactly as I expected. I could only hope otherwise. In any case, it doesn't lessen the value of this change. Thanks, jrap.
     
  8. WBahn

    Moderator

    Mar 31, 2012
    17,716
    4,788
    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.
     
    ErnieM and djsfantasi like this.
  9. WBahn

    Moderator

    Mar 31, 2012
    17,716
    4,788
    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.
     
  10. tjohnson

    Active Member

    Dec 23, 2014
    614
    121
    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: Jul 8, 2015
  11. jrap

    Thread Starter Administrator

    Jun 25, 2006
    961
    200
    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.

    It can be removed yea..but maybe if I just create a [ MONO ] BB code?
     
  12. WBahn

    Moderator

    Mar 31, 2012
    17,716
    4,788
    A [ MONO ] BB tag would do just fine.

    I'm using IE11.

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

    And if I copy/paste the text from your code block into a new code block it keeps the spaces.

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

    Code (Python):
    1.  
    2. for (i = 0; i < 100; i++):
    3.   if (x < 0):
    4.   x += 42
    5.   else:
    6.   x *= 2
    7.  
     
    Last edited by a moderator: Jul 8, 2015
  13. jrap

    Thread Starter Administrator

    Jun 25, 2006
    961
    200
    @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
     
  14. WBahn

    Moderator

    Mar 31, 2012
    17,716
    4,788
    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.
     
  15. wayneh

    Expert

    Sep 9, 2010
    12,093
    3,031
    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 (Text):
    1.     func convertP(P: Double, units: String)  -> Double {
    2.         if units == "psi" {
    3.             let P = P*51.71492410243892
    4.             return P
    5.         }
    6.         else if units == "atm"  {
    7.             let P = P*760.0
    8.             return P
    9.         return P
    10.     }
    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:
    Code (C):
    1.     func convertP(P: Double, units: String)  -> Double {
    2.         if units == "psi" {
    3.             let P = P*51.71492410243892
    4.             return P
    5.         }
    6.         else if units == "atm"  {
    7.             let P = P*760.0
    8.             return P
    9.         return P
    10.     }
     
    Last edited: Jul 8, 2015
  16. strantor

    AAC Fanatic!

    Oct 3, 2010
    4,302
    1,988
    Oh yeah, love this! Thank you!
     
  17. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    Here it is with "code=objc":
    Code ( (Unknown Language)):
    1.     func convertP(P: Double, units: String)  -> Double {
    2.         if units == "psi" {
    3.             let P = P*51.71492410243892
    4.             return P
    5.         }
    6.         else if units == "atm"  {
    7.             let P = P*760.0
    8.             return P
    9.         return P
    10.     }
    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...
     
  18. Alec_t

    AAC Fanatic!

    Sep 17, 2013
    5,773
    1,103
    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.
     
  19. jrap

    Thread Starter Administrator

    Jun 25, 2006
    961
    200
    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
    [​IMG]


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

    Thread Starter Administrator

    Jun 25, 2006
    961
    200
    @wayneh, fixed - thanks! "objective-c" is now supported.

    Code (Objective-C):
    1.  
    2. func convertP(P: Double, units: String)  -> Double {
    3.         if units == "psi" {
    4.             let P = P*51.71492410243892
    5.             return P
    6.         }
    7.         else if units == "atm"  {
    8.             let P = P*760.0
    9.             return P
    10.         return P
    11.     }
    12.  
    On a related note - boy do I hate Obj-C's syntax :)
     
    tshuck likes this.
Loading...