Click here to Skip to main content
15,886,873 members
Articles / Programming Languages / C#

Simple CSS Parser

Rate me:
Please Sign up or sign in to vote.
4.94/5 (102 votes)
3 Sep 2011CPOL14 min read 657.1K   9.1K   207  
An article about a simple CSS parser
using System;
using System.Xml.Serialization;

namespace BoneSoft.CSS {
	/// <summary></summary>
	public class Function {
		private string name;
		private Expression expression;

		/// <summary></summary>
		[XmlAttribute("name")]
		public string Name {
			get { return name; }
			set { name = value; }
		}

		/// <summary></summary>
		[XmlElement("Expression")]
		public Expression Expression {
			get { return expression; }
			set { expression = value; }
		}

		/// <summary></summary>
		/// <returns></returns>
		public override string ToString() {
			/*
function<out Function func> =	(. func = new Function();
									Expression exp = null;
								.)
	ident						(. func.Name = t.val; .)
	'(' expr<out exp>			(. func.Expression = exp; .)
	')'
.
			*/
			System.Text.StringBuilder txt = new System.Text.StringBuilder();
			txt.AppendFormat("{0}(", name);
			if (expression != null) {
				bool first = true;
				foreach (Term t in expression.Terms) {
					//if (t.Value.EndsWith("=")) {
					if (first) {
						first = false;
					} else if (!t.Value.EndsWith("=")) {
						txt.Append(", ");
					}

					bool quoteMe = false;
					if (t.Type == TermType.String && !t.Value.EndsWith("=")) {
						quoteMe = true;
					}
					if (quoteMe) { txt.Append("'"); }
					txt.Append(t.ToString());
					if (quoteMe) { txt.Append("'"); }
				}
			}
			txt.Append(")");
			return txt.ToString();
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) BoneSoft Software
United States United States
I've been in software development for more than a decade now. Originally with ASP 2.0 and VB6. I worked in Japan for a year doing Java. And have been with C# ever since.

In 2005 I founded BoneSoft Software where I sell a small number of developer tools.
This is a Organisation (No members)


Comments and Discussions