Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
2008-01-20-css-parser-class-in--net.textile
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace LaTrompa.Web
{
public class CssParser
{
private List<string> _styleSheets;
private SortedList<string,StyleClass> _scc;
public SortedList<string,StyleClass> Styles
{
get { return this._scc; }
set { this._scc = value; }
}
public CssParser()
{
this._styleSheets = new List<string>();
this._scc = new SortedList<string, StyleClass>();
}
public void AddStyleSheet(string path)
{
this._styleSheets.Add(path);
ProcessStyleSheet(path);
}
public string GetStyleSheet(int index)
{
return this._styleSheets[index];
}
private void ProcessStyleSheet(string path)
{
string content = CleanUp(File.ReadAllText(path));
string[] parts = content.Split('}');
foreach (string s in parts)
{
if (CleanUp(s).IndexOf('{') > -1)
{
FillStyleClass(s);
}
}
}
private void FillStyleClass(string s)
{
StyleClass sc = null;
string[] parts = s.Split('{');
string styleName = CleanUp(parts[0]).Trim().ToLower();
if (this._scc.ContainsKey(styleName))
{
sc = this._scc[styleName];
this._scc.Remove(styleName);
}
else
{
sc = new StyleClass();
}
sc.Name = styleName;
string[] atrs = CleanUp(parts[1]).Replace("}","").Split(';');
foreach (string a in atrs)
{
if (a.Contains(":"))
{
string _key = a.Split(':')[0].Trim().ToLower();
if (sc.Attributes.ContainsKey(_key))
{
sc.Attributes.Remove(_key);
}
sc.Attributes.Add(_key, a.Split(':')[1].Trim().ToLower());
}
}
this._scc.Add(sc.Name, sc);
}
private string CleanUp(string s)
{
string temp = s;
string reg = "(/\\*(.|[\r\n])*?\\*/)|(//.*)";
Regex r = new Regex(reg);
temp = r.Replace(temp,"");
temp = temp.Replace("\r", "").Replace("\n", "");
return temp;
}
public class StyleClass
{
private string _name = string.Empty;
public string Name
{
get { return _name; }
set { _name = value; }
}
private SortedList<string, string> _attributes = new SortedList<string,string>();
public SortedList<string, string> Attributes
{
get { return _attributes; }
set { _attributes = value; }
}
}
}
}
@Mo9a7i

This comment has been minimized.

Copy link

Mo9a7i commented Apr 8, 2013

This is a wonderful class, I liked the cleanup function.

I'm facing a problem in my project where I hoped this gist can help with, i'm trying to parse twitter's bootstrap.css but my function fails as twitter's bootstrap contains @media queries inside the file and those mess up the syntax a bit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.