Marking code blocks


  • Moderators

    @SGaist @sierdzio
    As of the discussion here

    i was not aware that there is an alternative for code blocks
    Blocks of code are either fenced by lines with three back-ticks ```, or are indented with four spaces. I recommend only using the fenced code blocks -- they're easier and only they support syntax highlighting.

    This has a couple of aspects:

    1. Some keyboards do not support backticks directly, since users complaint about the difficulty to get the backtick.
    2. Backticks seem to lead to syntax highlighting, the question is how to get to work. Apparently, it is currently not working.
    3. The alternative would be easier to support for everybody, since every keyboard supports spaces
    4. The four spaces are really a pain when copy and pasting code sections into the forum. However, assuming a general indation of 4 spaces might be used by many, that may not be to bad.
    5. Problems are with sections when the four spaces are not used, yet. Don't believe that everyone will add the four spaces over blocks.
    6. Well, moderators not add four spaces, because this takes more time than simply adding backticks.

    In my opinion a single way should be proposed and used.


  • Moderators

    1. Even if you don't have a backtick key directly on your keyboard you can still use the alt codes (hold left ALT key and enter 96 on the numeric keyboard block). Yup, you can do that with pretty much any character too.
    2. It works fine and I use it all the time:

    Using ```cpp

    //I'm a c++ code!
    if(foo)
       std::cout<< "bar";
    

    using ```python

    #I'm a python code!
    if foo
       print "bar"
    
    1. Spaces are bad idea in my opinion. People use different number of them, It's not easy to paste code from somewhere where they were not used. It's not easy to apply them to multiple lines at once. They are terrible for stuff like forum migration. They are not intuitive at all.
    2. Agree.
    3. Also it makes the code copy-pastable as a block. You don't have to edit these spaces every time you paste it somewhere. In general it's a better solution to surround code with something than to edit every single line of it.

    I would stick with surrounding code with backticks or use some other character, more widely present.
    The best, I think, would be if there was simply a button on the posting toolbar that would do that for you i.e. you write some code, select it and hit the button. This would also make this more accessible to people without backticks on their keyboards and to people that don't know the syntax (or are to lazy to read the big fat help text).


  • Moderators

    @Chris-Kawa
    Basically you do not have to use the alt codes which is for some also a bit cumbersome.
    Since the markdown tags and especially the backticks are given at the end of the thread section (not sure if this is true for new posts), you can simply mark them and use copy and paste to introduce them where required.

    @Chris-Kawa said:

    Using ```cpp

    I have tried, but it did not show a difference. Anyway I will keep in mind.

    AFAIK the button solution is not easy to implement, but it would be certainly preferrable.


  • Moderators

    Use ```cpp before and ``` after the block.
    Eg.
    ```cpp
    if(foo) bar();
    ```
    will result in

    if(foo) bar();
    

  • Lifetime Qt Champion

    Hi,

    I concur with @Chris-Kawa on all points.

    For me a formatting button is clearly missing, however I don't know what it would take to add it.



  • @SGaist said:

    Hi,

    I concur with @Chris-Kawa on all points.

    For me a formatting button is clearly missing, however I don't know what it would take to add it.

    I agree. In a forum discussing aspects of programming, inserting a block of code should be as simple as possible.



  • @Chris-Kawa Sadly the cpp syntax highlighting is obviously broken on qt.io
    Looking at the C++ sample at highlightjs.org we see it's not close to working here. What we urgently need is a default Qt code customized highlight mode for these forums. Like they have over at qtcentre.org reading Qt code is so much easier over there.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.