Fix "Fix invalid italic tags" with ASS tags - thx Jamakmake :)

This commit is contained in:
Nikolaj Olsson 2020-02-29 14:49:29 +01:00
parent 5741e1ab89
commit 34a63df46a
3 changed files with 34 additions and 4 deletions

View File

@ -543,6 +543,17 @@ namespace Nikse.SubtitleEdit.Core
{
var text = input;
var preTags = string.Empty;
if (text.StartsWith("{\\", StringComparison.Ordinal))
{
var endIdx = text.IndexOf('}', 2);
if (endIdx > 2)
{
preTags = text.Substring(0, endIdx + 1);
text = text.Remove(0, endIdx + 1);
}
}
const string beginTag = "<i>";
const string endTag = "</i>";
@ -846,7 +857,7 @@ namespace Nikse.SubtitleEdit.Core
text = text.Replace("<i> </i>", string.Empty);
text = text.Replace("<i> </i>", string.Empty);
}
return text;
return preTags + text;
}
public static string ToggleTag(string input, string tag)

View File

@ -1208,12 +1208,23 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
/// <summary>
/// Fix italic tags, lines starting with ">" - whole line is italic, words between &lt;&gt; is italic
/// </summary>
private static string FixItalics(string text)
private static string FixItalics(string input)
{
var pre = string.Empty;
var text = input;
if (text.StartsWith("{\\", StringComparison.Ordinal))
{
var endIdx = text.IndexOf('}', 2);
if (endIdx > 2)
{
pre = text.Substring(0, endIdx + 1);
text = text.Remove(0, endIdx + 1);
}
}
int index = text.IndexOf('<');
if (index < 0)
{
return text;
return input;
}
while (index >= 0 && index < text.Length - 1)
@ -1242,7 +1253,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
}
text = text.Replace("</i>", "</i> ");
text = text.Replace(" ", " ");
return text.Replace(" " + Environment.NewLine, Environment.NewLine).Trim();
return pre + text.Replace(" " + Environment.NewLine, Environment.NewLine).Trim();
}
public static Encoding GetEncoding(int codePage)

View File

@ -429,6 +429,14 @@ namespace Test.Logic
Assert.AreEqual(s2, "<i>Hallo!" + Environment.NewLine + "Hallo!" + Environment.NewLine + "Hallo!</i>");
}
[TestMethod]
public void FixInvalidItalicTagsWithAssTag()
{
var s1 = "{\\an8}<i>Hallo!<i/>" + Environment.NewLine + "<i>Hallo!<i/>";
string s2 = HtmlUtil.FixInvalidItalicTags(s1);
Assert.AreEqual(s2, "{\\an8}<i>Hallo!" + Environment.NewLine + "Hallo!</i>");
}
[TestMethod]
public void FixUnneededSpacesDoubleSpace1()
{