Thursday, September 11, 2008

Publishing Source Code in Blogs

This is a topic I spent sporadic time looking into here and there. The main issue is if you want to write about code on a web page or blog, how do you go about preserving the format and code highlighting as they are in most major editors and IDEs?

I've found two main solutions which I find to be helpful, one that uses javascript to highlight code as its presented in your code and another that uses an emacs buffer to translate code from its formatted and highlighted form into HTML source.

SyntaxHighlighter

This is the more convenient of the two methods as you can do it straight from blogger. It involves hosting some code and modifying your blogger template to utilize that code. Then when you post code to blogger you utilize certain codes to provide the syntax highlighting. The sites I found to be most informative were the following:

htmlize.el

This method involves the use of emacs but is not limited to the languages supported by SyntaxHighlighter. Since emacs is such a widely used editor I would assume that syntax highlighting is supported for just about every programming language. I have no experience using emacs, but its only been on my radar to try out one day, considering my extensive use of Linux and VI. What better time than now?

htmlize.el (follow the link for the source) is an emacs package that "converts the buffer text and the associated decorations to HTML" as quoted by the original author. It was interesting because Steve Yegge described in a blog post of how he attempted to implent such a tool but was later pointed to htmlize by one of the comments on his blog.

Conclusion

I'm going to try both methods as time permits and provide more feedback here. If you're not interested in syntax highlighting and just code formatting than you can always go with this blog, that provides a text box for pasting formatted code and converts it into the necessary html format. Hope this helps!

3 comments:

joyRuN said...

I sooo didn't understand that post - too cerebral for me! What I do know is that you'll help me jazz up my bloggity blog in your oodles of spare time, because I'm your Ate.

Please?

Ryan Castillo said...

Sure Ate, no problem. Seeing as you're probably the only person who reads this randomness

.NET Dev said...

If you don't have ur own hosting...
Don't worry ...
See
http://urenjoy.blogspot.com/2008/10/publish-source-code-in-blogger.html