using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
namespace Nikse.SubtitleEdit.Core.SubtitleFormats
{
public class Cappella : SubtitleFormat
{
public override string Extension => ".detx";
public override string Name => "Cappella";
public override string ToText(Subtitle subtitle, string title)
{
string xmlStructure = @"
".Replace('\'', '\"');
if (subtitle.Paragraphs.Count > 0)
{
xmlStructure = xmlStructure.Replace("00:00:00:00", ToTimeCode(subtitle.Paragraphs[subtitle.Paragraphs.Count -1].EndTime));
}
var xml = new XmlDocument();
xml.LoadXml(xmlStructure);
foreach (Paragraph p in subtitle.Paragraphs)
{
XmlNode paragraph = xml.CreateElement("line");
XmlAttribute roleAttr = xml.CreateAttribute("role");
roleAttr.InnerText = "sub";
paragraph.Attributes.Append(roleAttr);
XmlAttribute trackAttr = xml.CreateAttribute("track");
trackAttr.InnerText = "0";
paragraph.Attributes.Append(trackAttr);
XmlNode time = xml.CreateElement("lipsync");
XmlAttribute start = xml.CreateAttribute("timecode");
start.InnerText = ToTimeCode(p.StartTime);
time.Attributes.Append(start);
XmlAttribute type = xml.CreateAttribute("type");
type.InnerText = "in_open";
time.Attributes.Append(type);
paragraph.AppendChild(time);
XmlNode text = xml.CreateElement("text");
text.InnerText = HtmlUtil.RemoveHtmlTags(Utilities.UnbreakLine(p.Text), true);
paragraph.AppendChild(text);
time = xml.CreateElement("lipsync");
start = xml.CreateAttribute("timecode");
start.InnerText = ToTimeCode(p.EndTime);
time.Attributes.Append(start);
type = xml.CreateAttribute("type");
type.InnerText = "out_open";
time.Attributes.Append(type);
paragraph.AppendChild(time);
xml.DocumentElement.SelectSingleNode("body").AppendChild(paragraph);
}
return ToUtf8XmlString(xml);
}
private static string ToTimeCode(TimeCode tc)
{
return tc.ToHHMMSSFF();
}
public override void LoadSubtitle(Subtitle subtitle, List lines, string fileName)
{
_errorCount = 0;
var sb = new StringBuilder();
lines.ForEach(line => sb.AppendLine(line));
string xmlString = sb.ToString();
if (!xmlString.Contains("