May 20, 2009

Blogging Python Output: A Challenge

I spent half an hour yesterday in a battle with Bogger, trying to get it to render the output of this Python 3 program:
class MyCls:
pass

obj = MyCls()
print(obj)
Unfortunately, despite batting to with HTML entities for less than and the like, and even changing all spaces to non-breaking spaces*, the best I seem to be able to do is:

<__main__.mycls>

It starts out looking OK, but once I preview it, or publish it, Blogger just throws away everything after the first space up to the closing angle. Who can tell me what I am doing wrong? Or is Blogger being unfair.

* don't even think about blogging the actual entity codes: they'll be mangled too.

[The program will run, producing slightly different output, under Python 2. I suspect Blogger will mangle that output too]

8 comments:

Marius Gedminas said...

Have you spent any time trying to get the program itself rendered correctly? All the indentation is missing.

I think Blogger is simply unsuitable for blogs about programming.

Marius Gedminas said...

The program itself is incorrect too (all the indentation is missing).

The comments UI is horrible (it tends to eat my comment forcing me to retype it, e.g., just now).

I'm afraid Blogger is simply unsuitable for serious programming blogs.

Steve said...

Yes, I did indeed go through the code to ensure that the indentation was correct. Sadly Blogger "uncorrected" it. I am beginning to suspect you are right about its unsuitability.

Don't remember having these problems earlier. Maybe I'll look back and see what I did differently.

I should be publishing more code, then these problems would become apparent sooner!

Michael Barber said...

I've had good success using highlight. I blogged about how I deal with formatting program text. The example in that post is SML, but I've also used it with Python.

Benjamin Peterson said...

When I want to post code to my blog, I run it through pygmentize. It works great!

Steve said...

@Benjamin: Yes, pygmentize does seem to do the job. As you will see from this post and the preceding one I have added the styles to the blog to get colorization. Thanks!

Doug Hellmann said...

Do you have this problem sorted out? I found that I had to run program output through a script to replace angle brackets with their entity values. Using Pygment for source works well for me, too.

Steve said...

Yes, it's sorted, as demonstrated in Trying Again, thanks.

The crucial factor (for me) seemed to be the separation of the code from the rest of the entry in it's own <div>; only opening angle brackets need to be entities to avoid misinterpretation, I suspect.