New F# SyntaxHighlighter Brush

12. November 2008

Ever since my previous blog about Code Highlighters For Your Blog, I have heard several people interested in implementing the SyntaxHighlighter on their blog. If so, there is a good article by Guogang Hu about integrating it into your Blogger blog.

One of those individuals was my friend, Daniel Mohl (via twitter... @dmohl). However, Daniel had a need to support F#, which is not one of the out-of-the-box languages that is supported by the SyntaxHighlighter. I saw this as a fun side project to support this language for him.

F.Y.I. If you like F#, his blog is definitely one you should check out!

Here is an example F# program that is only a proof of concept that Dan put together for me to exercise the F# brush sufficiently... it doesn't actually work! :) I am just starting to learn the language, so please give me some slack!

If you find any areas that I missed while supporting the language, please let me know and I'll fix it. Feel free to download and use the F# SyntaxHighlighter Brush and provide any suggestions or comments.

Note: The JavaScript file has been minimized with JSMin, so it might be a little difficult to read.

I'd like to contribute the F# Brush to the Google Code project, but I am not yet a contributor on the project. If I am able to become a contributor, then I will post the code as part of the main project.

#light
namespace testforElijah
module testModuleForElijah
open System
let multiple x = x * x
val CalculateSeqExample: seq -> int
type ICustomerDao = interface
abstract GetById: int -> ICustomer
end
type CustomerDao = class
new: unit -> CustomerDao
interface ICustomerDao
end
if
then
elif
else
@"c:\test\test.xml"
'c'
"xyz"B
"tes"+"t"
[for x in list -> expr]
type 'a option =
| None
| Some of 'a

//This is a test
let showSomething (name, something)
match name with
| Some (some, thing) -> (* do something*)
| None

type node =
{ Name : string;
Links : list list;}
and link =
| Dangling
| Link of node
[More]

Tools, F# ,


How to use SyntaxHighlighter

31. October 2008
I recently tweeted that I thought the SyntaxHighlighter is the best way to display code on your website. In response, I had some who were a little confused how to get started with the tool.

The intent of this blog entry is to show you, step by step, how to integration SyntaxHighlighter into your website.
  1. Download SyntaxHighlighter
  2. Unzip the contents with your favorite unzipping tool (personally I used 7-Zip Portable)
  3. ... some code here ...
    Language Aliases
    C++ cpp, c, c++
    C# c#, c-sharp, csharp
    CSS css
    Delphi delphi, pascal
    Java java
    Java Script js, jscript, javascript
    PHP php
    Python py, python
    Ruby rb, ruby, rails, ror
    Sql sql
    VB vb, vb.net
    XML/HTML xml, html, xhtml, xslt



1. The solution above is found here: http://yehhou.blogspot.com/2007/06/blogger-dpsyntaxhighlighter.html. That blog was written in Chinese. I made some minor changes and posted here. The reason for the Javascript code to remove
tags is because BLOGGER has a setting to automatically add
where there is line break. It is very convenient feature for blog posters, but somehow syntax highlighter will display the
tag in plain text;
2. Clipboard for the SyntaxHighlither does not work well here. It tends to give you the text without line breaks, so I commented out the Javascript that setup the clipboard function;
3. When posting HTML code, remember to replace < with <, and replace > with >;
1. Download and find a place to host the library files for SyntaxHighlighter. A free choice is, of course, Google Page Creator;
2. Login to BLOGGER, go to: "Settings > Template > Edit HTML", make the following changes (credit goes to yehyeh's blog):
http://yehhou.blogspot.com/2007/06/blogger-dpsyntaxhighlighter.html [More]

Tools


An NDepth Look @ NDepend

31. October 2008
Synpopsis - I've been wanting to review NDepend for quite some time, but I figured it was bad form to run it against my production code from work. So, instead of running the tool against my work code, I thought I'd use the PetShop ASP.NET MVC project that I created for the .NET User's Group.

NDepend provides many ways to visualize the architecture of any particular project.  To create the following pictures and diagrams all I did was point NDepend to my Visual Studio solution file.

Dependency Graph

A useful way to get an understanding of how your application architecture is to generate a Dependency Graph based off your code. The following graph shows show the PetShop pieces relate to one another.

 

Dependency Matrix

For those of you who are graphically challenged, there is a Dependency Matrix that does a nice job of showing how many dependencies there are between 2 particular areas of your design.

  

Code Metrics

The next diagram has always impressed me. Even if you didn't know what it meant... it is pretty impressive to just look at :) The following diagram is actually showing the Code Metrics for your project. The following image represents the number of lines of code (LOC) in your project.  


CQL Queries

In addition to all these cool graphs, matrices, and images, there are some hard core queries that you can perform against your assemblies. NDepend has something called Code Query Language (CQL) that allows you to write custom queries to identify numerous statistics such as:
  • Which public methods have more than 30 lines of code?
  • Which classes implement System.IDisposable?
  • Which methods have been refactored recently and is not thoroughly covered by tests?
  • etc...

 

The following is an example of what a CQL query looks like. This query identifies the methods that are too big in your assembly. NDepend comes with a library of already created queries, such as this one, but you are able to add to or customize these quieres as well. 


Conclusion

There is so many more features that I haven't covered that NDepend provides, but I wanted to highlight some of the features that I thought are very useful and could help you in your current projects.

I think this tools adds some great functionality that dovetails nicely with the features of from the various Visual Studio suites.

Don't just take my word for it, I encourage you to download a trial version of NDepend and check it out for yourself.

Then if you like it, make a list of the features that are useful for your project, show it to your boss, and see if your work will pay for it :)
[More]

Tools


IE8 Beta 2 Pros

25. September 2008

I do like the Developer Tools that are embedded in IE 8 Beta 2. They are about equivalent to what Firebug is to Firefox, which is a great step in the right direction.

I have also noticed that the errors returned from JavaScript exceptions are much more descriptive and accurate compared to IE7 or before. [More]

Tools, Internet Explorer ,


jQuery Flexigrid Using C# 3.0 (.NET 3.5) & LINQ

23. July 2008

When I developed my first ASP.NET MVC application, I was a little disappointed with my options for a rich grid. I initially used the grid that is part of the MVCContrib project, but it is pretty simple and there aren’t many features out of the box.

I was very pleased with the code of my MVC application, but the presentation was so 1990's. With all of this new technology I thought the presentation deserved something snappy. That is when I found Flexigrid.

As I mentioned in my last blog entry, I started to use jQuery. To my joy, Flexigrid is a jQuery plugin! Flexigrid uses jQuery to asynchronously populate the contents of the grid using either XML or JSON input.

The following is an example of what the grid looks like. It contains features to sort, page, search, move columns, resize, etc…

flexigridExample

The User Interface portion was pretty straightforward to put together. You just need to define your columns, the data source, and some additional parameters (such as: search terms, size, etc…).
   1: "ctlFlex" style="display: none">
   2: 

I wanted to use this opportunity to try out some new features of .NET 3.5, so I wanted to incorporate LINQ and JSON serialization.

To do this, I needed to setup some classes that the Flexigrid will recognize once serialized. Here is what I came up with.

   1: public class FlexigridViewData
   2: {
   3:     public int page;
   4:     public int total;
   5:     public List rows = new List();
   6: }
   7:  
   8: public class FlexigridRow
   9: {
  10:     public long id;
  11:     public List<string> cell;
  12: }

Now is the part where the fun begins. I had already retrieved the content I needed from the Middle Tier. I use LINQ to query the generic list to obtain the correct page subset and then use a helper extension method to serialize the contents to JSON.

   1: public void Page_Load()
   2: {
   3:     Response.Clear();
   4:     Response.ContentType = "text/x-json";
   5:     Response.Write(GetPagedContent());
   6:     Response.Flush();
   7:     Response.End();
   8: }
   9:  
  10: private string GetPagedContent()
  11: {
  12:     var pageIndex = Convert.ToInt32(Request.Params["page"]);
  13:     var itemsPerPage = Convert.ToInt32(Request.Params["rp"]);
  14:     var sortName = Request.Params["sortname"];
  15:     var sortOrder = Request.Params["sortorder"];
  16:     var query = Request.Params["query"];
  17:  
  18:     IEnumerable pagedContacts;
  19:     if (string.IsNullOrEmpty(query))
  20:     {
  21:         pagedContacts = sortOrder.Equals("asc") ?
  22:             Contacts.OrderBy(contact => contact.GetPropertyValue(sortName)) :
  23:             Contacts.OrderByDescending(contact => contact.GetPropertyValue(sortName));                
  24:     }
  25:     else
  26:     {
  27:         Funcbool> whereClause = (contact => contact.GetPropertyValue<string>(sortName).Contains(query));
  28:         pagedContacts = sortOrder.Equals("asc", StringComparison.CurrentCultureIgnoreCase) ?
  29:             Contacts.Where(whereClause).OrderByDescending(contact => contact.GetPropertyValue(sortName)) :
  30:             Contacts.Where(whereClause).OrderBy(contact => contact.GetPropertyValue(sortName));
  31:     }
  32:     int count = pagedContacts.Count();
  33:     pagedContacts = pagedContacts.Skip((pageIndex - 1) * itemsPerPage).Take(itemsPerPage);
  34:  
  35:     const string imageLinkFormat = @""{0}"">"{1}"" border=""0"" />";
  36:     const string imageFormat = @""{0}"" border=""0"" />";
  37:     var flexigrid = new FlexigridViewData {page = pageIndex, total = count};
  38:     foreach (var contact in pagedContacts)
  39:     {
  40:         flexigrid.rows.Add(new FlexigridRow
  41:         {
  42:             id = contact.ID,
  43:             cell = new List<string> 
  44:             { 
  45:                 string.Format(imageLinkFormat, ResolveUrl("~/Contact.mvc/Detail/" + contact.ID), 
  46:                 ResolveUrl("~/Images/Detail.gif")), 
  47:                 contact.ID.ToString(), 
  48:                 contact.FirstName, 
  49:                 contact.LastName, 
  50:                 contact.DateOfBirth.ToShortDateString(), 
  51:             }
  52:         });
  53:     }
  54:  
  55:     return flexigrid.ToJson();    
  56: }

Here are some helper extension methods that I used to complete the above code snippets.

   1: public static class JsonHelper
   2: {
   3:     public static string ToJson(this object obj)
   4:     {
   5:         var serializer = new JavaScriptSerializer();
   6:  
   7:         return serializer.Serialize( obj );
   8:     }
   9:  
  10:     public static string ToJson(this object obj, int recursionDepth)
  11:     {
  12:         var serializer = new JavaScriptSerializer();
  13:  
  14:         serializer.RecursionLimit = recursionDepth;
  15:  
  16:         return serializer.Serialize( obj );
  17:     }
  18: }

   1: public static T GetPropertyValue(this object component, string propertyName)
   2: {
   3:     return (T) TypeDescriptor.GetProperties(component)[propertyName].GetValue(component);
   4: }

As a side note, I did have to dive into the JavaScript and fix two issues that I came across, but other than that it works like a charm.

I would prefer if the developer of the product had a better system of tracking bugs and maintaining a forum, but in the meantime what is setup is sufficient.

[More]

Tools, AJAX, ASP.NET MVC, jQuery , , ,


Olark Livehelp