Friday 14 December 2007

Blogger fix

I noticed yesterday that Blogger does not automatically escape html such as <p> when working in the editor in the 'Compose' tab. Since I'm going to be talking about markup, the first thing to do is to knock up a simple html escape utility so I can paste markup into this blog editor and have it come out correctly. The following should do the trick, implemented as a bash shell function, but probably isn't comprehensive:
# Escape html for unsmart html editors
function eschtml() {
  # Taking the input from a file is probably the easiest
  # , and let's save it to a to file of the same name with
  # .esc appended as an extension
  cat $1 | sed 's/\&/\&amp;/g' | sed 's/</\&lt;/g' | sed 's/>/\&gt;/g' >$1.esc
  echo "Escaped version now in $1.esc"
}
It seems to do the trick: The original file 'func.htm' looked like the above, but in order to post that, I had to paste the output of func.htm.esc into the editor. And for recursion lovers, that latter looked like:
# Escape html for unsmart html editors
function eschtml() {
  # Taking the input from a file is probably the easiest
  # , and let's save it to a to file of the same name with
  # .esc appended as an extension
  cat $1 | sed 's/\&amp;/\&amp;amp;/g' | sed 's/&lt;/\&amp;lt;/g' | sed 's/&gt;/\&amp;gt;/g' &gt;$1.esc
  echo "Escaped version now in $1.esc"
}
Yes, you guessed it - that was made from the output of func.htm.esc.esc

No comments: