Beispiel #1
0
        public static IDictionary<string, SmiMarker[]> ParseSmiFile(string content)
        {
            content = Regex.Replace(content, @"[\s\r\n]{2,}", " ", RegexOptions.Multiline);
            Dictionary<string, SmiMarker[]> smiContent = new Dictionary<string, SmiMarker[]>(strComp);
            StringReader reader = new StringReader(content);
            reader.ReadToMarkup();

            if (!reader.PeekIsMarkup())
                return smiContent;

            if (strComp.Compare(reader.ReadMarkup(), SamiMarkup) != 0)
                return smiContent;

            Dictionary<string, Dictionary<string, string>> styles = new Dictionary<string, Dictionary<string, string>>(strComp);
            ReadHead(reader, styles);

            return ReadBody(reader, styles);
        }
Beispiel #2
0
        internal static Dictionary<string, SmiMarker[]> ReadBody(StringReader reader, Dictionary<string, Dictionary<string, string>> styles)
        {
            // Be sure to be at the start of a markup tag
            string data = reader.ReadToMarkup();
            if (reader.Peek() == -1
                || !reader.PeekIsMarkup())
                return null;

            // Check if the next tag is Head
            MarkupContent markupContent = null;
            var markupType = reader.ReadMarkup(out markupContent, strComp);
            if (!(markupType == MarkupType.StartNode && strComp.Compare(markupContent.Name, BodyMarkup) == 0))
                return null;

            Dictionary<string, List<SmiMarker>> markersByLanguage = new Dictionary<string, List<SmiMarker>>(strComp);

            while (reader.Peek() != -1)
            {
                if (!reader.PeekIsMarkup())
                    reader.ReadToMarkup();

                if (!reader.PeekIsMarkup())
                    break;

                markupType = reader.ReadMarkup(out markupContent, strComp);
                MarkupContent invalidSamiNextMarkupContent = null;
                do
                {
                    if (invalidSamiNextMarkupContent != null)
                    {
                        markupType = invalidSamiNextMarkupContent.Type;
                        markupContent = invalidSamiNextMarkupContent;
                    }

                    if (markupType == MarkupType.ClosingNode && strComp.Compare(markupContent.Name, BodyMarkup) == 0)
                        break;

                    if (markupType == MarkupType.StartNode
                        && strComp.Compare(markupContent.Name, SyncMarkup) == 0)
                    {
                        invalidSamiNextMarkupContent = readSyncNode(reader, markupContent, styles, markersByLanguage);
                    }
                } while (invalidSamiNextMarkupContent != null);
            }

            var result = new Dictionary<string, SmiMarker[]>(strComp);
            foreach (var item in markersByLanguage)
            {
                var markers = item.Value.OrderBy(m => m.Position).ToList();
                for (int i = markers.Count - 1; i >= 0; i--)
                {
                    SmiMarker mrk = markers[i];
                    if (mrk.Content == null)
                    {
                        markers.RemoveAt(i);
                        if (i > 0)
                        {
                            SmiMarker prevMarker = markers[i - 1];
                            prevMarker.Duration = mrk.Position - prevMarker.Position;
                        }
                    }
                }
                result[item.Key] = markers.ToArray();
            }

            return result;
        }
Beispiel #3
0
        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;
        }
Beispiel #4
0
        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() != "&nbsp;")
                marker.Content = HttpUtility.HtmlDecode(markerContent.ToString()).Trim();
            else
            {
                marker.Content = null;
            }

            return malformedContent;
        }
Beispiel #5
0
        internal static void ReadHead(StringReader reader,
            Dictionary<string, Dictionary<string, string>> styles)
        {
            // Be sure to be at the start of a markup tag
            string data = reader.ReadToMarkup();
            if (reader.Peek() == -1
                || !reader.PeekIsMarkup())
                return;

            // Check if the next tag is Head
            MarkupContent markupContent;
            var markupType = reader.ReadMarkup(out markupContent, strComp);
            if (!(markupType == MarkupType.StartNode && strComp.Compare(markupContent.Name, HeadMarkup) == 0))
                return;

            while (reader.Peek() != -1)
            {
                if (!reader.PeekIsMarkup())
                    reader.ReadToMarkup();

                if (!reader.PeekIsMarkup())
                    return;

                markupType = reader.ReadMarkup(out markupContent, strComp);
                if (markupType == MarkupType.ClosingNode && strComp.Compare(markupContent.Name, HeadMarkup) == 0)
                    return;

                if (markupType == MarkupType.StartNode
                    && strComp.Compare(markupContent.Name, StyleMarkup) == 0)
                {
                    var styleContent = reader.ReadToMarkup() ?? string.Empty;
                    if (!reader.PeekIsMarkup())
                        return;

                    while (reader.Peek() != -1)
                    {
                        markupType = reader.ReadMarkup(out markupContent, strComp);
                        if (markupType == MarkupType.Comment)
                            styleContent += markupContent.Content;
                        else if (markupType == MarkupType.ClosingNode)
                            break;

                        reader.ReadToMarkup();
                        //if(!isClosingMarkup && strComp.Compare(markup, StyleMarkup))
                        //    sty
                    }

                    ParseStyle(styleContent, styles);
                }
                //if (reader.PeekIsMarkup())
                //{

                //    if ((isClosingMarkup && strComp.Compare(markup, HeadMarkup) == 0)
                //        || )
                //        return;
                //}
            }
        }