Tuesday, 8 March 2005

Posting code: the secret

OK. I've got it figured out from too much time spent trying to get the code in my last post to display properly. It's not only those blasted "less than" and "greater than" symbols, but also the ampersand.

So, a note while it's fresh - both for me and in case it may help anyone else.

1. Replace all the "&" with "&# 38;" (without the space) (or with "&")
2. Replace all the "<" with "&lt;"
3. Replace all the ">" with "&gt;"

(I'm getting the sequence at the end of no. 1 to display properly by actually typing in this sequence into my post (without the spaces): "& amp; 38;")

The order is important - replace any "&" in the code FIRST (i.e. do step no. 1 first) otherwise you could inadvertently replace the "&" that you need as part of the "<" and ">" replacements.

I do that in Word, with a macro I've now whipped up. Then, just to get rid of any horrid hidden codes or changes introduced by Word (it does terrible things to double quotation marks), I copy/paste the changed code into Notepad. Then I copy/paste from Notepad to my post. (Yes, I know I need to get a proper text editor!).

Conversion tool

[Added 21 May 2005:] Found a site that provides an automatic converter, yay: Centricle.

You just copy/paste your code into the first box and hit "Encode" and it does a search and replace for you automatically of the tricky characters you have to encode in order for code to show up properly. Just copy the code from the box for pasting into your post or webpage. It also converts the other way round - i.e. it decodes as well as encodes.

Now if only I can get long code to wrap in my posts without mucking up its workings... ([added 21 May 2005:] which I now can thanks to redryder52, another yay!)

Technorati Tags: , , , , , , , , ,


Six said...

What would be great is a form which would auto-replace all of the "<", ">", and "&" characters with the replacement codes you specified here. You'd paste your original text into a textbox, then just one click and bye-bye brackets and ampersands! =)

Improbulus said...

Absolutely, Davetta, and coincidentally I found one and was about to post the link! Which I've now done, see above. :-)

Six said...

Hey, now that's what I call fast service! Thanks for letting us know about this! =)