Fixed #10 - thx to zatch for reporting this :)

This commit is contained in:
nikse.dk 2014-02-19 20:50:30 +01:00
parent 82754849a1
commit 65cb58f955
5 changed files with 57 additions and 25 deletions

View File

@ -12,6 +12,7 @@
* FIXED:
* Fixed crash in spell check - thx e257496
* Fixed issue where it was not possible to move border in waveform
* Fixed issue regarding alignment tags (like {\an8}) in Json - thx zatch
3.3.13 (8th February 2014)
* NEW:

View File

@ -28,6 +28,52 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
return subtitle.Paragraphs.Count > _errorCount;
}
public static string EncodeJsonText(string text)
{
var sb = new StringBuilder();
for (int i = 0; i < text.Length; i++)
{
string s = text.Substring(i, 1);
if (s == "\"")
{
sb.Append("\\\"");
}
else if (s == "\\")
{
sb.Append("\\\\");
}
else
{
sb.Append(s);
}
}
return sb.ToString().Replace(Environment.NewLine, "<br />");
}
public static string DecodeJsonText(string text)
{
var sb = new StringBuilder();
text = text.Replace("<br />", Environment.NewLine);
text = text.Replace("<br>", Environment.NewLine);
text = text.Replace("<br/>", Environment.NewLine);
text = text.Replace("\\n", Environment.NewLine);
bool keepNext = false;
for (int i = 0; i < text.Length; i++)
{
string s = text.Substring(i, 1);
if (s == "\\" && !keepNext)
{
keepNext = true;
}
else
{
sb.Append(s);
keepNext = false;
}
}
return sb.ToString();
}
public override string ToText(Subtitle subtitle, string title)
{
var sb = new StringBuilder();
@ -42,7 +88,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
sb.Append(",\"end\":");
sb.Append(p.EndTime.TotalSeconds.ToString(System.Globalization.CultureInfo.InvariantCulture));
sb.Append(",\"text\":\"");
sb.Append(p.Text.Replace("\\", string.Empty).Replace("{", string.Empty).Replace("{", string.Empty).Replace("\"", "\\\"").Replace(Environment.NewLine, "<br />"));
sb.Append(EncodeJsonText(p.Text));
sb.Append("\"}");
count++;
}
@ -74,11 +120,8 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
double.TryParse(end, System.Globalization.NumberStyles.AllowDecimalPoint, System.Globalization.CultureInfo.InvariantCulture, out endSeconds) &&
text != null)
{
text = text.Replace("<br />", Environment.NewLine);
text = text.Replace("<br>", Environment.NewLine);
text = text.Replace("<br/>", Environment.NewLine);
text = text.Replace("\\n", Environment.NewLine);
subtitle.Paragraphs.Add(new Paragraph(text, startSeconds * 1000.0, endSeconds * 1000.0));
subtitle.Paragraphs.Add(new Paragraph(DecodeJsonText(text), startSeconds * 1000.0, endSeconds * 1000.0));
}
else
{
@ -128,7 +171,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
if (endIndex == -1)
return null;
if (res.Length > 1)
return res.Substring(1, endIndex - 1).Replace("@__1", "\"");
return res.Substring(1, endIndex - 1).Replace("@__1", "\\\"");
return string.Empty;
}
else

View File

@ -42,7 +42,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
sb.Append(",\"endMillis\":");
sb.Append(p.EndTime.TotalMilliseconds.ToString(System.Globalization.CultureInfo.InvariantCulture));
sb.Append(",\"text\":\"");
sb.Append(p.Text.Replace("\\", string.Empty).Replace("{", string.Empty).Replace("{", string.Empty).Replace("\"", "\\\"").Replace(Environment.NewLine, "\\n"));
sb.Append(Json.EncodeJsonText(p.Text));
sb.Append("\"}");
count++;
}
@ -74,11 +74,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
double.TryParse(end, System.Globalization.NumberStyles.AllowDecimalPoint, System.Globalization.CultureInfo.InvariantCulture, out endSeconds) &&
text != null)
{
text = text.Replace("<br />", Environment.NewLine);
text = text.Replace("<br>", Environment.NewLine);
text = text.Replace("<br/>", Environment.NewLine);
text = text.Replace("\\n", Environment.NewLine);
subtitle.Paragraphs.Add(new Paragraph(text, startSeconds, endSeconds));
subtitle.Paragraphs.Add(new Paragraph(Json.DecodeJsonText(text), startSeconds, endSeconds));
}
else
{

View File

@ -40,7 +40,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
sb.Append("{\"duration\":");
sb.Append(p.Duration.TotalMilliseconds.ToString(System.Globalization.CultureInfo.InvariantCulture));
sb.Append(",\"content\":\"");
sb.Append(p.Text.Replace("\\", string.Empty).Replace("{", string.Empty).Replace("{", string.Empty).Replace("\"", "\\\"").Replace(Environment.NewLine, "\\n") + "\"");
sb.Append(Json.EncodeJsonText(p.Text));
sb.Append(",\"startOfParagraph\":false");
sb.Append(",\"startTime\":");
sb.Append(p.StartTime.TotalMilliseconds.ToString(System.Globalization.CultureInfo.InvariantCulture));
@ -77,11 +77,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
double.TryParse(duration, System.Globalization.NumberStyles.AllowDecimalPoint, System.Globalization.CultureInfo.InvariantCulture, out durationSeconds) &&
content != null)
{
content = content.Replace("<br />", Environment.NewLine);
content = content.Replace("<br>", Environment.NewLine);
content = content.Replace("<br/>", Environment.NewLine);
content = content.Replace("\\n", Environment.NewLine);
subtitle.Paragraphs.Add(new Paragraph(content, startSeconds, startSeconds + durationSeconds));
subtitle.Paragraphs.Add(new Paragraph(Json.DecodeJsonText(content), startSeconds, startSeconds + durationSeconds));
}
else
{

View File

@ -47,7 +47,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
sb.Append(",\"guid\":\"" + guid + "\",\"segmentTypeId\":\"" + segmentTypeId + "\",\"endTime\":");
sb.Append(p.EndTime.TotalSeconds.ToString(System.Globalization.CultureInfo.InvariantCulture));
sb.Append(",\"id\":\"" + id + "\",\"metadata\":{\"Text\":\"");
sb.Append(p.Text.Replace("\\", string.Empty).Replace("{", string.Empty).Replace("{", string.Empty).Replace("\"", "\\\"").Replace(Environment.NewLine, "\\n") + "\"");
sb.Append(Json.EncodeJsonText(p.Text));
sb.Append(",\"ID\":\"\",\"Language\":\"en\"}}");
count++;
@ -82,11 +82,7 @@ namespace Nikse.SubtitleEdit.Logic.SubtitleFormats
double.TryParse(end, System.Globalization.NumberStyles.AllowDecimalPoint, System.Globalization.CultureInfo.InvariantCulture, out endSeconds) &&
content != null)
{
content = content.Replace("<br />", Environment.NewLine);
content = content.Replace("<br>", Environment.NewLine);
content = content.Replace("<br/>", Environment.NewLine);
content = content.Replace("\\n", Environment.NewLine);
subtitle.Paragraphs.Add(new Paragraph(content, startSeconds * 1000.0, endSeconds * 1000.0));
subtitle.Paragraphs.Add(new Paragraph(Json.DecodeJsonText(content), startSeconds * 1000.0, endSeconds * 1000.0));
}
else
{