Click here to Skip to main content
15,891,777 members
Articles / Web Development / CSS

Combres - WebForm & MVC Client-side Resource Combine Library

Rate me:
Please Sign up or sign in to vote.
4.50/5 (8 votes)
1 Nov 2009Apache13 min read 52.2K   3.1K   22  
A .NET library which enables minification, compression, combination, and caching of JavaScript and CSS resources for ASP.NET and ASP.NET MVC web applications. Simply put, it helps your applications rank better with YSlow and PageSpeed.
#region License
// Copyright 2009 Buu Nguyen (http://www.buunguyen.net/blog)
// 
// Licensed under the Apache License, Version 2.0 (the "License"); 
// you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at 
// 
// http://www.apache.org/licenses/LICENSE-2.0 
// 
// Unless required by applicable law or agreed to in writing, software 
// distributed under the License is distributed on an "AS IS" BASIS, 
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
// See the License for the specific language governing permissions and 
// limitations under the License.
// 
// The latest version of this file can be found at http://combres.codeplex.com
#endregion

using System.Text;
using System.Text.RegularExpressions;
using System.Linq;

namespace Combres.Filters
{
    /// <summary>
    /// Enables variable support in CSS.  Specifically, this filter allows you to have a CSS 
    /// file like this:
    /// <![CDATA[
    /// @define
    /// {
    ///     boxColor: #345131;
    ///     boxWidth: 150px;
    /// }
    /// p
    /// {
    ///     color: @boxColor;
    ///     width: @boxWidth;
    /// }
    /// ]]>
    /// 
    /// The filter will make it becomes the following:
    /// <![CDATA[
    /// p
    /// {
    ///     color: #345131;
    ///     width: 150px;
    /// }
    /// ]]>
    /// 
    /// Credit to Rory Neopoleon (http://neopoleon.com/home/blogs/neo/archive/2004/03/06/8705.aspx)
    /// for the original idea/code.
    /// </summary>
    public class HandleCssVariablesFilter : ICombresFilter
    {
        public string TransformSingleContent(Settings settings, Resource resource, string content)
        {
            if (resource.ParentSet.Type != ResourceType.CSS)
                return content;

            // Remove comments because it may mess up the result
            // @TODO: this duplicates with the work of the minification, but if
            // done after the minification, then it affects the combined file
            content = Regex.Replace(content, @"/\*.+?\*/", "", RegexOptions.Singleline);
            var regex = new Regex(@"@define\s*{(?<define>.*?)}", RegexOptions.Singleline);
            var match = regex.Match(content);
            if (!match.Success)
                return content;

            var value = match.Groups["define"].Value;
            var variables = value.Split(';');
            var sb = new StringBuilder(content);
            variables.ToList().ForEach(var =>
                          {
                             if (string.Empty == var.Trim())
                                 return;
                             var pair = var.Split(':');
                             sb.Replace("@" + pair[0].Trim(), pair[1].Trim());
                          });

            // Remove the variables declaration, it's not needed in the final output
            sb.Replace(match.ToString(), string.Empty);
            return sb.ToString();
        }

        public string TransformCombinedContent(Settings settings, ResourceSet set, string content)
        {
            return content;
        }

        public string TransformMinifiedContent(Settings settings, ResourceSet set, string content)
        {
            return content;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Apache License, Version 2.0


Written By
Chief Technology Officer KMS Technology
Vietnam Vietnam
You can visit Buu's blog at http://www.buunguyen.net/blog to read about his thoughts on software development.

Comments and Discussions