public override void LoadSubtitle(Subtitle subtitle, List <string> lines, string fileName) { _errorCount = 0; Paragraph p = null; string positionInfo = string.Empty; bool hadEmptyLine = false; for (var index = 0; index < lines.Count; index++) { string line = lines[index]; string next = string.Empty; if (index < lines.Count - 1) { next = lines[index + 1]; } string s = line; bool isTimeCode = line.Contains("-->"); if (isTimeCode && RegexTimeCodesMiddle.IsMatch(s)) { s = "00:" + s; // start is without hours, end is with hours } if (isTimeCode && RegexTimeCodesShort.IsMatch(s)) { s = "00:" + s.Replace("--> ", "--> 00:"); } if (isTimeCode && RegexTimeCodes.IsMatch(s)) { if (p != null) { p.Text = p.Text.TrimEnd(); subtitle.Paragraphs.Add(p); } try { var parts = s.Replace("-->", "@").Split(new[] { '@' }, StringSplitOptions.RemoveEmptyEntries); p = new Paragraph { StartTime = WebVTT.GetTimeCodeFromString(parts[0]), EndTime = WebVTT.GetTimeCodeFromString(parts[1]) }; positionInfo = WebVTT.GetPositionInfo(s); } catch (Exception exception) { System.Diagnostics.Debug.WriteLine(exception.Message); _errorCount++; p = null; } hadEmptyLine = false; } else if (subtitle.Paragraphs.Count == 0 && line.Trim() == "WEBVTT FILE") { subtitle.Header = "WEBVTT FILE"; } else if (p != null && hadEmptyLine && Utilities.IsInteger(line.RemoveChar('-')) && (RegexTimeCodesMiddle.IsMatch(next) || RegexTimeCodesShort.IsMatch(next) || RegexTimeCodes.IsMatch(next))) { // line number } else if (p != null) { string text = positionInfo + line.Trim(); if (string.IsNullOrEmpty(text)) { hadEmptyLine = true; } if (string.IsNullOrEmpty(p.Text)) { p.Text = text + Environment.NewLine; } else { p.Text += text + Environment.NewLine; } positionInfo = string.Empty; } } if (subtitle.Header == null && subtitle.Header != "WEBVTT FILE") { subtitle.Paragraphs.Clear(); _errorCount++; } if (p != null) { p.Text = p.Text.TrimEnd(); subtitle.Paragraphs.Add(p); } foreach (var paragraph in subtitle.Paragraphs) { paragraph.Text = WebVTT.ColorWebVttToHtml(paragraph.Text); paragraph.Text = System.Net.WebUtility.HtmlDecode(paragraph.Text); } subtitle.Renumber(); }
public override void LoadSubtitle(Subtitle subtitle, List <string> lines, string fileName) { _errorCount = 0; Paragraph p = null; bool textDone = true; string positionInfo = string.Empty; foreach (string line in lines) { string s = line; bool isTimeCode = line.Contains("-->"); if (isTimeCode && RegexTimeCodesMiddle.IsMatch(s)) { s = "00:" + s; // start is without hours, end is with hours } if (isTimeCode && RegexTimeCodesShort.IsMatch(s)) { s = "00:" + s.Replace("--> ", "--> 00:"); } if (isTimeCode && RegexTimeCodes.IsMatch(s)) { textDone = false; if (p != null) { subtitle.Paragraphs.Add(p); p = null; } try { string[] parts = s.Replace("-->", "@").Split(new[] { '@' }, StringSplitOptions.RemoveEmptyEntries); p = new Paragraph(); p.StartTime = WebVTT.GetTimeCodeFromString(parts[0]); p.EndTime = WebVTT.GetTimeCodeFromString(parts[1]); positionInfo = WebVTT.GetPositionInfo(s); } catch (Exception exception) { System.Diagnostics.Debug.WriteLine(exception.Message); _errorCount++; p = null; } } else if (subtitle.Paragraphs.Count == 0 && line.Trim() == "WEBVTT FILE") { subtitle.Header = "WEBVTT FILE"; } else if (p != null && !string.IsNullOrWhiteSpace(line)) { string text = positionInfo + line.Trim(); if (!textDone) { p.Text = (p.Text + Environment.NewLine + text).Trim(); } positionInfo = string.Empty; } else if (line.Length == 0) { textDone = true; } } if (subtitle.Header == null && subtitle.Header != "WEBVTT FILE") { subtitle.Paragraphs.Clear(); _errorCount++; } if (p != null) { subtitle.Paragraphs.Add(p); } subtitle.Renumber(); }