public static string Compress(string css, int columnWidth, CssCompressionType cssCompressionType) { string compressedCss = null; switch (cssCompressionType) { case CssCompressionType.StockYuiCompressor: compressedCss = YUICompressor.Compress(css, columnWidth); break; case CssCompressionType.MichaelAshRegexEnhancements: compressedCss = MichaelAshRegexCompressor.Compress(css, columnWidth); break; case CssCompressionType.Hybrid: // We need to try both types. We get the keep size. string yuiCompressedCss = YUICompressor.Compress(css, columnWidth); string michaelAshsRegexEnhancementsCompressedCss = MichaelAshRegexCompressor.Compress(css, columnWidth); compressedCss = yuiCompressedCss.Length < michaelAshsRegexEnhancementsCompressedCss.Length ? yuiCompressedCss : michaelAshsRegexEnhancementsCompressedCss; break; default: throw new InvalidOperationException("Unhandled CssCompressionType found when trying to determine which compression method to use."); } return compressedCss; }
public static string Compress(string css, int columnWidth, CssCompressionType cssCompressionType) { string compressedCss = null; switch (cssCompressionType) { case CssCompressionType.StockYuiCompressor: compressedCss = YUICompressor.Compress(css, columnWidth); break; case CssCompressionType.MichaelAshRegexEnhancements: compressedCss = MichaelAshRegexCompressor.Compress(css, columnWidth); break; case CssCompressionType.Hybrid: string yuiCompressedCss = YUICompressor.Compress(css, columnWidth); string michaelAshsRegexEnhancementsCompressedCss = MichaelAshRegexCompressor.Compress(css, columnWidth); compressedCss = yuiCompressedCss.Length < michaelAshsRegexEnhancementsCompressedCss.Length ? yuiCompressedCss : michaelAshsRegexEnhancementsCompressedCss; break; default: throw new InvalidOperationException("Unhandled CssCompressionType found when trying to determine which compression method to use."); } return(compressedCss); }
/// <inheritdoc cref="IResourceMinifier.Minify" /> public string Minify(Settings settings, ResourceSet resourceSet, string combinedContent) { var type = (CssCompressionType)CssCompressionType.ConvertToType( typeof(CssCompressionType), Yahoo.Yui.Compressor.CssCompressionType.StockYuiCompressor); return(CssCompressor.Compress(combinedContent, ColumnWidth == null ? -1 : ColumnWidth.Value, type, RemoveComments == null ? true : RemoveComments.Value)); }
public CssFileProcessor(IRetryableFileOpener retryableFileOpener, IPathProvider pathProvider, ICssPathRewriter cssPathRewriter, string filePath, CssCompressionType compressionType, Uri cssRootUri, bool appendHashToAssets) { _retryableFileOpener = retryableFileOpener; _pathProvider = pathProvider; _cssPathRewriter = cssPathRewriter; CompressionType = compressionType; _fileInfo = new FileInfo(new Uri(_pathProvider.MapPath(filePath)).LocalPath); _cssRootUri = cssRootUri; var absoluteUriDirectory = _pathProvider.GetAbsoluteUriDirectory(filePath); _modules = new List <ICssModule>() { new DotLessModule(), new RelativePathModule(absoluteUriDirectory, cssPathRewriter), new CssAssetsHashModule(appendHashToAssets, cssPathRewriter, pathProvider) }; }
public CssFileProcessor(IRetryableFileOpener retryableFileOpener, IPathProvider pathProvider, ICssPathRewriter cssPathRewriter, string filePath, CssCompressionType compressionType, Uri cssRootUri, bool appendHashToAssets) { _retryableFileOpener = retryableFileOpener; _pathProvider = pathProvider; _cssPathRewriter = cssPathRewriter; CompressionType = compressionType; _fileInfo = new FileInfo(new Uri(_pathProvider.MapPath(filePath)).LocalPath); _cssRootUri = cssRootUri; var absoluteUriDirectory = _pathProvider.GetAbsoluteUriDirectory(filePath); _modules = new List<ICssModule>() { new DotLessModule(), new RelativePathModule(absoluteUriDirectory, cssPathRewriter), new CssAssetsHashModule(appendHashToAssets, cssPathRewriter, pathProvider) }; }
private void InitialiseBuildSettings() { #region Required Elements if (string.IsNullOrEmpty(CssCompressionType)) { LogMessage("No Compression type defined. Defaulting to 'YuiStockCompression'."); CssCompressionType = "YUIStockCompression"; } switch (CssCompressionType.ToLowerInvariant()) { case "michaelashsregexenhancements": _cssCompressionType = Compressor.CssCompressionType.MichaelAshRegexEnhancements; break; case "havemycakeandeatit": case "bestofbothworlds": case "hybrid": _cssCompressionType = Compressor.CssCompressionType.Hybrid; break; default: _cssCompressionType = Compressor.CssCompressionType.StockYuiCompressor; break; } if (string.IsNullOrEmpty(LoggingType)) { Log.LogWarning("No logging argument defined. Defaulting to 'ALittleBit'."); LoggingType = "ALittleBit"; } switch (LoggingType) { case "none": _loggingType = MsBuild.LoggingType.None; break; case "hardcorebringiton": _loggingType = MsBuild.LoggingType.HardcoreBringItOn; break; default: _loggingType = MsBuild.LoggingType.ALittleBit; break; } #endregion #region Optional Elements // Optional property. _deleteCssFiles = !string.IsNullOrEmpty(DeleteCssFiles) && ParseSillyTrueFalseValue(DeleteCssFiles.ToUpperInvariant()); // Optional property. _deleteJavaScriptFiles = !string.IsNullOrEmpty(DeleteJavaScriptFiles) && ParseSillyTrueFalseValue(DeleteJavaScriptFiles.ToUpperInvariant()); // Optional Property. _obfuscateJavaScript = !string.IsNullOrEmpty(ObfuscateJavaScript) && ParseSillyTrueFalseValue(ObfuscateJavaScript.ToUpperInvariant()); // Optional Property. _preserveAllSemicolons = !string.IsNullOrEmpty(PreserveAllSemicolons) && ParseSillyTrueFalseValue(PreserveAllSemicolons.ToUpperInvariant()); // Optional Property. _disableOptimizations = !string.IsNullOrEmpty(DisableOptimizations) && ParseSillyTrueFalseValue(DisableOptimizations.ToUpperInvariant()); // Optional Property. int tempLineBreakPosition; if (!string.IsNullOrEmpty(LineBreakPosition) && int.TryParse(LineBreakPosition, out tempLineBreakPosition)) { _lineBreakPosition = tempLineBreakPosition; } else { _lineBreakPosition = -1; } // Optional Property. if (!string.IsNullOrEmpty(EncodingType)) { switch (EncodingType.ToLowerInvariant()) { case "ascii": _encoding = Encoding.ASCII; break; case "bigendianunicode": _encoding = Encoding.BigEndianUnicode; break; case "unicode": _encoding = Encoding.Unicode; break; case "utf32": case "utf-32": _encoding = Encoding.UTF32; break; case "utf7": case "utf-7": _encoding = Encoding.UTF7; break; case "": case "utf8": case "utf-8": _encoding = Encoding.UTF8; break; default: _encoding = Encoding.Default; break; } } else { _encoding = Encoding.Default; } // Optional Property. if (!string.IsNullOrEmpty(ThreadCulture)) { try { _threadCulture = CultureInfo.CreateSpecificCulture(ThreadCulture); } catch { LogMessage("Failed to read in a legitimate culture value. As such, this property will *not* be set."); } } else { _threadCulture = CultureInfo.CreateSpecificCulture("en-GB"); } // Optional property. _isEvalIgnored = !string.IsNullOrEmpty(IsEvalIgnored) && ParseSillyTrueFalseValue(IsEvalIgnored.ToUpperInvariant()); #endregion }
/// <summary> /// 压缩Css /// </summary> /// <param name="str">css</param> /// <param name="columnWidth">specified column width ,for nice reading </param> /// <param name="cssCompressionType">压缩类型</param> /// <param name="removeComments">是否删除注释</param> /// <returns></returns> public static string CssCompress(string css, int columnWidth, CssCompressionType cssCompressionType, bool removeComments) { return CssCompressor.Compress(css, columnWidth, cssCompressionType, removeComments); }
public YuiCompressor(int columnWidth, CssCompressionType compressionType) { this.columnWidth = columnWidth; this.compressionType = compressionType; }
public static string Compress(string css, int columnWidth, CssCompressionType cssCompressionType) { return CssCompressor.Compress(css, columnWidth, cssCompressionType, true); }
internal YuiCompressor(int columnWidth, CssCompressionType compressionType, bool removeComments) { this.columnWidth = columnWidth; this.compressionType = compressionType; this.removeComments = removeComments; }
internal YuiCompressor(int columnWidth, CssCompressionType compressionType) { this.columnWidth = columnWidth; this.compressionType = compressionType; }
/// <summary> /// 压缩 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnCompressor_Click(object sender, EventArgs e) { if (this.ListFile.Items.Count != 0) { progressBar1.Maximum = this.ListFile.Items.Count; progressBar1.Value = 0; DirectoryInfo info = new DirectoryInfo(this.strFilePath); Encoding encoding = this.GetEncoding(); int num = 0; long num2 = 0L; long num3 = 0L; int jswitdh = -1; int.TryParse(txt_HHWZ.Text, out jswitdh); int csswidth = -1; int.TryParse(txt_HCD.Text, out csswidth); bool blRemoveRemark = cb_RemoveRemark.Checked; bool blBackup = rb_bf1.Checked; int iCS = 25; int.TryParse(txt_CS.Text, out csswidth); iCS *= 1000; CssCompressionType ct = CssCompressionType.StockYuiCompressor; switch (cbb_YSLX.SelectedIndex) { case 0: ct = CssCompressionType.StockYuiCompressor; break; case 1: ct = CssCompressionType.Hybrid; break; case 2: ct = CssCompressionType.MichaelAshRegexEnhancements; break; } DirectoryInfo info2 = new DirectoryInfo(info.FullName.Substring(0, info.FullName.Length - 1) + "_bak"); if (blBackup) { if (!info2.Exists) { info2.Create(); } } for (int i = 0; i < this.fileList.Count; i++) { try { FileInfo infoold = this.fileList[i]; if (infoold.Name.Contains(".min.")) { this.ListFile.Items[i].SubItems[3].Text = "跳过"; } else { string strContent = File.ReadAllText(infoold.FullName, encoding); if (blBackup) {//备份 FileInfo infobak = new FileInfo(infoold.FullName.Replace(info.FullName, info2.FullName)); if (!infobak.Directory.Exists) { infobak.Directory.Create(); } //写入 File.WriteAllText(infobak.FullName, strContent); } string strMsg = ""; if (infoold.Extension.ToLower() == ".js") { if (cb_YSJS.Checked) { try { strMsg = new JavaScriptCompressor(strContent, false, encoding, CultureInfo.CurrentCulture).Compress(isObfus.Checked, semi.Checked, opt.Checked, jswitdh); this.ListFile.Items[i].SubItems[3].Text = "完成"; } catch (Exception ex) { if (cb_ZXYSJS.Checked) { //假如压缩失败(error),可进行在线压缩 string strMsg2 = RequestService("https://tool.oschina.net/action/jscompress/js_compress?munge=" + (isObfus.Checked ? "1" : "0") + "&linebreakpos=", RequestMethod.POST, strContent); if (strMsg2.StartsWith("{\"result\":")) { var rspObj = JsonConvert.DeserializeAnonymousType(strMsg2, new { result = "" }); strMsg = rspObj.result; //strMsg = strMsg2.Remove(strMsg2.Length - 2, 2).Remove(0, 10); //strMsg2.TrimStart('{').TrimEnd('}').Remove(0, 9).Trim('\"'); this.ListFile.Items[i].SubItems[3].Text = "完成-在线"; } else { //if (strMsg2.StartsWith("{\"msg\":")) //{ this.ListFile.Items[i].ForeColor = Color.Red; this.ListFile.Items[i].SubItems[2].Text = "0"; this.ListFile.Items[i].SubItems[3].Text = "错误(本地、在线):" + strMsg2; num++; //} } } else { throw ex; } } } } else if (infoold.Extension.ToLower() == ".css") { if (cb_YSCss.Checked) { try { bool ret = false; new System.Threading.Tasks.TaskFactory().StartNew(() => {//异步等待 strMsg = CssCompressor.Compress(strContent, csswidth, ct, blRemoveRemark); ret = true; }).Wait(iCS); if (!ret) { throw new Exception("超时"); } this.ListFile.Items[i].SubItems[3].Text = "完成"; } catch (Exception ex) { if (cb_ZXYSCss.Checked) { //假如压缩失败(error),可进行在线压缩 string strMsg2 = RequestService("https://tool.oschina.net/action/css_compress/js_compress?linebreakpos=", RequestMethod.POST, strContent); if (strMsg2.StartsWith("{\"result\":")) { var rspObj = JsonConvert.DeserializeAnonymousType(strMsg2, new { result = "" }); strMsg = rspObj.result; //strMsg = strMsg2.Remove(strMsg2.Length - 2, 2).Remove(0, 10); //strMsg2.TrimStart('{').TrimEnd('}').Remove(0, 9).Trim('\"'); this.ListFile.Items[i].SubItems[3].Text = "完成-在线:"; } else { //if (strMsg2.StartsWith("{\"msg\":")) //{ this.ListFile.Items[i].ForeColor = Color.Red; this.ListFile.Items[i].SubItems[2].Text = "0"; this.ListFile.Items[i].SubItems[3].Text = "错误(本地、在线):" + strMsg2; num++; //} } } else { throw ex; } } } else { this.ListFile.Items[i].SubItems[3].Text = "跳过"; } } if (this.ListFile.Items[i].SubItems[3].Text.Contains("完成")) { //覆盖 File.WriteAllText(infoold.FullName, strMsg); this.ListFile.Items[i].ForeColor = Color.Blue; this.ListFile.Items[i].SubItems[2].Text = this.FormatSize((long)strMsg.Length); //this.ListFile.Items[i].SubItems[3].Text = "完成"; num2 += strContent.Length; num3 += strMsg.Length; } } } catch (Exception exception) { this.ListFile.Items[i].ForeColor = Color.Red; this.ListFile.Items[i].SubItems[2].Text = "0"; this.ListFile.Items[i].SubItems[3].Text = "错误:" + exception.Message; num++; } progressBar1.PerformStep(); int num5 = i + 1; this.txtInfo.Text = "共" + this.fileList.Count.ToString() + "个文件!正处理第" + num5.ToString() + "个文件!"; Application.DoEvents(); } string text = "压缩完成!\r\n"; if (num > 0) { text = text + num.ToString() + "个文件压缩发生错误!\r\n"; } text = text + "总体压缩:"; text = text + Math.Round((((1.0 * num3) / ((double)num2)) * 100.0), 2) + "%"; this.txtInfo.Text = text.Replace("\r\n", " "); if (num > 0) { MessageBox.Show(text, "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand); } else { MessageBox.Show(text, "成功", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } } }