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()); }
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(23); 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; } if (StartOf(4)) { if (StartOf(4)) { expr(out exp); dir.Expression = exp; } else { medium(out m); dir.Mediums.Add(m); } } if (la.kind == 24) { Get(); if (StartOf(5)) { while (StartOf(1)) { if (dir.Type == DirectiveType.Page || dir.Type == DirectiveType.FontFace) { declaration(out dec); dir.Declarations.Add(dec); while (la.kind == 25) { Get(); if (la.val.Equals("}")) { Get(); return; } declaration(out dec); dir.Declarations.Add(dec); } if (la.kind == 25) { Get(); } } else if (StartOf(2)) { ruleset(out rset); dir.RuleSets.Add(rset); } else { directive(out dr); dir.Directives.Add(dr); } } } Expect(26); } else if (la.kind == 25) { Get(); } else SynErr(49); }
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)); }
static string Render(Directive dir) { return Render(dir, 0); }
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 RenderPage(Directive dir, string start) { System.Text.StringBuilder txt = new System.Text.StringBuilder(); txt.Append("<span class=\"directive_name\">@page</span> "); if (dir.Expression != null) { txt.AppendFormat("<span class=\"expression\">{0}</span> ", dir.Expression); } txt.Append("{\r\n"); bool 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("}"); return txt.ToString(); }
static string RenderMedia(Directive dir, int nesting) { System.Text.StringBuilder txt = new System.Text.StringBuilder(); txt.Append("<span class=\"directive_name\">@media</span>"); 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()); } txt.Append(" {\r\n"); foreach (RuleSet rules in dir.RuleSets) { txt.AppendFormat("<span class=\"ruleset\">{0}</span>\r\n", Render(rules, nesting + 1)); } txt.Append("}"); return txt.ToString(); }
static string RenderImport(Directive dir) { System.Text.StringBuilder txt = new System.Text.StringBuilder(); txt.Append("<span class=\"directive_name\">@import</span> "); if (dir.Expression != null) { txt.AppendFormat("<span class=\"expression\">{0}</span> ", Render(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()); } txt.Append(";"); return txt.ToString(); }
static string RenderFontFace(Directive dir, string start) { System.Text.StringBuilder txt = new System.Text.StringBuilder(); txt.Append("<span class=\"directive_name\">@font-face</span> {"); bool 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("</span>\r\n}"); return txt.ToString(); }