Skip to content

Instantly share code, notes, and snippets.

@DreamVB
Created July 15, 2016 09:13
Show Gist options
  • Save DreamVB/2de96bb1e7700ab2335e88680295ad7e to your computer and use it in GitHub Desktop.
Save DreamVB/2de96bb1e7700ab2335e88680295ad7e to your computer and use it in GitHub Desktop.
Compress a CSS Cascading Style Sheets With C#
/*
* CSS Packer
* By Ben a.k.a DreamVB
* e. [email protected]
*
* This code will allow you to compress your style sheets makeing them smaller and faster to load.
* If you use any parts of this code let me know if it has helped you.
*
* Also open to sugestions and freedback
*/
using System;
using System.Text;
using System.IO;
namespace packer
{
class csscomp
{
public static string NextError = string.Empty;
private static FileInfo fi = null;
private static double m_compressedSize = 0;
private static double m_originalSize = 0;
private static string FontWeightToValue(string value)
{
string temp = value.ToUpper();
if (temp == "NORMAL;")
{
return "400;";
}
if (temp == "BOLD;")
{
return "700;";
}
if (temp == "LIGHTER;")
{
return "200;";
}
if (temp == "LIGHT;")
{
return "300;";
}
if (temp == "MEDIUM;")
{
return "500;";
}
return value;
}
public static double OriginalSize()
{
try
{
return m_originalSize;
}
catch
{
return 0;
}
}
public static double CompressedSize()
{
return m_compressedSize;
}
public static double Ratio()
{
double mRatio = CompressedSize() / OriginalSize() * 100;
mRatio = Math.Round(mRatio * 100) / 100;
return mRatio;
}
public static int CompressCSS(string InputFile, string OutputFile)
{
string sLine = string.Empty;
StringBuilder sb = new StringBuilder();
string[] parms = { };
string[] cssFont = { };
int sPos = -1;
fi = new FileInfo(InputFile);
//Resize parms array
parms = new string[2];
try
{
//Get original size
m_originalSize = fi.Length;
//Open source file.
using (StreamReader sr = new StreamReader(File.OpenRead(fi.FullName)))
{
while (!sr.EndOfStream)
{
sPos = -1;
//Trim down the line.
sLine = sr.ReadLine().Trim();
if (sLine.Length > 0)
{
//Check for single line comments
if (sLine.StartsWith("//"))
{
sLine = string.Empty;
}
if (sLine.StartsWith("/*"))
{
sLine = string.Empty;
}
//Get position of css key and value.
sPos = sLine.IndexOf(":");
if (sPos != -1)
{
//Tidy the string
parms[0] = sLine.Substring(0, sPos).Trim();
parms[1] = sLine.Substring(sPos + 1).Trim();
//Compress font weaight
if (parms[0].ToUpper() == "FONT-WEIGHT")
{
parms[1] = FontWeightToValue(parms[1]);
}
//Compress font
if (parms[0].ToUpper() == "FONT-FAMILY")
{
cssFont = parms[1].Split(',');
sLine = string.Empty;
foreach (string s2 in cssFont)
{
sLine = sLine + s2.Trim() + ",";
}
//Remove last ,
if (sLine.EndsWith(","))
{
parms[1] = sLine.Remove(sLine.Length - 1, 1);
}
}
//Compress colors
if (parms[0].ToUpper() == "COLOR")
{
if (parms[1].ToUpper() == "WHITE;")
{
parms[1] = "#fff;";
}
}
//Compress 0px; to just 0;
if ((parms[1].Length == 4) && (parms[1].ToUpper() == "0PX;"))
{
parms[1] = "0;";
}
if (parms[1].EndsWith(" 0;"))
{
parms[1] = ";";
}
//Rebuild string.
sLine = parms[0] + ":" + parms[1];
}
//Commpress any spaces in string ie space {
if (sLine.EndsWith("{"))
{
sLine = sLine.Substring(0, sLine.Length - 1).Trim() + "{";
}
//Compress any spaces like eg space }
if (sLine.EndsWith("}"))
{
sLine = sLine.Substring(0, sLine.Length - 1).Trim() + "}";
}
//Replace ;} with single }
if (sLine.EndsWith(";}"))
{
sLine = sLine.Remove(sLine.Length - 2, 2) + "}";
}
//Replace space{ with single {
sLine = sLine.Replace(" {", "{");
if (sLine.Length > 0)
{
//Append to string builder.
sb.Append(sLine).Replace(";}", "}");
}
}
}
//Close file
sr.Close();
}
//
//Clear up
Array.Clear(parms, 0, parms.Length);
Array.Clear(cssFont, 0, cssFont.Length);
//Get compressed size
m_compressedSize = sb.Length;
//write to file.
using (StreamWriter sw = new StreamWriter(File.OpenWrite(OutputFile)))
{
//Write data
sw.Write(sb.ToString());
//Close file.
sw.Close();
}
//Clear sb
sb.Clear();
}
catch (Exception e)
{
NextError = e.Message;
return 0;
}
return 1;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment