Ejemplo n.º 1
0
 private string ParseSubtext(string html, CSSDocument doc)
 {
     Dictionary<string, string> selectorToColor = new Dictionary<string, string>();
     StringBuilder result = new StringBuilder();
     MatchCollection matches = PerLine.Matches(html);
     foreach (Match match in matches)
     {
         ParseLine(match.Groups["withinLine"].Value, doc, selectorToColor, result);
     }
     return result.ToString();
 }
Ejemplo n.º 2
0
 public CSSDocument ParseStream(Stream stream)
 {
     StringBuilder sb = new StringBuilder();
     TextWriter errorCatch = new StringWriter(sb);
     Scanner scanner = new Scanner(stream);
     Parser parser = new Parser(scanner);
     parser.errors.errorStream = errorCatch;
     parser.Parse();
     doc = parser.CSSDoc;
     SpitErrors(sb);
     return doc;
 }
Ejemplo n.º 3
0
        void CSS3()
        {
            CSSDoc = new CSSDocument();
            string    cset = null;
            RuleSet   rset = null;
            Directive dir  = null;

            while (la.kind == 5 || la.kind == 6)
            {
                if (la.kind == 5)
                {
                    Get();
                }
                else
                {
                    Get();
                }
            }
            while (StartOf(1))
            {
                if (StartOf(2))
                {
                    ruleset(out rset);
                    CSSDoc.RuleSets.Add(rset);
                }
                else
                {
                    directive(out dir);
                    CSSDoc.Directives.Add(dir);
                }
                while (la.kind == 5 || la.kind == 6)
                {
                    if (la.kind == 5)
                    {
                        Get();
                    }
                    else
                    {
                        Get();
                    }
                }
            }
        }
Ejemplo n.º 4
0
        public static string Render(CSSDocument css)
        {
            StringBuilder txt = new StringBuilder();

            txt.Append("<pre><span class=\"cssDoc\">");

            foreach (Directive dr in css.Directives)
            {
                txt.AppendFormat("<span class=\"directive\">{0}</span>\r\n", Render(dr));
            }
            if (txt.Length > 0) { txt.Append("\r\n"); }
            foreach (RuleSet rules in css.RuleSets)
            {
                txt.AppendFormat("<span class=\"ruleset\">{0}</span>\r\n", Render(rules, 0));
            }

            txt.Append("</span></pre>");

            return txt.ToString();
        }
Ejemplo n.º 5
0
        private string GetColorFromCss(string selector, Dictionary<string, string> selectorToColor, CSSDocument doc)
        {
            if (!selectorToColor.ContainsKey(selector))
            {
                foreach (RuleSet ruleSet in doc.RuleSets)
                {
                    foreach (Selector select in ruleSet.Selectors)
                    {
                        foreach (SimpleSelector sel in select.SimpleSelectors)
                        {
                            if (selector.Equals(sel.Class, StringComparison.Ordinal))
                            {
                                var decl = (from d in ruleSet.Declarations
                                            where d.Name.Equals("color", StringComparison.OrdinalIgnoreCase)
                                            select d).FirstOrDefault();
                                if (decl != null)
                                {
                                    string color = decl.Expression.ToString();
                                    Color result = Color.FromName(color);
                                    if (result.A == 0)
                                    {
                                        // Color.FromName returns all 0 if it does not match
                                        return color;
                                    }
                                    else
                                    {
                                        return string.Format("#{0:x2}{1:x2}{2:x2}", result.R, result.G, result.B);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                return selectorToColor[selector];
            }

            return null;
        }
Ejemplo n.º 6
0
        private void ParseLine(string line, CSSDocument doc, Dictionary<string, string> selectorToColor, StringBuilder result)
        {
            MatchCollection matches = Content.Matches(line);
            foreach (Match match in matches)
            {
                if (match.Groups["color"].Success)
                {
                    result.AppendFormat("[color={0}]{1}[/color]", match.Groups["color"].Value, match.Groups["textContent"].Value);
                }
                else
                {
                    string color = GetColorFromCss(match.Groups["className"].Value, selectorToColor, doc);
                    if (color == null) color = "#000000";
                    if (!selectorToColor.ContainsKey(match.Groups["className"].Value))
                        selectorToColor.Add(match.Groups["className"].Value, color);

                    result.AppendFormat("[color={0}]{1}[/color]", color, match.Groups["textContent"].Value);
                }
            }
            result.AppendLine();
        }
Ejemplo n.º 7
0
 public static string Render(CSSDocument css)
 {
     StringBuilder txt = new StringBuilder();
     //foreach (Import imp in css.Imports) {
     //    txt.AppendFormat("{0}\r\n", imp.ToString());
     //}
     //foreach (Media mtg in css.Medias) {
     //    txt.AppendFormat("@media{0} {{\r\n", mtg.Media != Media.None ? " " + mtg.Media.ToString() : "");
     //    foreach (Selector sel in mtg.Selectors) {
     //        txt.Append(Render(sel, 1));
     //        txt.Append("\r\n");
     //    }
     //    txt.Append("}\r\n");
     //}
     //foreach (Selector sel in css.Selectors) {
     //    txt.Append(Render(sel));
     //    txt.Append("\r\n");
     //}
     txt.Append(css.ToString());
     return txt.ToString();
 }
Ejemplo n.º 8
0
        public static string Render(CSSDocument css)
        {
            StringBuilder txt = new StringBuilder();

            //foreach (Import imp in css.Imports) {
            //    txt.AppendFormat("{0}\r\n", imp.ToString());
            //}
            //foreach (Media mtg in css.Medias) {
            //    txt.AppendFormat("@media{0} {{\r\n", mtg.Media != Media.None ? " " + mtg.Media.ToString() : "");
            //    foreach (Selector sel in mtg.Selectors) {
            //        txt.Append(Render(sel, 1));
            //        txt.Append("\r\n");
            //    }
            //    txt.Append("}\r\n");
            //}
            //foreach (Selector sel in css.Selectors) {
            //    txt.Append(Render(sel));
            //    txt.Append("\r\n");
            //}
            txt.Append(css.ToString());
            return(txt.ToString());
        }
Ejemplo n.º 9
0
        public static string Render(CSSDocument css)
        {
            StringBuilder txt = new StringBuilder();

            txt.Append("<pre><span class=\"cssDoc\">");

            foreach (Directive dr in css.Directives)
            {
                txt.AppendFormat("<span class=\"directive\">{0}</span>\r\n", Render(dr));
            }
            if (txt.Length > 0)
            {
                txt.Append("\r\n");
            }
            foreach (RuleSet rules in css.RuleSets)
            {
                txt.AppendFormat("<span class=\"ruleset\">{0}</span>\r\n", Render(rules, 0));
            }

            txt.Append("</span></pre>");

            return(txt.ToString());
        }
Ejemplo n.º 10
0
        void CSS3()
        {
            CSSDoc = new CSSDocument();
            string cset = null;
            RuleSet rset = null;
            Directive dir = null;

            while (la.kind == 5 || la.kind == 6)
            {
                if (la.kind == 5)
                {
                    Get();
                }
                else
                {
                    Get();
                }
            }
            while (StartOf(1))
            {
                if (StartOf(2))
                {
                    ruleset(out rset);
                    CSSDoc.RuleSets.Add(rset);
                }
                else
                {
                    directive(out dir);
                    CSSDoc.Directives.Add(dir);
                }
                while (la.kind == 5 || la.kind == 6)
                {
                    if (la.kind == 5)
                    {
                        Get();
                    }
                    else
                    {
                        Get();
                    }
                }
            }
        }