private static MarkupContent readSyncNode(StringReader reader, MarkupContent markupContent, Dictionary<string, Dictionary<string, string>> styles, Dictionary<string, List<SmiMarker>> markersByLanguage) { MarkupType markupType; TimeSpan position = TimeSpan.Zero; var start = markupContent.Parameters.TryGetValue("start"); var startInt = 0; if (!int.TryParse(start, out startInt)) return null; position = TimeSpan.FromMilliseconds(startInt); while (reader.Peek() != -1) { reader.ReadToMarkup(); if (!reader.PeekIsMarkup()) return null; markupType = reader.ReadMarkup(out markupContent, strComp); if (markupType == MarkupType.ClosingNode && strComp.Compare(markupContent.Name, SyncMarkup) == 0) break; if (markupType == MarkupType.StartNode && strComp.Compare(markupContent.Name, PMarkup) == 0) { var invalidSamiNextMarkupContent = ReadSyncPNode(reader, styles, markupContent, position, markersByLanguage); if (invalidSamiNextMarkupContent != null) { // malformed sami return invalidSamiNextMarkupContent; } } } return null; }
private static MarkupContent ReadSyncPNode(StringReader reader, Dictionary<string, Dictionary<string, string>> styles, MarkupContent markupContent, TimeSpan position, Dictionary<string, List<SmiMarker>> markersByLanguage) { MarkupContent malformedContent = null; MarkupType markupType; var cssList = new List<string>(); cssList.Add("p"); var cssClass = markupContent.Parameters.TryGetValue("class"); if (!string.IsNullOrEmpty(cssClass)) { cssList.AddRange(from css in cssClass.Split(' ') select String.Concat("." + css)); } SmiMarker marker = new SmiMarker(); marker.Position = position; StringBuilder markerContent = new StringBuilder(); foreach (var styleRules in cssList) { AssignPropertiesFromStyle(marker, styles.TryGetValue(styleRules)); } GetMarkerList(markersByLanguage, marker.Language ?? string.Empty).Add(marker); while (reader.Peek() != -1) { var newContent = reader.ReadToMarkup().Trim(); if (!string.IsNullOrEmpty(newContent)) { if (markerContent.Length > 0 && markerContent[markerContent.Length - 1] != '\n') { markerContent.Append(' '); } markerContent.Append(newContent); } if (reader.PeekIsMarkup()) { markupType = reader.ReadMarkup(out markupContent, strComp); if ((markupType == MarkupType.Node || markupType == MarkupType.StartNode) && strComp.Compare(markupContent.Name, "br") == 0) { markerContent.Append('\n'); } if ((markupType == MarkupType.ClosingNode && strComp.Compare(markupContent.Name, PMarkup) == 0)) { break; } if((markupType == MarkupType.StartNode && strComp.Compare(markupContent.Name, SyncMarkup) == 0)) { // Malformed Sami file. malformedContent = markupContent; break; } } } if (markerContent.ToString() != " ") marker.Content = HttpUtility.HtmlDecode(markerContent.ToString()).Trim(); else { marker.Content = null; } return malformedContent; }
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; } } }