Sunday, December 9, 2007

Publishing Perl, Shell and Other Code on Blogspot

Good Day

Today's post is slightly off-topic, although directly to the point of what this blog is about. Today, I'd like to pass on a few tips that might help fellow bloggers, like myself, in the future.

The tips we'll be going over today will help tremendously (especially if you're just getting started) if you publish a blog that includes lots of examples of scripting and/or code. The default setup and editors for Blogspot and Blogger provide certain mechanisms for making this possible, however, they're not entirely obvious. I spent a good deal of time piecing this stuff together from various posts in the Google Blog Help group and Blogspot's Help Center. Many thanks to all the people who contribute to these sites.

The first thing to note is that Blogspot/Blogger does allow for you to include a <code> tag, which it seems to honor. Unfortunately, this does not preserve the integrity of your code. All it does is indent the entire section of code slightly and remove all indentation!

The <blockquote> tag, also does approximately the same thing.

In order to get your code to post "as it is," you need to include both a <blockquote> and a <pre> tag (with closing tags, of course - e.g. </pre> </blockquote>).

But, that's not the end. If you use < redirects or Perl's while input markers (< and >) you'll get nailed with gigantic "Tags cannot be unclosed" errors that sometimes end up being larger than your post itself, and prevent it from publishing.

The only reason you can see the one's I've put up in this post is that I entered them as "&lt;" and "&gt;" (for < and > respectively). Even more confusing; in order for me to print out those special characters so that they showed up the way you need to type them, I had to enter them differently. For instance &lt; had to be entered as & amp; lt; (Just remove the spaces between the 3 elements. I'm getting dizzy from all the dereferencing ;)

I'll be going through all my old posts and throwing in those format tags (luckily I've only been at this for a few months (about 70 odd posts))

Hopefully, this post will be helpful for anyone looking for information specific to including computer code and script in your Blogspot/Blogger posts.

BTW, you would not believe how convoluted this post looks in the editor ;)

Example straight code:


if [ this -gt that ]
then
how_about_that=1
fi < /export/home/food.txt
<-- Note space added before /export to avoid getting the publishing error.

Example formatted code:


if [ this -gt that ]
then
how_about_that=1
fi </export/home/food.txt


Best Wishes,

, Mike