Example #1
0
        private static void ParseStyle(string styleContent, Dictionary<string, Dictionary<string, string>> styles)
        {
            StringReader reader = new StringReader(styleContent);
            while (reader.Peek() != -1)
            {
                string names = reader.ReadToBeforeChar(CurlyBraceOpen).Trim();
                // Consume {
                reader.Read();

                string block = reader.ReadToBeforeChar(CurlyBraceClose);
                Dictionary<string, string> styleValues = new Dictionary<string, string>(strComp);
                foreach (var keyValue in block.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries))
                {
                    var idxColumn = keyValue.IndexOf(':');
                    if (idxColumn == -1)
                        continue;

                    var name = keyValue.Substring(0, idxColumn).Trim();
                    var value = idxColumn + 1 < keyValue.Length ? keyValue.Substring(idxColumn + 1).Trim() : string.Empty;
                    styleValues[name] = value;
                }
                // Consume }
                reader.Read();

                foreach (var n in names.Split(','))
                {
                    var name = n.Trim();
                    Dictionary<string, string> currentValues;
                    if (styles.TryGetValue(n, out currentValues))
                    {
                        foreach (var item in styleValues)
                        {
                            currentValues[item.Key] = item.Value;
                        }
                    }
                    else
                    {
                        styles[n] = styleValues;
                    }
                }
            }
        }
        public static MarkupType ReadMarkup(this StringReader reader, out MarkupContent content, StringComparer comparer)
        {
            content = null;

            if (!reader.PeekIsMarkup())
            {
                if (content != null)
                    content.Type = MarkupType.Raw;
                return MarkupType.Raw;
            }

            //Skip Markup
            reader.Read();
            int intChar = reader.Peek();
            if (intChar == -1)
            {
                if (content != null)
                    content.Type = MarkupType.Raw;
                return MarkupType.Raw;
            }

            var isClosingMarkup = intChar == slash;
            if (isClosingMarkup)
            {
                // Consume /
                reader.Read();

                var name = reader.ReadToBeforeChar(gt);
                content = new MarkupContent { Name = name };
                // Consume >
                reader.Read();
                content.Type = MarkupType.ClosingNode;
                return content.Type;
            }
            else
            {
                bool isComment = false;

                StringBuilder data = new StringBuilder();
                data.Append(reader.ReadToBeforeChar(gt));
                if (data.Length > 3 && data.ToString(0, 3) == "!--")
                {
                    isComment = true;

                    while (data.ToString(data.Length - 2, 2) != "--")
                    {
                        char[] cBuffer = new char[1];
                        if (reader.Read(cBuffer, 0, 1) == 0)
                            break;

                        data.Append(cBuffer[0]);
                        if (reader.Peek() != -1)
                            data.Append(reader.ReadToBeforeChar(gt));
                    }
                }
                // Consume >
                reader.Read();
                if (isComment)
                {
                    content = new MarkupContent { Content = data.ToString(3, data.Length - 5).Trim(), Name = "!--" };
                    content.Type = MarkupType.Comment;
                    return content.Type;
                }
                else
                {
                    bool isClosed = false;
                    if(data[data.Length - 1] == slash)
                    {
                        data.Remove(data.Length - 1, 1);
                        isClosed = true;
                    }

                    content = new MarkupContent { Content = data.ToString() };
                    StringReader elementReader = new StringReader(content.Content);
                    string elementName = elementReader.ReadToBeforeChar(space);
                    content.Name = elementName;

                    content.Parameters = attributesRegex.Matches(elementReader.ReadToEnd()).OfType<Match>().ToDictionary(
                        (e) => e.Groups["key"].Value,
                        (e) => e.Groups["value"].Value,
                        comparer);
                    content.Type = isClosed ? MarkupType.Node : MarkupType.StartNode;
                    return content.Type;
                }
            }
        }