Пример #1
0
        protected bool compressYUI(string input, out string output)
        {
            bool success;

            try
            {
                var reporter = new LinterECMAErrorReporter();
                Yahoo.Yui.Compressor.JavaScriptCompressor compressor;
                compressor = new JavaScriptCompressor(input, true, Encoding.UTF8,
                                                      System.Globalization.CultureInfo.CurrentCulture,
                                                      AllowEval,
                                                      reporter);
                output  = compressor.Compress(true, true, false, 80);
                success = Reporter.Errors.Count == 0;
            }
            catch
            {
                output  = "";
                success = false;
            }

            return(success);
        }
Пример #2
0
        protected bool compressYUI(string input, out string output)
        {
            bool success;
            try
            {
                var reporter = new LinterECMAErrorReporter();
                Yahoo.Yui.Compressor.JavaScriptCompressor compressor;
                compressor = new JavaScriptCompressor(input, true, Encoding.UTF8,
                System.Globalization.CultureInfo.CurrentCulture,
                    AllowEval,
                    reporter);
                output = compressor.Compress(true, true,false, 80);
                success = Reporter.Errors.Count == 0;
            }
            catch
            {
                output = "";
                success = false;
            }

            return success;
        }
Пример #3
0
        public bool Minimize()
        {
            if (String.IsNullOrEmpty(Input)) {
                throw new Exception("Input must be specified.:");
            }
            if (CompressorType == 0)
            {
                throw new Exception("No compressor type specified.");
            }
            string compressedYui = String.Empty;
            string compressedPacker = String.Empty;
            string header = String.Empty;
            string javascript = Input;
            //string javascript = File.ReadAllText(path);
            if (KeepHeader)
            {
                int pos = javascript.IndexOf("/*");
                int endPos = -1;
                string leadin = String.Empty;
                if (pos >= 0)
                {
                    leadin = javascript.Substring(0, pos);
                    if (leadin.Trim() == string.Empty)
                    {
                        endPos = javascript.IndexOf("*/", pos + 1);
                        header = javascript.Substring(pos, endPos + 2) + Environment.NewLine;
                        javascript = javascript.Substring(endPos + 2);

                    }
                }
            }

            if (CompressorType == CompressorType.yui || CompressorType == CompressorType.best)
            {

                var reporter = new LinterECMAErrorReporter();
                Yahoo.Yui.Compressor.JavaScriptCompressor compressor;
                compressor = new JavaScriptCompressor(javascript, true, Encoding.UTF8,
                System.Globalization.CultureInfo.CurrentCulture,
                    true,
                    reporter);
                compressedYui = compressor.Compress(true, true, false, 80);
            }
            if (CompressorType == CompressorType.packer || CompressorType == CompressorType.best)
            {
                JavascriptPacker jsPacker = new JavascriptPacker(JavascriptPacker.PackerEncoding.None, false, false);
                compressedPacker = jsPacker.Pack(javascript);
            }
            CompressorType finalPacker = CompressorType != CompressorType.best ? CompressorType :
                    (compressedYui.Length < compressedPacker.Length ? CompressorType.yui : CompressorType.packer);

            Output = header + (finalPacker == CompressorType.yui ? compressedYui : compressedPacker);

            Statistics = finalPacker.ToString() + ": " + javascript.Length + "/" + Output.Length + ", " + Math.Round(100 * ((decimal)Output.Length / (decimal)javascript.Length), 0) + "%";
            return Success;
        }