public static MvcHtmlString IncludeRejuicedCssFor(this HtmlHelper instance, string filename) { var cachedValue = GetCachedIncludesFor(filename); if (cachedValue != null) { return(cachedValue.RenderHtml()); } var toInclude = GetIncludesFor(filename); var config = RejuicerEngine.GetConfigFor(filename); if (config == null) { return(MvcHtmlString.Create("")); } var dependencies = config.GetDependencies(); var links = MvcHtmlString.Create(string.Join("\n", toInclude.Select(f => { // Output <script src='' type=''> var link = new TagBuilder("link"); link.Attributes.Add("href", virtualPathResolver.GetRelativeUrl(f)); link.Attributes.Add("rel", "stylesheet"); link.Attributes.Add("type", "text/css"); return(link.ToString(TagRenderMode.SelfClosing)); }).ToArray())); var cachedIncludes = new IncludesCacheModel { IncludesHtml = links, HashValue = config.GetHashValue(cacheProvider) }; SetCachedIncludesFor(filename, cachedIncludes, dependencies); return(cachedIncludes.RenderHtml()); }
public byte[] TransformFile(PhysicalFileSource source, byte[] inputContent) { Log.WriteLine("Transforming CSS For '{0}'", source.VirtualPath); string stringValue = inputContent.ReadString(); stringValue = Regex.Replace(stringValue, "url\\((?<quotation>['\"]?)(?<capturedUrl>[^\\)]*)", x => { var url = x.Groups["capturedUrl"]; if (url != null) { var urlValue = url.Value.Trim(); if (!urlValue.StartsWith("~")) { return(x.Value); } var quotation = x.Groups["quotation"]; var quoteWrapper = quotation != null ? quotation.Value : ""; if (urlValue.EndsWith(quoteWrapper)) { urlValue = urlValue.Substring(0, urlValue.Length - quoteWrapper.Length); } var virtualPath = urlValue; // look for a placeholder in the virtual path if (virtualPath.Contains(RejuicerConfigurationSource.FilenameUniquePlaceholder)) { // Look for a configuration if (!RejuicerEngine.HasConfigurationFor(virtualPath)) { // Create a configuration for this file OnRequest.ForImage(virtualPath) .Compact .File(virtualPath.Replace(RejuicerConfigurationSource.FilenameUniquePlaceholder, "")) .Configure(); } // get the timestamp and write it out into the URL... var config = RejuicerEngine.GetConfigFor(virtualPath); if (config != null) { // don't timestamp the URL in pass through mode virtualPath = RejuicerEngine.IsPassThroughEnabled ? config.GetNonTimestampedUrl(_cacheProvider) : config.GetTimestampedUrl(_cacheProvider); // Need to add a dependency of this CSS file to the now linked image. source.AddDependency(RejuicerEngine.GetConfigFor(urlValue)); } else { // Could not configure rejuicer for this file. It may not exist, // so just return the url return(url.Value); } } var outputUrl = _virtualPathResolver.GetRelativeUrl(virtualPath); return(string.Format("url({1}{0}{1}", outputUrl, quoteWrapper)); } return(x.Value); }); return(stringValue.AsBytes()); }