private static string CompressCSS(string path)
 {
     var rawcss = File.ReadAllText(path);
     var cssCompressor = new CssCompressor();
     var compressed = cssCompressor.Compress(rawcss);
     return compressed;
 }
Beispiel #2
0
 /// <inheritdoc cref="IResourceMinifier.Minify" />
 public string Minify(Settings settings, ResourceSet resourceSet, string combinedContent)
 {
     var compressor = new CssCompressor();
     compressor.LineBreakPosition = LineBreakPosition == null ? -1 : LineBreakPosition.Value;
     compressor.RemoveComments = RemoveComments == null ? true : RemoveComments.Value;
     return compressor.Compress(combinedContent);
 }
        public override void Process(BundleContext context, BundleResponse response)
        {
            var compressor = new CssCompressor();

            response.Content = compressor.Compress(response.Content);
            response.ContentType = ContentTypes.Css;
        }    
Beispiel #4
0
        /// <summary>
        /// Process the request
        /// </summary>
        public void ProcessRequest(HttpContext context)
        {
            DateTime mod = new FileInfo(Assembly.GetExecutingAssembly().Location).LastWriteTime ;
            var compressor = new CssCompressor() ;

            if (File.Exists(context.Server.MapPath("~/Areas/Manager/Content/Css/Style.css"))) {
                FileInfo file = new FileInfo(context.Server.MapPath("~/Areas/Manager/Content/Css/Style.css")) ;
                mod = file.LastWriteTime > mod ? file.LastWriteTime : mod ;
            }

            if (!ClientCache.HandleClientCache(context, resource, mod)) {
                StreamReader io = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream(resource)) ;
                context.Response.ContentType = "text/css" ;
            #if DEBUG
                context.Response.Write(io.ReadToEnd()) ;
            #else
                context.Response.Write(compressor.Compress(io.ReadToEnd()).Replace("\n","")) ;
            #endif
                io.Close() ;

                // Now apply standard theme
                if (ConfigurationManager.AppSettings["disable_manager_theme"] != "1") {
                    io = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream(theme)) ;
            #if DEBUG
                    context.Response.Write(io.ReadToEnd()) ;
            #else
                    context.Response.Write(compressor.Compress(io.ReadToEnd()).Replace("\n","")) ;
            #endif
                    io.Close() ;
                }

                // Now check for application specific styles
                if (File.Exists(context.Server.MapPath("~/Areas/Manager/Content/Css/Style.css"))) {
                    io = new StreamReader(context.Server.MapPath("~/Areas/Manager/Content/Css/Style.css")) ;
            #if DEBUG
                    context.Response.Write(io.ReadToEnd()) ;
            #else
                    context.Response.Write(compressor.Compress(io.ReadToEnd()).Replace("\n","")) ;
            #endif
                    io.Close() ;
                }
            }
        }
 public string Minify(string content)
 {
     CssCompressor cssCompressor = new CssCompressor
     {
         CompressionType = CompressionType.Standard,
         RemoveComments = true,
         LineBreakPosition = -1
     };
     return cssCompressor.Compress(content);
 }
Beispiel #6
0
 public static void cssInPlaceMinify(string dplUrl) {
   var files = FileSources.pathsFromDpl(dplUrl).ToArray();
   var compr = new CssCompressor();
   foreach (var fn in files) {
     var res = compr.Compress(File.ReadAllText(fn, Encoding.UTF8));
     var destFn = fn.Replace(".css", ".min.css");
     File.WriteAllText(destFn, res);
     Trace.TraceInformation("Minified CSS: " + destFn);
   }
 }
        public override void Process(BundleContext context, BundleResponse response)
        {
            response.ContentType = ContentTypes.Css;

            if (context.HttpContext.IsDebuggingEnabled)
            {
                return;
            }

            var compressor = new CssCompressor();
            response.Content = compressor.Compress(response.Content);
        }    
Beispiel #8
0
 public CompileResults Compile(string inPath, string outPath)
 {
     using (StreamReader sr = new StreamReader(inPath))
     {
         string content = sr.ReadToEnd();
         var x = new CssCompressor();
         string output = x.Compress(content);
         using (StreamWriter sw = new StreamWriter(outPath))
         {
             sw.Write(output);
         }
     }
     return null;
 }
Beispiel #9
0
        private static string CompressCss(string content)
        {
            string results = "";
            try
            {
                var cssCompressor = new CssCompressor();
                results = cssCompressor.Compress(content);
            }
            catch (Exception ex)
            {
                WriteLog(ex);
            }

            return results;
        }
Beispiel #10
0
        public static MvcHtmlString MinifyInlineCss(this HtmlHelper html, Func <object, object> markup)
        {
            string notMinifiedCss = (markup.Invoke(html.ViewContext)?.ToString() ?? "").TrimStart("<style>")
                                    .TrimStart("<text>").TrimEnd("</style>").TrimEnd("</text>");

            var minifier = new Yahoo.Yui.Compressor.CssCompressor()
            {
                RemoveComments = true
            };

            minifier.CompressionType = Yahoo.Yui.Compressor.CompressionType.Standard;
            var minifiedCss = minifier.Compress(notMinifiedCss);

            return(new MvcHtmlString("<style>" + minifiedCss + "</style>"));
        }
 /// <summary>
 /// Filters the assets
 /// </summary>
 /// <param name="Assets">Assets to filter</param>
 /// <returns>The filtered assets</returns>
 public IList<IAsset> Filter(IList<IAsset> Assets)
 {
     if (Assets == null || Assets.Count == 0)
         return new List<IAsset>();
     if (Assets.FirstOrDefault().Type != AssetType.CSS)
         return Assets;
     var Processable = Assets.Where(x => !x.Minified);
     if (Processable.FirstOrDefault() == null)
         return Assets;
     var Minifier = new CssCompressor { CompressionType = CompressionType.Standard, RemoveComments = true };
     foreach (IAsset Asset in Processable.Where(x => x != null))
     {
         try
         {
             Asset.Content = Minifier.Compress(Asset.Content);
             Asset.Minified = true;
         }
         catch { }
     }
     return Assets;
 }
Beispiel #12
0
        static void Main(string[] args)
        {
            foreach (var arg in args)
                Console.WriteLine(arg);

            List<string> cssFiles = new List<string>();
            List<string> jsFiles = new List<string>();

            foreach (var file in args)
            {
                if (file.EndsWith(".css"))
                    cssFiles.Add(file);
                else if (file.EndsWith(".js"))
                    jsFiles.Add(file);
            }

            var cssCompressor = new CssCompressor();
            var jsCompressor = new JavaScriptCompressor();

            foreach(var jsFile in jsFiles){
                var compress = jsCompressor.Compress(File.ReadAllText(jsFile));
                var newPath = jsFile.Replace(".js", "") + ".min.js";
                using (StreamWriter swr = new StreamWriter(newPath,false))
                {
                    swr.Write(compress);
                    swr.Close();
                }
            }

            foreach (var cssFile in cssFiles)
            {
                var compress = cssCompressor.Compress(File.ReadAllText(cssFile));
                var newPath = cssFile.Replace(".css", "") + ".min.css";
                using (StreamWriter swr = new StreamWriter(newPath, false))
                {
                    swr.Write(compress);
                    swr.Close();
                }
            }
        }
Beispiel #13
0
 internal static string CSS(string css)
 {
     var cssCom = new CssCompressor();
     return cssCom.Compress(css);
 }
        private void Minify(FileInfo source, FileInfo file)
        {
            if (Options.IsDebugLoggingEnabled)
                Logger.Log("Generating minified css file.");

            try
            {
                var css = File.ReadAllText(source.FullName);
                string minified = "";
                if (!string.IsNullOrEmpty(css))
                {
                    var compressor = new CssCompressor { RemoveComments = true };
                    minified = compressor.Compress(css);
                }

                InteropHelper.CheckOut(file.FullName);
                File.WriteAllText(file.FullName, minified, UTF8_ENCODING);

                // nest
                if (Options.IncludeCssInProject)
                    AddFileToProject(source, file, Options);
            }
            catch (Exception ex)
            {
                Logger.Log(ex, "Failed to generate minified css file.");
                if (Options.ReplaceCssWithException)
                    SaveExceptionToFile(ex, file);
            }
        }
 public string Compress(string contents)
 {
     var cssCompressor = new CssCompressor();
     return cssCompressor.Compress(contents);
 }
Beispiel #16
0
        string YUI_CSS(string filesource, ConfigSettings.YUICompressionSettings.CSS CssSettings)
        {
            try
            {
                var csscompressor = new CssCompressor();
                csscompressor.CompressionType = CssSettings.CompressionType;
                csscompressor.LineBreakPosition = CssSettings.LineBreakPosition;
                csscompressor.RemoveComments = CssSettings.RemoveComments;

                return csscompressor.Compress(filesource);
            }
            catch (Exception ex)
            {
                var msg = ex.Message;
                return filesource;
            }
        }
 /// <summary>
 ///     Minimizes the CSS code.
 /// </summary>
 /// <param name="inputCssFile">The input CSS file.</param>
 /// <returns></returns>
 public string MinimizeCssCode(string inputCssFile)
 {
     var cssContents = File.ReadAllText(this.RapContext.MapPath(inputCssFile));
     var cssCompress = new CssCompressor();
     return cssCompress.Compress(cssContents);
 }
        private string TransformContent(BundleResource bundleResource, HttpContext context)
        {
            var bundleType = bundleResource.Type;
            var content = bundleResource.Content;
            var serverpath = bundleResource.ServerPath;

            try
            {
                if (bundleType == BundleResourceType.EmbeddedScript || bundleType == BundleResourceType.ScriptFile)
                {
                    var compressor = new JavaScriptCompressor
                    {
                        CompressionType = CompressionType.Standard,
                        Encoding = Encoding.UTF8,
                        ObfuscateJavascript = bundleResource.ObfuscateJs
                    };


                    //Minimize
                    var contentOut = compressor.Compress(content.Trim()) + ";";

                    //Return deffered execution
                    if (ClientSettings.IsJavascriptDefferingEnabled)
                    {
                        return string.Format(ClientSettings.JavascriptDefferingScript,
                                      JsonConvert.SerializeObject(contentOut));
                    }
                    return contentOut;
                }
                if (!string.IsNullOrEmpty(serverpath))
                {
                    string directoryName = Path.GetDirectoryName(serverpath);
                    if (directoryName != null)
                        serverpath = directoryName.Replace('\\', '/');
                }
                if (bundleType == BundleResourceType.EmbeddedStyle || bundleType == BundleResourceType.StyleFile)
                {
                    var compressor = new CssCompressor
                                         {
                                             CompressionType = CompressionType.Standard,
                                             RemoveComments = true
                                         };

                    return ReplaceUrls(compressor.Compress(content), serverpath, context);
                }
                if (bundleType == BundleResourceType.LessFile)
                {
                    DotlessConfiguration cfg = DotlessConfiguration.GetDefaultWeb();
                    cfg.Web = true;
                    cfg.MinifyOutput = true;
                    cfg.MapPathsToWeb = true;
                    cfg.CacheEnabled = false;

                    //Prefilter
                    content = ReplaceImportRegex.Replace(content, match => ReplaceImports(match, serverpath));
                    string processed = ReplaceUrls(LessWeb.Parse(content, cfg), serverpath, context);
                    return processed;
                }
            }
            catch (EcmaScript.NET.EcmaScriptException e)
            {
                _log.ErrorFormat("EcmaScriptException: {0} in {1} at {2} ({3}, {4}) at ", e.Message, serverpath, e.LineSource, e.LineNumber, e.ColumnNumber, e.ScriptStackTrace);
            }
            catch (Exception e)
            {
                _log.Error(e);
            }
            return content;
        }
Beispiel #19
0
        /// <summary>
        /// 获取文件压缩内容
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <param name="inputCharset">web服务器编码格式</param>
        /// <returns>文件压缩内容</returns>
        private string GetCompressContent(string fileName, string inputCharset)
        {
            Encoding encoding = Encoding.UTF8;

            if (!string.IsNullOrWhiteSpace(inputCharset))
            {
                try
                {
                    encoding = Encoding.GetEncoding(inputCharset);
                }
                catch (Exception ex)
                {
                    LogUtil.Log("XFramework文件压缩编码转换异常", ex, LogLevel.Warn);
                }
            }

            string rtnRst = string.Empty;

            try
            {
                if (!string.IsNullOrWhiteSpace(DirectoryPath)) DirectoryPath = "\\" + DirectoryPath + "\\";

                string filePath = HttpContext.Current.Server.MapPath(DirectoryPath + fileName);

                if (!File.Exists(filePath)) return null;

                rtnRst = File.ReadAllText(filePath, encoding);

                if (IsJsFile)
                {
                    if (filePath.EndsWith(".no.js") && !filePath.EndsWith(".min.js")) return rtnRst;

                    var compressor = new JavaScriptCompressor { Encoding = encoding };

                    rtnRst = compressor.Compress(rtnRst);
                }
                else
                {
                    if (filePath.EndsWith(".no.css") && !filePath.EndsWith(".min.css")) return rtnRst;

                    var compressor = new CssCompressor();

                    rtnRst = compressor.Compress(rtnRst);
                }
            }
            catch (Exception ex)
            {
                LogUtil.Log("XFramework文件压缩异常", ex, LogLevel.Warn);
            }

            return rtnRst;
        }