SmoothFriction.nl

posts - 39, comments - 20, trackbacks - 0

LessCSS, the .NET edition

I’ve been hesitant on publishing this, because I’m never happy with the code I write. As it happens, I have the same problem with writing articles for my blog as well (some people tell me I simply try too hard…). Anyway, without further delay, I present to you my latest pet project: Less.NET!

What is Less.NET?

Less.NET is a C# port of the LessCSS ruby gem. I have been using LessCSS in a few test projects, and I am really happy with the way it structures my CSS files (see http://www.lesscss.org for more info).

Why port to .NET?

This is actually a multipart story. First off, I want to be able to simply use a IHttpHandler implementation which I can wire up in my web.config to parse lessCSS files and output CSS, directly. With the ruby gem I managed this by writing a very crude wrapper which fires off a new process which executes the less compiler. Not only is this very clunky, it’s also not really reliable. I needed (read: wanted) a better way to do this.

The second part is that ruby is simply too slow to my liking (cue ‘define slow’ discussion!). The parsing of the files starts to take a significant amount of time when files get bigger/more complex, which I think is better spent coding.

Third, and perhaps the most important reason: I want to learn. Parsing CSS is a nice way to learn how to parse a DSL using Antlr, something I wanted to try for a while. Little did I know that CSS can get pretty complex when adding all those little, unknown, hardly supported features that are in CSS3… I also wanted to get some hands on experience with running an open-source project, although I don’t know if I’ll really get the community input I’m hoping for.

How it works

Less.NET is written in C#, using Antlr to generate a lexer/parser, Linq to objects for a few collection based operations and mainly a lot of (way too verbose) code. It’s still in a pre-pre-pre-alpha stage, possibly even a throw-away-prototype. The flow of the library is basically this:

(Less)CSS –> Lexer –> Parser –> Less.NET object tree –> some magic on the tree –> CSS

I’ve highlighted the bits that are at the core of the library. The object tree is a pretty simple model, starting with a simple document, branching to rules, variables, selectors and properties.

  • Document
    • Rules
      • Selectors
      • Properties
      • Rules
      • Mixins
    • Variables

Using this tree, I can easily traverse the tree, flatten it (merge all nested rules with their parents), evaluate the mixins/variables and output some CSS. That’s all there is to it!

Roadmap

Currently, I’m getting all specs taken from the ruby sources to pass, or at least to generate equivalent CSS. Due to different processing steps it might be possible that the CSS output string is not 100% equal to the ruby gem’s output. The semantics of the output should be the same though. Aside from that, I’m trying to come up with meaningful things to test. I’ve noticed that while writing/changing the grammar, I’ve broken my build many times. I think the first thing I’m going to write tests for is the parser, testing all rules for expected output.

To put it into measurable blocks, this is what I want to have before adding the version 0.1 label to the project:

Where to find it

You can find the sources on github: http://github.com/erikvanbrakel/less.net/

A few notes on the sources. It might not build. It might contain duplicate code. It might be ugly altogether. Please remember that it’s a prototype, with a LOT of refactoring to do. If you want to help me out, drop me a line on twitter (@erikvanbrakel), or via github. Help can be anything ranging from antlr work (grammar definition) to C# coding, even simply giving suggestions or tips on how to improve the code is good. Heck, even simply saying you’re following it helps, because that gives me incentive to put more time in it!

Thanks to:

Now go, go and give me some feedback!

Update

LessCss for .NET is now available in the form of .Less. I teamed up with Chris and Daniel, we moved to Chris’ codebase and worked from there. The source, latest builds and google community are linked directly from the site at http://www.dotlesscss.com/.

Game on!

Print | posted on Thursday, August 13, 2009 11:48 PM |

Feedback

Gravatar

# re: LessCSS, the .NET edition

Absolutely gutted :-). I started on the route of porting this too and to be fair your further along the path than I am. See: http://github.com/chrisjowen/nLess/tree/master. To be honest I knew nothing about parsing theory or Ruby before I started, so it was a great learning curve either way. I'm glad that someone got this out for the .NET sphere anyway, its a great idea, Have you talked to the guys at Less about publicizing your version on their site, its definitely worthwhile to the community, and I would be glad to help out where I could.
8/22/2009 9:45 PM | Chris Owen
Gravatar

# re: LessCSS, the .NET edition

Erik it's probably worth editing this post to point to the http://www.dotlesscss.com site as this post still ranks quite hight for "Less css for .NET" in Google.
1/7/2010 11:42 AM | Chris Owen
Gravatar

# re: LessCSS, the .NET edition

You rock Phil! This looks to be awesome. Except, I'm having trouble getting it going in my project. It doesn't seem to matter where I put the dotless.core.dll, I keep getting "Compiling transformation: Metadata file 'dotless.Core.dll' could not be found". I've included it in a solution folder like your screenshot above, and your sample solution doesn't give me the same problem, so I'm kinda out of ideas. Anybody got any tips for me?
3/4/2010 1:19 PM | gioco del Poker

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 8 and 4 and type the answer here: