17 July, 2007

HowTo post html code into blog

Sometimes bloggers need to publish some text, which not displayed correctly into their blog.

For example, you can't post this html directly into your blog:

<html>
<head>
<title>XLam</title>
</head>
<body>
<script language='javascript' src='xqx.web.xlam.XLam.nocache.js'></script>
<div id='uid'></div>
</body>
</html>

but you can convert it into this format:

&lt;html&gt;
&lt;head&gt;
&lt;title&gt;XLam&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;script language='javascript' src='xqx.web.xlam.XLam.nocache.js'&gt;&lt;/script&gt;
&lt;div id='uid'&gt;&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;

and then successfully publish it.

For this purpose, bloggers may use this script:

#!/bin/bash

#This simple shell script replaces symbols in the given xml/html file and writes it to file file.blog
#Symbols to replace:
# '<' with &lt;
# '>' with &gt;
# '&' with &amp;
#
#Usage: html2blog.sh file

sed -e 's/\x26/\&amp;/g;s/\x3c/\&lt;/g;s/\x3e/\&gt;/g' $1 >$1.blog


It works on Linux/Unix platforms or under Cygwin environment.

No comments: