Posting code without losing indentation and/or text after

  • Unknown's avatar

    Hi ‘support’,

    i’m having an issue posting some Python code to a page on my blog.
    Initially, text after ‘<‘ characters would sometimes disappear.

    i then found the ‘Posting Source Code’ help page and tried putting the [code] and [/code] tags around my source code. However, this is wiping out leading white space and killing the indentation which is significant in Python and will break the code.

    i noticed that if i actually enter spaces in the text field of the browser and ‘Update’ the page, then the white space stays. However, if i paste my code in to the text field of the browser, the white space is removed. i even tried putting the whole lot in as ‘Preformatted’ text, which usually preserves leading white space, but that didn’t work either.

    Thinking that this could be a problem with the browser, i also tried Firefox on MS-Windows (i usually use Firefox on Linux), as that has the more conventional copy/paste method (as opposed to the select and middle mouse button method on Linux/X-Windows). i didn’t try Internet Explorer as my version was too old, and i couldn’t be bothered upgrading it.

    To remove the browser text field altogether, i exported the problem page to an XML file, loaded my script in to the corresponding spot in the XML file, then imported the XML file.
    This preserved the leading white space, but removed text after a ‘<‘ character!

    i also notice that it isn’t possible to just upload my script file and have it served as a file, which is pretty much all that i want to do.

    Is there any way that i can get source code files on to my blog without having to do potentially lots of manual editing/messing around?

    Also, another thing that would be nice, but i must admit i didn’t spend too much time trying to figure this one out, would be if i could get the [code] [/code] section to display using the same colours that my theme uses, rather than looking completely different like it has been fetched from an external web site? i’m suspecting CSS, but some pointers as to which CSS file, where to edit it, which tags to use, etc. would be good.

    Thanks,
    karl…

  • Unknown's avatar

    Hi there,

    When you mention that you were using the “[code]" shortcode, were you specifying the language which you were going to be including? If you include the full shortcode, including the language, it should work for you, and you should be able to copy/paste your code in with your tabs and angle-brackets in place.

    That article again is right here for your reference:

    http://en.support.wordpress.com/code/posting-source-code/

    To be sure, I did a quick test using the python code which you have in your most recent draft (after having substituted out the code for the angled brackets for real ones), and I was able to display your code without any issue, so you should be good to go!

    Let me know if you have any questions!

  • Unknown's avatar

    Oh! And in regards to your question about the background for the “code”, it would indeed be a CSS change. You can go to our CSS forum to review the posts regarding making that change. If no one else has asked that question, you can post about it and one of our friendly volunteers or staff can help you out! The address for the CSS forum is right here:

    https://en.forums.wordpress.com/forum/css-customization

  • Unknown's avatar

    Hi amightywp,

    Thanks for your reply and help with this.

    Yes, i did try specifying the language, but removed it as i wasn’t a big fan of all the different colours.

    i just retested this and in doing so i noticed that the text was all there when i previewed it, but that the ‘<‘ text was disappearing when i published it.
    Noticing the Proofreader popping up with suggestions, and suspecting that the text was being sent to the Proofreader via a web interface (which i suspect occasionally times out on larger posts as i often get it come back after a while saying ‘no errors’ when i almost always have a few sentences in the passive voice for it to find), i disabled the automatic proofreading on publishing option.
    That fixed the problem.

    Moral of the story, disable the Proofreader if you are posting code!

    Also, i’ll try playing with the CSS stuff at some point — thanks for the link.

    Thanks,
    karl…

  • The topic ‘Posting code without losing indentation and/or text after’ is closed to new replies.