Thursday, February 21, 2008

WayFormat: Source Code Color Formatter

There are lots of tools and ways to color format source code snippets so that they appear on blog posts and sites as they do in Visual Studio or other source editing tools. One of them is available at http://www.manoli.net/csharpformat/ which nicely formats the code to colorful HTML. But I was looking for a small desktop application that we can take along to use offline. Thankfully Manoli has provided the source code on the website for anyone to make use of it.



So, I made a small utility and named it WayFormat (Yeah… you guessed it right, the ‘Way’ comes from ‘Coolwayfarer’!). This standalone windows client application color formats the source code snippets in C, C++, C#, HTML, ASP, ASPX, XML,XAML, JScript, Java Script, Visual Basic, VB.Net, SQL, TSQL, PL/SQL and MSH (code name Monad) to style sheet enabled HTML code for publishing on a web site or in a blog. The tool has option to display line numbers, provide alternate line background color, and embed CSS.


Here is a sample code snippet formatted using WayFormat. I have hosted the css file at http://sameerct.googlepages.com/csharp.css and added a link tag in the head section of my blogger template pointing to this style sheet.


/// <summary>
/// Provides an option to save the CSS file.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void lnkCSS_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
try
{
FileInfo fileCSS = new FileInfo("csharp.css");

SaveFileDialog dlgCSS = new SaveFileDialog();
dlgCSS.FileName = "csharp.css";
dlgCSS.Filter = "Cascading Style Sheet|*.css";
dlgCSS.AddExtension = true;
dlgCSS.DefaultExt = "*.css";

if (dlgCSS.ShowDialog() == DialogResult.OK) fileCSS.CopyTo(dlgCSS.FileName);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}

I have made the source code open and is hosted at CodePlex at http://www.codeplex.com/wayformat so that it is available to anyone interested to add more features or use it along with other applications. Download the executable or source code from CodePlex and use CodePlex Discussion Forum and Issue Tracker to post your comments or report any bugs you may find.

And well, there is an easier way to color format source code without using any tools. Copy paste your source code from Visual Studio to Microsoft Word and the save the word document as html web page. Now copy the content from web page (not the source) and paste directly in the blog post editor. It will look as below:

FileInfo fileCSS = new FileInfo("csharp.css");

See that the system classes like FileInfo are color coded too. :)

1 comment:

  1. Great little application.

    Thanks for making it available (back in '08 ;).

    ReplyDelete