static string Render(Directive dir, int nesting) { string start = ""; for (int i = 0; i < nesting; i++) { start += "\t"; } switch (dir.Type) { case DirectiveType.Charset: return(RenderCharSet(dir, start)); case DirectiveType.Page: return(RenderPage(dir, start)); case DirectiveType.Media: return(RenderMedia(dir, nesting)); case DirectiveType.Import: return(RenderImport(dir)); case DirectiveType.FontFace: return(RenderFontFace(dir, start)); } System.Text.StringBuilder txt = new System.Text.StringBuilder(); txt.AppendFormat("<span class=\"directive_name\">{0}</span> ", dir.Name); if (dir.Expression != null) { txt.AppendFormat("<span class=\"expression\">{0}</span> ", dir.Expression); } bool first = true; foreach (Medium m in dir.Mediums) { if (first) { first = false; } else { txt.Append(", "); } txt.AppendFormat(" <span class=\"medium\">{0}</span>", m.ToString()); } bool HasBlock = (dir.Declarations.Count > 0 || dir.Directives.Count > 0 || dir.RuleSets.Count > 0); if (!HasBlock) { txt.Append(";"); return(txt.ToString()); } txt.Append(" {\r\n" + start); foreach (Directive dr in dir.Directives) { txt.AppendFormat("<span class=\"directive\">{0}</span>\r\n", Render(dr, nesting + 1)); } foreach (RuleSet rules in dir.RuleSets) { txt.AppendFormat("<span class=\"ruleset\">{0}</span>\r\n", rules.ToString(nesting + 1)); } first = true; foreach (Declaration dec in dir.Declarations) { if (first) { first = false; } else { txt.Append(";"); } txt.Append("\r\n\t" + start); txt.AppendFormat("<span class=\"declaration\">{0}</span>", Render(dec)); } txt.Append("\r\n}"); return(txt.ToString()); }
static string RenderCharSet(Directive dir, string start) { return(string.Format("{2}<span class=\"directive_name\">{0}</span> <span class=\"expression\">{1}</span>", dir.Name, Render(dir.Expression), start)); }
static string Render(Directive dir) { return(Render(dir, 0)); }
void directive(out Directive dir) { dir = new Directive(); Declaration dec = null; RuleSet rset = null; Expression exp = null; Directive dr = null; string ident = null; Medium m; Expect(24); identity(out ident); dir.Name = "@" + ident; switch (dir.Name.ToLower()) { case "@media": dir.Type = DirectiveType.Media; break; case "@import": dir.Type = DirectiveType.Import; break; case "@charset": dir.Type = DirectiveType.Charset; break; case "@page": dir.Type = DirectiveType.Page; break; case "@font-face": dir.Type = DirectiveType.FontFace; break; case "@namespace": dir.Type = DirectiveType.Namespace; break; default: dir.Type = DirectiveType.Other; break; } while (la.kind == 4) { Get(); } if (StartOf(4)) { if (StartOf(5)) { medium(out m); dir.Mediums.Add(m); while (la.kind == 4) { Get(); } while (la.kind == 25) { Get(); while (la.kind == 4) { Get(); } medium(out m); dir.Mediums.Add(m); while (la.kind == 4) { Get(); } } } else { expr(out exp); dir.Expression = exp; while (la.kind == 4) { Get(); } } } if (la.kind == 26) { Get(); while (la.kind == 4) { Get(); } if (StartOf(6)) { while (StartOf(1)) { if (dir.Type == DirectiveType.Page || dir.Type == DirectiveType.FontFace) { declaration(out dec); dir.Declarations.Add(dec); while (la.kind == 4) { Get(); } while (la.kind == 27) { Get(); while (la.kind == 4) { Get(); } if (la.val.Equals("}")) { Get(); return; } declaration(out dec); dir.Declarations.Add(dec); while (la.kind == 4) { Get(); } } if (la.kind == 27) { Get(); while (la.kind == 4) { Get(); } } } else if (StartOf(2)) { ruleset(out rset); dir.RuleSets.Add(rset); while (la.kind == 4) { Get(); } } else { directive(out dr); dir.Directives.Add(dr); while (la.kind == 4) { Get(); } } } } Expect(28); while (la.kind == 4) { Get(); } } else if (la.kind == 27) { Get(); while (la.kind == 4) { Get(); } } else { SynErr(49); } }