2

solved per accepted answer. I would also encourage readers to read @Nathan Tuggy's answer, which has some related details that may help you understand the issue more quickly


Question

I have some Python code I'm trying to format, but only part of the block comes out formatted. I've pasted the problem formatting below; originally from https://stackoverflow.com/a/41812246

I've tried:

  • Ctrl + K indent the whole block

  • (Should be the same) but manually 4-space indent the block

  • Use triple back-tics

  • Ensure new-lines are between all formatting changes

  • < !-- language: lang-python -- >

All have the same result, which is that only part of the code block is rendered as code, and other parts are just rendered as text.

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xx below this should be **ALL** code xx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

@DavidPosthill - I don't see how the subsequent code block is preceded by a list item here.

``` import sys, time, msvcrt

def readInput( caption, default, timeout = 5):

start_time = time.time()
sys.stdout.write('%s(%s):'%(caption, default))
sys.stdout.flush()
input = ''
while True:
    if msvcrt.kbhit():
        byte_arr = msvcrt.getche()
        if ord(byte_arr) == 13: # enter_key
            break
        elif ord(byte_arr) >= 32: #space_char
            input += "".join(map(chr,byte_arr))
    if len(input) == 0 and (time.time() - start_time) > timeout:
        print("timing out, using default value.")
        break

print('')  # needed to move to next line
if len(input) > 0:
    return input
else:
    return default

And some examples of usage

ans = readInput('Please type a name', 'john') print( 'The name is %s' % ans) ans = readInput('Please enter a number', 10 ) print( 'The number is %s' % ans) ```

@DavidPosthill - or how the list-item explanation applies to the code stoping, mid-block, and returning to paragraph formatting

1

Your code immediately follows a list item. In that case you need to indent by 8 spaces instead of 4:

  • A List item

    Code with 4 leading spaces

  • Another List item

    Code with 8 leading spaces
    

How to correctly formatted the question (as it currently stands)

  • Remove all backticks.

  • Indent code by 4 spaces.

  • Add blank line after <!-- language: lang-python -->.

  • Corrected my name.

Correctly formatted question follows

I have googled for this and wasn't able to get find anyone else seeing this. I'm sure it's a query problem, but am hoping someone on meta can help. Thanks!

I have some Python code I'm trying to format, but only part of the block comes out formatted. I've pasted the problem formatting below; originally from https://stackoverflow.com/a/41812246/309433

I've tried:

  • Ctrl + K indent the whole block

  • (Should be the same) but manually 4-space indent the block

  • Use triple back-tics

  • Ensure new-lines are between all formatting changes

  • < !-- language: lang-python -- >

All have the same result, which is that only part of the code block is rendered as code, and other parts are just rendered as text.

Thanks!

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xx below this should be **ALL** code xx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

@DavidPostill - I don't see how the subsequent code block is preceded by a list item here.

import sys, time, msvcrt

def readInput( caption, default, timeout = 5):

    start_time = time.time()
    sys.stdout.write('%s(%s):'%(caption, default))
    sys.stdout.flush()
    input = ''
    while True:
        if msvcrt.kbhit():
            byte_arr = msvcrt.getche()
            if ord(byte_arr) == 13: # enter_key
                break
            elif ord(byte_arr) >= 32: #space_char
                input += "".join(map(chr,byte_arr))
        if len(input) == 0 and (time.time() - start_time) > timeout:
            print("timing out, using default value.")
            break

    print('')  # needed to move to next line
    if len(input) > 0:
        return input
    else:
        return default

# And some examples of usage
ans = readInput('Please type a name', 'john')
print( 'The name is %s' % ans)
ans = readInput('Please enter a number', 10 )
print( 'The number is %s' % ans)

@DavidPostill - or how the list-item explanation applies to the code stoping, mid-block, and returning to paragraph formatting

  • Hi @DavidPostill, thanks for your response. I'm not following you; perhaps i don't know what item means here. Would the non-indented, separated by multiple empty lines, be interpreted as the item preceding the code markup? – mike Jan 23 '17 at 18:11
  • @mike List items are the lines of text in a list with bullet markers ... – DavidPostill Jan 23 '17 at 18:13
  • I'm referring to the copied code in this Meta question; I do see that after an unrelated edit (which happened to place some text between the code and the list that preceded it) the code now DOES format. @DavidPostill - above in this question, the code that is failing to format comes after both a normal non-list paragraph, and after a separate code block. – mike Jan 23 '17 at 18:13
  • @mike Exactly. Normal text after the list terminates the list. Then you can use 4 spaces again. – DavidPostill Jan 23 '17 at 18:14
  • @mike I could edit the question to fix it, but then you wouldn't see the problem! :) – DavidPostill Jan 23 '17 at 18:15
  • @mike Note you also need one blank line before any code block. – DavidPostill Jan 23 '17 at 18:16
  • I added some additional text in my question with @DavidPosthill. It seems to me that the item preceding my code block is either a non-list paragraph, or another code block, and there are definitely blank lines separating various sections. Sorry, I'm sure i'm just not understanding part of your answer. – mike Jan 23 '17 at 18:19
  • @mike Get rid of all the backticks for a start. As I said "I could edit the question to fix it, but then you wouldn't see the problem! :)". You need to leave it otherwise you invalidate the question (and my answer). – DavidPostill Jan 23 '17 at 18:40
  • @mike I will add your question to my answer so you can see how to correctly format it ... – DavidPostill Jan 23 '17 at 18:42
  • Ok, I think i see what was wrong. I understand 4-space is the intended code block formatting; it appears that SO has (partial?) support for 3-backtick blocks. I had changed to this to try to 'simplify' the problematic post, and then didn't notice as another edit i did added the text between the bullet and code. @DavidPostill frustrating this is so finicky, but thanks for the help. – mike Jan 24 '17 at 3:04
  • full 3-backtick code blocks ('fenced' / github style) is apparently recognized as needed. Possibly could come someday. meta.stackexchange.com/questions/125148/… – mike Jan 24 '17 at 3:05
1

Triple backticks (unlike e.g. GitHub-flavor Markdown) do not mark fenced codeblocks, just inline code that includes single or double backticks inside. So it's breaking on the combination of newlines and header formatting, because it does not do what you think it does. (There are properly-indented blocks within that region that are being formatted as codeblocks, but that has nothing to do with the backticks.)

Instead, you need to use four-space indentation (with Ctrl-K or otherwise) and make sure that a) the preceding line does not have anything on it, especially not the non-functional triple backticks, and b) there's no preceding (un)numbered list item that hasn't been closed out with intervening text of some kind.

In this case, it's probably the triple backticks. Those not only are not helping, they are actively hurting if you're leaving them in place and are probably the only reason the code is not formatted correctly. Until SE gets fenced codeblocks, perhaps upon adopting CommonMark, only use triple (or double, or quadruple) backticks if you really need an inline code span that has backticks inside.

  • Thanks @Nathan. I didn't see/read this until after I had accepted David's answer; I'm OK with that he did go back and forth helping me figure it out. You answer is also helpful, and probably a little more specifically accurate. Upvote. – mike Jan 24 '17 at 3:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .