private static WebVTTContent ParseCueContent(string contentText, TimeSpan timeOffset)
        {
            var result = new WebVTTContent();

            Stack<WebVTTInternalNode> nodeStack = new Stack<WebVTTInternalNode>();
            nodeStack.Push(result);

            using (var reader = new StringReader(contentText))
            {
                var contentReader = new WebVTTContentReader(reader);
                while (true)
                {
                    var contentObj = contentReader.Read();
                    if (contentObj == null) break;

                    if (contentObj is WebVTTContentNodeStart)
                    {
                        WebVTTSpanNode child = null;
                        var content = (WebVTTContentNodeStart)contentObj;
                        switch (content.Name)
                        {
                            case "c":
                                child = new WebVTTClassNode();
                                break;
                            case "u":
                                child = new WebVTTUnderlineNode();
                                break;
                            case "b":
                                child = new WebVTTBoldNode();
                                break;
                            case "i":
                                child = new WebVTTItalicNode();
                                break;
                            case "v":
                                child = new WebVTTVoiceNode() { Name = content.Annotation };
                                break;
                            case "lang":
                                child = new WebVTTLanguageNode() { Language = content.Annotation };
                                break;
                            case "ruby":
                                child = new WebVTTRubyNode();
                                break;
                            case "rt":
                                child = new WebVTTRubyTextNode();
                                break;
                            default:
                                child = new WebVTTTimestampNode() { Timestamp = ParseTimeStamp(content.NameAndClass).Add(timeOffset) };
                                break;
                        }

                        if (child != null)
                        {
                            foreach (var name in content.Classes)
                            {
                                child.Classes.Add(name);
                            }

                            nodeStack.Peek().Nodes.Add(child);
                            nodeStack.Push(child);
                        }
                    }
                    else if (contentObj is WebVTTContentNodeEnd)
                    {
                        var content = (WebVTTContentNodeEnd)contentObj;
                        nodeStack.Pop();
                    }
                    else if (contentObj is WebVTTContentText)
                    {
                        var content = (WebVTTContentText)contentObj;
                        var child = new WebVTTTextNode() { Text = content.Text };
                        nodeStack.Peek().Nodes.Add(child);
                    }
                }
            }
            return result;
        }
Exemplo n.º 2
0
        private static WebVTTContent ParseCueContent(string contentText, TimeSpan timeOffset)
        {
            var result = new WebVTTContent();

            Stack <WebVTTInternalNode> nodeStack = new Stack <WebVTTInternalNode>();

            nodeStack.Push(result);

            using (var reader = new StringReader(contentText))
            {
                var contentReader = new WebVTTContentReader(reader);
                while (true)
                {
                    var contentObj = contentReader.Read();
                    if (contentObj == null)
                    {
                        break;
                    }

                    if (contentObj is WebVTTContentNodeStart)
                    {
                        WebVTTSpanNode child   = null;
                        var            content = (WebVTTContentNodeStart)contentObj;
                        switch (content.Name)
                        {
                        case "c":
                            child = new WebVTTClassNode();
                            break;

                        case "u":
                            child = new WebVTTUnderlineNode();
                            break;

                        case "b":
                            child = new WebVTTBoldNode();
                            break;

                        case "i":
                            child = new WebVTTItalicNode();
                            break;

                        case "v":
                            child = new WebVTTVoiceNode()
                            {
                                Name = content.Annotation
                            };
                            break;

                        case "lang":
                            child = new WebVTTLanguageNode()
                            {
                                Language = content.Annotation
                            };
                            break;

                        case "ruby":
                            child = new WebVTTRubyNode();
                            break;

                        case "rt":
                            child = new WebVTTRubyTextNode();
                            break;

                        default:
                            child = new WebVTTTimestampNode()
                            {
                                Timestamp = ParseTimeStamp(content.NameAndClass).Add(timeOffset)
                            };
                            break;
                        }

                        if (child != null)
                        {
                            foreach (var name in content.Classes)
                            {
                                child.Classes.Add(name);
                            }

                            nodeStack.Peek().Nodes.Add(child);
                            nodeStack.Push(child);
                        }
                    }
                    else if (contentObj is WebVTTContentNodeEnd)
                    {
                        var content = (WebVTTContentNodeEnd)contentObj;
                        if (nodeStack != null && nodeStack.Count > 1)
                        {
                            nodeStack.Pop();
                        }
                    }
                    else if (contentObj is WebVTTContentText)
                    {
                        var content = (WebVTTContentText)contentObj;
                        var child   = new WebVTTTextNode()
                        {
                            Text = content.Text
                        };
                        if (nodeStack != null)
                        {
                            var stack = nodeStack.Peek();
                            if (stack != null && stack.Nodes != null)
                            {
                                stack.Nodes.Add(child);
                            }
                        }
                    }
                }
            }
            return(result);
        }