Thursday, May 28, 2009

Sed for Blogger

Blogger/Blogspot has an unfortunate error that if you type a list like this:

test1
test2
test3

directly into a <pre> block, it looks something like this:

test1

test2

test3


As you can see, Blogger likes to mangle the newlines, effectively double-spacing the text. The solution is to remove all of the newlines and then replace them with <br> tags. There are a couple of online tools to do this, but its even easier using this sed command line:

sed 's/</\&lt;/g' INPUT_FILE | sed 's/>/\&gt;/g' | sed ':a;N;$!ba;s/\n/<br>/g'


The first sed converts < to the HTML-equivalent &lt;. The second sed does the same thing for >. The third one removes all newlines and replaces each one with <br>. The result is a single line that can be embedded in a pre and that will show up correctly in Blogger (or anywhere else, newline bug or not):

test1
test2
test3

No comments:

Post a Comment